2 * Copyright (c) 2016 - 2020 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_macros_wrapper.h>
25 #include <ckm/ckm-type.h>
26 #include <exception.h>
27 #include <message-buffer.h>
31 BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST)
33 NEGATIVE_TEST_CASE(CONSTRUCTOR)
35 BOOST_REQUIRE_THROW(DataType(KeyType::KEY_NONE), Exc::InputParam);
36 BOOST_REQUIRE_THROW(DataType(static_cast<KeyType>(static_cast<int>(KeyType::KEY_AES) + 1)),
38 BOOST_REQUIRE_THROW(DataType(static_cast<DataType::Type>(DataType::DB_FIRST - 1)),
40 BOOST_REQUIRE_THROW(DataType(static_cast<DataType::Type>(DataType::DB_LAST + 1)),
44 NEGATIVE_TEST_CASE(SERIALIZATION)
47 msg.Push(SerializeMessage(static_cast<int>(DataType::DB_FIRST - 1)));
48 BOOST_REQUIRE_THROW(DataType{msg}, Exc::InputParam);
49 msg.Push(SerializeMessage(static_cast<int>(DataType::DB_LAST + 1)));
50 BOOST_REQUIRE_THROW(DataType{msg}, Exc::InputParam);
53 POSITIVE_TEST_CASE(SERIALIZATION)
55 auto checkSerialization = [](DataType::Type typeToCheck){
59 BOOST_REQUIRE_NO_THROW(msg.Push(SerializeMessage(DataType(typeToCheck))));
60 BOOST_REQUIRE_NO_THROW(dt = DataType{msg});
61 BOOST_REQUIRE(dt == typeToCheck);
64 checkSerialization(DataType::DB_FIRST);
65 checkSerialization(DataType::DB_LAST);
68 POSITIVE_TEST_CASE(KEY_TYPE_CASTING)
70 std::vector<std::pair<DataType, KeyType>> pairs;
72 pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
73 pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
75 pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
76 pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
78 pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
79 pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
81 pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
84 BOOST_REQUIRE(p.first == DataType(p.second));
87 POSITIVE_TEST_CASE(UNARY_OPERATIONS)
89 BOOST_REQUIRE(DataType(DataType::KEY_AES).isSymmetricKey());
90 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSymmetricKey());
92 BOOST_REQUIRE(DataType(DataType::DB_CHAIN_FIRST).isChainCert());
93 BOOST_REQUIRE(DataType(DataType::DB_CHAIN_LAST).isChainCert());
94 BOOST_REQUIRE(!DataType(DataType::KEY_AES).isChainCert());
96 BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKeyPublic());
97 BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKeyPublic());
98 BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPublic());
99 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PRIVATE).isKeyPublic());
100 BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PRIVATE).isKeyPublic());
101 BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPublic());
102 BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPublic());
103 BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isKeyPublic());
105 BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKeyPrivate());
106 BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKeyPrivate());
107 BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPrivate());
108 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isKeyPrivate());
109 BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isKeyPrivate());
110 BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPrivate());
111 BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPrivate());
112 BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKeyPrivate());
114 BOOST_REQUIRE(DataType(DataType::CERTIFICATE).isCertificate());
115 BOOST_REQUIRE(!DataType(DataType::KEY_AES).isCertificate());
116 BOOST_REQUIRE(!DataType().isCertificate());
117 BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isCertificate());
119 BOOST_REQUIRE(DataType().isBinaryData());
120 BOOST_REQUIRE(DataType(DataType::BINARY_DATA).isBinaryData());
121 BOOST_REQUIRE(!DataType(DataType::KEY_AES).isBinaryData());
122 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isBinaryData());
123 BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isBinaryData());
125 BOOST_REQUIRE(DataType(DataType::DB_KEY_FIRST).isKey());
126 BOOST_REQUIRE(DataType(DataType::DB_KEY_LAST).isKey());
127 BOOST_REQUIRE(DataType(DataType::KEY_AES).isKey());
128 BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKey());
129 BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKey());
130 BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKey());
131 BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKey());
132 BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKey());
133 BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKey());
134 BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKey());
135 BOOST_REQUIRE(!DataType(DataType::CERTIFICATE).isKey());
136 BOOST_REQUIRE(!DataType().isKey());
138 BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isEllipticCurve());
139 BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isEllipticCurve());
140 BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PRIVATE).isEllipticCurve());
141 BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isEllipticCurve());
142 BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PRIVATE).isEllipticCurve());
143 BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isEllipticCurve());
144 BOOST_REQUIRE(!DataType().isEllipticCurve());
147 POSITIVE_TEST_CASE(GET_CHAIN_TYPE)
151 BOOST_REQUIRE(type.getChainDatatype(0) == DataType(DataType::DB_CHAIN_FIRST));
152 BOOST_REQUIRE(type.getChainDatatype(5) == DataType(DataType::CHAIN_CERT_5));
153 BOOST_REQUIRE(type.getChainDatatype(8) == DataType(DataType::CHAIN_CERT_8));
154 BOOST_REQUIRE(type.getChainDatatype(13) == DataType(DataType::CHAIN_CERT_13));
155 BOOST_REQUIRE(type.getChainDatatype(15) == DataType(DataType::DB_CHAIN_LAST));
158 NEGATIVE_TEST_CASE(GET_CHAIN_TYPE)
162 BOOST_REQUIRE_THROW(type.getChainDatatype(16), Exc::InputParam);
165 BOOST_AUTO_TEST_SUITE_END()