Unification of import methods in gstore
[platform/core/security/key-manager.git] / tests / test_generic-backend.cpp
1 /*
2  *  Copyright (c) 2017 - 2018 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 <generic-backend/gobj.h>
17 #include <generic-backend/gstore.h>
18 //#include <generic-backend/algo-validation.h>
19
20 #include <boost/test/unit_test.hpp>
21
22 using namespace CKM;
23
24 namespace {
25
26 class GObjTest : public Crypto::GObj {
27 public:
28         GObjTest() : Crypto::GObj() {}
29 };
30
31 class GStoreTest : public Crypto::GStore {
32 public:
33         GStoreTest(CryptoBackend backendId) : Crypto::GStore(backendId) {}
34 };
35
36 struct TestException : public std::exception {};
37
38 class ThrowingHandlerTest {
39 public:
40         static void Handle(const std::string &)
41         {
42                 throw TestException();
43         }
44 };
45
46 } // namespace anonymous
47
48 BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST)
49
50 BOOST_AUTO_TEST_CASE(gobj)
51 {
52         GObjTest obj;
53
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);
63 }
64
65 BOOST_AUTO_TEST_CASE(gstore)
66 {
67         GStoreTest store(static_cast<CryptoBackend>(0));
68
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);
79 }
80
81 #if 0
82 BOOST_AUTO_TEST_CASE(algo_validation_not_mandatory)
83 {
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>>;
88
89         Checker checker;
90         CryptoAlgorithm ca;
91
92         BOOST_REQUIRE_NO_THROW(checker.Check(ca));
93
94         for (int i = 0; i < 5; ++i) {
95                 ca.setParam(pn, i);
96                 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
97         }
98
99         for (int i = 5; i < 10; ++i) {
100                 ca.setParam(pn, i);
101                 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
102         }
103 }
104
105 BOOST_AUTO_TEST_CASE(algo_validation_mandatory)
106 {
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>>;
111
112         Checker checker;
113         CryptoAlgorithm ca;
114
115         BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
116
117         for (int i = 0; i < 5; ++i) {
118                 ca.setParam(pn, i);
119                 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
120         }
121
122         for (int i = 5; i < 10; ++i) {
123                 ca.setParam(pn, i);
124                 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
125         }
126 }
127
128 BOOST_AUTO_TEST_CASE(algo_validation_throwing_handler)
129 {
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>;
134
135         Checker checker;
136         CryptoAlgorithm ca;
137         BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
138
139         for (int i = 0; i < 5; ++i) {
140                 ca.setParam(pn, i);
141                 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
142         }
143
144         for (int i = 5; i < 10; ++i) {
145                 ca.setParam(pn, i);
146                 BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
147         }
148 }
149 #endif
150
151 BOOST_AUTO_TEST_SUITE_END()