Change RawBuffer into SafeBuffer.
[platform/core/security/key-manager.git] / tests / test_db_crypto.cpp
1 #include <boost/test/unit_test.hpp>
2 #include <boost/test/results_reporter.hpp>
3 #include <unistd.h>
4 #include <db-crypto.h>
5 #include <iostream>
6 #include <ckm/ckm-error.h>
7 #include <errno.h>
8
9 #include <test_common.h>
10
11 BOOST_GLOBAL_FIXTURE(TestConfig)
12
13 using namespace CKM;
14
15 const char* default_alias = "alias";
16 const char* default_label = "label";
17
18 const char* crypto_db = "/tmp/testme.db";
19
20 const int restricted_local = 1;
21 const int restricted_global = 0;
22
23 DBRow createDefaultRow(int restricted = restricted_local,
24         DBDataType type = DBDataType::BINARY_DATA) {
25     DBRow row;
26     row.alias = default_alias;
27     row.smackLabel = default_label;
28     row.exportable = 1;
29     row.restricted = restricted;
30     row.algorithmType = DBCMAlgType::AES_CBC_256;
31     row.dataType = type;
32     row.iv = createDefaultPass();
33
34     return row;
35 }
36
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);
41
42     BOOST_CHECK_MESSAGE(lhs.smackLabel == rhs.smackLabel,
43             "smackLabel didn't match! Got: " << rhs.smackLabel
44                 << " , expected : " << lhs.smackLabel);
45
46     BOOST_CHECK_MESSAGE(lhs.restricted == rhs.restricted,
47             "restricted didn't match! Got: " << rhs.restricted
48                 << " , expected : " << lhs.restricted);
49
50     BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
51             "exportable didn't match! Got: " << rhs.exportable
52                 << " , expected : " << lhs.exportable);
53
54     BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
55             "iv didn't match! Got: " << rhs.iv.size()
56                 << " , expected : " << lhs.iv.size());
57
58     BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
59             "data didn't match! Got: " << rhs.data.size()
60                 << " , expected : " << lhs.data.size());
61 }
62
63 void checkDBIntegrity(const DBRow &rowPattern, DBCrypto &db) {
64
65     BOOST_REQUIRE_NO_THROW(db.saveDBRow(rowPattern));
66     DBRow selectRow = rowPattern;
67
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");
71
72     compareDBRow(selectRow, rowPattern);
73     DBRow alias_duplicate = rowPattern;
74     alias_duplicate.data = createDefaultPass();
75     alias_duplicate.dataSize = alias_duplicate.data.size();
76
77     BOOST_REQUIRE_THROW(db.saveDBRow(alias_duplicate), DBCrypto::Exception::AliasExists);
78     BOOST_REQUIRE_NO_THROW(db.deleteDBRow("alias", "label"));
79
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");
83 }
84
85 BOOST_AUTO_TEST_SUITE(DBCRYPTO_TEST)
86 BOOST_AUTO_TEST_CASE(DBtestSimple) {
87
88     BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
89     DBCrypto db;
90     BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
91
92     DBRow rowPattern = createDefaultRow();
93     rowPattern.data = SafeBuffer(32, 1);
94     rowPattern.dataSize = rowPattern.data.size();
95
96     checkDBIntegrity(rowPattern, db);
97 }
98 BOOST_AUTO_TEST_CASE(DBtestBIG) {
99     BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
100     DBCrypto db;
101     BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
102
103     DBRow rowPattern = createDefaultRow();
104     rowPattern.data = createBigBlob(4096);
105     rowPattern.dataSize = rowPattern.data.size();
106
107     checkDBIntegrity(rowPattern, db);
108 }
109 BOOST_AUTO_TEST_CASE(DBtestGlobal) {
110     BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
111     DBCrypto db;
112     BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
113
114     DBRow rowPattern = createDefaultRow(restricted_global);
115     rowPattern.data = SafeBuffer(1024, 2);
116     rowPattern.dataSize = rowPattern.data.size();
117
118     BOOST_REQUIRE_NO_THROW(db.saveDBRow(rowPattern));
119
120     DBRow alias_duplicate = rowPattern;
121     rowPattern.smackLabel = rowPattern.smackLabel + "1";
122
123     BOOST_REQUIRE_THROW(db.saveDBRow(alias_duplicate),
124             DBCrypto::Exception::AliasExists);
125 }
126 BOOST_AUTO_TEST_CASE(DBtestTransaction) {
127     BOOST_CHECK(unlink(crypto_db) == 0 || errno == ENOENT);
128     DBCrypto db;
129     BOOST_REQUIRE_NO_THROW(db = DBCrypto(crypto_db, defaultPass));
130
131     DBRow rowPattern = createDefaultRow(0);
132     rowPattern.data = SafeBuffer(100, 20);
133     rowPattern.dataSize = rowPattern.data.size();
134     DBCrypto::Transaction transaction(&db);
135
136     BOOST_REQUIRE_NO_THROW(db.saveDBRow(rowPattern));
137     BOOST_REQUIRE_NO_THROW(transaction.rollback());
138
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");
143
144 }
145 BOOST_AUTO_TEST_SUITE_END()