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)), DataType::Exception::OutOfRange);
36 BOOST_REQUIRE_THROW(DataType(static_cast<KeyType>(999)), DataType::Exception::OutOfRange);
38 std::vector<DataType> types;
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));
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));
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));
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));
73 DataType(static_cast<AlgoType>(-1)),
74 DataType::Exception::OutOfRange);
77 BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING)
79 std::vector<std::pair<DataType, KeyType>> pairs;
81 pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
82 pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
84 pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
85 pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
87 pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
88 pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
90 pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
93 BOOST_REQUIRE(p.second == DataType(static_cast<KeyType>(p.first)));
96 BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS)
98 BOOST_REQUIRE(DataType(DataType::KEY_AES).isSKey());
99 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSKey());
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());
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());
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());
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());
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());
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());
148 BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE)
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));
158 BOOST_REQUIRE_THROW(type.getChainDatatype(16), DataType::Exception::OutOfRange);
161 BOOST_AUTO_TEST_SUITE_END()