Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_data-type.cpp
1 /*
2  *  Copyright (c) 2016 - 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  * @file        test_data-type.cpp
17  * @author      Kyungwook Tak (k.tak@samsung.com)
18  * @version     1.0
19  * @brief       DataType class test
20  */
21 #include <data-type.h>
22
23 #include <boost_macros_wrapper.h>
24
25 #include <ckm/ckm-type.h>
26 #include <exception.h>
27 #include <message-buffer.h>
28
29 using namespace CKM;
30
31 BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST)
32
33 NEGATIVE_TEST_CASE(CONSTRUCTOR)
34 {
35         BOOST_REQUIRE_THROW(DataType(KeyType::KEY_NONE), Exc::InputParam);
36         BOOST_REQUIRE_THROW(DataType(static_cast<KeyType>(static_cast<int>(KeyType::KEY_AES) + 1)),
37                                                 Exc::InputParam);
38         BOOST_REQUIRE_THROW(DataType(static_cast<DataType::Type>(DataType::DB_FIRST - 1)),
39                                                 Exc::InputParam);
40         BOOST_REQUIRE_THROW(DataType(static_cast<DataType::Type>(DataType::DB_LAST + 1)),
41                                                 Exc::InputParam);
42 }
43
44 NEGATIVE_TEST_CASE(SERIALIZATION)
45 {
46         MessageBuffer msg;
47         msg.Push(SerializeMessage(static_cast<int>(DataType::DB_FIRST - 1)));
48         BOOST_REQUIRE_THROW(DataType{msg}, Exc::InputParam);
49         msg.Push(SerializeMessage(static_cast<int>(DataType::DB_LAST + 1)));
50         BOOST_REQUIRE_THROW(DataType{msg}, Exc::InputParam);
51 }
52
53 POSITIVE_TEST_CASE(SERIALIZATION)
54 {
55         auto checkSerialization = [](DataType::Type typeToCheck){
56                 MessageBuffer msg;
57                 DataType dt;
58
59                 BOOST_REQUIRE_NO_THROW(msg.Push(SerializeMessage(DataType(typeToCheck))));
60                 BOOST_REQUIRE_NO_THROW(dt = DataType{msg});
61                 BOOST_REQUIRE(dt == typeToCheck);
62         };
63
64         checkSerialization(DataType::DB_FIRST);
65         checkSerialization(DataType::DB_LAST);
66 }
67
68 POSITIVE_TEST_CASE(KEY_TYPE_CASTING)
69 {
70         std::vector<std::pair<DataType, KeyType>> pairs;
71
72         pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
73         pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
74
75         pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
76         pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
77
78         pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
79         pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
80
81         pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
82
83         for (auto &p : pairs)
84                 BOOST_REQUIRE(p.first == DataType(p.second));
85 }
86
87 POSITIVE_TEST_CASE(UNARY_OPERATIONS)
88 {
89         BOOST_REQUIRE(DataType(DataType::KEY_AES).isSymmetricKey());
90         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSymmetricKey());
91
92         BOOST_REQUIRE(DataType(DataType::DB_CHAIN_FIRST).isChainCert());
93         BOOST_REQUIRE(DataType(DataType::DB_CHAIN_LAST).isChainCert());
94         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isChainCert());
95
96         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKeyPublic());
97         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKeyPublic());
98         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPublic());
99         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PRIVATE).isKeyPublic());
100         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PRIVATE).isKeyPublic());
101         BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPublic());
102         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPublic());
103         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isKeyPublic());
104
105         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKeyPrivate());
106         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKeyPrivate());
107         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPrivate());
108         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isKeyPrivate());
109         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isKeyPrivate());
110         BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPrivate());
111         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPrivate());
112         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKeyPrivate());
113
114         BOOST_REQUIRE(DataType(DataType::CERTIFICATE).isCertificate());
115         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isCertificate());
116         BOOST_REQUIRE(!DataType().isCertificate());
117         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isCertificate());
118
119         BOOST_REQUIRE(DataType().isBinaryData());
120         BOOST_REQUIRE(DataType(DataType::BINARY_DATA).isBinaryData());
121         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isBinaryData());
122         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isBinaryData());
123         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isBinaryData());
124
125         BOOST_REQUIRE(DataType(DataType::DB_KEY_FIRST).isKey());
126         BOOST_REQUIRE(DataType(DataType::DB_KEY_LAST).isKey());
127         BOOST_REQUIRE(DataType(DataType::KEY_AES).isKey());
128         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKey());
129         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKey());
130         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKey());
131         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKey());
132         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKey());
133         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKey());
134         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKey());
135         BOOST_REQUIRE(!DataType(DataType::CERTIFICATE).isKey());
136         BOOST_REQUIRE(!DataType().isKey());
137
138         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isEllipticCurve());
139         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isEllipticCurve());
140         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PRIVATE).isEllipticCurve());
141         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isEllipticCurve());
142         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PRIVATE).isEllipticCurve());
143         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isEllipticCurve());
144         BOOST_REQUIRE(!DataType().isEllipticCurve());
145 }
146
147 POSITIVE_TEST_CASE(GET_CHAIN_TYPE)
148 {
149         DataType type;
150
151         BOOST_REQUIRE(type.getChainDatatype(0) == DataType(DataType::DB_CHAIN_FIRST));
152         BOOST_REQUIRE(type.getChainDatatype(5) == DataType(DataType::CHAIN_CERT_5));
153         BOOST_REQUIRE(type.getChainDatatype(8) == DataType(DataType::CHAIN_CERT_8));
154         BOOST_REQUIRE(type.getChainDatatype(13) == DataType(DataType::CHAIN_CERT_13));
155         BOOST_REQUIRE(type.getChainDatatype(15) == DataType(DataType::DB_CHAIN_LAST));
156 }
157
158 NEGATIVE_TEST_CASE(GET_CHAIN_TYPE)
159 {
160         DataType type;
161
162         BOOST_REQUIRE_THROW(type.getChainDatatype(16), Exc::InputParam);
163 }
164
165 BOOST_AUTO_TEST_SUITE_END()