Add DB performance analysis tests and performance improvements.
[platform/core/security/key-manager.git] / tests / DBFixture.cpp
1 #include <boost/test/unit_test.hpp>
2 #include <db-crypto.h>
3 #include <ckm/ckm-error.h>
4 #include <DBFixture.h>
5
6 using namespace CKM;
7 using namespace std::chrono;
8
9
10 DBFixture::DBFixture()
11 {
12     high_resolution_clock::time_point srand_feed = high_resolution_clock::now();
13     srand(srand_feed.time_since_epoch().count());
14
15     BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
16     BOOST_REQUIRE_NO_THROW(m_db = DBCrypto(m_crypto_db_fname, defaultPass));
17 }
18
19 double DBFixture::performance_get_time_elapsed_ms()
20 {
21     return duration_cast<milliseconds>(m_end_time - m_start_time).count();
22 }
23
24 void DBFixture::performance_start(const char *operation_name)
25 {
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();
29 }
30
31 void DBFixture::performance_stop(long num_operations_performed)
32 {
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]");
38 }
39
40 void DBFixture::generate_alias(unsigned int id, std::string & output)
41 {
42     std::stringstream ss;
43     ss << "alias_no_" << id;
44     output = ss.str();
45 }
46
47 void DBFixture::generate_label(unsigned int id, std::string & output)
48 {
49     std::stringstream ss;
50     ss << "label_no_" << id;
51     output = ss.str();
52 }
53
54 void DBFixture::generate_perf_DB(unsigned int num_alias, unsigned int num_label)
55 {
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);
61
62     for(unsigned int i=0; i<num_alias; i++)
63     {
64         generate_alias(i, rowPattern.alias);
65         generate_label(i/num_label, rowPattern.smackLabel);
66
67         BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
68     }
69 }
70
71 long DBFixture::add_full_access_rights(unsigned int num_alias, unsigned int num_alias_per_label)
72 {
73     long iterations = 0;
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++)
77     {
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++)
81         {
82             // bypass the owner label
83             if(l == (a/num_alias_per_label))
84                 continue;
85
86             // add permission
87             generate_label(l, accessor_label);
88             add_permission(alias, owner_label, accessor_label);
89             iterations ++;
90         }
91     }
92
93     return iterations;
94 }
95
96 DBRow DBFixture::create_default_row(DBDataType type)
97 {
98     return create_default_row(m_default_alias, m_default_label, type);
99 }
100
101 DBRow DBFixture::create_default_row(const std::string &alias,
102                                     const std::string &label,
103                                     DBDataType type)
104 {
105     DBRow row;
106     row.alias = alias;
107     row.smackLabel = label;
108     row.exportable = 1;
109     row.algorithmType = DBCMAlgType::AES_GCM_256;
110     row.dataType = type;
111     row.iv = createDefaultPass();
112     row.encryptionScheme = 0;
113     row.dataSize = 0;
114
115     return row;
116 }
117
118 void DBFixture::compare_row(const DBRow &lhs, const DBRow &rhs)
119 {
120     BOOST_CHECK_MESSAGE(lhs.alias == rhs.alias,
121             "Aliases didn't match! Got: " << rhs.alias
122                 << " , expected : " << lhs.alias);
123
124     BOOST_CHECK_MESSAGE(lhs.smackLabel == rhs.smackLabel,
125             "smackLabel didn't match! Got: " << rhs.smackLabel
126                 << " , expected : " << lhs.smackLabel);
127
128     BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
129             "exportable didn't match! Got: " << rhs.exportable
130                 << " , expected : " << lhs.exportable);
131
132     BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
133             "iv didn't match! Got: " << rhs.iv.size()
134                 << " , expected : " << lhs.iv.size());
135
136     BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
137             "data didn't match! Got: " << rhs.data.size()
138                 << " , expected : " << lhs.data.size());
139 }
140
141 void DBFixture::check_DB_integrity(const DBRow &rowPattern)
142 {
143     BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
144     DBRow selectRow = rowPattern;
145
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");
149
150     compare_row(selectRow, rowPattern);
151     DBRow alias_duplicate = rowPattern;
152     alias_duplicate.data = createDefaultPass();
153     alias_duplicate.dataSize = alias_duplicate.data.size();
154
155     BOOST_REQUIRE_THROW(m_db.saveDBRow(alias_duplicate), DBCrypto::Exception::AliasExists);
156     unsigned int erased;
157     BOOST_REQUIRE_NO_THROW(erased = m_db.deleteDBRow("alias", "label"));
158     BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
159
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");
163 }
164
165 void DBFixture::insert_row()
166 {
167     insert_row(m_default_alias, m_default_label);
168 }
169
170 void DBFixture::insert_row(const std::string &alias, const std::string &accessor_label)
171 {
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));
177 }
178
179 void DBFixture::delete_row(const std::string &alias, const std::string &accessor_label)
180 {
181     bool exit_flag;
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");
184 }
185
186 void DBFixture::add_permission(const std::string &alias, const std::string &owner_label, const std::string &accessor_label)
187 {
188     int ec;
189     BOOST_REQUIRE_NO_THROW(ec = m_db.setAccessRights(owner_label,
190                                                    alias,
191                                                    accessor_label,
192                                                    CKM::AccessRight::AR_READ_REMOVE));
193     BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == ec, "add permission failed: " << ec);
194 }
195
196 void DBFixture::read_row_expect_fail(const std::string &alias, const std::string &accessor_label)
197 {
198     DBCrypto::DBRowOptional row;
199     BOOST_REQUIRE_THROW(row = m_db.getDBRow(alias, accessor_label, DBDataType::BINARY_DATA), DBCrypto::Exception::PermissionDenied);
200 }
201
202 void DBFixture::read_row_expect_success(const std::string &alias, const std::string &accessor_label)
203 {
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");
208 }