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,
43 BOOST_REQUIRE_MESSAGE(algo.getParam(name, integer),
44 "Failed to get parameter " << static_cast<int>(name));
45 BOOST_REQUIRE_MESSAGE(
47 "Parameter " << static_cast<int>(name) <<
48 " expected value: " << expected <<
52 void checkIntParamNegative(const CryptoAlgorithm &algo, ParamName name)
55 BOOST_REQUIRE_MESSAGE(!algo.getParam(name, integer),
56 "Getting int parameter " << static_cast<int>(name) << " should fail");
59 void checkBufferParam(const CryptoAlgorithm &algo, ParamName name,
63 BOOST_REQUIRE_MESSAGE(algo.getParam(name, buffer),
64 "Failed to get buffer parameter " << static_cast<int>(name));
65 BOOST_REQUIRE_MESSAGE(buffer == expected,
66 "Parameter " << static_cast<int>(name) << " different than expected");
69 void checkBufferParamNegative(const CryptoAlgorithm &algo, ParamName name)
72 BOOST_REQUIRE_MESSAGE(!algo.getParam(name, buffer),
73 "Getting buffer parameter " << static_cast<int>(name) << " should fail");
77 void setParam(CryptoAlgorithm &algo, ParamName name, const T &value,
80 BOOST_REQUIRE_MESSAGE(success == algo.setParam(name, value),
81 "Adding param " << static_cast<int>(name) <<
82 " should " << (success ? "succeed" : "fail"));
85 } // namespace anonymous
87 BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
89 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm)
92 setParam(ca, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM),
94 setParam(ca, ParamName::ED_IV, AAD, true);
95 setParam(ca, ParamName::ED_IV, IV, true); // try to overwrite
96 setParam(ca, ParamName::ED_TAG_LEN, 128, true);
97 setParam(ca, ParamName::ED_AAD, AAD, true);
99 CryptoAlgorithmSerializable input(ca);
100 CryptoAlgorithmSerializable output;
101 auto msg = MessageBuffer::Serialize(input);
102 RawBuffer buffer = msg.Pop();
105 resp.Deserialize(output);
107 checkIntParam(output, ParamName::ALGO_TYPE,
108 static_cast<uint64_t>(AlgoType::AES_GCM));
109 checkBufferParam(output, ParamName::ED_IV, IV);
110 checkIntParam(output, ParamName::ED_TAG_LEN, 128);
111 checkBufferParam(output, ParamName::ED_AAD, AAD);
114 checkBufferParamNegative(output, ParamName::ALGO_TYPE);
115 checkIntParamNegative(output, ParamName::ED_IV);
118 checkIntParamNegative(output, ParamName::ED_CTR_LEN);
119 checkBufferParamNegative(output, ParamName::ED_LABEL);
120 checkIntParamNegative(output, ParamName::GEN_KEY_LEN);
121 checkIntParamNegative(output, ParamName::GEN_EC);
122 checkIntParamNegative(output, ParamName::SV_HASH_ALGO);
123 checkIntParamNegative(output, ParamName::SV_RSA_PADDING);
125 checkIntParamNegative(output, static_cast<ParamName>(666));
128 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name)
131 // param name out of range
132 setParam(ca, static_cast<ParamName>(666), 666, false);
133 // param name not supported by serializer
134 setParam(ca, static_cast<ParamName>(10), 666, true);
136 CryptoAlgorithmSerializable input(ca);
137 CryptoAlgorithmSerializable output;
138 auto msg = MessageBuffer::Serialize(input);
139 RawBuffer buffer = msg.Pop();
142 BOOST_REQUIRE_THROW(resp.Deserialize(output),
143 CryptoAlgorithmSerializable::UnsupportedParam);
146 BOOST_AUTO_TEST_SUITE_END()