summaryrefslogtreecommitdiff
path: root/tests/test_key.cpp
blob: fa805045bc35d5983d0bb0b05daca4e7c12a1ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 *  Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
#include <key-impl.h>
#include <key-aes-impl.h>

#include <boost/test/unit_test.hpp>
#include <string>

namespace {
const std::string PUBKEY_PEM =
	"-----BEGIN PUBLIC KEY-----\n"
	"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0EJRdUtd2th0vTVF7Qxv\n"
	"DKzyFCF3w9vC9IDE/Yr12w+a9jd0s7/eG96qTHIYffS3B7x2MB+d4n+SR3W0qmYh\n"
	"7xk8qfEgH3daeDoV59IZ9r543KM+g8jm6KffYGX1bIJVVY5OhBRbO9nY6byYpd5k\n"
	"bCIUB6dCf7/WrQl1aIdLGFIegAzPGFPXDcU6F192686x54bxt/itMX4agHJ9ZC/r\n"
	"rTBIZghVsjJo5/AH5WZpasv8sfrGiiohAxtieoYoJkv5MOYP4/2lPlOY+Cgw1Yoz\n"
	"+HHv31AllgFsBquBb/kJVmCCNsAOcnvQzTZUsW/TXz9G2nwRdqI1nSy2JvVjZGsq\n"
	"GQIDAQAB\n"
	"-----END PUBLIC KEY-----\n";
}

using namespace CKM;

BOOST_AUTO_TEST_SUITE(KEY_TEST)

BOOST_AUTO_TEST_CASE(constructors)
{
	RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end());

	KeyImpl key(keybuf);
	BOOST_REQUIRE(!key.empty());

	// valid key type case
	BOOST_REQUIRE(!KeyImpl(key.getEvpShPtr(), KeyType::KEY_RSA_PUBLIC).empty());

	// invalid key type cases
	BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_DSA_PUBLIC).empty());
	BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_DSA_PRIVATE).empty());
	BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_ECDSA_PUBLIC).empty());
	BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_ECDSA_PRIVATE).empty());
	BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_AES).empty());
	BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), static_cast<KeyType>(999999)).empty());
}

BOOST_AUTO_TEST_CASE(get_size)
{
	RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end());

	KeyImpl key(keybuf);
	BOOST_REQUIRE(!key.empty());

	// not ipmlemented yet but test for coverage. It'll just return 0
	BOOST_REQUIRE_NO_THROW(key.getSize());
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(AES_KEY_TEST)

BOOST_AUTO_TEST_CASE(constructors)
{
	// invalid key size
	RawBuffer keybuf({0x01, 0x02, 0x03, 0x04});
	BOOST_REQUIRE(!Key::createAES(keybuf));

	keybuf.clear();
	for (size_t i = 0; i < 16; ++i)
		keybuf.push_back(i);
	BOOST_REQUIRE(Key::createAES(keybuf));

	keybuf.clear();
	for (size_t i = 0; i < 24; ++i)
		keybuf.push_back(i);
	BOOST_REQUIRE(Key::createAES(keybuf));

	keybuf.clear();
	for (size_t i = 0; i < 32; ++i)
		keybuf.push_back(i);
	BOOST_REQUIRE(Key::createAES(keybuf));
}

BOOST_AUTO_TEST_SUITE_END()