From 043c8429d47e75e41658868daadc58eba0a50526 Mon Sep 17 00:00:00 2001 From: Matt Frantz Date: Wed, 24 Oct 2018 17:40:11 -0700 Subject: [PATCH 1/2] issue-33: Add test showing how to invoke move ctor --- tests/CMakeLists.txt | 3 +++ tests/test_jwt_object.cc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/test_jwt_object.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c27feb2..c52e256 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,9 @@ link_directories(${OPENSSL_LIBRARIES}) SET(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/certs") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") +add_executable(test_jwt_object test_jwt_object.cc) +target_link_libraries(test_jwt_object ssl crypto gtest) + add_executable(test_jwt_encode test_jwt_encode.cc) target_link_libraries(test_jwt_encode ssl crypto gtest) diff --git a/tests/test_jwt_object.cc b/tests/test_jwt_object.cc new file mode 100644 index 0000000..5b19955 --- /dev/null +++ b/tests/test_jwt_object.cc @@ -0,0 +1,35 @@ +#include "gtest/gtest.h" +#include "jwt/jwt.hpp" + +namespace { + +struct Wrapper +{ + // The std::move here is required to resolve to the move ctor + // rather than to the universal reference ctor. + Wrapper(jwt::jwt_object&& obj) : object{std::move(obj)} {} + jwt::jwt_object object; +}; + +} // END namespace + +TEST (ObjectTest, MoveConstructor) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret")}; + + obj.add_claim("iss", "arun.muralidharan"); + + auto wrapper = Wrapper{std::move(obj)}; + + EXPECT_EQ(wrapper.object.header().algo(), jwt::algorithm::HS256); + EXPECT_EQ(wrapper.object.secret(), "secret"); + EXPECT_TRUE(wrapper.object.payload().has_claim_with_value("iss", "arun.muralidharan")); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From 1b105f47727099bb577857b436460ea877c745c6 Mon Sep 17 00:00:00 2001 From: Matt Frantz Date: Thu, 1 Nov 2018 13:28:49 -0700 Subject: [PATCH 2/2] SFINAE guard on jwt_object's universal reference constructor This allows the move constructor to be preferred in some cases. --- include/jwt/impl/jwt.ipp | 4 +++- include/jwt/jwt.hpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/jwt/impl/jwt.ipp b/include/jwt/impl/jwt.ipp index 8e3649b..e5474e3 100644 --- a/include/jwt/impl/jwt.ipp +++ b/include/jwt/impl/jwt.ipp @@ -290,7 +290,9 @@ jwt_signature::get_verify_algorithm_impl(const jwt_header& hdr) const noexcept // template -jwt_object::jwt_object(First&& first, Rest&&... rest) +jwt_object::jwt_object( + std::enable_if_t::value, First>&& first, + Rest&&... rest) { static_assert (detail::meta::is_parameter_concept::value && detail::meta::are_all_params::value, diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 76eacdd..fbb496e 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -889,7 +889,7 @@ public: // 'tors * to populate header. Not much useful unless JWE is supported. */ template - jwt_object(First&& first, Rest&&... rest); + jwt_object(std::enable_if_t::value, First>&& first, Rest&&... rest); public: // Exposed static APIs /**