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_alias(unsigned int id, std::string & output)
43 ss << "alias_no_" << id;
47 void DBFixture::generate_label(unsigned int id, std::string & output)
50 ss << "label_no_" << id;
54 void DBFixture::generate_perf_DB(unsigned int num_alias, 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_alias; i++)
64 generate_alias(i, rowPattern.alias);
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_alias, unsigned int num_alias_per_label)
74 unsigned int num_labels = num_alias / num_alias_per_label;
75 std::string alias, owner_label, accessor_label;
76 for(unsigned int a=0; a<num_alias; a++)
78 generate_alias(a, alias);
79 generate_label(a/num_alias_per_label, owner_label);
80 for(unsigned int l=0; l<num_labels; l++)
82 // bypass the owner label
83 if(l == (a/num_alias_per_label))
87 generate_label(l, accessor_label);
88 add_permission(alias, owner_label, accessor_label);
96 DBRow DBFixture::create_default_row(DBDataType type)
98 return create_default_row(m_default_alias, m_default_label, type);
101 DBRow DBFixture::create_default_row(const std::string &alias,
102 const std::string &label,
107 row.smackLabel = label;
109 row.algorithmType = DBCMAlgType::AES_GCM_256;
111 row.iv = createDefaultPass();
112 row.encryptionScheme = 0;
118 void DBFixture::compare_row(const DBRow &lhs, const DBRow &rhs)
120 BOOST_CHECK_MESSAGE(lhs.alias == rhs.alias,
121 "Aliases didn't match! Got: " << rhs.alias
122 << " , expected : " << lhs.alias);
124 BOOST_CHECK_MESSAGE(lhs.smackLabel == rhs.smackLabel,
125 "smackLabel didn't match! Got: " << rhs.smackLabel
126 << " , expected : " << lhs.smackLabel);
128 BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
129 "exportable didn't match! Got: " << rhs.exportable
130 << " , expected : " << lhs.exportable);
132 BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
133 "iv didn't match! Got: " << rhs.iv.size()
134 << " , expected : " << lhs.iv.size());
136 BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
137 "data didn't match! Got: " << rhs.data.size()
138 << " , expected : " << lhs.data.size());
141 void DBFixture::check_DB_integrity(const DBRow &rowPattern)
143 BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
144 DBRow selectRow = rowPattern;
146 DBCrypto::DBRowOptional optional_row;
147 BOOST_REQUIRE_NO_THROW(optional_row = m_db.getDBRow("alias", "label", DBDataType::BINARY_DATA));
148 BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");
150 compare_row(selectRow, rowPattern);
151 DBRow alias_duplicate = rowPattern;
152 alias_duplicate.data = createDefaultPass();
153 alias_duplicate.dataSize = alias_duplicate.data.size();
155 BOOST_REQUIRE_THROW(m_db.saveDBRow(alias_duplicate), DBCrypto::Exception::AliasExists);
157 BOOST_REQUIRE_NO_THROW(erased = m_db.deleteDBRow("alias", "label"));
158 BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
160 DBCrypto::DBRowOptional row_optional;
161 BOOST_REQUIRE_NO_THROW(row_optional = m_db.getDBRow("alias", "label", DBDataType::BINARY_DATA));
162 BOOST_REQUIRE_MESSAGE(!row_optional, "Select should not return row after deletion");
165 void DBFixture::insert_row()
167 insert_row(m_default_alias, m_default_label);
170 void DBFixture::insert_row(const std::string &alias, const std::string &accessor_label)
172 DBRow rowPattern = create_default_row(alias, accessor_label, DBDataType::BINARY_DATA);
173 rowPattern.data = RawBuffer(100, 20);
174 rowPattern.dataSize = rowPattern.data.size();
175 rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
176 BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
179 void DBFixture::delete_row(const std::string &alias, const std::string &accessor_label)
182 BOOST_REQUIRE_NO_THROW(exit_flag = m_db.deleteDBRow(alias, accessor_label));
183 BOOST_REQUIRE_MESSAGE(true == exit_flag, "remove alias failed: no rows removed");
186 void DBFixture::add_permission(const std::string &alias, const std::string &owner_label, const std::string &accessor_label)
189 BOOST_REQUIRE_NO_THROW(ec = m_db.setAccessRights(owner_label,
192 CKM::AccessRight::AR_READ_REMOVE));
193 BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == ec, "add permission failed: " << ec);
196 void DBFixture::read_row_expect_fail(const std::string &alias, const std::string &accessor_label)
198 DBCrypto::DBRowOptional row;
199 BOOST_REQUIRE_THROW(row = m_db.getDBRow(alias, accessor_label, DBDataType::BINARY_DATA), DBCrypto::Exception::PermissionDenied);
202 void DBFixture::read_row_expect_success(const std::string &alias, const std::string &accessor_label)
204 DBCrypto::DBRowOptional row;
205 BOOST_REQUIRE_NO_THROW(row = m_db.getDBRow(alias, accessor_label, DBDataType::BINARY_DATA));
206 BOOST_REQUIRE_MESSAGE(row, "row is empty");
207 BOOST_REQUIRE_MESSAGE(row->alias == alias, "alias is not valid");