Coding style applied according to style checker
[platform/core/security/key-manager.git] / tests / test_data-type.cpp
1 /*
2  *  Copyright (c) 2016 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/test/unit_test.hpp>
24
25 #include <ckm/ckm-type.h>
26
27 using CKM::DataType;
28 using CKM::KeyType;
29 using CKM::AlgoType;
30
31 BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST)
32
33 BOOST_AUTO_TEST_CASE(CONSTRUCTOR)
34 {
35         BOOST_REQUIRE_THROW(DataType(static_cast<DataType::Type>(999)),
36                                                 DataType::Exception::OutOfRange);
37         BOOST_REQUIRE_THROW(DataType(static_cast<KeyType>(999)),
38                                                 DataType::Exception::OutOfRange);
39
40         std::vector<DataType> types;
41
42         types.emplace_back(AlgoType::AES_CTR);
43         types.emplace_back(AlgoType::AES_CBC);
44         types.emplace_back(AlgoType::AES_GCM);
45         types.emplace_back(AlgoType::AES_CFB);
46         types.emplace_back(AlgoType::AES_GEN);
47
48         for (auto &type : types)
49                 BOOST_REQUIRE(type == DataType(DataType::KEY_AES));
50
51         types.clear();
52
53         types.emplace_back(AlgoType::RSA_SV);
54         types.emplace_back(AlgoType::RSA_OAEP);
55         types.emplace_back(AlgoType::RSA_GEN);
56
57         for (auto &type : types)
58                 BOOST_REQUIRE(type == DataType(DataType::KEY_RSA_PUBLIC));
59
60         types.clear();
61
62         types.emplace_back(AlgoType::DSA_SV);
63         types.emplace_back(AlgoType::DSA_GEN);
64
65         for (auto &type : types)
66                 BOOST_REQUIRE(type == DataType(DataType::KEY_DSA_PUBLIC));
67
68         types.clear();
69
70         types.emplace_back(AlgoType::ECDSA_SV);
71         types.emplace_back(AlgoType::ECDSA_GEN);
72
73         for (auto &type : types)
74                 BOOST_REQUIRE(type == DataType(DataType::KEY_ECDSA_PUBLIC));
75
76         types.clear();
77
78         BOOST_REQUIRE_THROW(
79                 DataType(static_cast<AlgoType>(-1)),
80                 DataType::Exception::OutOfRange);
81 }
82
83 BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING)
84 {
85         std::vector<std::pair<DataType, KeyType>> pairs;
86
87         pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
88         pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
89
90         pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
91         pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
92
93         pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
94         pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
95
96         pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
97
98         for (auto &p : pairs)
99                 BOOST_REQUIRE(p.second == DataType(static_cast<KeyType>(p.first)));
100 }
101
102 BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS)
103 {
104         BOOST_REQUIRE(DataType(DataType::KEY_AES).isSKey());
105         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSKey());
106
107         BOOST_REQUIRE(DataType(DataType::DB_CHAIN_FIRST).isChainCert());
108         BOOST_REQUIRE(DataType(DataType::DB_CHAIN_LAST).isChainCert());
109         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isChainCert());
110
111         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKeyPublic());
112         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKeyPublic());
113         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPublic());
114         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PRIVATE).isKeyPublic());
115         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PRIVATE).isKeyPublic());
116         BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPublic());
117         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPublic());
118         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isKeyPublic());
119
120         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKeyPrivate());
121         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKeyPrivate());
122         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPrivate());
123         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isKeyPrivate());
124         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isKeyPrivate());
125         BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPrivate());
126         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPrivate());
127         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKeyPrivate());
128
129         BOOST_REQUIRE(DataType(DataType::CERTIFICATE).isCertificate());
130         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isCertificate());
131         BOOST_REQUIRE(!DataType().isCertificate());
132         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isCertificate());
133
134         BOOST_REQUIRE(DataType().isBinaryData());
135         BOOST_REQUIRE(DataType(DataType::BINARY_DATA).isBinaryData());
136         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isBinaryData());
137         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isBinaryData());
138         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isBinaryData());
139
140         BOOST_REQUIRE(DataType(DataType::DB_KEY_FIRST).isKey());
141         BOOST_REQUIRE(DataType(DataType::DB_KEY_LAST).isKey());
142         BOOST_REQUIRE(DataType(DataType::KEY_AES).isKey());
143         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKey());
144         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKey());
145         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKey());
146         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKey());
147         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKey());
148         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKey());
149         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKey());
150         BOOST_REQUIRE(!DataType(DataType::CERTIFICATE).isKey());
151         BOOST_REQUIRE(!DataType().isKey());
152 }
153
154 BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE)
155 {
156         DataType type;
157
158         BOOST_REQUIRE(type.getChainDatatype(0) == DataType(DataType::DB_CHAIN_FIRST));
159         BOOST_REQUIRE(type.getChainDatatype(5) == DataType(DataType::CHAIN_CERT_5));
160         BOOST_REQUIRE(type.getChainDatatype(8) == DataType(DataType::CHAIN_CERT_8));
161         BOOST_REQUIRE(type.getChainDatatype(13) == DataType(DataType::CHAIN_CERT_13));
162         BOOST_REQUIRE(type.getChainDatatype(15) == DataType(DataType::DB_CHAIN_LAST));
163
164         BOOST_REQUIRE_THROW(type.getChainDatatype(16), DataType::Exception::OutOfRange);
165 }
166
167 BOOST_AUTO_TEST_SUITE_END()