Unification of import methods in gstore
[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 void checkIntParam(const CryptoAlgorithm &algo, ParamName name,
40                                    uint64_t expected)
41 {
42         uint64_t integer;
43         BOOST_REQUIRE_MESSAGE(algo.getParam(name, integer),
44                                                   "Failed to get parameter " << static_cast<int>(name));
45         BOOST_REQUIRE_MESSAGE(
46                 integer == expected,
47                 "Parameter " << static_cast<int>(name) <<
48                 " expected value: " << expected <<
49                 " got: " << integer);
50 }
51
52 void checkIntParamNegative(const CryptoAlgorithm &algo, ParamName name)
53 {
54         uint64_t integer;
55         BOOST_REQUIRE_MESSAGE(!algo.getParam(name, integer),
56                                                   "Getting int parameter " << static_cast<int>(name) << " should fail");
57 }
58
59 void checkBufferParam(const CryptoAlgorithm &algo, ParamName name,
60                                           RawBuffer expected)
61 {
62         RawBuffer buffer;
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");
67 }
68
69 void checkBufferParamNegative(const CryptoAlgorithm &algo, ParamName name)
70 {
71         RawBuffer buffer;
72         BOOST_REQUIRE_MESSAGE(!algo.getParam(name, buffer),
73                                                   "Getting buffer parameter " << static_cast<int>(name) << " should fail");
74 }
75
76 template <typename T>
77 void setParam(CryptoAlgorithm &algo, ParamName name, const T &value,
78                           bool success)
79 {
80         BOOST_REQUIRE_MESSAGE(success == algo.setParam(name, value),
81                                                   "Adding param " << static_cast<int>(name) <<
82                                                   " should " << (success ? "succeed" : "fail"));
83 }
84
85 } // namespace anonymous
86
87 BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
88
89 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm)
90 {
91         CryptoAlgorithm ca;
92         setParam(ca, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM),
93                          true);
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);
98
99         CryptoAlgorithmSerializable input(ca);
100         CryptoAlgorithmSerializable output;
101         auto msg = MessageBuffer::Serialize(input);
102         RawBuffer buffer = msg.Pop();
103         MessageBuffer resp;
104         resp.Push(buffer);
105         resp.Deserialize(output);
106
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);
112
113         // wrong type
114         checkBufferParamNegative(output, ParamName::ALGO_TYPE);
115         checkIntParamNegative(output, ParamName::ED_IV);
116
117         // non-existing
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);
124
125         checkIntParamNegative(output, static_cast<ParamName>(666));
126 }
127
128 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name)
129 {
130         CryptoAlgorithm ca;
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);
135
136         CryptoAlgorithmSerializable input(ca);
137         CryptoAlgorithmSerializable output;
138         auto msg = MessageBuffer::Serialize(input);
139         RawBuffer buffer = msg.Pop();
140         MessageBuffer resp;
141         resp.Push(buffer);
142         BOOST_REQUIRE_THROW(resp.Deserialize(output),
143                                                 CryptoAlgorithmSerializable::UnsupportedParam);
144 }
145
146 BOOST_AUTO_TEST_SUITE_END()