diff options
Diffstat (limited to 'tests/test_serialization.cpp')
-rw-r--r-- | tests/test_serialization.cpp | 158 |
1 files changed, 83 insertions, 75 deletions
diff --git a/tests/test_serialization.cpp b/tests/test_serialization.cpp index 653e5fd2..5fff3af8 100644 --- a/tests/test_serialization.cpp +++ b/tests/test_serialization.cpp @@ -36,103 +36,111 @@ std::string AAD_STR("sdfdsgsghrtkghwiuho3irhfoewituhre"); RawBuffer IV(IV_STR.begin(), IV_STR.end()); RawBuffer AAD(AAD_STR.begin(), AAD_STR.end()); -void checkIntParam(const CryptoAlgorithm& algo, ParamName name, uint64_t expected) +void checkIntParam(const CryptoAlgorithm &algo, ParamName name, + uint64_t expected) { - uint64_t integer; - BOOST_REQUIRE_MESSAGE(algo.getParam(name, integer), - "Failed to get parameter " << static_cast<int>(name)); - BOOST_REQUIRE_MESSAGE( - integer == expected, - "Parameter " << static_cast<int>(name) << - " expected value: " << expected << - " got: " << integer); + uint64_t integer; + BOOST_REQUIRE_MESSAGE(algo.getParam(name, integer), + "Failed to get parameter " << static_cast<int>(name)); + BOOST_REQUIRE_MESSAGE( + integer == expected, + "Parameter " << static_cast<int>(name) << + " expected value: " << expected << + " got: " << integer); } -void checkIntParamNegative(const CryptoAlgorithm& algo, ParamName name) +void checkIntParamNegative(const CryptoAlgorithm &algo, ParamName name) { - uint64_t integer; - BOOST_REQUIRE_MESSAGE(!algo.getParam(name, integer), - "Getting int parameter " << static_cast<int>(name) << " should fail"); + uint64_t integer; + BOOST_REQUIRE_MESSAGE(!algo.getParam(name, integer), + "Getting int parameter " << static_cast<int>(name) << " should fail"); } -void checkBufferParam(const CryptoAlgorithm& algo, ParamName name, RawBuffer expected) +void checkBufferParam(const CryptoAlgorithm &algo, ParamName name, + RawBuffer expected) { - RawBuffer buffer; - BOOST_REQUIRE_MESSAGE(algo.getParam(name, buffer), - "Failed to get buffer parameter " << static_cast<int>(name)); - BOOST_REQUIRE_MESSAGE(buffer == expected, - "Parameter " << static_cast<int>(name) << " different than expected"); + RawBuffer buffer; + BOOST_REQUIRE_MESSAGE(algo.getParam(name, buffer), + "Failed to get buffer parameter " << static_cast<int>(name)); + BOOST_REQUIRE_MESSAGE(buffer == expected, + "Parameter " << static_cast<int>(name) << " different than expected"); } -void checkBufferParamNegative(const CryptoAlgorithm& algo, ParamName name) +void checkBufferParamNegative(const CryptoAlgorithm &algo, ParamName name) { - RawBuffer buffer; - BOOST_REQUIRE_MESSAGE(!algo.getParam(name, buffer), - "Getting buffer parameter " << static_cast<int>(name) << " should fail"); + RawBuffer buffer; + BOOST_REQUIRE_MESSAGE(!algo.getParam(name, buffer), + "Getting buffer parameter " << static_cast<int>(name) << " should fail"); } template <typename T> -void setParam(CryptoAlgorithm& algo, ParamName name, const T& value, bool success) +void setParam(CryptoAlgorithm &algo, ParamName name, const T &value, + bool success) { - BOOST_REQUIRE_MESSAGE(success == algo.setParam(name, value), - "Adding param " << static_cast<int>(name) << - " should " << (success ? "succeed":"fail")); + BOOST_REQUIRE_MESSAGE(success == algo.setParam(name, value), + "Adding param " << static_cast<int>(name) << + " should " << (success ? "succeed" : "fail")); } } // namespace anonymous BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST) -BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) { - CryptoAlgorithm ca; - setParam(ca,ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM), true); - setParam(ca,ParamName::ED_IV, AAD, true); - setParam(ca,ParamName::ED_IV, IV, true); // try to overwrite - setParam(ca,ParamName::ED_TAG_LEN, 128, true); - setParam(ca,ParamName::ED_AAD, AAD, true); - - CryptoAlgorithmSerializable input(ca); - CryptoAlgorithmSerializable output; - auto msg = MessageBuffer::Serialize(input); - RawBuffer buffer = msg.Pop(); - MessageBuffer resp; - resp.Push(buffer); - resp.Deserialize(output); - - checkIntParam(output, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM)); - checkBufferParam(output, ParamName::ED_IV, IV); - checkIntParam(output, ParamName::ED_TAG_LEN, 128); - checkBufferParam(output, ParamName::ED_AAD, AAD); - - // wrong type - checkBufferParamNegative(output, ParamName::ALGO_TYPE); - checkIntParamNegative(output, ParamName::ED_IV); - - // non-existing - checkIntParamNegative(output, ParamName::ED_CTR_LEN); - checkBufferParamNegative(output, ParamName::ED_LABEL); - checkIntParamNegative(output, ParamName::GEN_KEY_LEN); - checkIntParamNegative(output, ParamName::GEN_EC); - checkIntParamNegative(output, ParamName::SV_HASH_ALGO); - checkIntParamNegative(output, ParamName::SV_RSA_PADDING); - - checkIntParamNegative(output, static_cast<ParamName>(666)); +BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) +{ + CryptoAlgorithm ca; + setParam(ca, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM), + true); + setParam(ca, ParamName::ED_IV, AAD, true); + setParam(ca, ParamName::ED_IV, IV, true); // try to overwrite + setParam(ca, ParamName::ED_TAG_LEN, 128, true); + setParam(ca, ParamName::ED_AAD, AAD, true); + + CryptoAlgorithmSerializable input(ca); + CryptoAlgorithmSerializable output; + auto msg = MessageBuffer::Serialize(input); + RawBuffer buffer = msg.Pop(); + MessageBuffer resp; + resp.Push(buffer); + resp.Deserialize(output); + + checkIntParam(output, ParamName::ALGO_TYPE, + static_cast<uint64_t>(AlgoType::AES_GCM)); + checkBufferParam(output, ParamName::ED_IV, IV); + checkIntParam(output, ParamName::ED_TAG_LEN, 128); + checkBufferParam(output, ParamName::ED_AAD, AAD); + + // wrong type + checkBufferParamNegative(output, ParamName::ALGO_TYPE); + checkIntParamNegative(output, ParamName::ED_IV); + + // non-existing + checkIntParamNegative(output, ParamName::ED_CTR_LEN); + checkBufferParamNegative(output, ParamName::ED_LABEL); + checkIntParamNegative(output, ParamName::GEN_KEY_LEN); + checkIntParamNegative(output, ParamName::GEN_EC); + checkIntParamNegative(output, ParamName::SV_HASH_ALGO); + checkIntParamNegative(output, ParamName::SV_RSA_PADDING); + + checkIntParamNegative(output, static_cast<ParamName>(666)); } -BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) { - CryptoAlgorithm ca; - // param name out of range - setParam(ca, static_cast<ParamName>(666), 666, false); - // param name not supported by serializer - setParam(ca, static_cast<ParamName>(10), 666, true); - - CryptoAlgorithmSerializable input(ca); - CryptoAlgorithmSerializable output; - auto msg = MessageBuffer::Serialize(input); - RawBuffer buffer = msg.Pop(); - MessageBuffer resp; - resp.Push(buffer); - BOOST_REQUIRE_THROW(resp.Deserialize(output), CryptoAlgorithmSerializable::UnsupportedParam); +BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) +{ + CryptoAlgorithm ca; + // param name out of range + setParam(ca, static_cast<ParamName>(666), 666, false); + // param name not supported by serializer + setParam(ca, static_cast<ParamName>(10), 666, true); + + CryptoAlgorithmSerializable input(ca); + CryptoAlgorithmSerializable output; + auto msg = MessageBuffer::Serialize(input); + RawBuffer buffer = msg.Pop(); + MessageBuffer resp; + resp.Push(buffer); + BOOST_REQUIRE_THROW(resp.Deserialize(output), + CryptoAlgorithmSerializable::UnsupportedParam); } BOOST_AUTO_TEST_SUITE_END() |