1 #include <boost/test/unit_test.hpp>
3 #include <ckm/ckm-error.h>
8 using namespace std::chrono;
11 DBFixture::DBFixture()
13 BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
16 DBFixture::DBFixture(const char *db_fname)
18 BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
21 std::ifstream f1(db_fname, std::fstream::binary);
22 std::ofstream f2(m_crypto_db_fname, std::fstream::trunc|std::fstream::binary);
30 void DBFixture::init()
32 high_resolution_clock::time_point srand_feed = high_resolution_clock::now();
33 srand(srand_feed.time_since_epoch().count());
35 BOOST_REQUIRE_NO_THROW(m_db = DB::Crypto(m_crypto_db_fname, defaultPass));
38 double DBFixture::performance_get_time_elapsed_ms()
40 return duration_cast<milliseconds>(m_end_time - m_start_time).count();
43 void DBFixture::performance_start(const char *operation_name)
45 m_operation = std::string(operation_name?operation_name:"unknown");
46 BOOST_TEST_MESSAGE("\t<performance> running " << m_operation << " performance test...");
47 m_start_time = high_resolution_clock::now();
50 void DBFixture::performance_stop(long num_operations_performed)
52 m_end_time = high_resolution_clock::now();
53 double time_elapsed_ms = performance_get_time_elapsed_ms();
54 BOOST_TEST_MESSAGE("\t<performance> time elapsed: " << time_elapsed_ms << "[ms], number of " << m_operation << ": " << num_operations_performed);
55 if(num_operations_performed>0)
56 BOOST_TEST_MESSAGE("\t<performance> average time per " << m_operation << ": " << time_elapsed_ms/num_operations_performed << "[ms]");
59 void DBFixture::generate_name(unsigned int id, Name & output)
62 ss << "name_no_" << id;
66 void DBFixture::generate_label(unsigned int id, Label & output)
69 ss << "label_no_" << id;
73 void DBFixture::generate_perf_DB(unsigned int num_name, unsigned int num_elements)
75 // to speed up data creation - cache the row
76 DB::Row rowPattern = create_default_row(DataType::BINARY_DATA);
77 rowPattern.data = RawBuffer(100, 20);
78 rowPattern.dataSize = rowPattern.data.size();
79 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
81 for(unsigned int i=0; i<num_name; i++)
83 generate_name(i, rowPattern.name);
84 generate_label(i/num_elements, rowPattern.ownerLabel);
86 BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
90 long DBFixture::add_full_access_rights(unsigned int num_name, unsigned int num_name_per_label)
93 unsigned int num_labels = num_name / num_name_per_label;
95 Label owner_label, accessor_label;
96 for(unsigned int a=0; a<num_name; a++)
98 generate_name(a, name);
99 generate_label(a/num_name_per_label, owner_label);
100 for(unsigned int l=0; l<num_labels; l++)
102 // bypass the owner label
103 if(l == (a/num_name_per_label))
107 generate_label(l, accessor_label);
108 add_permission(name, owner_label, accessor_label);
116 DB::Row DBFixture::create_default_row(DataType type)
118 return create_default_row(m_default_name, m_default_label, type);
121 DB::Row DBFixture::create_default_row(const Name &name,
127 row.ownerLabel = label;
129 row.algorithmType = DBCMAlgType::AES_GCM_256;
131 row.iv = createDefaultPass();
132 row.encryptionScheme = 0;
138 void DBFixture::compare_row(const DB::Row &lhs, const DB::Row &rhs)
140 BOOST_CHECK_MESSAGE(lhs.name == rhs.name,
141 "namees didn't match! Got: " << rhs.name
142 << " , expected : " << lhs.name);
144 BOOST_CHECK_MESSAGE(lhs.ownerLabel == rhs.ownerLabel,
145 "smackLabel didn't match! Got: " << rhs.ownerLabel
146 << " , expected : " << lhs.ownerLabel);
148 BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
149 "exportable didn't match! Got: " << rhs.exportable
150 << " , expected : " << lhs.exportable);
152 BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
153 "iv didn't match! Got: " << rhs.iv.size()
154 << " , expected : " << lhs.iv.size());
156 BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
157 "data didn't match! Got: " << rhs.data.size()
158 << " , expected : " << lhs.data.size());
161 void DBFixture::check_DB_integrity(const DB::Row &rowPattern)
163 BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
165 DB::Crypto::RowOptional optional_row;
166 BOOST_REQUIRE_NO_THROW(optional_row = m_db.getRow("name", "label", DataType::BINARY_DATA));
167 BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");
169 compare_row(*optional_row, rowPattern);
170 DB::Row name_duplicate = rowPattern;
171 name_duplicate.data = createDefaultPass();
172 name_duplicate.dataSize = name_duplicate.data.size();
175 BOOST_REQUIRE_NO_THROW(erased = m_db.deleteRow("name", "label"));
176 BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
178 DB::Crypto::RowOptional row_optional;
179 BOOST_REQUIRE_NO_THROW(row_optional = m_db.getRow("name", "label", DataType::BINARY_DATA));
180 BOOST_REQUIRE_MESSAGE(!row_optional, "Select should not return row after deletion");
183 void DBFixture::insert_row()
185 insert_row(m_default_name, m_default_label);
188 void DBFixture::insert_row(const Name &name, const Label &owner_label)
190 DB::Row rowPattern = create_default_row(name, owner_label, DataType::BINARY_DATA);
191 rowPattern.data = RawBuffer(100, 20);
192 rowPattern.dataSize = rowPattern.data.size();
193 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
194 BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
197 void DBFixture::delete_row(const Name &name, const Label &owner_label)
200 BOOST_REQUIRE_NO_THROW(exit_flag = m_db.deleteRow(name, owner_label));
201 BOOST_REQUIRE_MESSAGE(true == exit_flag, "remove name failed: no rows removed");
204 void DBFixture::add_permission(const Name &name, const Label &owner_label, const Label &accessor_label)
206 BOOST_REQUIRE_NO_THROW(m_db.setPermission(name,
209 CKM::Permission::READ | CKM::Permission::REMOVE));
212 void DBFixture::read_row_expect_success(const Name &name, const Label &owner_label)
214 DB::Crypto::RowOptional row;
215 BOOST_REQUIRE_NO_THROW(row = m_db.getRow(name, owner_label, DataType::BINARY_DATA));
216 BOOST_REQUIRE_MESSAGE(row, "row is empty");
217 BOOST_REQUIRE_MESSAGE(row->name == name, "name is not valid");