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
|
#include <stdlib.h>
#include <sys/stat.h>
#include <test.h>
#include <known_dirs.h>
#include <cf3.defs.h>
#include <dbm_api.h>
#include <file_lib.h>
#include <files_copy.h>
#include <misc_lib.h> /* xsnprintf */
char CFWORKDIR[CF_BUFSIZE];
void tests_setup(void)
{
static char env[] = /* Needs to be static for putenv() */
"CFENGINE_TEST_OVERRIDE_WORKDIR=/tmp/db_test.XXXXXX";
char *workdir = strchr(env, '=') + 1; /* start of the path */
assert(workdir - 1 && workdir[0] == '/');
mkdtemp(workdir);
strlcpy(CFWORKDIR, workdir, CF_BUFSIZE);
putenv(env);
mkdir(GetStateDir(), (S_IRWXU | S_IRWXG | S_IRWXO));
}
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", GetStateDir());
CreateGarbage(tcdb_db);
#endif
#ifdef HAVE_LIBQDBM
char qdbm_db[CF_BUFSIZE];
xsnprintf(qdbm_db, CF_BUFSIZE, "%s/cf_classes.qdbm", GetStateDir());
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);
}
void test_old_workdir_db_location(void)
{
#ifndef LMDB
// We manipulate the LMDB file name directly. Not adapted to the others.
return;
#endif
CF_DB *db;
char *state_dir;
xasprintf(&state_dir, "%s%cstate", GetWorkDir(), FILE_SEPARATOR);
if (strcmp(GetStateDir(), state_dir) != 0)
{
// Test only works when statedir is $(workdir)/state.
free(state_dir);
return;
}
assert_true(OpenDB(&db, dbid_lastseen));
assert_true(WriteDB(db, "key", "first_value", strlen("first_value") + 1));
CloseDB(db);
char *old_db, *orig_db, *new_db;
// Due to caching of the path we need to use a different db when opening the
// second time, otherwise the path is not rechecked.
xasprintf(&orig_db, "%s%ccf_lastseen.lmdb", GetStateDir(), FILE_SEPARATOR);
xasprintf(&old_db, "%s%ccf_audit.lmdb", GetWorkDir(), FILE_SEPARATOR);
xasprintf(&new_db, "%s%ccf_audit.lmdb", GetStateDir(), FILE_SEPARATOR);
// Copy database to old location.
assert_true(CopyRegularFileDisk(orig_db, old_db));
// Change content.
assert_true(OpenDB(&db, dbid_lastseen));
assert_true(WriteDB(db, "key", "second_value", strlen("second_value") + 1));
CloseDB(db);
// Copy database to new location.
assert_true(CopyRegularFileDisk(orig_db, new_db));
char value[CF_BUFSIZE];
// Old location should take precedence.
assert_true(OpenDB(&db, dbid_audit));
assert_true(ReadDB(db, "key", value, sizeof(value)));
assert_string_equal(value, "first_value");
CloseDB(db);
free(state_dir);
free(old_db);
free(orig_db);
free(new_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),
unit_test(test_old_workdir_db_location),
};
PRINT_TEST_BANNER();
int ret = run_tests(tests);
tests_teardown();
return ret;
}
/* STUBS */
void FatalError(ARG_UNUSED char *s, ...)
{
fail();
exit(42);
}
|