diff --git a/.gitmodules b/.gitmodules index 9f11f61..6f945a7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "test/catch"] path = test/catch - url = https://github.com/philsquared/Catch.git + url = https://github.com/catchorg/Catch2.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 40e6972..d61af07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,27 @@ -cmake_minimum_required(VERSION 2.8) -project(cppcodec) +cmake_minimum_required(VERSION 2.8.5) +project(cppcodec CXX) +set(PROJECT_VERSION 0.1) -option(build_tests "Build tests (run as 'test' target)" ON) +include(GNUInstallDirs) +include(CTest) # These flags are for binaries built by this particular CMake project (test_cppcodec, base64enc, etc.). # In your own project that uses cppcodec, you might want to specify a different standard or error level. if (MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3") else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic") + + # Request C++11 + if(${CMAKE_VERSION} VERSION_LESS 3.1) + # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD + # remove this block once CMake >=3.1 has fixated in the ecosystem + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + else() + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(CMAKE_CXX_EXTENSIONS OFF) + endif() endif() set(PUBLIC_HEADERS @@ -46,12 +59,16 @@ add_library(cppcodec OBJECT ${PUBLIC_HEADERS}) # unnecessary for building, but m set_target_properties(cppcodec PROPERTIES LINKER_LANGUAGE CXX) add_subdirectory(tool) -if (build_tests) - enable_testing() +if (BUILD_TESTING) add_subdirectory(test) endif() foreach(h ${PUBLIC_HEADERS}) - get_filename_component(HEADER_INCLUDE_DIRECTORY include/${h} PATH) # use DIRECTORY instead of PATH once requiring CMake 3.0 - install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${h} DESTINATION ${HEADER_INCLUDE_DIRECTORY} COMPONENT "headers") + get_filename_component(FINAL_PATH ${h} PATH) # use DIRECTORY instead of PATH once requiring CMake 3.0 + install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${h} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${FINAL_PATH} COMPONENT "headers") endforeach() + +if (NOT WIN32) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cppcodec.pc.in ${CMAKE_CURRENT_BINARY_DIR}/cppcodec-1.pc @ONLY) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cppcodec-1.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) +endif() diff --git a/cppcodec.pc.in b/cppcodec.pc.in new file mode 100644 index 0000000..69fac69 --- /dev/null +++ b/cppcodec.pc.in @@ -0,0 +1,7 @@ +includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ + +Name: @CMAKE_PROJECT_NAME@ +Description: C++ header-only library to encode/decode base64, base64url, base32, base32hex and hex +URL: https://github.com/tplgy/cppcodec +Version: @PROJECT_VERSION@ +Cflags: -I${includedir} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0a20bcb..22eb286 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,18 @@ # For cppcodec itself, don't prefer system headers over development ones. include_directories(BEFORE ${PROJECT_SOURCE_DIR}) +find_package(PkgConfig) +if (PKG_CONFIG_FOUND) + pkg_check_modules(CATCH2 catch) +endif() + +if (CATCH2_FOUND) + message(STATUS "Found system Catch2, not using bundled version") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CATCH2_CFLAGS}") +else() + message(STATUS "Did NOT find system Catch2, instead using bundled version") + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/catch/single_include) +endif() + add_executable(test_cppcodec test_cppcodec.cpp) add_test(cppcodec test_cppcodec) diff --git a/test/test_cppcodec.cpp b/test/test_cppcodec.cpp index bbda970..5a3ed6c 100644 --- a/test/test_cppcodec.cpp +++ b/test/test_cppcodec.cpp @@ -22,7 +22,7 @@ */ #define CATCH_CONFIG_MAIN -#include "catch/single_include/catch.hpp" +#include #include #include @@ -125,16 +125,16 @@ TEST_CASE("Douglas Crockford's base32", "[base32][crockford]") { REQUIRE(base32::decode("-C-SQ--PY-") == "foo"); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(base32::decode("0"), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("000"), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("000000"), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("000000000"), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(base32::decode("0"), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("000"), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("000000"), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("000000000"), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(base32::decode("00======"), cppcodec::symbol_error&); // no padding for Crockford - REQUIRE_THROWS_AS(base32::decode("Uu"), cppcodec::symbol_error&); // only a checksum symbol here - REQUIRE_THROWS_AS(base32::decode("++"), cppcodec::symbol_error&); // make sure it's not base64 - REQUIRE_THROWS_AS(base32::decode("//"), cppcodec::symbol_error&); // ...ditto + REQUIRE_THROWS_AS(base32::decode("00======"), cppcodec::symbol_error); // no padding for Crockford + REQUIRE_THROWS_AS(base32::decode("Uu"), cppcodec::symbol_error); // only a checksum symbol here + REQUIRE_THROWS_AS(base32::decode("++"), cppcodec::symbol_error); // make sure it's not base64 + REQUIRE_THROWS_AS(base32::decode("//"), cppcodec::symbol_error); // ...ditto } // Only test overloads once (for base32_crockford, since it happens to be the first one). @@ -324,20 +324,20 @@ TEST_CASE("base32hex", "[base32][hex]") { REQUIRE(base32::decode("cPnMuOj1") == "fooba"); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(base32::decode("0"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base32::decode("00"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base32::decode("00==="), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base32::decode("0======="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("000====="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("000000=="), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(base32::decode("0"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base32::decode("00"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base32::decode("00==="), cppcodec::padding_error); + REQUIRE_THROWS_AS(base32::decode("0======="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("000====="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("000000=="), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(base32::decode("W0======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("X0======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("Y0======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("Z0======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("CPNM UOJ1"), cppcodec::symbol_error&); // no spaces - REQUIRE_THROWS_AS(base32::decode("CPNM-UOJ1"), cppcodec::symbol_error&); // no dashes + REQUIRE_THROWS_AS(base32::decode("W0======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("X0======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("Y0======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("Z0======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("CPNM UOJ1"), cppcodec::symbol_error); // no spaces + REQUIRE_THROWS_AS(base32::decode("CPNM-UOJ1"), cppcodec::symbol_error); // no dashes } } @@ -431,20 +431,20 @@ TEST_CASE("base32 (RFC 4648)", "[base32][rfc4648]") { REQUIRE(base32::decode("mZxW6yTb") == "fooba"); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(base32::decode("A"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base32::decode("AA"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base32::decode("AA==="), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base32::decode("A======="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("AAA====="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base32::decode("AAAAAA=="), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(base32::decode("A"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base32::decode("AA"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base32::decode("AA==="), cppcodec::padding_error); + REQUIRE_THROWS_AS(base32::decode("A======="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("AAA====="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base32::decode("AAAAAA=="), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(base32::decode("0A======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("1A======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("8A======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("9A======"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base32::decode("GEZD GNBV"), cppcodec::symbol_error&); // no spaces - REQUIRE_THROWS_AS(base32::decode("GEZD-GNBV"), cppcodec::symbol_error&); // no dashes + REQUIRE_THROWS_AS(base32::decode("0A======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("1A======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("8A======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("9A======"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base32::decode("GEZD GNBV"), cppcodec::symbol_error); // no spaces + REQUIRE_THROWS_AS(base32::decode("GEZD-GNBV"), cppcodec::symbol_error); // no dashes } } @@ -571,17 +571,17 @@ TEST_CASE("base64 (RFC 4648)", "[base64][rfc4648]") { REQUIRE(base64::decode("////") == std::vector({255, 255, 255})); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(base64::decode("A"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base64::decode("AA"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base64::decode("ABCDE"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base64::decode("A==="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base64::decode("AAAA===="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base64::decode("AAAAA==="), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(base64::decode("A"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base64::decode("AA"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base64::decode("ABCDE"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base64::decode("A==="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base64::decode("AAAA===="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base64::decode("AAAAA==="), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(base64::decode("A&B="), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base64::decode("--"), cppcodec::symbol_error&); // this is not base64url - REQUIRE_THROWS_AS(base64::decode("__"), cppcodec::symbol_error&); // ...ditto + REQUIRE_THROWS_AS(base64::decode("A&B="), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base64::decode("--"), cppcodec::symbol_error); // this is not base64url + REQUIRE_THROWS_AS(base64::decode("__"), cppcodec::symbol_error); // ...ditto } } @@ -679,13 +679,13 @@ TEST_CASE("base64 (unpadded URL-safe)", "[base64][url_unpadded]") { REQUIRE(base64::decode("____") == std::vector({255, 255, 255})); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(base64::decode("A"), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base64::decode("AAAAA"), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(base64::decode("A"), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base64::decode("AAAAA"), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(base64::decode("A&B"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base64::decode("++"), cppcodec::symbol_error&); // this is not standard base64 - REQUIRE_THROWS_AS(base64::decode("//"), cppcodec::symbol_error&); // ...ditto + REQUIRE_THROWS_AS(base64::decode("A&B"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base64::decode("++"), cppcodec::symbol_error); // this is not standard base64 + REQUIRE_THROWS_AS(base64::decode("//"), cppcodec::symbol_error); // ...ditto } } @@ -779,17 +779,17 @@ TEST_CASE("base64 (URL-safe)", "[base64][url]") { REQUIRE(base64::decode("____") == std::vector({255, 255, 255})); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(base64::decode("A"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base64::decode("AA"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base64::decode("ABCDE"), cppcodec::padding_error&); - REQUIRE_THROWS_AS(base64::decode("A==="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base64::decode("AAAA===="), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(base64::decode("AAAAA==="), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(base64::decode("A"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base64::decode("AA"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base64::decode("ABCDE"), cppcodec::padding_error); + REQUIRE_THROWS_AS(base64::decode("A==="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base64::decode("AAAA===="), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(base64::decode("AAAAA==="), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(base64::decode("A&B="), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(base64::decode("++"), cppcodec::symbol_error&); // this is not standard base64 - REQUIRE_THROWS_AS(base64::decode("//"), cppcodec::symbol_error&); // ...ditto + REQUIRE_THROWS_AS(base64::decode("A&B="), cppcodec::symbol_error); + REQUIRE_THROWS_AS(base64::decode("++"), cppcodec::symbol_error); // this is not standard base64 + REQUIRE_THROWS_AS(base64::decode("//"), cppcodec::symbol_error); // ...ditto } } @@ -880,13 +880,13 @@ TEST_CASE("hex (lowercase)", "[hex][lower]") { REQUIRE(hex::decode("666F6f6261") == "fooba"); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(hex::decode("0"), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(hex::decode("000"), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(hex::decode("0"), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(hex::decode("000"), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(hex::decode("1g"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(hex::decode("66 6f"), cppcodec::symbol_error&); // no spaces - REQUIRE_THROWS_AS(hex::decode("66-6f"), cppcodec::symbol_error&); // no dashes + REQUIRE_THROWS_AS(hex::decode("1g"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(hex::decode("66 6f"), cppcodec::symbol_error); // no spaces + REQUIRE_THROWS_AS(hex::decode("66-6f"), cppcodec::symbol_error); // no dashes } } @@ -977,12 +977,12 @@ TEST_CASE("hex (uppercase)", "[hex][upper]") { REQUIRE(hex::decode("666f6F6261") == "fooba"); // An invalid number of symbols should throw the right kind of parse_error. - REQUIRE_THROWS_AS(hex::decode("0"), cppcodec::invalid_input_length&); - REQUIRE_THROWS_AS(hex::decode("000"), cppcodec::invalid_input_length&); + REQUIRE_THROWS_AS(hex::decode("0"), cppcodec::invalid_input_length); + REQUIRE_THROWS_AS(hex::decode("000"), cppcodec::invalid_input_length); // An invalid symbol should throw a symbol error. - REQUIRE_THROWS_AS(hex::decode("1G"), cppcodec::symbol_error&); - REQUIRE_THROWS_AS(hex::decode("66 6F"), cppcodec::symbol_error&); // no spaces - REQUIRE_THROWS_AS(hex::decode("66-6F"), cppcodec::symbol_error&); // no dashes + REQUIRE_THROWS_AS(hex::decode("1G"), cppcodec::symbol_error); + REQUIRE_THROWS_AS(hex::decode("66 6F"), cppcodec::symbol_error); // no spaces + REQUIRE_THROWS_AS(hex::decode("66-6F"), cppcodec::symbol_error); // no dashes } }