Merge "Increase backlog for listening sockets" into tizen
[platform/core/security/key-manager.git] / tests / test_crypto-logic.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 <crypto-logic.h>
17 #include <platform/decider.h>
18 #include <generic-backend/gstore.h>
19 #include <db-row.h>
20
21 #include <utility>
22 #include <cstdlib>
23 #include <time.h>
24 #include <boost/test/unit_test.hpp>
25
26 #include "test_common.h"
27
28 using namespace CKM;
29
30 namespace {
31
32 Password createRandomPass(size_t size)
33 {
34         static unsigned int seed = ::time(nullptr);
35
36         Password buf(size, 0x00);
37         for (size_t i = 0; i < size; ++i)
38                 buf[i] = static_cast<Password::value_type>(::rand_r(&seed) % 256);
39
40         return buf;
41 }
42
43 } // namespace anonymous
44
45 BOOST_AUTO_TEST_SUITE(CRYPTO_LOGIC_TEST)
46
47 BOOST_AUTO_TEST_CASE(move_semantics)
48 {
49         CryptoLogic logic;
50
51         const ClientId client = "test_client";
52         BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10)));
53
54         CryptoLogic moved(std::move(logic));
55         BOOST_REQUIRE(!logic.haveKey(client));
56         BOOST_REQUIRE(moved.haveKey(client));
57
58         CryptoLogic moveAssigned = std::move(moved);
59         BOOST_REQUIRE(!moved.haveKey(client));
60         BOOST_REQUIRE(moveAssigned.haveKey(client));
61
62         moveAssigned = std::move(moveAssigned);
63         BOOST_REQUIRE(moveAssigned.haveKey(client));
64 }
65
66 BOOST_AUTO_TEST_CASE(push_key)
67 {
68         CryptoLogic logic;
69
70         const ClientId client = "test_client";
71         BOOST_REQUIRE_THROW(logic.pushKey(std::string(), createRandom(10)),
72                                                 Exc::InternalError);
73         BOOST_REQUIRE_THROW(logic.pushKey(client, RawBuffer()),
74                                                 Exc::InternalError);
75
76         BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10)));
77         BOOST_REQUIRE_THROW(logic.pushKey(client, createRandom(10)),
78                                                 Exc::InternalError);
79
80         ClientId increasingOwner = "a";
81         for (size_t i = 0; i < 20; ++i, increasingOwner.push_back('a')) {
82                 BOOST_REQUIRE_NO_THROW(logic.pushKey(increasingOwner, createRandom(10)));
83                 BOOST_REQUIRE_THROW(logic.pushKey(increasingOwner, createRandom(10)),
84                                                         Exc::InternalError);
85         }
86 }
87
88 BOOST_AUTO_TEST_CASE(row_encryption)
89 {
90         Policy policy(Password(), true);
91         Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10));
92         Crypto::Decider decider;
93         Crypto::GStore &store = decider.getStore(data.type, policy);
94         Token token = store.import(data, policy.password, Crypto::EncryptionParams());
95
96         Name name = "test_data";
97         ClientId owner = "test_owner";
98         DB::Row row(token, name, owner, static_cast<int>(policy.extractable));
99
100         CryptoLogic logic;
101
102         BOOST_REQUIRE_THROW(logic.encryptRow(row), Exc::InternalError);
103
104         auto key = createRandom(32);
105         BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));
106         BOOST_REQUIRE_NO_THROW(logic.encryptRow(row));
107         BOOST_REQUIRE_NO_THROW(logic.decryptRow(policy.password, row));
108 }
109
110 BOOST_AUTO_TEST_CASE(row_encryption_negatives)
111 {
112         Policy policy(Password(), true);
113         Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10));
114         Crypto::Decider decider;
115         Crypto::GStore &store = decider.getStore(data.type, policy);
116         Token token = store.import(data, policy.password, Crypto::EncryptionParams());
117
118         Name name = "test_data";
119         ClientId owner = "test_owner";
120         DB::Row row(token, name, owner, static_cast<int>(policy.extractable));
121
122         CryptoLogic logic;
123
124         auto key = createRandom(32);
125         BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));
126         BOOST_REQUIRE_NO_THROW(logic.encryptRow(row));
127
128         BOOST_REQUIRE_THROW(logic.decryptRow(createRandomPass(10), row),
129                                                 Exc::AuthenticationFailed);
130
131         BOOST_REQUIRE_NO_THROW(logic.removeKey(owner));
132         BOOST_REQUIRE_THROW(logic.decryptRow(Password(), row),
133                                                 Exc::AuthenticationFailed);
134         BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));
135
136         row.algorithmType = DBCMAlgType::NONE;
137         BOOST_REQUIRE_THROW(logic.decryptRow(Password(), row),
138                                                 Exc::AuthenticationFailed);
139 }
140
141 BOOST_AUTO_TEST_SUITE_END() // CRYPTO_LOGIC_TEST