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 struct BrokenParam : public BaseParam {
40 static BaseParamPtr create() { return BaseParamPtr(new BrokenParam()); }
43 } // namespace anonymous
45 BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
47 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_positive) {
49 ca.m_type = AlgoType::AES_GCM;
50 ca.m_params.emplace(ParamName::ED_IV, BufferParam::create(IV));
51 ca.m_params.emplace(ParamName::ED_TAG_LEN, IntParam::create(128));
52 ca.m_params.emplace(ParamName::ED_AAD, BufferParam::create(AAD));
54 CryptoAlgorithmSerializable input(std::move(ca));
55 CryptoAlgorithmSerializable output;
56 auto msg = MessageBuffer::Serialize(input);
57 RawBuffer buffer = msg.Pop();
60 resp.Deserialize(output);
62 BOOST_REQUIRE_MESSAGE(input.m_type == output.m_type,
63 "Algorithm types don't match: " << static_cast<int>(input.m_type) << "!="
64 << static_cast<int>(output.m_type));
67 auto iit = input.m_params.cbegin();
68 auto oit = output.m_params.cbegin();
69 for(;iit != input.m_params.cend() && oit != output.m_params.cend(); iit++, oit++ )
71 BOOST_REQUIRE_MESSAGE(iit->first == oit->first,
72 "Param names do not match :" << static_cast<int>(iit->first) << "!="
73 << static_cast<int>(oit->first));
76 if(CKM_API_SUCCESS == iit->second->getInt(integer[0]))
78 BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == oit->second->getInt(integer[1]),
79 "Param types do not match");
80 BOOST_REQUIRE_MESSAGE(integer[0] == integer[1], "Integer params do not match");
82 else if(CKM_API_SUCCESS == iit->second->getBuffer(buffer[0]))
84 BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == oit->second->getBuffer(buffer[1]),
85 "Param types do not match");
86 BOOST_REQUIRE_MESSAGE(buffer[0] == buffer[1], "Integer params do not match");
89 BOOST_FAIL("Wrong param type");
93 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_broken_param) {
95 ca.m_type = AlgoType::AES_GCM;
96 // unuspported param type
97 ca.m_params.emplace(ParamName::ED_IV, BrokenParam::create());
99 CryptoAlgorithmSerializable input(std::move(ca));
100 BOOST_REQUIRE_THROW(auto buffer = MessageBuffer::Serialize(input),
101 CryptoAlgorithmSerializable::UnsupportedParam);
104 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) {
106 ca.m_type = AlgoType::AES_GCM;
107 // unuspported param name
108 ca.m_params.emplace(static_cast<ParamName>(666), IntParam::create(666));
110 CryptoAlgorithmSerializable input(std::move(ca));
111 CryptoAlgorithmSerializable output;
112 auto msg = MessageBuffer::Serialize(input);
113 RawBuffer buffer = msg.Pop();
116 BOOST_REQUIRE_THROW(resp.Deserialize(output), CryptoAlgorithmSerializable::UnsupportedParam);
119 BOOST_AUTO_TEST_SUITE_END()