summaryrefslogtreecommitdiff
path: root/tests/DBFixture.cpp
blob: 67241c55d923dba74b942e66e211b9396b8373aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Contact: Kyungwook Tak <k.tak@samsung.com>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 * @file        DBFixture.cpp
 * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
 * @version
 * @brief
 */
#include <boost/test/unit_test.hpp>
#include <db-crypto.h>
#include <ckm/ckm-error.h>
#include <DBFixture.h>
#include <fstream>

using namespace CKM;
using namespace std::chrono;


DBFixture::DBFixture()
{
	BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
	init();
}
DBFixture::DBFixture(const char *db_fname)
{
	BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);

	// copy file
	std::ifstream f1(db_fname, std::fstream::binary);
	std::ofstream f2(m_crypto_db_fname, std::fstream::trunc | std::fstream::binary);
	f2 << f1.rdbuf();
	f2.close();
	f1.close();

	init();
}

void DBFixture::init()
{
	high_resolution_clock::time_point srand_feed = high_resolution_clock::now();
	srand(srand_feed.time_since_epoch().count());

	BOOST_REQUIRE_NO_THROW(m_db = DB::Crypto(m_crypto_db_fname, defaultPass));
}

double DBFixture::performance_get_time_elapsed_ms()
{
	return duration_cast<milliseconds>(m_end_time - m_start_time).count();
}

void DBFixture::performance_start(const char *operation_name)
{
	m_operation = std::string(operation_name ? operation_name : "unknown");
	BOOST_TEST_MESSAGE("\t<performance> running " << m_operation <<
					   " performance test...");
	m_start_time = high_resolution_clock::now();
}

void DBFixture::performance_stop(long num_operations_performed)
{
	m_end_time = high_resolution_clock::now();
	double time_elapsed_ms = performance_get_time_elapsed_ms();
	BOOST_TEST_MESSAGE("\t<performance> time elapsed: " << time_elapsed_ms <<
					   "[ms], number of " << m_operation << ": " << num_operations_performed);

	if (num_operations_performed > 0)
		BOOST_TEST_MESSAGE("\t<performance> average time per " << m_operation << ": " <<
						   time_elapsed_ms / num_operations_performed << "[ms]");
}

void DBFixture::generate_name(unsigned int id, Name &output)
{
	std::stringstream ss;
	ss << "name_no_" << id;
	output = ss.str();
}

void DBFixture::generate_owner(unsigned int id, ClientId &output)
{
	std::stringstream ss;
	ss << "label_no_" << id;
	output = ss.str();
}

void DBFixture::generate_perf_DB(unsigned int num_name,
								 unsigned int names_per_owner)
{
	// to speed up data creation - cache the row
	DB::Row rowPattern = create_default_row(DataType::BINARY_DATA);
	rowPattern.data = RawBuffer(100, 20);
	rowPattern.dataSize = rowPattern.data.size();
	rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);

	for (unsigned int i = 0; i < num_name; i++) {
		generate_name(i, rowPattern.name);
		generate_owner(i / names_per_owner, rowPattern.owner);

		BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
	}
}

long DBFixture::add_full_access_rights(unsigned int num_name,
									   unsigned int num_name_per_owner)
{
	long iterations = 0;
	unsigned int num_owners = num_name / num_name_per_owner;
	Name name;
	ClientId owner, accessor;

	for (unsigned int a = 0; a < num_name; a++) {
		generate_name(a, name);
		generate_owner(a / num_name_per_owner, owner);

		for (unsigned int l = 0; l < num_owners; l++) {
			// bypass the owner
			if (l == (a / num_name_per_owner))
				continue;

			// add permission
			generate_owner(l, accessor);
			add_permission(name, owner, accessor);
			iterations++;
		}
	}

	return iterations;
}

DB::Row DBFixture::create_default_row(DataType type)
{
	return create_default_row(m_default_name, m_default_owner, type);
}

