Unification of import methods in gstore
[platform/core/security/key-manager.git] / tests / test_key.cpp
1 /*
2  *  Copyright (c) 2000 - 2017 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 #include <key-impl.h>
17 #include <key-aes-impl.h>
18
19 #include <boost/test/unit_test.hpp>
20 #include <string>
21
22 namespace {
23 const std::string PUBKEY_PEM =
24         "-----BEGIN PUBLIC KEY-----\n"
25         "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0EJRdUtd2th0vTVF7Qxv\n"
26         "DKzyFCF3w9vC9IDE/Yr12w+a9jd0s7/eG96qTHIYffS3B7x2MB+d4n+SR3W0qmYh\n"
27         "7xk8qfEgH3daeDoV59IZ9r543KM+g8jm6KffYGX1bIJVVY5OhBRbO9nY6byYpd5k\n"
28         "bCIUB6dCf7/WrQl1aIdLGFIegAzPGFPXDcU6F192686x54bxt/itMX4agHJ9ZC/r\n"
29         "rTBIZghVsjJo5/AH5WZpasv8sfrGiiohAxtieoYoJkv5MOYP4/2lPlOY+Cgw1Yoz\n"
30         "+HHv31AllgFsBquBb/kJVmCCNsAOcnvQzTZUsW/TXz9G2nwRdqI1nSy2JvVjZGsq\n"
31         "GQIDAQAB\n"
32         "-----END PUBLIC KEY-----\n";
33 }
34
35 using namespace CKM;
36
37 BOOST_AUTO_TEST_SUITE(KEY_TEST)
38
39 BOOST_AUTO_TEST_CASE(constructors)
40 {
41         RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end());
42
43         KeyImpl key(keybuf);
44         BOOST_REQUIRE(!key.empty());
45
46         // valid key type case
47         BOOST_REQUIRE(!KeyImpl(key.getEvpShPtr(), KeyType::KEY_RSA_PUBLIC).empty());
48
49         // invalid key type cases
50         BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_DSA_PUBLIC).empty());
51         BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_DSA_PRIVATE).empty());
52         BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_ECDSA_PUBLIC).empty());
53         BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_ECDSA_PRIVATE).empty());
54         BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_AES).empty());
55         BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), static_cast<KeyType>(999999)).empty());
56 }
57
58 BOOST_AUTO_TEST_CASE(get_size)
59 {
60         RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end());
61
62         KeyImpl key(keybuf);
63         BOOST_REQUIRE(!key.empty());
64
65         // not ipmlemented yet but test for coverage. It'll just return 0
66         BOOST_REQUIRE_NO_THROW(key.getSize());
67 }
68
69 BOOST_AUTO_TEST_SUITE_END()
70
71 BOOST_AUTO_TEST_SUITE(AES_KEY_TEST)
72
73 BOOST_AUTO_TEST_CASE(constructors)
74 {
75         // invalid key size
76         RawBuffer keybuf({0x01, 0x02, 0x03, 0x04});
77         BOOST_REQUIRE(!Key::createAES(keybuf));
78
79         keybuf.clear();
80         for (size_t i = 0; i < 16; ++i)
81                 keybuf.push_back(i);
82         BOOST_REQUIRE(Key::createAES(keybuf));
83
84         keybuf.clear();
85         for (size_t i = 0; i < 24; ++i)
86                 keybuf.push_back(i);
87         BOOST_REQUIRE(Key::createAES(keybuf));
88
89         keybuf.clear();
90         for (size_t i = 0; i < 32; ++i)
91                 keybuf.push_back(i);
92         BOOST_REQUIRE(Key::createAES(keybuf));
93 }
94
95 BOOST_AUTO_TEST_SUITE_END()