summaryrefslogtreecommitdiff
path: root/tests/test_db_crypto.cpp
blob: 6539876f26f7e86acd70b13f7bc0fa3d52ad803e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <boost/test/unit_test.hpp>
#include <unistd.h>
#include <db-crypto.h>
#include <iostream>
#include <ckm/ckm-type.h>
#include <ckm/ckm-error.h>
#include <errno.h>
#include <test_common.h>
#include <DBFixture.h>

using namespace CKM;

namespace
{
const int restricted_local = 1;
const int restricted_global = 0;

const unsigned int c_test_retries = 1000;
const unsigned int c_num_names = 500;
const unsigned int c_num_names_add_test = 5000;
const unsigned int c_names_per_label = 15;
} // namespace anonymous

BOOST_FIXTURE_TEST_SUITE(DBCRYPTO_TEST, DBFixture)
BOOST_AUTO_TEST_CASE(DBtestSimple) {
    DBRow rowPattern = create_default_row();
    rowPattern.data = RawBuffer(32, 1);
    rowPattern.dataSize = rowPattern.data.size();
    rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);

    check_DB_integrity(rowPattern);
}
BOOST_AUTO_TEST_CASE(DBtestBIG) {
    DBRow rowPattern = create_default_row();
    rowPattern.data = createBigBlob(4096);
    rowPattern.dataSize = rowPattern.data.size();
    rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);

    check_DB_integrity(rowPattern);
}
BOOST_AUTO_TEST_CASE(DBtestGlobal) {
    DBRow rowPattern = create_default_row();
    rowPattern.data = RawBuffer(1024, 2);
    rowPattern.dataSize = rowPattern.data.size();
    rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);

    BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));

    DBRow name_duplicate = rowPattern;
    rowPattern.ownerLabel = rowPattern.ownerLabel + "1";
}
BOOST_AUTO_TEST_CASE(DBtestTransaction) {
    DBRow rowPattern = create_default_row();
    rowPattern.data = RawBuffer(100, 20);
    rowPattern.dataSize = rowPattern.data.size();
    rowPattern.tag = RawBuffer(AES_GCM_TAG_SIZE, 1);
    DBCrypto::Transaction transaction(&m_db);

    BOOST_REQUIRE_NO_THROW(m_db.saveDBRow(rowPattern));
    BOOST_REQUIRE_NO_THROW(transaction.rollback());

    DBCrypto::DBRowOptional row_optional;
    BOOST_REQUIRE_NO_THROW(row_optional = m_db.getDBRow(m_default_name, m_default_label,
                                                        DBDataType::BINARY_DATA));
    BOOST_CHECK_MESSAGE(!row_optional, "Row still present after rollback");
}

BOOST_AUTO_TEST_SUITE_END()



BOOST_FIXTURE_TEST_SUITE(DBCRYPTO_PERF_TEST, DBFixture)

BOOST_AUTO_TEST_CASE(DBperfAddNames)
{
    // actual test
    performance_start("saveDBRow");
    {
        generate_perf_DB(c_num_names_add_test, c_names_per_label);
    }
    performance_stop(c_num_names_add_test);
}

BOOST_AUTO_TEST_CASE(DBperfLookupAliasByOwner)
{
    // prepare data
    generate_perf_DB(c_num_names, c_names_per_label);

    unsigned int num_labels = c_num_names/c_names_per_label;
    Name name;
    Label label;

    // actual test - successful lookup
    performance_start("getDBRow");
    for(unsigned int t=0; t<c_test_retries; t++)
    {
        int label_num = rand() % num_labels;
        generate_label(label_num, label);

        unsigned int start_name = label_num*c_names_per_label;
        for(unsigned int name_num=start_name; name_num<(start_name+c_names_per_label); name_num++)
        {
            generate_name(name_num, name);
            read_row_expect_success(name, label);
        }
    }
    performance_stop(c_test_retries * c_num_names);
}

BOOST_AUTO_TEST_CASE(DBperfLookupAliasRandomOwnershipNoPermissions)
{
    // prepare data
    generate_perf_DB(c_num_names, c_names_per_label);

    Name name;
    Label owner_label;
    Label smack_label;
    unsigned int num_labels = c_num_names / c_names_per_label;

    // actual test - random lookup
    performance_start("getDBRow");
    for(unsigned int t=0; t<c_test_retries; t++)
    {
        int name_idx = rand()%c_num_names;
        generate_name(name_idx, name);
        generate_label(name_idx/c_names_per_label, owner_label);
        generate_label(rand()%num_labels, smack_label);

        // do not care of result
        m_db.getDBRow(name, owner_label, DBDataType::BINARY_DATA);
    }
    performance_stop(c_test_retries * c_num_names);
}

BOOST_AUTO_TEST_CASE(DBperfAddPermissions)
{
    // prepare data
    generate_perf_DB(c_num_names, c_names_per_label);

    // actual test - add access rights
    performance_start("setPermission");
    long iterations = add_full_access_rights(c_num_names, c_names_per_label);
    performance_stop(iterations);
}

