Skip to content

Commit

Permalink
test: improve cov (#32)
Browse files Browse the repository at this point in the history
test: improve cov

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Mar 22, 2020
1 parent 27508a3 commit 3bf827c
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 102 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ before_script:
- mkdir build-debug && cd build-debug
- |
if [ $TRAVIS_OS_NAME == osx ]; then
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DBUILD_SHARED_LIBS=ON -DOPENSSL_ROOT_DIR=$(brew --prefix openssl) -DCMAKE_INSTALL_PREFIX=/usr/local ..
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Debug -DJWTPP_WITH_COVERAGE=ON -DJWTPP_WITH_TESTS=ON -DJWTPP_WITH_SHARED_LIBS=ON -DOPENSSL_ROOT_DIR=$(brew --prefix openssl) -DCMAKE_INSTALL_PREFIX=/usr/local ..
else
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Debug -DJWTPP_WITH_COVERAGE=ON -DJWTPP_WITH_TESTS=ON -DJWTPP_WITH_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..
fi
- cd .. && mkdir build-release && cd build-release
- |
if [ $TRAVIS_OS_NAME == osx ]; then
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Release -DWITH_TESTS=ON -DBUILD_SHARED_LIBS=ON -DOPENSSL_ROOT_DIR=$(brew --prefix openssl) -DCMAKE_INSTALL_PREFIX=/usr/local ..
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Release -DJWTPP_WITH_TESTS=ON -DJWTPP_WITH_SHARED_LIBS=ON -DOPENSSL_ROOT_DIR=$(brew --prefix openssl) -DCMAKE_INSTALL_PREFIX=/usr/local ..
else
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Release -DWITH_TESTS=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..
cmake -Wno-dev -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE=Release -DJWTPP_WITH_TESTS=ON -DJWTPP_WITH_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..
fi
script:
- make
Expand Down
77 changes: 28 additions & 49 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ set(VERSION 0.1.0)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules")

option(WITH_TESTS "Build tests" OFF)
option(WITH_INSTALL "Allow root targets to not issue install" ON)
option(BUILD_SHARED_LIBS "Build shared library" OFF)
option(JWTPP_WITH_TESTS "Build tests" OFF)
option(JWTPP_WITH_COVERAGE "Enable coverage tests" OFF)
option(JWTPP_WITH_INSTALL "Allow root targets to not issue install" ON)
option(JWTPP_WITH_SHARED_LIBS "Build shared library" OFF)

if(WITH_TESTS AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
if(JWTPP_WITH_TESTS AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
include(CodeCoverage)
endif ()

Expand All @@ -29,17 +30,12 @@ set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation d
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")

find_package(OpenSSL REQUIRED)

if (NOT WIN32 AND NOT JsonCPP_FOUND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(JsonCPP REQUIRED jsoncpp)
endif ()

set(eddsa_support OFF)

if (OPENSSL_VERSION VERSION_GREATER_EQUAL 1.1.1)
set(eddsa_support ON)
endif()

include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
include_directories(SYSTEM ${JsonCPP_INCLUDE_DIRS})
link_directories(${JsonCPP_LIBRARY_DIRS})
Expand Down Expand Up @@ -69,6 +65,7 @@ set(LIB_SOURCES
src/crypto.cpp
src/digest.cpp
src/ecdsa.cpp
src/eddsa.cpp
src/header.cpp
src/hmac.cpp
src/jwtpp.cpp
Expand All @@ -85,13 +82,6 @@ add_library(
${PROJECT_NAME}-static
STATIC ${LIB_SOURCES}
)
if (eddsa_support)
target_sources(
${PROJECT_NAME}-static
PRIVATE
src/eddsa.cpp
)
endif()

set_target_properties(${PROJECT_NAME}-static PROPERTIES OUTPUT_NAME ${PROJECT_NAME} CLEAN_DIRECT_OUTPUT 1)

Expand All @@ -110,7 +100,7 @@ target_link_libraries(
${JsonCPP_LIBRARIES}
)

if (WITH_INSTALL)
if (JWTPP_WITH_INSTALL)
install(
TARGETS
${PROJECT_NAME}-static
Expand All @@ -120,15 +110,8 @@ if (WITH_INSTALL)
)
endif ()

if (BUILD_SHARED_LIBS)
if (JWTPP_WITH_SHARED_LIBS)
add_library(${PROJECT_NAME}-shared SHARED ${LIB_SOURCES})
if (eddsa_support)
target_sources(
${PROJECT_NAME}-shared
PRIVATE
src/eddsa.cpp
)
endif()

set_target_properties(${PROJECT_NAME}-shared PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
set_target_properties(${PROJECT_NAME}-shared PROPERTIES OUTPUT_NAME ${PROJECT_NAME} CLEAN_DIRECT_OUTPUT 1)
Expand Down Expand Up @@ -159,7 +142,7 @@ if (BUILD_SHARED_LIBS)
endif ()
endif ()

if (WITH_INSTALL)
if (JWTPP_WITH_INSTALL)
install(
DIRECTORY
include/export/jwtpp
Expand All @@ -168,19 +151,19 @@ if (WITH_INSTALL)
)
endif ()

set(JOSEPP_PC ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc)
set(JWTPP_PC ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc)

if (WITH_INSTALL)
if (JWTPP_WITH_INSTALL)
configure_file(
pkgconfig.pc.in
${JOSEPP_PC}
${JWTPP_PC}
@ONLY
)

install(FILES ${JOSEPP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
install(FILES ${JWTPP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
endif ()

if (WITH_TESTS)
if (JWTPP_WITH_TESTS)
enable_testing()
add_subdirectory(gtest EXCLUDE_FROM_ALL)

Expand All @@ -189,22 +172,16 @@ if (WITH_TESTS)
add_executable(jwtpp_test
tests/b64.cpp
tests/claims.cpp
tests/crypto.cpp
tests/digest.cpp
tests/ecdsa.cpp
tests/eddsa.cpp
tests/header.cpp
tests/hmac.cpp
tests/pss.cpp
tests/rsa.cpp
)

if (eddsa_support)
target_sources(
jwtpp_test
PRIVATE
tests/eddsa.cpp
)
endif()

if (WIN32)
set(WIN32_DEP_LIBS crypt32.lib ws2_32.lib)
endif (WIN32)
Expand All @@ -223,13 +200,15 @@ if (WITH_TESTS)
PRIVATE
-DTEST_RSA_KEY_PATH=\"${CMAKE_SOURCE_DIR}/tests/rsa.pem\"
)
#
# if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# append_coverage_compiler_flags()
#
# setup_target_for_coverage_lcov(
# NAME coverage
# EXECUTABLE jwtpp_test
# )
# endif ()

if (JWTPP_WITH_COVERAGE)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
append_coverage_compiler_flags()

setup_target_for_coverage_lcov(
NAME coverage
EXECUTABLE jwtpp_test
)
endif ()
endif ()
endif ()
19 changes: 8 additions & 11 deletions include/export/jwtpp/jwtpp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
# define __NODISCARD
#endif

#if OPENSSL_VERSION_NUMBER >= 0x10101000L
# define JWTPP_SUPPORTED_EDDSA
#endif

namespace jwtpp {

class claims;
Expand All @@ -71,18 +75,12 @@ enum class alg_t {
PS256,
PS384,
PS512,
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
#if defined(JWTPP_SUPPORTED_EDDSA)
EdDSA,
#endif // OPENSSL_VERSION_NUMBER >= 0x10101000L
#endif // defined(JWTPP_SUPPORTED_EDDSA)
UNKNOWN
};

#if defined(_MSC_VER) && (_MSC_VER < 1700)

#else

#endif // defined(_MSC_VER) && (_MSC_VER < 1700)

#if defined(_MSC_VER) && (_MSC_VER < 1700)
# define final

Expand Down Expand Up @@ -647,7 +645,7 @@ private:
sp_ecdsa_key _e;
};

#if OPENSSL_VERSION_NUMBER >= 0x10101000L
#if defined(JWTPP_SUPPORTED_EDDSA)
class eddsa : public crypto {
public:
explicit eddsa(sp_evp_key key, alg_t a = alg_t::EdDSA);
Expand All @@ -673,7 +671,7 @@ public:
private:
sp_evp_key _e;
};
#endif // OPENSSL_VERSION_NUMBER >= 0x10101000L
#endif // defined(JWTPP_SUPPORTED_EDDSA)

class pss : public crypto {
public:
Expand All @@ -683,7 +681,6 @@ public:

public:
std::string sign(const std::string &data) override;

bool verify(const std::string &data, const std::string &sig) override;

private:
Expand Down
12 changes: 6 additions & 6 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ crypto::crypto(alg_t a)
_hash_type = digest::type::SHA384;
} else if (a == alg_t::HS512 || a == alg_t::RS512 || a == alg_t::ES512 || a == alg_t::PS512) {
_hash_type = digest::type::SHA512;
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
#if defined(JWTPP_SUPPORTED_EDDSA)
} else if (a == alg_t::EdDSA) {
// ED25519 does not support digests
#endif // OPENSSL_VERSION_NUMBER >= 0x10101000L
#endif // defined(JWTPP_SUPPORTED_EDDSA)
} else {
throw std::runtime_error("invalid algorithm");
}
Expand Down Expand Up @@ -74,10 +74,10 @@ const char *crypto::alg2str(alg_t a) {
return "PS384";
case alg_t::PS512:
return "PS512";
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
#if defined(JWTPP_SUPPORTED_EDDSA)
case alg_t::EdDSA:
return "EdDSA";
#endif // OPENSSL_VERSION_NUMBER >= 0x10101000L
#endif // defined(JWTPP_SUPPORTED_EDDSA)
default:
return nullptr;
}
Expand Down Expand Up @@ -111,9 +111,9 @@ alg_t crypto::str2alg(const std::string &a) {
} else if (a == "PS512") {
return alg_t::PS512;
} else if (a == "EdDSA") {
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
#if defined(JWTPP_SUPPORTED_EDDSA)
return alg_t::EdDSA;
#endif // OPENSSL_VERSION_NUMBER >= 0x10101000L
#endif // defined(JWTPP_SUPPORTED_EDDSA)
} else {
return alg_t::UNKNOWN;
}
Expand Down
1 change: 1 addition & 0 deletions src/digest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <sstream>
#include <iomanip>
#include <cstring>

#include <jwtpp/jwtpp.hh>

Expand Down
4 changes: 4 additions & 0 deletions src/ecdsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ecdsa::ecdsa(sp_ecdsa_key key, alg_t a)
}

std::string ecdsa::sign(const std::string &data) {
if (data.empty()) {
throw std::invalid_argument("data is empty");
}

auto sig = std::shared_ptr<uint8_t>(new uint8_t[ECDSA_size(_e.get())], std::default_delete<uint8_t[]>());

digest d(_hash_type, reinterpret_cast<const uint8_t *>(data.data()), data.length());
Expand Down
11 changes: 9 additions & 2 deletions src/eddsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <jwtpp/jwtpp.hh>

#if defined(JWTPP_SUPPORTED_EDDSA)
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/crypto.h>

#include <jwtpp/jwtpp.hh>

namespace jwtpp {

eddsa::eddsa(sp_evp_key key, alg_t a)
Expand All @@ -38,6 +39,10 @@ eddsa::eddsa(sp_evp_key key, alg_t a)
}

std::string eddsa::sign(const std::string &data) {
if (data.empty()) {
throw std::invalid_argument("data is empty");
}

auto md = sp_evp_md_ctx(EVP_MD_CTX_new(), ::EVP_MD_CTX_free);

EVP_MD_CTX_init(md.get());
Expand Down Expand Up @@ -103,3 +108,5 @@ sp_evp_key eddsa::get_pub(sp_evp_key priv) {
}

} // namespace jwtpp

#endif // defined(JWTPP_SUPPORTED_EDDSA)
2 changes: 1 addition & 1 deletion src/hmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ hmac::hmac(const secure_string &secret, alg_t a)

std::string hmac::sign(const std::string &data) {
if (data.empty()) {
throw std::invalid_argument("Data is empty");
throw std::invalid_argument("data is empty");
}

const EVP_MD *evp;
Expand Down
4 changes: 4 additions & 0 deletions src/pss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pss::pss(sp_rsa_key key, alg_t a)
}

std::string pss::sign(const std::string &data) {
if (data.empty()) {
throw std::invalid_argument("data is empty");
}

digest d(_hash_type, reinterpret_cast<const uint8_t *>(data.data()), data.length());

auto padded = std::shared_ptr<uint8_t>(new uint8_t[_key_size], std::default_delete<uint8_t[]>());
Expand Down
4 changes: 4 additions & 0 deletions src/rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ rsa::~rsa() {
}

std::string rsa::sign(const std::string &data) {
if (data.empty()) {
throw std::invalid_argument("data is empty");
}

std::shared_ptr<uint8_t> sig = std::shared_ptr<uint8_t>(new uint8_t[_key_size], std::default_delete<uint8_t[]>());

digest d(_hash_type, reinterpret_cast<const uint8_t *>(data.data()), data.length());
Expand Down
2 changes: 1 addition & 1 deletion tests/b64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <jwtpp/jwtpp.hh>

TEST(JosePP, b64)
TEST(jwtpp, b64)
{
std::vector<uint8_t> in;
in.reserve(128);
Expand Down
6 changes: 5 additions & 1 deletion tests/claims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <jwtpp/jwtpp.hh>

TEST(JosePP, create_close_claims)
TEST(jwtpp, create_close_claims)
{
EXPECT_NO_THROW(jwtpp::claims cl);
EXPECT_THROW(jwtpp::claims cl(""), std::exception);
Expand All @@ -35,8 +35,12 @@ TEST(JosePP, create_close_claims)

EXPECT_NO_THROW(cl = std::make_shared<jwtpp::claims>());

EXPECT_THROW(cl->set().any("", "val"), std::exception);
EXPECT_THROW(cl->set().any("key", ""), std::exception);

EXPECT_NO_THROW(cl->set().iss("troian"));
EXPECT_NO_THROW(cl->set().iss("troian"));

EXPECT_FALSE(cl->has().aud());

EXPECT_EQ("troian", cl->get().iss());
Expand Down
Loading

0 comments on commit 3bf827c

Please sign in to comment.