Merge "Increase backlog for listening sockets" into tizen
[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 #include <generic-backend/encryption-params.h>
20
21 #include <boost/test/unit_test.hpp>
22
23 using namespace CKM;
24
25 namespace {
26
27 class GObjTest : public Crypto::GObj {
28 public:
29         GObjTest() : Crypto::GObj() {}
30 };
31
32 class GStoreTest : public Crypto::GStore {
33 public:
34         GStoreTest(CryptoBackend backendId) : Crypto::GStore(backendId) {}
35 };
36
37 struct TestException : public std::exception {};
38
39 class ThrowingHandlerTest {
40 public:
41         static void Handle(const std::string &)
42         {
43                 throw TestException();
44         }
45 };
46
47 } // namespace anonymous
48
49 BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST)
50
51 BOOST_AUTO_TEST_CASE(gobj)
52 {
53         GObjTest obj;
54
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);
64 }
65
66 BOOST_AUTO_TEST_CASE(gstore)
67 {
68         GStoreTest store(static_cast<CryptoBackend>(0));
69
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);
80 }
81
82 #if 0
83 BOOST_AUTO_TEST_CASE(algo_validation_not_mandatory)
84 {
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>>;
89
90         Checker checker;
91         CryptoAlgorithm ca;
92
93         BOOST_REQUIRE_NO_THROW(checker.Check(ca));
94
95         for (int i = 0; i < 5; ++i) {
96                 ca.setParam(pn, i);
97                 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
98         }
99
100         for (int i = 5; i < 10; ++i) {
101                 ca.setParam(pn, i);
102                 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
103         }
104 }
105
106 BOOST_AUTO_TEST_CASE(algo_validation_mandatory)
107 {
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>>;
112
113         Checker checker;
114         CryptoAlgorithm ca;
115
116         BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
117
118         for (int i = 0; i < 5; ++i) {
119                 ca.setParam(pn, i);
120                 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
121         }
122
123         for (int i = 5; i < 10; ++i) {
124                 ca.setParam(pn, i);
125                 BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam);
126         }
127 }
128
129 BOOST_AUTO_TEST_CASE(algo_validation_throwing_handler)
130 {
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>;
135
136         Checker checker;
137         CryptoAlgorithm ca;
138         BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
139
140         for (int i = 0; i < 5; ++i) {
141                 ca.setParam(pn, i);
142                 BOOST_REQUIRE_NO_THROW(checker.Check(ca));
143         }
144
145         for (int i = 5; i < 10; ++i) {
146                 ca.setParam(pn, i);
147                 BOOST_REQUIRE_THROW(checker.Check(ca), TestException);
148         }
149 }
150 #endif
151
152 BOOST_AUTO_TEST_SUITE_END()