2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Kyungwook Tak <k.tak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
19 * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
23 #include <boost/test/unit_test.hpp>
24 #include <db-crypto.h>
25 #include <ckm/ckm-error.h>
26 #include <DBFixture.h>
30 using namespace std::chrono;
33 DBFixture::DBFixture()
35 BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
38 DBFixture::DBFixture(const char *db_fname)
40 BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
43 std::ifstream f1(db_fname, std::fstream::binary);
44 std::ofstream f2(m_crypto_db_fname, std::fstream::trunc|std::fstream::binary);
52 void DBFixture::init()
54 high_resolution_clock::time_point srand_feed = high_resolution_clock::now();
55 srand(srand_feed.time_since_epoch().count());
57 BOOST_REQUIRE_NO_THROW(m_db = DB::Crypto(m_crypto_db_fname, defaultPass));
60 double DBFixture::performance_get_time_elapsed_ms()
62 return duration_cast<milliseconds>(m_end_time - m_start_time).count();
65 void DBFixture::performance_start(const char *operation_name)
67 m_operation = std::string(operation_name?operation_name:"unknown");
68 BOOST_TEST_MESSAGE("\t<performance> running " << m_operation << " performance test...");
69 m_start_time = high_resolution_clock::now();
72 void DBFixture::performance_stop(long num_operations_performed)
74 m_end_time = high_resolution_clock::now();
75 double time_elapsed_ms = performance_get_time_elapsed_ms();
76 BOOST_TEST_MESSAGE("\t<performance> time elapsed: " << time_elapsed_ms << "[ms], number of " << m_operation << ": " << num_operations_performed);
77 if(num_operations_performed>0)
78 BOOST_TEST_MESSAGE("\t<performance> average time per " << m_operation << ": " << time_elapsed_ms/num_operations_performed << "[ms]");
81 void DBFixture::generate_name(unsigned int id, Name & output)
84 ss << "name_no_" << id;
88 void DBFixture::generate_label(unsigned int id, Label & output)
91 ss << "label_no_" << id;
95 void DBFixture::generate_perf_DB(unsigned int num_name, unsigned int num_elements)
97 // to speed up data creation - cache the row
98 DB::Row rowPattern = create_default_row(DataType::BINARY_DATA);
99 rowPattern.data = RawBuffer(100, 20);
100 rowPattern.dataSize = rowPattern.data.size();
101 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
103 for(unsigned int i=0; i<num_name; i++)
105 generate_name(i, rowPattern.name);
106 generate_label(i/num_elements, rowPattern.ownerLabel);
108 BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
112 long DBFixture::add_full_access_rights(unsigned int num_name, unsigned int num_name_per_label)
115 unsigned int num_labels = num_name / num_name_per_label;
117 Label owner_label, accessor_label;
118 for(unsigned int a=0; a<num_name; a++)
120 generate_name(a, name);
121 generate_label(a/num_name_per_label, owner_label);
122 for(unsigned int l=0; l<num_labels; l++)
124 // bypass the owner label
125 if(l == (a/num_name_per_label))
129 generate_label(l, accessor_label);
130 add_permission(name, owner_label, accessor_label);
138 DB::Row DBFixture::create_default_row(DataType type)
140 return create_default_row(m_default_name, m_default_label, type);
143 DB::Row DBFixture::create_default_row(const Name &name,
149 row.ownerLabel = label;
151 row.algorithmType = DBCMAlgType::AES_GCM_256;
153 row.iv = createDefaultPass();
154 row.encryptionScheme = 0;
156 row.backendId = CryptoBackend::OpenSSL;
161 void DBFixture::compare_row(const DB::Row &lhs, const DB::Row &rhs)
163 BOOST_CHECK_MESSAGE(lhs.name == rhs.name,
164 "namees didn't match! Got: " << rhs.name
165 << " , expected : " << lhs.name);
167 BOOST_CHECK_MESSAGE(lhs.ownerLabel == rhs.ownerLabel,
168 "smackLabel didn't match! Got: " << rhs.ownerLabel
169 << " , expected : " << lhs.ownerLabel);
171 BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
172 "exportable didn't match! Got: " << rhs.exportable
173 << " , expected : " << lhs.exportable);
175 BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
176 "iv didn't match! Got: " << rhs.iv.size()
177 << " , expected : " << lhs.iv.size());
179 BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
180 "data didn't match! Got: " << rhs.data.size()
181 << " , expected : " << lhs.data.size());
183 BOOST_CHECK_MESSAGE(lhs.backendId == rhs.backendId,
184 "backendId didn't match! Got: " << static_cast<int>(rhs.backendId)
185 << " , expected : " << static_cast<int>(lhs.backendId));
188 void DBFixture::check_DB_integrity(const DB::Row &rowPattern)
190 BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
192 DB::Crypto::RowOptional optional_row;
193 BOOST_REQUIRE_NO_THROW(optional_row = m_db.getRow("name", "label", DataType::BINARY_DATA));
194 BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");
196 compare_row(*optional_row, rowPattern);
197 DB::Row name_duplicate = rowPattern;
198 name_duplicate.data = createDefaultPass();
199 name_duplicate.dataSize = name_duplicate.data.size();
202 BOOST_REQUIRE_NO_THROW(erased = m_db.deleteRow("name", "label"));
203 BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
205 DB::Crypto::RowOptional row_optional;
206 BOOST_REQUIRE_NO_THROW(row_optional = m_db.getRow("name", "label", DataType::BINARY_DATA));
207 BOOST_REQUIRE_MESSAGE(!row_optional, "Select should not return row after deletion");
210 void DBFixture::insert_row()
212 insert_row(m_default_name, m_default_label);
215 void DBFixture::insert_row(const Name &name, const Label &owner_label)
217 DB::Row rowPattern = create_default_row(name, owner_label, DataType::BINARY_DATA);
218 rowPattern.data = RawBuffer(100, 20);
219 rowPattern.dataSize = rowPattern.data.size();
220 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
221 BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
224 void DBFixture::delete_row(const Name &name, const Label &owner_label)
227 BOOST_REQUIRE_NO_THROW(exit_flag = m_db.deleteRow(name, owner_label));
228 BOOST_REQUIRE_MESSAGE(true == exit_flag, "remove name failed: no rows removed");
231 void DBFixture::add_permission(const Name &name, const Label &owner_label, const Label &accessor_label)
233 BOOST_REQUIRE_NO_THROW(m_db.setPermission(name,
236 CKM::Permission::READ | CKM::Permission::REMOVE));
239 void DBFixture::read_row_expect_success(const Name &name, const Label &owner_label)
241 DB::Crypto::RowOptional row;
242 BOOST_REQUIRE_NO_THROW(row = m_db.getRow(name, owner_label, DataType::BINARY_DATA));
243 BOOST_REQUIRE_MESSAGE(row, "row is empty");
244 BOOST_REQUIRE_MESSAGE(row->name == name, "name is not valid");