1 #include <boost/test/unit_test.hpp>
3 #include <ckm/ckm-error.h>
7 using namespace std::chrono;
10 DBFixture::DBFixture()
12 high_resolution_clock::time_point srand_feed = high_resolution_clock::now();
13 srand(srand_feed.time_since_epoch().count());
15 BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
16 BOOST_REQUIRE_NO_THROW(m_db = DBCrypto(m_crypto_db_fname, defaultPass));
19 double DBFixture::performance_get_time_elapsed_ms()
21 return duration_cast<milliseconds>(m_end_time - m_start_time).count();
24 void DBFixture::performance_start(const char *operation_name)
26 m_operation = std::string(operation_name?operation_name:"unknown");
27 BOOST_TEST_MESSAGE("\t<performance> running " << m_operation << " performance test...");
28 m_start_time = high_resolution_clock::now();
31 void DBFixture::performance_stop(long num_operations_performed)
33 m_end_time = high_resolution_clock::now();
34 double time_elapsed_ms = performance_get_time_elapsed_ms();
35 BOOST_TEST_MESSAGE("\t<performance> time elapsed: " << time_elapsed_ms << "[ms], number of " << m_operation << ": " << num_operations_performed);
36 if(num_operations_performed>0)
37 BOOST_TEST_MESSAGE("\t<performance> average time per " << m_operation << ": " << time_elapsed_ms/num_operations_performed << "[ms]");
40 void DBFixture::generate_name(unsigned int id, Name & output)
43 ss << "name_no_" << id;
47 void DBFixture::generate_label(unsigned int id, Label & output)
50 ss << "label_no_" << id;
54 void DBFixture::generate_perf_DB(unsigned int num_name, unsigned int num_label)
56 // to speed up data creation - cache the row
57 DBRow rowPattern = create_default_row(DBDataType::BINARY_DATA);
58 rowPattern.data = RawBuffer(100, 20);
59 rowPattern.dataSize = rowPattern.data.size();
60 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
62 for(unsigned int i=0; i<num_name; i++)
64 generate_name(i, rowPattern.name);
65 generate_label(i/num_label, rowPattern.smackLabel);
67 BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
71 long DBFixture::add_full_access_rights(unsigned int num_name, unsigned int num_name_per_label)
74 unsigned int num_labels = num_name / num_name_per_label;
76 Label owner_label, accessor_label;
77 for(unsigned int a=0; a<num_name; a++)
79 generate_name(a, name);
80 generate_label(a/num_name_per_label, owner_label);
81 for(unsigned int l=0; l<num_labels; l++)
83 // bypass the owner label
84 if(l == (a/num_name_per_label))
88 generate_label(l, accessor_label);
89 add_permission(name, owner_label, accessor_label);
97 DBRow DBFixture::create_default_row(DBDataType type)
99 return create_default_row(m_default_name, m_default_label, type);
102 DBRow DBFixture::create_default_row(const Name &name,
108 row.smackLabel = label;
110 row.algorithmType = DBCMAlgType::AES_GCM_256;
112 row.iv = createDefaultPass();
113 row.encryptionScheme = 0;
119 void DBFixture::compare_row(const DBRow &lhs, const DBRow &rhs)
121 BOOST_CHECK_MESSAGE(lhs.name == rhs.name,
122 "namees didn't match! Got: " << rhs.name
123 << " , expected : " << lhs.name);
125 BOOST_CHECK_MESSAGE(lhs.smackLabel == rhs.smackLabel,
126 "smackLabel didn't match! Got: " << rhs.smackLabel
127 << " , expected : " << lhs.smackLabel);
129 BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
130 "exportable didn't match! Got: " << rhs.exportable
131 << " , expected : " << lhs.exportable);
133 BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
134 "iv didn't match! Got: " << rhs.iv.size()
135 << " , expected : " << lhs.iv.size());
137 BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
138 "data didn't match! Got: " << rhs.data.size()
139 << " , expected : " << lhs.data.size());
142 void DBFixture::check_DB_integrity(const DBRow &rowPattern)
144 BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
145 DBRow selectRow = rowPattern;
147 DBCrypto::DBRowOptional optional_row;
148 BOOST_REQUIRE_NO_THROW(optional_row = m_db.getDBRow("name", "label", "label", DBDataType::BINARY_DATA));
149 BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");
151 compare_row(selectRow, rowPattern);
152 DBRow name_duplicate = rowPattern;
153 name_duplicate.data = createDefaultPass();
154 name_duplicate.dataSize = name_duplicate.data.size();
156 BOOST_REQUIRE_THROW(m_db.saveDBRow(name_duplicate), DBCrypto::Exception::NameExists);
158 BOOST_REQUIRE_NO_THROW(erased = m_db.deleteDBRow("name", "label", "label"));
159 BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
161 DBCrypto::DBRowOptional row_optional;
162 BOOST_REQUIRE_NO_THROW(row_optional = m_db.getDBRow("name", "label", "label", DBDataType::BINARY_DATA));
163 BOOST_REQUIRE_MESSAGE(!row_optional, "Select should not return row after deletion");
166 void DBFixture::insert_row()
168 insert_row(m_default_name, m_default_label);
171 void DBFixture::insert_row(const Name &name, const Label &owner_label)
173 DBRow rowPattern = create_default_row(name, owner_label, DBDataType::BINARY_DATA);
174 rowPattern.data = RawBuffer(100, 20);
175 rowPattern.dataSize = rowPattern.data.size();
176 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
177 BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
180 void DBFixture::delete_row(const Name &name, const Label &owner_label, const Label &accessor_label)
183 BOOST_REQUIRE_NO_THROW(exit_flag = m_db.deleteDBRow(name, owner_label, accessor_label));
184 BOOST_REQUIRE_MESSAGE(true == exit_flag, "remove name failed: no rows removed");
187 void DBFixture::add_permission(const Name &name, const Label &owner_label, const Label &accessor_label)
190 BOOST_REQUIRE_NO_THROW(ec = m_db.setAccessRights(name,
193 CKM::AccessRight::AR_READ_REMOVE));
194 BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == ec, "add permission failed: " << ec);
197 void DBFixture::read_row_expect_fail(const Name &name, const Label &owner_label, const Label &accessor_label)
199 DBCrypto::DBRowOptional row;
200 BOOST_REQUIRE_NO_THROW(row = m_db.getDBRow(name, owner_label, accessor_label, DBDataType::BINARY_DATA));
204 void DBFixture::read_row_expect_success(const Name &name, const Label &owner_label, const Label &accessor_label)
206 DBCrypto::DBRowOptional row;
207 BOOST_REQUIRE_NO_THROW(row = m_db.getDBRow(name, owner_label, accessor_label, DBDataType::BINARY_DATA));
208 BOOST_REQUIRE_MESSAGE(row, "row is empty");
209 BOOST_REQUIRE_MESSAGE(row->name == name, "name is not valid");