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>
26 #include <exception.h>
32 BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST)
34 BOOST_AUTO_TEST_CASE(CONSTRUCTOR)
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);
41 std::vector<DataType> types;
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);
49 for (auto &type : types)
50 BOOST_REQUIRE(type == DataType(DataType::KEY_AES));
54 types.emplace_back(AlgoType::RSA_SV);
55 types.emplace_back(AlgoType::RSA_OAEP);
56 types.emplace_back(AlgoType::RSA_GEN);
58 for (auto &type : types)
59 BOOST_REQUIRE(type == DataType(DataType::KEY_RSA_PUBLIC));
63 types.emplace_back(AlgoType::DSA_SV);
64 types.emplace_back(AlgoType::DSA_GEN);
66 for (auto &type : types)
67 BOOST_REQUIRE(type == DataType(DataType::KEY_DSA_PUBLIC));
71 types.emplace_back(AlgoType::ECDSA_SV);
72 types.emplace_back(AlgoType::ECDSA_GEN);
74 for (auto &type : types)
75 BOOST_REQUIRE(type == DataType(DataType::KEY_ECDSA_PUBLIC));
80 DataType(static_cast<AlgoType>(-1)),
81 CKM::Exc::InputParam);
84 BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING)
86 std::vector<std::pair<DataType, KeyType>> pairs;
88 pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
89 pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
91 pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
92 pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
94 pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
95 pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
97 pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
100 BOOST_REQUIRE(p.second == DataType(static_cast<KeyType>(p.first)));
103 BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS)
105 BOOST_REQUIRE(DataType(DataType::KEY_AES).isSKey());
106 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSKey());
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());
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());
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());
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());
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());
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());
155 BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE)
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));
165 BOOST_REQUIRE_THROW(type.getChainDatatype(16), CKM::Exc::InputParam);
168 BOOST_AUTO_TEST_SUITE_END()