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>
20 #include <boost/test/unit_test.hpp>
26 class GObjTest : public Crypto::GObj {
28 GObjTest() : Crypto::GObj() {}
31 class GStoreTest : public Crypto::GStore {
33 GStoreTest(CryptoBackend backendId) : Crypto::GStore(backendId) {}
36 struct TestException : public std::exception {};
38 class ThrowingHandlerTest {
40 static void Handle(const std::string &)
42 throw TestException();
46 } // namespace anonymous
48 BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST)
50 BOOST_AUTO_TEST_CASE(gobj)
54 BOOST_REQUIRE_THROW(obj.getBinary(), Exc::Crypto::OperationNotSupported);
55 BOOST_REQUIRE_THROW(obj.encrypt(CryptoAlgorithm(), RawBuffer()),
56 Exc::Crypto::OperationNotSupported);
57 BOOST_REQUIRE_THROW(obj.decrypt(CryptoAlgorithm(), RawBuffer()),
58 Exc::Crypto::OperationNotSupported);
59 BOOST_REQUIRE_THROW(obj.sign(CryptoAlgorithm(), RawBuffer()),
60 Exc::Crypto::OperationNotSupported);
61 BOOST_REQUIRE_THROW(obj.verify(CryptoAlgorithm(), RawBuffer(), RawBuffer()),
62 Exc::Crypto::OperationNotSupported);
65 BOOST_AUTO_TEST_CASE(gstore)
67 GStoreTest store(static_cast<CryptoBackend>(0));
69 BOOST_REQUIRE_THROW(store.getObject(Token(), Password()),
70 Exc::Crypto::OperationNotSupported);
71 BOOST_REQUIRE_THROW(store.generateAKey(CryptoAlgorithm(), Password(), Password()),
72 Exc::Crypto::OperationNotSupported);
73 BOOST_REQUIRE_THROW(store.generateSKey(CryptoAlgorithm(), Password()),
74 Exc::Crypto::OperationNotSupported);
75 BOOST_REQUIRE_THROW(store.import(Crypto::Data(), Password(), RawBuffer()),
76 Exc::Crypto::OperationNotSupported);
77 BOOST_REQUIRE_THROW(store.destroy(Token()),
78 Exc::Crypto::OperationNotSupported);
82 BOOST_AUTO_TEST_CASE(algo_validation_not_mandatory)
84 constexpr ParamName pn = ParamName::ALGO_TYPE;
85 using Checker = Crypto::ParamCheck<
86 pn, int, false, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
87 Crypto::DefaultGetter<int>>;
92 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
94 for (int i = 0; i < 5; ++i) {
96 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
99 for (int i = 5; i < 10; ++i) {
101 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
105 BOOST_AUTO_TEST_CASE(algo_validation_mandatory)
107 constexpr ParamName pn = ParamName::ALGO_TYPE;
108 using Checker = Crypto::ParamCheck<
109 pn, int, true, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
110 Crypto::DefaultGetter<int>>;
115 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
117 for (int i = 0; i < 5; ++i) {
119 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
122 for (int i = 5; i < 10; ++i) {
124 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
128 BOOST_AUTO_TEST_CASE(algo_validation_throwing_handler)
130 constexpr ParamName pn = ParamName::ALGO_TYPE;
131 using Checker = Crypto::ParamCheck<
132 pn, int, true, Crypto::Type<int>::Equals<0, 1, 2, 3, 4>,
133 Crypto::DefaultGetter<int>, ThrowingHandlerTest>;
137 BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
139 for (int i = 0; i < 5; ++i) {
141 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
144 for (int i = 5; i < 10; ++i) {
146 BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
151 BOOST_AUTO_TEST_SUITE_END()