Add backend id to database scheme
[platform/core/security/key-manager.git] / tests / test_serialization.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       test_serialization.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <string>
23
24 #include <boost/test/unit_test.hpp>
25 #include <test_common.h>
26
27 #include <ckm/ckm-raw-buffer.h>
28 #include <protocols.h>
29 #include <message-buffer.h>
30
31 using namespace CKM;
32
33 namespace {
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());
38
39 struct BrokenParam : public BaseParam {
40     static BaseParamPtr create() { return BaseParamPtr(new BrokenParam()); }
41 };
42
43 } // namespace anonymous
44
45 BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
46
47 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_positive) {
48     CryptoAlgorithm ca;
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));
53
54     CryptoAlgorithmSerializable input(std::move(ca));
55     CryptoAlgorithmSerializable output;
56     auto msg = MessageBuffer::Serialize(input);
57     RawBuffer buffer = msg.Pop();
58     MessageBuffer resp;
59     resp.Push(buffer);
60     resp.Deserialize(output);
61
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));
65
66     // compare params
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++ )
70     {
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));
74         uint64_t integer[2];
75         RawBuffer buffer[2];
76         if(CKM_API_SUCCESS == iit->second->getInt(integer[0]))
77         {
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");
81         }
82         else if(CKM_API_SUCCESS == iit->second->getBuffer(buffer[0]))
83         {
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");
87         }
88         else
89             BOOST_FAIL("Wrong param type");
90     }
91 }
92
93 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_broken_param) {
94     CryptoAlgorithm ca;
95     ca.m_type = AlgoType::AES_GCM;
96     // unuspported param type
97     ca.m_params.emplace(ParamName::ED_IV, BrokenParam::create());
98
99     CryptoAlgorithmSerializable input(std::move(ca));
100     BOOST_REQUIRE_THROW(auto buffer = MessageBuffer::Serialize(input),
101                         CryptoAlgorithmSerializable::UnsupportedParam);
102 }
103
104 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) {
105     CryptoAlgorithm ca;
106     ca.m_type = AlgoType::AES_GCM;
107     // unuspported param name
108     ca.m_params.emplace(static_cast<ParamName>(666), IntParam::create(666));
109
110     CryptoAlgorithmSerializable input(std::move(ca));
111     CryptoAlgorithmSerializable output;
112     auto msg = MessageBuffer::Serialize(input);
113     RawBuffer buffer = msg.Pop();
114     MessageBuffer resp;
115     resp.Push(buffer);
116     BOOST_REQUIRE_THROW(resp.Deserialize(output), CryptoAlgorithmSerializable::UnsupportedParam);
117 }
118
119 BOOST_AUTO_TEST_SUITE_END()