File: db_test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (188 lines) | stat: -rw-r--r-- 4,733 bytes parent folder | download
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
#include <test.h>

#include <cf3.defs.h>
#include <dbm_api.h>
#include <misc_lib.h>                                          /* xsnprintf */


char CFWORKDIR[CF_BUFSIZE];

void tests_setup(void)
{
    xsnprintf(CFWORKDIR, CF_BUFSIZE, "/tmp/db_test.XXXXXX");
    mkdtemp(CFWORKDIR);
}

void tests_teardown(void)
{
    char cmd[CF_BUFSIZE];
    xsnprintf(cmd, CF_BUFSIZE, "rm -rf '%s'", CFWORKDIR);
    system(cmd);
}

void test_open_close(void)
{
    // Test that we can simply open and close a database without doing anything.
    CF_DB *db;
    assert_int_equal(OpenDB(&db, dbid_classes), true);
    CloseDB(db);
}

void test_read_write(void)
{
    // Test that we can do normal reads and write, and that the values are
    // reflected in the database.
    CF_DB *db;
    char value[CF_BUFSIZE];
    strcpy(value, "myvalue");
    int vsize = strlen(value) + 1;

    assert_int_equal(OpenDB(&db, dbid_classes), true);

    assert_int_equal(ReadDB(db, "written_entry", &value, vsize), false);
    assert_string_equal(value, "myvalue");
    assert_int_equal(WriteDB(db, "written_entry", value, vsize), true);
    strcpy(value, "");
    assert_int_equal(ReadDB(db, "written_entry", &value, vsize), true);
    assert_string_equal(value, "myvalue");

    CloseDB(db);

    // Check also after we reopen the database.
    assert_int_equal(OpenDB(&db, dbid_classes), true);
    strcpy(value, "");
    assert_int_equal(ReadDB(db, "written_entry", &value, vsize), true);
    assert_string_equal(value, "myvalue");

    CloseDB(db);
}

void test_iter_modify_entry(void)
{
    /* Test that deleting entry under cursor does not interrupt iteration */

    CF_DB *db;
    assert_int_equal(OpenDB(&db, dbid_classes), true);

    assert_int_equal(WriteDB(db, "foobar", "abc", 3), true);
    assert_int_equal(WriteDB(db, "bazbaz", "def", 3), true);
    assert_int_equal(WriteDB(db, "booo", "ghi", 3), true);

    CF_DBC *cursor;
    assert_int_equal(NewDBCursor(db, &cursor), true);

    char *key;
    int ksize;
    void *value;
    int vsize;

    assert_int_equal(NextDB(cursor, &key, &ksize, &value, &vsize), true);

    assert_int_equal(DBCursorWriteEntry(cursor, "eee", 3), true);

    assert_int_equal(NextDB(cursor, &key, &ksize, &value, &vsize), true);
    assert_int_equal(NextDB(cursor, &key, &ksize, &value, &vsize), true);

    assert_int_equal(DeleteDBCursor(cursor), true);

    CloseDB(db);
}


void test_iter_delete_entry(void)
{
    /* Test that deleting entry under cursor does not interrupt iteration */

    CF_DB *db;
    assert_int_equal(OpenDB(&db, dbid_classes), true);

    assert_int_equal(WriteDB(db, "foobar", "abc", 3), true);
    assert_int_equal(WriteDB(db, "bazbaz", "def", 3), true);
    assert_int_equal(WriteDB(db, "booo", "ghi", 3), true);

    CF_DBC *cursor;
    assert_int_equal(NewDBCursor(db, &cursor), true);

    char *key;
    int ksize;
    void *value;
    int vsize;

    assert_int_equal(NextDB(cursor, &key, &ksize, &value, &vsize), true);

    assert_int_equal(DBCursorDeleteEntry(cursor), true);

    assert_int_equal(NextDB(cursor, &key, &ksize, &value, &vsize), true);
    assert_int_equal(NextDB(cursor, &key, &ksize, &value, &vsize), true);

    assert_int_equal(DeleteDBCursor(cursor), true);

    CloseDB(db);
}

#if defined(HAVE_LIBTOKYOCABINET) || defined(HAVE_LIBQDBM) || defined(HAVE_LIBLMDB)
static void CreateGarbage(const char *filename)
{
    FILE *fh = fopen(filename, "w");
    for(int i = 0; i < 2; ++i)
    {
        fwrite("some garbage!", 14, 1, fh);
    }
    fclose(fh);
}
#endif /* HAVE_LIBTOKYOCABINET || HAVE_LIBQDBM */

void test_recreate(void)
{
    /* Test that recreating database works properly */
#ifdef HAVE_LIBTOKYOCABINET
    char tcdb_db[CF_BUFSIZE];
    xsnprintf(tcdb_db, CF_BUFSIZE, "%s/cf_classes.tcdb", CFWORKDIR);
    CreateGarbage(tcdb_db);
#endif
#ifdef HAVE_LIBQDBM
    char qdbm_db[CF_BUFSIZE];
    xsnprintf(qdbm_db, CF_BUFSIZE, "%s/cf_classes.qdbm", CFWORKDIR);
    CreateGarbage(qdbm_db);
#endif
#ifdef HAVE_LIBLMDB
    char lmdb_db[CF_BUFSIZE];
    xsnprintf(lmdb_db, CF_BUFSIZE, "%s/cf_classes.lmdb", CFWORKDIR);
    CreateGarbage(lmdb_db);
#endif

    CF_DB *db;
    assert_int_equal(OpenDB(&db, dbid_classes), true);
    CloseDB(db);
}

int main()
{
    PRINT_TEST_BANNER();
    tests_setup();

    const UnitTest tests[] =
        {
            unit_test(test_open_close),
            unit_test(test_read_write),
            unit_test(test_iter_modify_entry),
            unit_test(test_iter_delete_entry),
            unit_test(test_recreate),
        };

    PRINT_TEST_BANNER();
    int ret = run_tests(tests);

    tests_teardown();
    return ret;
}

/* STUBS */

void FatalError(ARG_UNUSED char *s, ...)
{
    fail();
    exit(42);
}