1 #include <boost/test/unit_test.hpp>
2 #include <boost/test/results_reporter.hpp>
6 #include <ckm/ckm-error.h>
9 #include <test_common.h>
11 BOOST_GLOBAL_FIXTURE(TestConfig)
15 const char* default_alias = "alias";
16 const char* default_label = "label";
18 const char* crypto_db = "/tmp/testme.db";
20 const int restricted_local = 1;
21 const int restricted_global = 0;
23 DBRow createDefaultRow(int restricted = restricted_local,
24 DBDataType type = DBDataType::BINARY_DATA) {
26 row.alias = default_alias;
27 row.smackLabel = default_label;
29 row.restricted = restricted;
30 row.algorithmType = DBCMAlgType::AES_CBC_256;
32 row.iv = createDefaultPass();
37 void compareDBRow(const DBRow &lhs, const DBRow &rhs) {
38 BOOST_CHECK_MESSAGE(lhs.alias == rhs.alias,
39 "Aliases didn't match! Got: " << rhs.alias
40 << " , expected : " << lhs.alias);
42 BOOST_CHECK_MESSAGE(lhs.smackLabel == rhs.smackLabel,
43 "smackLabel didn't match! Got: " << rhs.smackLabel
44 << " , expected : " << lhs.smackLabel);
46 BOOST_CHECK_MESSAGE(lhs.restricted == rhs.restricted,
47 "restricted didn't match! Got: " << rhs.restricted
48 << " , expected : " << lhs.restricted);
50 BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
51 "exportable didn't match! Got: " << rhs.exportable
52 << " , expected : " << lhs.exportable);
54 BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
55 "iv didn't match! Got: " << rhs.iv.size()
56 << " , expected : " << lhs.iv.size());
58 BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
59 "data didn't match! Got: " << rhs.data.size()
60 << " , expected : " << lhs.data.size());
63 void checkDBIntegrity(const DBRow &rowPattern, DBCrypto &db) {
65 BOOST_REQUIRE_NO_THROW(db.saveDBRow(rowPattern));
66 DBRow selectRow = rowPattern;
68 DBCrypto::DBRowOptional optional_row;
69 BOOST_REQUIRE_NO_THROW(optional_row = db.getDBRow("alias", "label", DBDataType::BINARY_DATA));
70 BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");
72 compareDBRow(selectRow, rowPattern);
73 DBRow alias_duplicate = rowPattern;
74 alias_duplicate.data = createDefaultPass();
75 alias_duplicate.dataSize = alias_duplicate.data.size();
77 BOOST_REQUIRE_THROW(db.saveDBRow(alias_duplicate), DBCrypto::Exception::AliasExists);
78 BOOST_REQUIRE_NO_THROW(db.deleteDBRow("alias", "label"));
80 DBCrypto::DBRowOptional row_optional;
81 BOOST_REQUIRE_NO_THROW(row_optional = db.getDBRow("alias", "label", DBDataType::BINARY_DATA));
82 BOOST_REQUIRE_MESSAGE(!row_optional, "Select should not return row after deletion");
85 BOOST_AUTO_TEST_SUITE(DBCRYPTO_TEST)
86 BOOST_AUTO_TEST_CASE(DBtestSimple) {
88 BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
90 BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
92 DBRow rowPattern = createDefaultRow();
93 rowPattern.data = SafeBuffer(32, 1);
94 rowPattern.dataSize = rowPattern.data.size();
96 checkDBIntegrity(rowPattern, db);
98 BOOST_AUTO_TEST_CASE(DBtestBIG) {
99 BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
101 BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
103 DBRow rowPattern = createDefaultRow();
104 rowPattern.data = createBigBlob(4096);
105 rowPattern.dataSize = rowPattern.data.size();
107 checkDBIntegrity(rowPattern, db);
109 BOOST_AUTO_TEST_CASE(DBtestGlobal) {
110 BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
112 BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
114 DBRow rowPattern = createDefaultRow(restricted_global);
115 rowPattern.data = SafeBuffer(1024, 2);
116 rowPattern.dataSize = rowPattern.data.size();
118 BOOST_REQUIRE_NO_THROW(db.saveDBRow(rowPattern));
120 DBRow alias_duplicate = rowPattern;
121 rowPattern.smackLabel = rowPattern.smackLabel + "1";
123 BOOST_REQUIRE_THROW(db.saveDBRow(alias_duplicate),
124 DBCrypto::Exception::AliasExists);
126 BOOST_AUTO_TEST_CASE(DBtestTransaction) {
127 BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
129 BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
131 DBRow rowPattern = createDefaultRow(0);
132 rowPattern.data = SafeBuffer(100, 20);
133 rowPattern.dataSize = rowPattern.data.size();
134 DBCrypto::Transaction transaction(&db);
136 BOOST_REQUIRE_NO_THROW(db.saveDBRow(rowPattern));
137 BOOST_REQUIRE_NO_THROW(transaction.rollback());
139 DBCrypto::DBRowOptional row_optional;
140 BOOST_REQUIRE_NO_THROW(row_optional = db.getDBRow(default_alias, default_label,
141 DBDataType::BINARY_DATA));
142 BOOST_CHECK_MESSAGE(!row_optional, "Row still present after rollback");
145 BOOST_AUTO_TEST_SUITE_END()