From 98b31b8c0b98481a9a819ee64c56ea4ea8a77eea Mon Sep 17 00:00:00 2001 From: Alejandro Isaza <167236+alejandro-isaza@users.noreply.github.com> Date: Fri, 5 Apr 2019 15:49:33 -0700 Subject: [PATCH] Add code coverage reporting (#267) --- .gitignore | 4 ++++ CMakeLists.txt | 7 ++++++- include/TrustWalletCore/TWBitcoin.h | 14 +++++++------- src/PublicKey.h | 8 +++++++- src/uint256.h | 5 +++-- tests/CMakeLists.txt | 25 +++++++++++++++++++++++-- tools/coverage | 23 +++++++++++++++++++++++ 7 files changed, 73 insertions(+), 13 deletions(-) create mode 100755 tools/coverage diff --git a/.gitignore b/.gitignore index b6665c95fdd..8274cf8729e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,7 @@ lib/protobuf jni/cpp/generated jni/java swift/Sources/Generated + +# Code coverage files +coverage.info +coverage/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 43a87e81f08..b6ffb444e5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,12 @@ include(ExternalProject) # Dependencies include(cmake/Protobuf.cmake) +option(CODE_COVERAGE "Enable coverage reporting" OFF) +if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage") + set(CMAKE_EXE_LINKER_FLAGS "-fprofile-arcs -ftest-coverage") +endif() + # Source files if(${ANDROID}) message("Configuring for JNI") @@ -56,7 +62,6 @@ set_target_properties(TrustWalletCore PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON - COMPILE_FLAGS "${WARNING_FLAGS}" ) # Define headers for this library. PUBLIC headers are used for compiling the diff --git a/include/TrustWalletCore/TWBitcoin.h b/include/TrustWalletCore/TWBitcoin.h index f2f05e458fc..d8dbef75fa9 100644 --- a/include/TrustWalletCore/TWBitcoin.h +++ b/include/TrustWalletCore/TWBitcoin.h @@ -16,6 +16,13 @@ static const uint32_t TWSignatureHashTypeSingle = 0x03; static const uint32_t TWSignatureHashTypeFork = 0x40; static const uint32_t TWSignatureHashTypeAnyoneCanPay = 0x80; +enum TWBitcoinSignatureVersion { + BASE, + WITNESS_V0 +}; + +TW_EXTERN_C_END + inline bool TWSignatureHashTypeIsSingle(uint32_t type) { return (type & 0x1f) == TWSignatureHashTypeSingle; } @@ -23,10 +30,3 @@ inline bool TWSignatureHashTypeIsSingle(uint32_t type) { inline bool TWSignatureHashTypeIsNone(uint32_t type) { return (type & 0x1f) == TWSignatureHashTypeNone; } - -enum TWBitcoinSignatureVersion { - BASE, - WITNESS_V0 -}; - -TW_EXTERN_C_END diff --git a/src/PublicKey.h b/src/PublicKey.h index 5c4180b1f6d..6a2449c1b92 100644 --- a/src/PublicKey.h +++ b/src/PublicKey.h @@ -10,6 +10,8 @@ #include "Hash.h" #include +#include +#include #include namespace TW { @@ -76,9 +78,13 @@ class PublicKey { } /// Initializes a public key with a collection of bytes. + /// + /// @throws std::invalid_argument if the data is not a valid public key. template explicit PublicKey(const T& data) { - assert(isValid(data)); + if (!isValid(data)) { + throw std::invalid_argument("Invalid public key data"); + } bytes.reserve(data.size()); std::copy(std::begin(data), std::end(data), std::back_inserter(bytes)); } diff --git a/src/uint256.h b/src/uint256.h index d48cf8ea03f..6d0ee67bfc6 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -7,6 +7,7 @@ #pragma once #include "Data.h" + #include namespace TW { @@ -17,7 +18,7 @@ using uint256_t = boost::multiprecision::uint256_t; /// Loads a `uint256_t` from a collection of bytes. inline uint256_t load(const Data& data) { using boost::multiprecision::cpp_int; - if (std::empty(data)) { + if (data.empty()) { return uint256_t(0); } uint256_t result; @@ -29,7 +30,7 @@ inline uint256_t load(const Data& data) { /// std::string). inline uint256_t load(const std::string& data) { using boost::multiprecision::cpp_int; - if (std::empty(data)) { + if (data.empty()) { return uint256_t(0); } uint256_t result; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dba6fab6172..319e4ff4743 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,8 +17,29 @@ include_directories(${Protobuf_INCLUDE_DIRS}) # Test executable file(GLOB_RECURSE test_sources *.cpp **/*.cpp) add_executable(tests ${test_sources}) -target_compile_features(tests PRIVATE cxx_std_17) -target_link_libraries(tests gtest_main TrustWalletCore protobuf Boost::boost) +target_link_libraries(tests gtest_main TrezorCrypto TrustWalletCore protobuf Boost::boost) target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/src) +target_compile_options(tests PRIVATE "-Wall") + +set_target_properties(tests + PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED ON +) + +option(CODE_COVERAGE "Enable coverage reporting" OFF) +if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + # Add required flags (GCC & LLVM/Clang) + target_compile_options(tests INTERFACE + -O0 # no optimization + -g # generate debug info + --coverage # sets all required flags + ) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13) + target_link_options(tests INTERFACE --coverage) + else() + target_link_libraries(tests INTERFACE --coverage) + endif() +endif() add_test(NAME example_test COMMAND tests) diff --git a/tools/coverage b/tools/coverage new file mode 100755 index 00000000000..01a14fb5c83 --- /dev/null +++ b/tools/coverage @@ -0,0 +1,23 @@ +#!/bin/bash +# +# This script captures test code coverage information. + +set -e + +lcov --capture --directory . --output-file coverage.info +lcov --remove coverage.info '/usr/*' --output-file coverage.info +lcov --remove coverage.info '/Applications/*' --output-file coverage.info +lcov --remove coverage.info '*/build/*' --output-file coverage.info +lcov --remove coverage.info '*.pb.cc' --output-file coverage.info +lcov --remove coverage.info '*.pb.h' --output-file coverage.info +lcov --remove coverage.info '*/tests/*' --output-file coverage.info +bash <(curl -s https://codecov.io/bash) -f coverage.info -t 1faef852-b196-4697-885b-0ff22a625181 || echo "Codecov did not collect coverage reports" + +# Generate HTML report +if [[ "$1" == "html" ]]; then + genhtml --output-directory coverage \ + --demangle-cpp --num-spaces 4 --sort \ + --title "Trust Wallet Core Test Coverage" \ + --function-coverage --branch-coverage --legend \ + coverage.info +fi