Make some single arg constructors explicit
[platform/core/security/key-manager.git] / tests / test_data-type.cpp
1 /*
2  *  Copyright (c) 2016 - 2019 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 #include <exception.h>
27
28 using CKM::DataType;
29 using CKM::KeyType;
30 using CKM::AlgoType;
31
32 BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST)
33
34 BOOST_AUTO_TEST_CASE(CONSTRUCTOR)
35 {
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);
40 }
41
42 BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING)
43 {
44         std::vector<std::pair<DataType, KeyType>> pairs;
45
46         pairs.emplace_back(DataType::KEY_RSA_PUBLIC, KeyType::KEY_RSA_PUBLIC);
47         pairs.emplace_back(DataType::KEY_RSA_PRIVATE, KeyType::KEY_RSA_PRIVATE);
48
49         pairs.emplace_back(DataType::KEY_DSA_PUBLIC, KeyType::KEY_DSA_PUBLIC);
50         pairs.emplace_back(DataType::KEY_DSA_PRIVATE, KeyType::KEY_DSA_PRIVATE);
51
52         pairs.emplace_back(DataType::KEY_ECDSA_PUBLIC, KeyType::KEY_ECDSA_PUBLIC);
53         pairs.emplace_back(DataType::KEY_ECDSA_PRIVATE, KeyType::KEY_ECDSA_PRIVATE);
54
55         pairs.emplace_back(DataType::KEY_AES, KeyType::KEY_AES);
56
57         for (auto &p : pairs)
58                 BOOST_REQUIRE(p.second == DataType(static_cast<KeyType>(p.first)));
59 }
60
61 BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS)
62 {
63         BOOST_REQUIRE(DataType(DataType::KEY_AES).isSKey());
64         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSKey());
65
66         BOOST_REQUIRE(DataType(DataType::DB_CHAIN_FIRST).isChainCert());
67         BOOST_REQUIRE(DataType(DataType::DB_CHAIN_LAST).isChainCert());
68         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isChainCert());
69
70         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKeyPublic());
71         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKeyPublic());
72         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPublic());
73         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PRIVATE).isKeyPublic());
74         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PRIVATE).isKeyPublic());
75         BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPublic());
76         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPublic());
77         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isKeyPublic());
78
79         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKeyPrivate());
80         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKeyPrivate());
81         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKeyPrivate());
82         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isKeyPrivate());
83         BOOST_REQUIRE(!DataType(DataType::KEY_DSA_PUBLIC).isKeyPrivate());
84         BOOST_REQUIRE(!DataType(DataType::KEY_ECDSA_PUBLIC).isKeyPrivate());
85         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isKeyPrivate());
86         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKeyPrivate());
87
88         BOOST_REQUIRE(DataType(DataType::CERTIFICATE).isCertificate());
89         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isCertificate());
90         BOOST_REQUIRE(!DataType().isCertificate());
91         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isCertificate());
92
93         BOOST_REQUIRE(DataType().isBinaryData());
94         BOOST_REQUIRE(DataType(DataType::BINARY_DATA).isBinaryData());
95         BOOST_REQUIRE(!DataType(DataType::KEY_AES).isBinaryData());
96         BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isBinaryData());
97         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_LAST).isBinaryData());
98
99         BOOST_REQUIRE(DataType(DataType::DB_KEY_FIRST).isKey());
100         BOOST_REQUIRE(DataType(DataType::DB_KEY_LAST).isKey());
101         BOOST_REQUIRE(DataType(DataType::KEY_AES).isKey());
102         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PUBLIC).isKey());
103         BOOST_REQUIRE(DataType(DataType::KEY_RSA_PRIVATE).isKey());
104         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PUBLIC).isKey());
105         BOOST_REQUIRE(DataType(DataType::KEY_DSA_PRIVATE).isKey());
106         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PUBLIC).isKey());
107         BOOST_REQUIRE(DataType(DataType::KEY_ECDSA_PRIVATE).isKey());
108         BOOST_REQUIRE(!DataType(DataType::DB_CHAIN_FIRST).isKey());
109         BOOST_REQUIRE(!DataType(DataType::CERTIFICATE).isKey());
110         BOOST_REQUIRE(!DataType().isKey());
111 }
112
113 BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE)
114 {
115         DataType type;
116
117         BOOST_REQUIRE(type.getChainDatatype(0) == DataType(DataType::DB_CHAIN_FIRST));
118         BOOST_REQUIRE(type.getChainDatatype(5) == DataType(DataType::CHAIN_CERT_5));
119         BOOST_REQUIRE(type.getChainDatatype(8) == DataType(DataType::CHAIN_CERT_8));
120         BOOST_REQUIRE(type.getChainDatatype(13) == DataType(DataType::CHAIN_CERT_13));
121         BOOST_REQUIRE(type.getChainDatatype(15) == DataType(DataType::DB_CHAIN_LAST));
122
123         BOOST_REQUIRE_THROW(type.getChainDatatype(16), CKM::Exc::InputParam);
124 }
125
126 BOOST_AUTO_TEST_SUITE_END()