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