Make CryptoAlgorithm copyable.
[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, uint64_t expected)
40 {
41     uint64_t integer;
42     BOOST_REQUIRE_MESSAGE(algo.getParam(name, integer),
43                           "Failed to get parameter " << static_cast<int>(name));
44     BOOST_REQUIRE_MESSAGE(
45             integer == expected,
46             "Parameter " << static_cast<int>(name) <<
47             " expected value: " << expected <<
48             " got: " << integer);
49 }
50
51 void checkIntParamNegative(const CryptoAlgorithm& algo, ParamName name)
52 {
53     uint64_t integer;
54     BOOST_REQUIRE_MESSAGE(!algo.getParam(name, integer),
55                           "Getting int parameter " << static_cast<int>(name) << " should fail");
56 }
57
58 void checkBufferParam(const CryptoAlgorithm& algo, ParamName name, RawBuffer expected)
59 {
60     RawBuffer buffer;
61     BOOST_REQUIRE_MESSAGE(algo.getParam(name, buffer),
62                           "Failed to get buffer parameter " << static_cast<int>(name));
63     BOOST_REQUIRE_MESSAGE(buffer == expected,
64                           "Parameter " << static_cast<int>(name) << " different than expected");
65 }
66
67 void checkBufferParamNegative(const CryptoAlgorithm& algo, ParamName name)
68 {
69     RawBuffer buffer;
70     BOOST_REQUIRE_MESSAGE(!algo.getParam(name, buffer),
71                           "Getting buffer parameter " << static_cast<int>(name) << " should fail");
72 }
73
74 template <typename T>
75 void addParam(CryptoAlgorithm& algo, ParamName name, const T& value, bool success)
76 {
77     BOOST_REQUIRE_MESSAGE(success == algo.addParam(name, value),
78                           "Adding param " << static_cast<int>(name) <<
79                           " should " << (success ? "succeed":"fail"));
80 }
81
82 } // namespace anonymous
83
84 BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
85
86 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) {
87     CryptoAlgorithm ca;
88     addParam(ca,ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM), true);
89     addParam(ca,ParamName::ED_IV, IV, true);
90     addParam(ca,ParamName::ED_IV, AAD, false); // try to overwrite
91     addParam(ca,ParamName::ED_TAG_LEN, 128, true);
92     addParam(ca,ParamName::ED_AAD, AAD, true);
93
94     CryptoAlgorithmSerializable input(ca);
95     CryptoAlgorithmSerializable output;
96     auto msg = MessageBuffer::Serialize(input);
97     RawBuffer buffer = msg.Pop();
98     MessageBuffer resp;
99     resp.Push(buffer);
100     resp.Deserialize(output);
101
102     checkIntParam(output, ParamName::ALGO_TYPE, static_cast<uint64_t>(AlgoType::AES_GCM));
103     checkBufferParam(output, ParamName::ED_IV, IV);
104     checkIntParam(output, ParamName::ED_TAG_LEN, 128);
105     checkBufferParam(output, ParamName::ED_AAD, AAD);
106
107     // wrong type
108     checkBufferParamNegative(output, ParamName::ALGO_TYPE);
109     checkIntParamNegative(output, ParamName::ED_IV);
110
111     // non-existing
112     checkBufferParamNegative(output, ParamName::ED_CTR);
113     checkIntParamNegative(output, ParamName::ED_CTR_LEN);
114     checkBufferParamNegative(output, ParamName::ED_LABEL);
115     checkIntParamNegative(output, ParamName::GEN_KEY_LEN);
116     checkIntParamNegative(output, ParamName::GEN_EC);
117     checkIntParamNegative(output, ParamName::SV_HASH_ALGO);
118     checkIntParamNegative(output, ParamName::SV_RSA_PADDING);
119
120     checkIntParamNegative(output, static_cast<ParamName>(666));
121 }
122
123 BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) {
124     CryptoAlgorithm ca;
125     // unuspported param name
126     addParam(ca, static_cast<ParamName>(666), 666, true);
127
128     CryptoAlgorithmSerializable input(ca);
129     CryptoAlgorithmSerializable output;
130     auto msg = MessageBuffer::Serialize(input);
131     RawBuffer buffer = msg.Pop();
132     MessageBuffer resp;
133     resp.Push(buffer);
134     BOOST_REQUIRE_THROW(resp.Deserialize(output), CryptoAlgorithmSerializable::UnsupportedParam);
135 }
136
137 BOOST_AUTO_TEST_SUITE_END()