Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_serialization.cpp
1 /*
2  *  Copyright (c) 2015 - 2020 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_macros_wrapper.h>
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 POSITIVE_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         MessageBuffer resp;
102         resp.Push(SerializeMessage(input));
103         resp.Deserialize(output);
104
105         checkIntParam(output, ParamName::ALGO_TYPE,
106                                   static_cast<uint64_t>(AlgoType::AES_GCM));
107         checkBufferParam(output, ParamName::ED_IV, IV);
108         checkIntParam(output, ParamName::ED_TAG_LEN, 128);
109         checkBufferParam(output, ParamName::ED_AAD, AAD);
110 }
111
112 NEGATIVE_TEST_CASE(Serialization_CryptoAlgorithm)
113 {
114         CryptoAlgorithm ca;
115         setParam(ca, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM),
116                          true);
117         setParam(ca, ParamName::ED_IV, IV, true);
118         setParam(ca, ParamName::ED_TAG_LEN, 128, true);
119         setParam(ca, ParamName::ED_AAD, AAD, true);
120
121         CryptoAlgorithmSerializable input(ca);
122         CryptoAlgorithmSerializable output;
123         MessageBuffer resp;
124         resp.Push(SerializeMessage(input));
125         resp.Deserialize(output);
126
127         // wrong type
128         checkBufferParamNegative(output, ParamName::ALGO_TYPE);
129         checkIntParamNegative(output, ParamName::ED_IV);
130
131         // non-existing
132         checkIntParamNegative(output, ParamName::ED_CTR_LEN);
133         checkBufferParamNegative(output, ParamName::ED_LABEL);
134         checkIntParamNegative(output, ParamName::GEN_KEY_LEN);
135         checkIntParamNegative(output, ParamName::GEN_EC);
136         checkIntParamNegative(output, ParamName::SV_HASH_ALGO);
137         checkIntParamNegative(output, ParamName::SV_RSA_PADDING);
138
139         checkIntParamNegative(output, static_cast<ParamName>(666));
140 }
141
142 NEGATIVE_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name)
143 {
144         CryptoAlgorithm ca;
145         // param name out of range
146         setParam(ca, static_cast<ParamName>(666), 666, false);
147         // param name not supported by serializer
148         setParam(ca, static_cast<ParamName>(10), 666, true);
149
150         CryptoAlgorithmSerializable input(ca);
151         CryptoAlgorithmSerializable output;
152         MessageBuffer resp;
153         resp.Push(SerializeMessage(input));
154         BOOST_REQUIRE_THROW(resp.Deserialize(output),
155                                                 CryptoAlgorithmSerializable::UnsupportedParam);
156 }
157
158 BOOST_AUTO_TEST_SUITE_END()