2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * @file test_data-type.cpp
17 * @author Kyungwook Tak (k.tak@samsung.com)
19 * @brief DataType class test
21 #include <data-type.h>
23 #include <boost/test/unit_test.hpp>
25 #include <ckm/ckm-type.h>
31 BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST)
33 BOOST_AUTO_TEST_CASE(CONSTRUCTOR)
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);
40 std::vector<DataType> types;
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);
48 for (auto &type : types)
49 BOOST_REQUIRE(type == DataType(DataType::KEY_AES));
53 types.emplace_back(AlgoType::RSA_SV);
54 types.emplace_back(AlgoType::RSA_OAEP);
55 types.emplace_back(AlgoType::RSA_GEN);
57 for (auto &type : types)
58 BOOST_REQUIRE(type == DataType(DataType::KEY_RSA_PUBLIC));
62 types.emplace_back(AlgoType::DSA_SV);
63 types.emplace_back(AlgoType::DSA_GEN);
65 for (auto &type : types)
66 BOOST_REQUIRE(type == DataType(DataType::KEY_DSA_PUBLIC));
70 types.emplace_back(AlgoType::ECDSA_SV);
71 types.emplace_back(AlgoType::ECDSA_GEN);
73 for (auto &type : types)
74 BOOST_REQUIRE(type == DataType(DataType::KEY_ECDSA_PUBLIC));
79 DataType(static_cast<AlgoType>(-1)),
80 DataType::Exception::OutOfRange);
83 BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING)
85 std::vector<std::pair<DataType, KeyType>> pairs;
87 pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
88 pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
90 pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
91 pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
93 pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
94 pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
96 pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
99 BOOST_REQUIRE(p.second == DataType(static_cast<KeyType>(p.first)));
102 BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS)
104 BOOST_REQUIRE(DataType(DataType::KEY_AES).isSKey());
105 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSKey());
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());
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());
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());
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());
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());
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());
154 BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE)
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));
164 BOOST_REQUIRE_THROW(type.getChainDatatype(16), DataType::Exception::OutOfRange);
167 BOOST_AUTO_TEST_SUITE_END()