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 <crypto-logic.h>
17 #include <platform/decider.h>
18 #include <generic-backend/gstore.h>
24 #include <boost/test/unit_test.hpp>
26 #include "test_common.h"
32 Password createRandomPass(size_t size)
34 static unsigned int seed = ::time(nullptr);
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);
43 } // namespace anonymous
45 BOOST_AUTO_TEST_SUITE(CRYPTO_LOGIC_TEST)
47 BOOST_AUTO_TEST_CASE(move_semantics)
51 const ClientId client = "test_client";
52 BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10)));
54 CryptoLogic moved(std::move(logic));
55 BOOST_REQUIRE(!logic.haveKey(client));
56 BOOST_REQUIRE(moved.haveKey(client));
58 CryptoLogic moveAssigned = std::move(moved);
59 BOOST_REQUIRE(!moved.haveKey(client));
60 BOOST_REQUIRE(moveAssigned.haveKey(client));
62 moveAssigned = std::move(moveAssigned);
63 BOOST_REQUIRE(moveAssigned.haveKey(client));
66 BOOST_AUTO_TEST_CASE(push_key)
70 const ClientId client = "test_client";
71 BOOST_REQUIRE_THROW(logic.pushKey(std::string(), createRandom(10)),
73 BOOST_REQUIRE_THROW(logic.pushKey(client, RawBuffer()),
76 BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10)));
77 BOOST_REQUIRE_THROW(logic.pushKey(client, createRandom(10)),
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)),
88 BOOST_AUTO_TEST_CASE(row_encryption)
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, RawBuffer());
96 Name name = "test_data";
97 ClientId owner = "test_owner";
98 DB::Row row(token, name, owner, static_cast<int>(policy.extractable));
102 BOOST_REQUIRE_THROW(logic.encryptRow(row), Exc::InternalError);
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));
110 BOOST_AUTO_TEST_CASE(row_encryption_negatives)
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, RawBuffer());
118 Name name = "test_data";
119 ClientId owner = "test_owner";
120 DB::Row row(token, name, owner, static_cast<int>(policy.extractable));
124 auto key = createRandom(32);
125 BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key));
126 BOOST_REQUIRE_NO_THROW(logic.encryptRow(row));
128 BOOST_REQUIRE_THROW(logic.decryptRow(createRandomPass(10), row),
129 Exc::AuthenticationFailed);
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));
136 row.algorithmType = DBCMAlgType::NONE;
137 BOOST_REQUIRE_THROW(logic.decryptRow(Password(), row),
138 Exc::AuthenticationFailed);
141 BOOST_AUTO_TEST_SUITE_END() // CRYPTO_LOGIC_TEST