74c51461c612643e330dba89e15e33aa9efadbdf
[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 #include <fstream>
6
7 using namespace CKM;
8 using namespace std::chrono;
9
10
11 DBFixture::DBFixture()
12 {
13     BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
14     init();
15 }
16 DBFixture::DBFixture(const char *db_fname)
17 {
18     BOOST_CHECK(unlink(m_crypto_db_fname) == 0 || errno == ENOENT);
19
20     // copy file
21     std::ifstream f1(db_fname, std::fstream::binary);
22     std::ofstream f2(m_crypto_db_fname, std::fstream::trunc|std::fstream::binary);
23     f2 << f1.rdbuf();
24     f2.close();
25     f1.close();
26
27     init();
28 }
29
30 void DBFixture::init()
31 {
32     high_resolution_clock::time_point srand_feed = high_resolution_clock::now();
33     srand(srand_feed.time_since_epoch().count());
34
35     BOOST_REQUIRE_NO_THROW(m_db = DB::Crypto(m_crypto_db_fname, defaultPass));
36 }
37
38 double DBFixture::performance_get_time_elapsed_ms()
39 {
40     return duration_cast<milliseconds>(m_end_time - m_start_time).count();
41 }
42
43 void DBFixture::performance_start(const char *operation_name)
44 {
45     m_operation = std::string(operation_name?operation_name:"unknown");
46     BOOST_TEST_MESSAGE("\t<performance> running " << m_operation << " performance test...");
47     m_start_time = high_resolution_clock::now();
48 }
49
50 void DBFixture::performance_stop(long num_operations_performed)
51 {
52     m_end_time = high_resolution_clock::now();
53     double time_elapsed_ms = performance_get_time_elapsed_ms();
54     BOOST_TEST_MESSAGE("\t<performance> time elapsed: " << time_elapsed_ms << "[ms], number of " << m_operation << ": " << num_operations_performed);
55     if(num_operations_performed>0)
56         BOOST_TEST_MESSAGE("\t<performance> average time per " << m_operation << ": " << time_elapsed_ms/num_operations_performed << "[ms]");
57 }
58
59 void DBFixture::generate_name(unsigned int id, Name & output)
60 {
61     std::stringstream ss;
62     ss << "name_no_" << id;
63     output = ss.str();
64 }
65
66 void DBFixture::generate_label(unsigned int id, Label & output)
67 {
68     std::stringstream ss;
69     ss << "label_no_" << id;
70     output = ss.str();
71 }
72
73 void DBFixture::generate_perf_DB(unsigned int num_name, unsigned int num_elements)
74 {
75     // to speed up data creation - cache the row
76     DB::Row rowPattern = create_default_row(DataType::BINARY_DATA);
77     rowPattern.data = RawBuffer(100, 20);
78     rowPattern.dataSize = rowPattern.data.size();
79     rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
80
81     for(unsigned int i=0; i<num_name; i++)
82     {
83         generate_name(i, rowPattern.name);
84         generate_label(i/num_elements, rowPattern.ownerLabel);
85
86         BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
87     }
88 }
89
90 long DBFixture::add_full_access_rights(unsigned int num_name, unsigned int num_name_per_label)
91 {
92     long iterations = 0;
93     unsigned int num_labels = num_name / num_name_per_label;
94     Name name;
95     Label owner_label, accessor_label;
96     for(unsigned int a=0; a<num_name; a++)
97     {
98         generate_name(a, name);
99         generate_label(a/num_name_per_label, owner_label);
100         for(unsigned int l=0; l<num_labels; l++)
101         {
102             // bypass the owner label
103             if(l == (a/num_name_per_label))
104                 continue;
105
106             // add permission
107             generate_label(l, accessor_label);
108             add_permission(name, owner_label, accessor_label);
109             iterations ++;
110         }
111     }
112
113     return iterations;
114 }
115
116 DB::Row DBFixture::create_default_row(DataType type)
117 {
118     return create_default_row(m_default_name, m_default_label, type);
119 }
120
121 DB::Row DBFixture::create_default_row(const Name &name,
122                                     const Label &label,
123                                     DataType type)
124 {
125     DB::Row row;
126     row.name = name;
127     row.ownerLabel = label;
128     row.exportable = 1;
129     row.algorithmType = DBCMAlgType::AES_GCM_256;
130     row.dataType = type;
131     row.iv = createDefaultPass();
132     row.encryptionScheme = 0;
133     row.dataSize = 0;
134
135     return row;
136 }
137
138 void DBFixture::compare_row(const DB::Row &lhs, const DB::Row &rhs)
139 {
140     BOOST_CHECK_MESSAGE(lhs.name == rhs.name,
141             "namees didn't match! Got: " << rhs.name
142                 << " , expected : " << lhs.name);
143
144     BOOST_CHECK_MESSAGE(lhs.ownerLabel == rhs.ownerLabel,
145             "smackLabel didn't match! Got: " << rhs.ownerLabel
146                 << " , expected : " << lhs.ownerLabel);
147
148     BOOST_CHECK_MESSAGE(lhs.exportable == rhs.exportable,
149             "exportable didn't match! Got: " << rhs.exportable
150                 << " , expected : " << lhs.exportable);
151
152     BOOST_CHECK_MESSAGE(lhs.iv == rhs.iv,
153             "iv didn't match! Got: " << rhs.iv.size()
154                 << " , expected : " << lhs.iv.size());
155
156     BOOST_CHECK_MESSAGE(lhs.data == rhs.data,
157             "data didn't match! Got: " << rhs.data.size()
158                 << " , expected : " << lhs.data.size());
159 }
160
161 void DBFixture::check_DB_integrity(const DB::Row &rowPattern)
162 {
163     BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
164
165     DB::Crypto::RowOptional optional_row;
166     BOOST_REQUIRE_NO_THROW(optional_row = m_db.getRow("name", "label", DataType::BINARY_DATA));
167     BOOST_REQUIRE_MESSAGE(optional_row, "Select didn't return any row");
168
169     compare_row(*optional_row, rowPattern);
170     DB::Row name_duplicate = rowPattern;
171     name_duplicate.data = createDefaultPass();
172     name_duplicate.dataSize = name_duplicate.data.size();
173
174     unsigned int erased;
175     BOOST_REQUIRE_NO_THROW(erased = m_db.deleteRow("name", "label"));
176     BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
177
178     DB::Crypto::RowOptional row_optional;
179     BOOST_REQUIRE_NO_THROW(row_optional = m_db.getRow("name", "label", DataType::BINARY_DATA));
180     BOOST_REQUIRE_MESSAGE(!row_optional, "Select should not return row after deletion");
181 }
182
183 void DBFixture::insert_row()
184 {
185     insert_row(m_default_name, m_default_label);
186 }
187
188 void DBFixture::insert_row(const Name &name, const Label &owner_label)
189 {
190     DB::Row rowPattern = create_default_row(name, owner_label, DataType::BINARY_DATA);
191     rowPattern.data = RawBuffer(100, 20);
192     rowPattern.dataSize = rowPattern.data.size();
193     rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
194     BOOST_REQUIRE_NO_THROW(m_db.saveRow(rowPattern));
195 }
196
197 void DBFixture::delete_row(const Name &name, const Label &owner_label)
198 {
199     bool exit_flag;
200     BOOST_REQUIRE_NO_THROW(exit_flag = m_db.deleteRow(name, owner_label));
201     BOOST_REQUIRE_MESSAGE(true == exit_flag, "remove name failed: no rows removed");
202 }
203
204 void DBFixture::add_permission(const Name &name, const Label &owner_label, const Label &accessor_label)
205 {
206     BOOST_REQUIRE_NO_THROW(m_db.setPermission(name,
207                                               owner_label,
208                                               accessor_label,
209                                               CKM::Permission::READ | CKM::Permission::REMOVE));
210 }
211
212 void DBFixture::read_row_expect_success(const Name &name, const Label &owner_label)
213 {
214     DB::Crypto::RowOptional row;
215     BOOST_REQUIRE_NO_THROW(row = m_db.getRow(name, owner_label, DataType::BINARY_DATA));
216     BOOST_REQUIRE_MESSAGE(row, "row is empty");
217     BOOST_REQUIRE_MESSAGE(row->name == name, "name is not valid");
218 }