DB::Row DBFixture::create_default_row(const Name &name,
									  const ClientId &owner,
									  DataType type)
{
	DB::Row row;
	row.name = name;
	row.owner = owner;
	row.exportable = 1;
	row.algorithmType = DBCMAlgType::AES_GCM_256;
	row.dataType = type;
	row.iv = createDefaultPass();
	row.encryptionScheme = 0;
	row.dataSize = 0;
	row.backendId = CryptoBackend::OpenSSL;

	return row;
}

void DBFixture::compare_row(const DB::Row &lhs, const DB::Row &rhs)
{
	BOOST_CHECK_MESSAGE(lhs.name == rhs.name,
						"namees didn't match! Got: " << rhs.name
						<< " , expected : " << lhs.name);

	BOOST_CHECK_MESSAGE(lhs.owner == rhs.owner,
						"owner didn't match! Got: " << rhs.owner
						<< " , expected : " << lhs.owner);

	BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
						"exportable didn't match! Got: " << rhs.exportable
						<< " , expected : " << lhs.exportable);

	BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
						"iv didn't match! Got: " << rhs.iv.size()
						<< " , expected : " << lhs.iv.size());

	BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
						"data didn't match! Got: " << rhs.data.size()
						<< " , expected : " << lhs.data.size());

	BOOST_CHECK_MESSAGE(lhs.backendId == rhs.backendId,
						"backendId didn't match! Got: " << static_cast<int>(rhs.backendId)
						<< " , expected : " << static_cast<int>(lhs.backendId));
}

void DBFixture::check_DB_integrity(const DB::Row &rowPattern)
{
	BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));

	DB::Crypto::RowOptional optional_row;
	BOOST_REQUIRE_NO_THROW(optional_row = m_db.getRow(m_default_name, m_default_owner,
										  DataType::BINARY_DATA));
	BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");

	compare_row(*optional_row, rowPattern);
	DB::Row name_duplicate = rowPattern;
	name_duplicate.data = createDefaultPass();
	name_duplicate.dataSize = name_duplicate.data.size();

	unsigned int erased;
	BOOST_REQUIRE_NO_THROW(erased = m_db.deleteRow(m_default_name, m_default_owner));
	BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");

	DB::Crypto::RowOptional row_optional;
	BOOST_REQUIRE_NO_THROW(row_optional = m_db.getRow(m_default_name, m_default_owner,
										  DataType::BINARY_DATA));
	BOOST_REQUIRE_MESSAGE(!row_optional,
						  "Select should not return row after deletion");
}

void DBFixture::insert_row()
{
	insert_row(m_default_name, m_default_owner);
}

void DBFixture::insert_row(const Name &name, const ClientId &owner)
{
	DB::Row rowPattern = create_default_row(name, owner,
											DataType::BINARY_DATA);
	rowPattern.data = RawBuffer(100, 20);
	rowPattern.dataSize = rowPattern.data.size();
	rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
	BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
}

void DBFixture::delete_row(const Name &name, const ClientId &owner)
{
	bool exit_flag;
	BOOST_REQUIRE_NO_THROW(exit_flag = m_db.deleteRow(name, owner));
	BOOST_REQUIRE_MESSAGE(true == exit_flag, "remove name failed: no rows removed");
}

void DBFixture::add_permission(const Name &name, const ClientId &owner,
							   const ClientId &accessor)
{
	BOOST_REQUIRE_NO_THROW(m_db.setPermission(name,
						   owner,
						   accessor,
						   CKM::Permission::READ | CKM::Permission::REMOVE));
}

void DBFixture::read_row_expect_success(const Name &name,
										const ClientId &owner)
{
	DB::Crypto::RowOptional row;
	BOOST_REQUIRE_NO_THROW(row = m_db.getRow(name, owner,
								 DataType::BINARY_DATA));
	BOOST_REQUIRE_MESSAGE(row, "row is empty");
	BOOST_REQUIRE_MESSAGE(row->name == name, "name is not valid");
}