BOOST_AUTO_TEST_CASE(DBperfAliasRemoval)
{
    // prepare data
    generate_perf_DB(c_num_names, c_names_per_label);
    add_full_access_rights(c_num_names, c_names_per_label);

    // actual test - random lookup
    performance_start("deleteDBRow");
    Name name;
    Label label;
    for(unsigned int t=0; t<c_num_names; t++)
    {
        generate_name(t, name);
        generate_label(t/c_names_per_label, label);

        BOOST_REQUIRE_NO_THROW(m_db.deleteDBRow(name, label));
    }
    performance_stop(c_num_names);

    // verify everything has been removed
    unsigned int num_labels = c_num_names / c_names_per_label;
    for(unsigned int l=0; l<num_labels; l++)
    {
        generate_label(l, label);
        LabelNameVector expect_no_data;
        BOOST_REQUIRE_NO_THROW(m_db.listNames(label, expect_no_data, DBDataType::BINARY_DATA));
        BOOST_REQUIRE(0 == expect_no_data.size());
    }
}

BOOST_AUTO_TEST_CASE(DBperfGetAliasList)
{
    // prepare data
    generate_perf_DB(c_num_names, c_names_per_label);
    add_full_access_rights(c_num_names, c_names_per_label);

    unsigned int num_labels = c_num_names / c_names_per_label;
    Label label;

    // actual test - random lookup
    performance_start("listNames");
    for(unsigned int t=0; t<(c_test_retries/num_labels); t++)
    {
        LabelNameVector ret_list;
        generate_label(rand()%num_labels, label);

        BOOST_REQUIRE_NO_THROW(m_db.listNames(label, ret_list, DBDataType::BINARY_DATA));
        BOOST_REQUIRE(c_num_names == ret_list.size());
        ret_list.clear();
    }
    performance_stop(c_test_retries/num_labels);
}
BOOST_AUTO_TEST_SUITE_END()


BOOST_AUTO_TEST_SUITE(DBCRYPTO_MIGRATION_TEST)
namespace
{
const unsigned migration_names = 16107;
const unsigned migration_labels = 273;
const unsigned migration_reference_label_idx = 0;
const unsigned migration_accessed_element_idx = 7;

void verifyDBisValid(DBFixture & fixture)
{
    /**
     * there are (migration_labels), each having (migration_names)/(migration_labels) entries.
     * reference label (migration_reference_label_idx) exists such that it has access to
     * all others' label element with index (migration_accessed_element_idx).
     *
     * Example:
     * - migration_label_63 has access to all items owned by migration_label_63,
     *   which gives (migration_names)/(migration_labels) entries.
     *
     * - migration_label_0 (0 is the reference label) has access to all items
     *   owned by migration_label_0 and all others' label element index 7,
     *   which gives (migration_names)/(migration_labels)  + (migration_labels-1) entries.
     *
     */
    Label reference_label;
    fixture.generate_label(migration_reference_label_idx, reference_label);

    // check number of elements accessible to the reference label
    LabelNameVector ret_list;
    BOOST_REQUIRE_NO_THROW(fixture.m_db.listNames(reference_label, ret_list, DBDataType::BINARY_DATA));
    BOOST_REQUIRE((migration_names/migration_labels)/*own items*/ + (migration_labels-1)/*other labels'*/ == ret_list.size());
    ret_list.clear();

    // check number of elements accessible to the other labels
    for(unsigned int l=0; l<migration_labels; l++)
    {
        // bypass the reference owner label
        if(l == migration_reference_label_idx)
            continue;

        Label current_label;
        fixture.generate_label(l, current_label);
        BOOST_REQUIRE_NO_THROW(fixture.m_db.listNames(current_label, ret_list, DBDataType::BINARY_DATA));
        BOOST_REQUIRE((migration_names/migration_labels) == ret_list.size());
        for(auto it: ret_list)
            BOOST_REQUIRE(it.first == current_label);
        ret_list.clear();
    }
}
struct DBVer1Migration : public DBFixture
{
    DBVer1Migration() : DBFixture("/usr/share/ckm-db-test/testme_ver1.db")
    {}
};

struct DBVer2Migration : public DBFixture
{
    DBVer2Migration() : DBFixture("/usr/share/ckm-db-test/testme_ver2.db")
    {}
};
}

BOOST_AUTO_TEST_CASE(DBMigrationDBVer1)
{
    DBVer1Migration DBver1;
    verifyDBisValid(DBver1);
}

BOOST_AUTO_TEST_CASE(DBMigrationDBVer2)
{
    DBVer2Migration DBver2;
    verifyDBisValid(DBver2);
}

BOOST_AUTO_TEST_CASE(DBMigrationDBCurrent)
{
    DBFixture currentDB;

    // prepare data using current DB mechanism
    Label reference_label;
    currentDB.generate_label(migration_reference_label_idx, reference_label);
    {
        currentDB.generate_perf_DB(migration_names, migration_names/migration_labels);

        // only the reference label has access to the other labels element <migration_accessed_element_idx>
        for(unsigned int l=0; l<migration_labels; l++)
        {
            // bypass the reference owner label
            if(l == migration_reference_label_idx)
                continue;

            unsigned element_index = migration_accessed_element_idx + l*migration_names/migration_labels;

            // add permission
            Name accessed_name;
            currentDB.generate_name(element_index, accessed_name);
            Label current_label;
            currentDB.generate_label(l, current_label);
            currentDB.add_permission(accessed_name, current_label, reference_label);
        }
    }

    verifyDBisValid(currentDB);
}

BOOST_AUTO_TEST_SUITE_END()