2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file test_serialization.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
24 #include <boost/test/unit_test.hpp>
25 #include <test_common.h>
27 #include <ckm/ckm-raw-buffer.h>
28 #include <protocols.h>
29 #include <message-buffer.h>
34 std::string IV_STR("1234567890123456");
35 std::string AAD_STR("sdfdsgsghrtkghwiuho3irhfoewituhre");
36 RawBuffer IV(IV_STR.begin(), IV_STR.end());
37 RawBuffer AAD(AAD_STR.begin(), AAD_STR.end());
39 void checkIntParam(const CryptoAlgorithm& algo, ParamName name, uint64_t expected)
42 BOOST_REQUIRE_MESSAGE(algo.getParam(name, integer),
43 "Failed to get parameter " << static_cast<int>(name));
44 BOOST_REQUIRE_MESSAGE(
46 "Parameter " << static_cast<int>(name) <<
47 " expected value: " << expected <<
51 void checkIntParamNegative(const CryptoAlgorithm& algo, ParamName name)
54 BOOST_REQUIRE_MESSAGE(!algo.getParam(name, integer),
55 "Getting int parameter " << static_cast<int>(name) << " should fail");
58 void checkBufferParam(const CryptoAlgorithm& algo, ParamName name, RawBuffer expected)
61 BOOST_REQUIRE_MESSAGE(algo.getParam(name, buffer),
62 "Failed to get buffer parameter " << static_cast<int>(name));
63 BOOST_REQUIRE_MESSAGE(buffer == expected,
64 "Parameter " << static_cast<int>(name) << " different than expected");
67 void checkBufferParamNegative(const CryptoAlgorithm& algo, ParamName name)
70 BOOST_REQUIRE_MESSAGE(!algo.getParam(name, buffer),
71 "Getting buffer parameter " << static_cast<int>(name) << " should fail");
75 void setParam(CryptoAlgorithm& algo, ParamName name, const T& value, bool success)
77 BOOST_REQUIRE_MESSAGE(success == algo.setParam(name, value),
78 "Adding param " << static_cast<int>(name) <<
79 " should " << (success ? "succeed":"fail"));
82 } // namespace anonymous
84 BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
86 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) {
88 setParam(ca,ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM), true);
89 setParam(ca,ParamName::ED_IV, AAD, true);
90 setParam(ca,ParamName::ED_IV, IV, true); // try to overwrite
91 setParam(ca,ParamName::ED_TAG_LEN, 128, true);
92 setParam(ca,ParamName::ED_AAD, AAD, true);
94 CryptoAlgorithmSerializable input(ca);
95 CryptoAlgorithmSerializable output;
96 auto msg = MessageBuffer::Serialize(input);
97 RawBuffer buffer = msg.Pop();
100 resp.Deserialize(output);
102 checkIntParam(output, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM));
103 checkBufferParam(output, ParamName::ED_IV, IV);
104 checkIntParam(output, ParamName::ED_TAG_LEN, 128);
105 checkBufferParam(output, ParamName::ED_AAD, AAD);
108 checkBufferParamNegative(output, ParamName::ALGO_TYPE);
109 checkIntParamNegative(output, ParamName::ED_IV);
112 checkIntParamNegative(output, ParamName::ED_CTR_LEN);
113 checkBufferParamNegative(output, ParamName::ED_LABEL);
114 checkIntParamNegative(output, ParamName::GEN_KEY_LEN);
115 checkIntParamNegative(output, ParamName::GEN_EC);
116 checkIntParamNegative(output, ParamName::SV_HASH_ALGO);
117 checkIntParamNegative(output, ParamName::SV_RSA_PADDING);
119 checkIntParamNegative(output, static_cast<ParamName>(666));
122 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) {
124 // param name out of range
125 setParam(ca, static_cast<ParamName>(666), 666, false);
126 // param name not supported by serializer
127 setParam(ca, static_cast<ParamName>(10), 666, true);
129 CryptoAlgorithmSerializable input(ca);
130 CryptoAlgorithmSerializable output;
131 auto msg = MessageBuffer::Serialize(input);
132 RawBuffer buffer = msg.Pop();
135 BOOST_REQUIRE_THROW(resp.Deserialize(output), CryptoAlgorithmSerializable::UnsupportedParam);
138 BOOST_AUTO_TEST_SUITE_END()