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