summaryrefslogtreecommitdiff
path: root/tests/test_generic-backend.cpp
blob: 0b39bf375c5a2ee8c947f3f41eda6c767be36deb (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 *  Copyright (c) 2017 - 2018 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 <generic-backend/gobj.h>
#include <generic-backend/gstore.h>
//#include <generic-backend/algo-validation.h>

#include <boost/test/unit_test.hpp>

using namespace CKM;

namespace {

class GObjTest : public Crypto::GObj {
public:
	GObjTest() : Crypto::GObj() {}
};

class GStoreTest : public Crypto::GStore {
public:
	GStoreTest(CryptoBackend backendId) : Crypto::GStore(backendId) {}
};

struct TestException : public std::exception {};

class ThrowingHandlerTest {
public:
	static void Handle(const std::string &)
	{
		throw TestException();
	}
};

} // namespace anonymous

BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST)

BOOST_AUTO_TEST_CASE(gobj)
{
	GObjTest obj;

	BOOST_REQUIRE_THROW(obj.getBinary(), Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(obj.encrypt(CryptoAlgorithm(), RawBuffer()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(obj.decrypt(CryptoAlgorithm(), RawBuffer()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(obj.sign(CryptoAlgorithm(), RawBuffer()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(obj.verify(CryptoAlgorithm(), RawBuffer(), RawBuffer()),
						Exc::Crypto::OperationNotSupported);
}

BOOST_AUTO_TEST_CASE(gstore)
{
	GStoreTest store(static_cast<CryptoBackend>(0));

	BOOST_REQUIRE_THROW(store.getObject(Token(), Password()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(store.generateAKey(CryptoAlgorithm(), Password(), Password()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(store.generateSKey(CryptoAlgorithm(), Password()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(store.import(Crypto::Data(), Password(), RawBuffer()),
						Exc::Crypto::OperationNotSupported);
	BOOST_REQUIRE_THROW(store.destroy(Token()),
						Exc::Crypto::OperationNotSupported);
}

#if 0
BOOST_AUTO_TEST_CASE(algo_validation_not_mandatory)
{
	constexpr ParamName pn = ParamName::ALGO_TYPE;
	using Checker = Crypto::ParamCheck<
			pn, int, false, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
			Crypto::DefaultGetter<int>>;

	Checker checker;
	CryptoAlgorithm ca;

	BOOST_REQUIRE_NO_THROW(checker.Check(ca));

	for (int i = 0; i < 5; ++i) {
		ca.setParam(pn, i);
		BOOST_REQUIRE_NO_THROW(checker.Check(ca));
	}

	for (int i = 5; i < 10; ++i) {
		ca.setParam(pn, i);
		BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
	}
}

BOOST_AUTO_TEST_CASE(algo_validation_mandatory)
{
	constexpr ParamName pn = ParamName::ALGO_TYPE;
	using Checker = Crypto::ParamCheck<
			pn, int, true, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
			Crypto::DefaultGetter<int>>;

	Checker checker;
	CryptoAlgorithm ca;

	BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);

	for (int i = 0; i < 5; ++i) {
		ca.setParam(pn, i);
		BOOST_REQUIRE_NO_THROW(checker.Check(ca));
	}

	for (int i = 5; i < 10; ++i) {
		ca.setParam(pn, i);
		BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
	}
}

BOOST_AUTO_TEST_CASE(algo_validation_throwing_handler)
{
	constexpr ParamName pn = ParamName::ALGO_TYPE;
	using Checker = Crypto::ParamCheck<
			pn, int, true, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
			Crypto::DefaultGetter<int>, ThrowingHandlerTest>;

	Checker checker;
	CryptoAlgorithm ca;
	BOOST_REQUIRE_THROW(checker.Check(ca), TestException);

	for (int i = 0; i < 5; ++i) {
		ca.setParam(pn, i);
		BOOST_REQUIRE_NO_THROW(checker.Check(ca));
	}

	for (int i = 5; i < 10; ++i) {
		ca.setParam(pn, i);
		BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
	}
}
#endif

BOOST_AUTO_TEST_SUITE_END()