2 * Copyright (c) 2017 - 2018 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 #include <generic-backend/gobj.h>
17 #include <generic-backend/gstore.h>
18 #include <generic-backend/algo-validation.h>
19 #include <generic-backend/encryption-params.h>
21 #include <boost/test/unit_test.hpp>
27 class GObjTest : public Crypto::GObj {
29 GObjTest() : Crypto::GObj() {}
32 class GStoreTest : public Crypto::GStore {
34 GStoreTest(CryptoBackend backendId) : Crypto::GStore(backendId) {}
37 struct TestException : public std::exception {};
39 class ThrowingHandlerTest {
41 static void Handle(const std::string &)
43 throw TestException();
47 } // namespace anonymous
49 BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST)
51 BOOST_AUTO_TEST_CASE(gobj)
55 BOOST_REQUIRE_THROW(obj.getBinary(), Exc::Crypto::OperationNotSupported);
56 BOOST_REQUIRE_THROW(obj.encrypt(CryptoAlgorithm(), RawBuffer()),
57 Exc::Crypto::OperationNotSupported);
58 BOOST_REQUIRE_THROW(obj.decrypt(CryptoAlgorithm(), RawBuffer()),
59 Exc::Crypto::OperationNotSupported);
60 BOOST_REQUIRE_THROW(obj.sign(CryptoAlgorithm(), RawBuffer()),
61 Exc::Crypto::OperationNotSupported);
62 BOOST_REQUIRE_THROW(obj.verify(CryptoAlgorithm(), RawBuffer(), RawBuffer()),
63 Exc::Crypto::OperationNotSupported);
66 BOOST_AUTO_TEST_CASE(gstore)
68 GStoreTest store(static_cast<CryptoBackend>(0));
70 BOOST_REQUIRE_THROW(store.getObject(Token(), Password()),
71 Exc::Crypto::OperationNotSupported);
72 BOOST_REQUIRE_THROW(store.generateAKey(CryptoAlgorithm(), Password(), Password()),
73 Exc::Crypto::OperationNotSupported);
74 BOOST_REQUIRE_THROW(store.generateSKey(CryptoAlgorithm(), Password()),
75 Exc::Crypto::OperationNotSupported);
76 BOOST_REQUIRE_THROW(store.import(Crypto::Data(), Password(), Crypto::EncryptionParams()),
77 Exc::Crypto::OperationNotSupported);
78 BOOST_REQUIRE_THROW(store.destroy(Token()),
79 Exc::Crypto::OperationNotSupported);
83 BOOST_AUTO_TEST_CASE(algo_validation_not_mandatory)
85 constexpr ParamName pn = ParamName::ALGO_TYPE;
86 using Checker = Crypto::ParamCheck<
87 pn, int, false, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
88 Crypto::DefaultGetter<int>>;
93 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
95 for (int i = 0; i < 5; ++i) {
97 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
100 for (int i = 5; i < 10; ++i) {
102 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
106 BOOST_AUTO_TEST_CASE(algo_validation_mandatory)
108 constexpr ParamName pn = ParamName::ALGO_TYPE;
109 using Checker = Crypto::ParamCheck<
110 pn, int, true, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
111 Crypto::DefaultGetter<int>>;
116 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
118 for (int i = 0; i < 5; ++i) {
120 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
123 for (int i = 5; i < 10; ++i) {
125 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
129 BOOST_AUTO_TEST_CASE(algo_validation_throwing_handler)
131 constexpr ParamName pn = ParamName::ALGO_TYPE;
132 using Checker = Crypto::ParamCheck<
133 pn, int, true, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
134 Crypto::DefaultGetter<int>, ThrowingHandlerTest>;
138 BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
140 for (int i = 0; i < 5; ++i) {
142 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
145 for (int i = 5; i < 10; ++i) {
147 BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
152 BOOST_AUTO_TEST_SUITE_END()