void test_readfstab(const char * file) { Key * parentKey = keyNew ("user/tests/fstab", KEY_VALUE, srcdir_file(file), KEY_END); KeySet *conf = 0; PLUGIN_OPEN("fstab"); KeySet *ks=ksNew(0, KS_END); printf ("Reading fstab using file: %s\n", file); succeed_if (plugin->kdbGet(plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); // output_keyset(ks); Key *key = ksLookupByName(ks, "user/tests/fstab/\\//device",0); exit_if_fail (key, "rootfs device not found"); succeed_if (strcmp( "/dev/sda1", keyValue(key)) == 0, "device not correct"); key = ksLookupByName(ks, "user/tests/fstab/\\/media\\/ext4/device",0); exit_if_fail (key, "media device not found"); succeed_if (strcmp( "/dev/sdg1", keyValue(key)) == 0, "device not correct"); exit_if_fail (key = ksLookupByName(ks, "user/tests/fstab/\\/media\\/ext4/dumpfreq",0), "rootfs device not found"); succeed_if (strcmp( "0", keyValue(key)) == 0, "dumpfreq not correct"); ksDel(ks); keyDel(parentKey); PLUGIN_CLOSE(); }
// test loading lua twice static void test_two_scripts (void) { printf ("Testing loading of two active lua plugins...\n"); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin.lua"), KEY_END), keyNew ("user/print", KEY_END), KS_END); KeySet * conf2 = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin2.lua"), KEY_END), keyNew ("user/print", KEY_END), KS_END); Key * errorKey = keyNew ("", KEY_END); Plugin * plugin = elektraPluginOpen ("lua", modules, conf, errorKey); succeed_if (output_warnings (errorKey), "warnings in kdbOpen"); succeed_if (output_error (errorKey), "errors in kdbOpen"); exit_if_fail (plugin != NULL, "unable to load lua plugin"); keyDel (errorKey); Key * errorKey2 = keyNew ("", KEY_END); Plugin * plugin2 = elektraPluginOpen ("lua", modules, conf2, errorKey2); succeed_if (output_warnings (errorKey2), "warnings in kdbOpen"); succeed_if (output_error (errorKey2), "errors in kdbOpen"); exit_if_fail (plugin2 != NULL, "unable to load lua plugin again"); keyDel (errorKey2); elektraPluginClose (plugin2, 0); elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); }
static void test_plainIniRead (char * fileName) { Key * parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("ini"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if (output_error (parentKey), "error in kdbGet"); succeed_if (output_warnings (parentKey), "warnings in kdbGet"); Key * key = ksLookupByName (ks, "user/tests/ini-read/nosectionkey", KDB_O_NONE); exit_if_fail (key, "nosectionkey not found"); succeed_if (!strcmp ("nosectionvalue", keyString (key)), "nosectionkey contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-read/section1", KDB_O_NONE); exit_if_fail (key, "section1 not found"); succeed_if (!keyValue (key), "section value was not empty"); key = ksLookupByName (ks, "user/tests/ini-read/section1/key1", KDB_O_NONE); exit_if_fail (key, "key1 not found"); succeed_if (!strcmp ("value1", keyString (key)), "key1 contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-read/section2/emptykey", KDB_O_NONE); exit_if_fail (key, "emptykey not found"); succeed_if (!strcmp ("", keyString (key)), "emptykey contained invalid data"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_multilineIniRead (char * fileName) { Key * parentKey = keyNew ("user/tests/ini-multiline-read", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (30, keyNew ("system/multiline", KEY_VALUE, "1", KEY_END), keyNew ("system/linecont", KEY_VALUE, "\t", KEY_END), KS_END); PLUGIN_OPEN ("ini"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if (output_error (parentKey), "error in kdbGet"); succeed_if (output_warnings (parentKey), "warnings in kdbGet"); Key * key = ksLookupByName (ks, "user/tests/ini-multiline-read/multilinesection/key1", KDB_O_NONE); exit_if_fail (key, "key1 not found"); succeed_if (!strcmp ("value1\nwith continuation\nlines", keyString (key)), "key1 contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-multiline-read/singlelinesection/key2", KDB_O_NONE); exit_if_fail (key, "key2 not found"); succeed_if (!strcmp ("", keyString (key)), "key2 contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-multiline-read/singlelinesection/key3", KDB_O_NONE); exit_if_fail (key, "key3 not found"); succeed_if (!strcmp ("value3", keyString (key)), "key3 contained invalid data"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
void test_zeroMatchFlags (void) { Key * parentKey = keyNew ("user/tests/glob", KEY_END); KeySet * conf = ksNew (20, keyNew ("user/glob/#1", KEY_VALUE, "*test1", KEY_META, "testmetakey1", "testvalue1", KEY_END), /* disable default pathname globbing behaviour */ keyNew ("user/glob/#1/flags", KEY_VALUE, "", KEY_END), KS_END); PLUGIN_OPEN ("glob"); KeySet * ks = createKeys (); succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful"); succeed_if (output_error (parentKey), "error in kdbSet"); succeed_if (output_warnings (parentKey), "warnings in kdbSet"); Key * key = ksLookupByName (ks, "user/tests/glob/test1", 0); exit_if_fail (key, "key user/tests/glob/test1 not found"); const Key * metaKey1 = keyGetMeta (key, "testmetakey1"); exit_if_fail (metaKey1, "testmetakey1 not found"); succeed_if (strcmp ("testvalue1", keyValue (metaKey1)) == 0, "value of metakey testmetakey1 not correct"); key = ksLookupByName (ks, "user/tests/glob/test3", 0); exit_if_fail (key, "user/tests/glob/test3 not found"); succeed_if (!keyGetMeta (key, "testmetakey1"), "testmetakey1 copied to wrong key"); key = ksLookupByName (ks, "user/tests/glob/test2/subtest1", 0); exit_if_fail (key, "user/tests/glob/test2/subtest1 not found"); const Key * metaKey2 = keyGetMeta (key, "testmetakey1"); exit_if_fail (metaKey2, "testmetakey1 not found"); succeed_if (strcmp ("testvalue1", keyValue (metaKey2)) == 0, "value of metakey testmetakey1 not correct"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_readPref (char * fileName) { Key * parentKey = keyNew ("user/tests/pref-read", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("mozprefs"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key * key = ksLookupByName (ks, "user/tests/pref-read/user/a/user/key", KDB_O_NONE); exit_if_fail (key, "Key a.user.key not found"); succeed_if (!strcmp (keyString (key), "usertest"), "Key a.user.key contains invalid data"); key = ksLookupByName (ks, "user/tests/pref-read/lock/a/lock/key", KDB_O_NONE); exit_if_fail (key, "Key a.lock.key not found"); succeed_if (!strcmp (keyString (key), "true"), "Key a.lock.key contains invalid data"); key = ksLookupByName (ks, "user/tests/pref-read/pref/a/default/key", KDB_O_NONE); exit_if_fail (key, "Key a.default.key not found"); succeed_if (!strcmp (keyString (key), "1"), "Key a.default.key contains invalid data"); key = ksLookupByName (ks, "user/tests/pref-read/sticky/a/sticky/key", KDB_O_NONE); exit_if_fail (key, "Key a.sticky.key not found"); succeed_if (!strcmp (keyString (key), "false"), "Key a.sticky.key contains invalid data"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
void test_readline(){ char * filename = srcdir_file("line/linetest"); Key * parentKey = keyNew ("user/tests/line", KEY_VALUE, filename, KEY_END); KeySet *conf = 0; PLUGIN_OPEN("line"); printf("%s\n", filename); KeySet *ks=ksNew(0, KS_END); succeed_if (plugin->kdbGet(plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key *key = ksLookupByName(ks, "user/tests/line/#0", 0); exit_if_fail (key, "line1 key not found"); succeed_if (strcmp("test1", keyValue(key)) == 0, "line ´ does not match"); key = ksLookupByName(ks, "user/tests/line/#_10", 0); exit_if_fail (key, "line11 key not found"); succeed_if (strcmp("", keyValue(key)) == 0, "line 10 should be blank"); key = ksLookupByName(ks, "user/tests/line/#_13", 0); exit_if_fail (key, "line14 key not found"); succeed_if (strcmp("printf(\"hello world\\n\");", keyValue(key)) == 0, "line 13 not correct"); ksDel (ks); keyDel(parentKey); PLUGIN_CLOSE(); }
void test_hostLensDelete(char *sourceFile, char *compFile) { Key *parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (sourceFile), KEY_END); KeySet *conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END); PLUGIN_OPEN("augeas"); KeySet *ks = ksNew(0, KS_END); succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if(output_error (parentKey), "error in kdbGet"); succeed_if(output_warnings (parentKey), "warnings in kdbGet"); Key *key = ksLookupByName (ks, "user/tests/augeas-hosts/1", 0); exit_if_fail(key, "localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); key = ksLookupByName (ks, "user/tests/augeas-hosts/1/ipaddr", 0); exit_if_fail(key, "ip address of localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); key = ksLookupByName (ks, "user/tests/augeas-hosts/1/canonical", 0); exit_if_fail(key, "canonical of localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); key = ksLookupByName (ks, "user/tests/augeas-hosts/1/#comment", 0); exit_if_fail(key, "comment of localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); keySetString (parentKey, elektraFilename()); succeed_if(plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if(output_error (parentKey), "error in kdbSet"); succeed_if(output_warnings (parentKey), "warnings in kdbSet"); succeed_if( compare_line_files (srcdir_file (compFile), keyString (parentKey)), "files do not match as expected"); ksDel (ks); elektraUnlink(keyString (parentKey)); keyDel (parentKey); PLUGIN_CLOSE () ; }
void test_resolve () { int pathLen = tempHomeLen + 1 + strlen (KDB_DB_USER) + 12 + 1; char * path = elektraMalloc (pathLen); exit_if_fail (path != 0, "elektraMalloc failed"); snprintf (path, pathLen, "%s/%s/elektra.ecf", tempHome, KDB_DB_USER); printf ("Resolve Filename\n"); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); Key * parentKey = keyNew ("system", KEY_END); Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0); exit_if_fail (plugin, "could not load resolver plugin"); KeySet * test_config = set_pluginconf (); KeySet * config = elektraPluginGetConfig (plugin); succeed_if (config != 0, "there should be a config"); compare_keyset (config, test_config); ksDel (test_config); succeed_if (plugin->kdbOpen != 0, "no open pointer"); succeed_if (plugin->kdbClose != 0, "no open pointer"); succeed_if (plugin->kdbGet != 0, "no open pointer"); succeed_if (plugin->kdbSet != 0, "no open pointer"); succeed_if (plugin->kdbError != 0, "no open pointer"); succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name"); resolverHandles * h = elektraPluginGetData (plugin); exit_if_fail (h != 0, "no plugin handle"); succeed_if (!strcmp (h->system.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp (h->system.filename, KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct"); succeed_if_same_string (h->user.path, "elektra.ecf"); succeed_if_same_string (h->user.filename, path); plugin->kdbClose (plugin, parentKey); // reinit with system path only plugin->kdbOpen (plugin, parentKey); h = elektraPluginGetData (plugin); exit_if_fail (h != 0, "no plugin handle"); succeed_if (!strcmp (h->system.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp (h->system.filename, KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct"); succeed_if (h->user.filename == NULL, "user was initialized, but is not needed"); plugin->kdbClose (plugin, parentKey); keyDel (parentKey); elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); elektraFree (path); }
void test_checkfile () { printf ("Check file\n"); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0); exit_if_fail (plugin, "did not find a resolver"); Key * root = keyNew ("system/elektra/modules", KEY_END); keyAddBaseName (root, plugin->name); KeySet * contract = ksNew (5, KS_END); plugin->kdbGet (plugin, contract, root); keyAddName (root, "/exports/checkfile"); Key * found = ksLookup (contract, root, 0); exit_if_fail (found, "did not find checkfile symbol"); typedef int (*func_t) (const char *); union { func_t f; void * v; } conversation; succeed_if (keyGetBinary (found, &conversation.v, sizeof (conversation)) == sizeof (conversation), "could not get binary"); func_t checkFile = conversation.f; succeed_if (checkFile ("valid") == 1, "valid file not recognised"); succeed_if (checkFile ("/valid") == 0, "valid absolute file not recognised"); succeed_if (checkFile ("/absolute/valid") == 0, "valid absolute file not recognised"); succeed_if (checkFile ("../valid") == -1, "invalid file not recognised"); succeed_if (checkFile ("valid/..") == -1, "invalid file not recognised"); succeed_if (checkFile ("/../valid") == -1, "invalid absolute file not recognised"); succeed_if (checkFile ("/valid/..") == -1, "invalid absolute file not recognised"); succeed_if (checkFile ("very..strict") == -1, "resolver is currently very strict"); succeed_if (checkFile ("very/..strict") == -1, "resolver is currently very strict"); succeed_if (checkFile ("very../strict") == -1, "resolver is currently very strict"); succeed_if (checkFile ("very/../strict") == -1, "resolver is currently very strict"); succeed_if (checkFile ("/") == -1, "invalid absolute file not recognised"); succeed_if (checkFile (".") == -1, "invalid file not recognised"); succeed_if (checkFile ("..") == -1, "invalid file not recognised"); ksDel (contract); keyDel (root); elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); }
static void test_backref() { printf ("Test back references\n"); KeySet *modules = ksNew(0, KS_END); elektraModulesInit(modules, 0); Backend *backend = elektraBackendOpen(set_backref(), modules, 0); succeed_if (backend != 0, "there should be a backend"); succeed_if (backend->getplugins[0] == 0, "there should be no plugin"); exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin"); succeed_if (backend->getplugins[2] == 0, "there should be no plugin"); succeed_if (backend->setplugins[0] == 0, "there should be no plugin"); exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin"); succeed_if (backend->setplugins[2] == 0, "there should be no plugin"); Key *mp; succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName(mp), "user/tests/backend/backref"); succeed_if_same_string (keyString(mp), "backref"); Plugin *plugin1 = backend->getplugins[1]; Plugin *plugin2 = backend->setplugins[1]; Plugin *plugin3 = backend->errorplugins[1]; succeed_if (plugin1 != 0, "there should be a plugin"); succeed_if (plugin2 != 0, "there should be a plugin"); succeed_if (plugin3 != 0, "there should be a plugin"); succeed_if (plugin1 == plugin2, "it should be the same plugin"); succeed_if (plugin2 == plugin3, "it should be the same plugin"); succeed_if (plugin1 == plugin3, "it should be the same plugin"); succeed_if (plugin1->refcounter == 3, "ref counter should be 3"); KeySet *test_config = set_pluginconf(); KeySet *config = elektraPluginGetConfig (plugin1); succeed_if (config != 0, "there should be a config"); compare_keyset(config, test_config); ksDel (test_config); succeed_if (plugin1->kdbGet != 0, "no get pointer"); succeed_if (plugin1->kdbSet != 0, "no set pointer"); succeed_if (plugin2->kdbGet != 0, "no get pointer"); succeed_if (plugin2->kdbSet != 0, "no set pointer"); elektraBackendClose (backend, 0); elektraModulesClose (modules, 0); ksDel (modules); }
static void test_default() { printf ("Test default " KDB_DEFAULT_STORAGE "\n"); KeySet *modules = ksNew(0, KS_END); elektraModulesInit(modules, 0); Plugin *plugin = elektraPluginOpen(KDB_DEFAULT_STORAGE, modules, set_pluginconf(), 0); exit_if_fail (plugin, "KDB_DEFAULT_STORAGE: " KDB_DEFAULT_STORAGE " plugin could not be loaded"); KeySet *test_config = set_pluginconf(); KeySet *config = elektraPluginGetConfig (plugin); succeed_if (config != 0, "there should be a config"); compare_keyset(config, test_config); ksDel (test_config); succeed_if (plugin->kdbGet != 0, "no get pointer"); succeed_if (plugin->kdbSet != 0, "no set pointer"); elektraPluginClose(plugin, 0); Backend *backend = elektraBackendOpenDefault(modules, KDB_DB_FILE, 0); Key *mp; succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName(mp), ""); succeed_if_same_string (keyString(mp), "default"); elektraBackendClose(backend, 0); elektraModulesClose (modules, 0); ksDel (modules); }
void test_iterate() { Key *key; key = keyNew ("user/test", KEY_END); exit_if_fail (key, "could not create new key"); succeed_if (keyRewindMeta(key) == 0, "Could not rewind empty key"); succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty"); succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty"); keySetMeta (key, "meta1", "meta_value"); succeed_if (keyRewindMeta(key) == 0, "Could not rewind key"); succeed_if (!strcmp(keyName(keyNextMeta(key)), "meta1"), "keyNextMeta does not work at 1. iteration"); succeed_if (!strcmp(keyValue(keyCurrentMeta(key)), "meta_value"), "keyCurrentMeta does not work at 1. iteration"); succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty at 2. iteration"); succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty at 2. iteration"); succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty at 3. iteration"); succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty at 3. iteration"); succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty at 4. iteration"); succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty at 4. iteration"); keyDel (key); }
void test_writing (void) { KeySet * conf = ksNew (0, KS_END); Key * parentKey = keyNew ("user/tests/yajl", KEY_VALUE, "/proc/self/fd/1", KEY_END); Plugin * plugin = elektraPluginOpen ("yajl", modules, conf, 0); exit_if_fail (plugin != 0, "could not open plugin"); KeySet * ks = getNullKeys (); /* output_keyset(ks); succeed_if(plugin->kdbSet(plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if(plugin->kdbSet(plugin, getBooleanKeys(), parentKey) == 1, "kdbSet was not successful"); succeed_if(plugin->kdbSet(plugin, getNumberKeys(), parentKey) == 1, "kdbSet was not successful"); succeed_if(plugin->kdbSet(plugin, getStringKeys(), parentKey) == 1, "kdbSet was not successful"); succeed_if(plugin->kdbSet(plugin, getMapKeys(), parentKey) == 1, "kdbSet was not successful"); succeed_if(plugin->kdbSet(plugin, getArrayKeys(), parentKey) == 1, "kdbSet was not successful"); ksDel(ks); ks = getOpenICCKeys(); succeed_if(plugin->kdbSet(plugin, ks, parentKey) == 1, "kdbSet was not successful"); */ ksDel (ks); keyDel (parentKey); elektraPluginClose (plugin, 0); }
void test_readWrite (const char * fileName, KeySet * conf) { printf ("Test read write with %s\n", srcdir_file (fileName)); Plugin * plugin = elektraPluginOpen ("yajl", modules, conf, 0); exit_if_fail (plugin != 0, "could not open plugin"); // printf ("Test with %s\n", srcdir_file(fileName)); Key * parentKey = keyNew ("user/tests/yajl", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * keys = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, keys, parentKey) == 1, "kdbGet was not successful"); succeed_if (output_error (parentKey), "error in kdbGet"); succeed_if (output_warnings (parentKey), "warnings in kdbGet"); // output_keyset(keys); keySetString (parentKey, elektraFilename ()); // keySetString(parentKey, "/proc/self/fd/1"); // printf("File name is: %s\n", keyString(parentKey)); succeed_if (plugin->kdbSet (plugin, keys, parentKey) == 1, "kdbSet was not successful"); succeed_if (output_error (parentKey), "error in kdbSet"); succeed_if (output_warnings (parentKey), "warnings in kdbSet"); succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected"); elektraUnlink (keyString (parentKey)); keyDel (parentKey); ksDel (keys); elektraPluginClose (plugin, 0); }
static void test_elektraMalloc (void) { char * buffer = 0; buffer = elektraMalloc (50); exit_if_fail (buffer, "buffer must not be 0 after allocation"); elektraRealloc ((void **)&buffer, 100); exit_if_fail (buffer, "buffer must not be 0 after reallocation"); elektraRealloc ((void **)&buffer, 20); exit_if_fail (buffer, "buffer must not be 0 after reallocation"); elektraFree (buffer); buffer = elektraCalloc (50); exit_if_fail (buffer, "buffer must not be 0 after allocation"); for (int i = 0; i < 50; ++i) { succeed_if (buffer[i] == 0, "elektraCalloc did not initialize buffer with zeros"); } elektraRealloc ((void **)&buffer, 100); exit_if_fail (buffer, "buffer must not be 0 after reallocation"); elektraRealloc ((void **)&buffer, 20); exit_if_fail (buffer, "buffer must not be 0 after reallocation"); char * dup = elektraStrNDup (buffer, 20); exit_if_fail (dup, "could not duplicate buffer"); elektraFree (buffer); buffer = 0; for (int i = 0; i < 20; ++i) { succeed_if (dup[i] == 0, "elektraStrNDup did not correctly copy zero-buffer"); } elektraFree (dup); }
void testKeys (KeySet * ks) { Key * key = ksLookupByName (ks, "user/tests/glob/test1", 0); exit_if_fail (key, "key user/tests/glob/test1 not found"); const Key * metaKey = keyGetMeta (key, "testmetakey1"); exit_if_fail (metaKey, "testmetakey1 not found"); succeed_if (strcmp ("testvalue1", keyValue (metaKey)) == 0, "value of metakey testmetakey1 not correct"); metaKey = keyGetMeta (key, "testmetakey2"); exit_if_fail (metaKey, "testmetakey2 not found"); succeed_if (strcmp ("testvalue2", keyValue (metaKey)) == 0, "value of metakey testmetakey2 not correct"); key = ksLookupByName (ks, "user/tests/glob/test2/subtest1", 0); exit_if_fail (key, "key user/test1/subtest1 not found"); succeed_if (!keyGetMeta (key, "testmetakey1"), "testmetakey1 copied to wrong key"); succeed_if (!keyGetMeta (key, "testmetakey2"), "testmetakey2 copied to wrong key"); key = ksLookupByName (ks, "user/tests/glob/test3", 0); exit_if_fail (key, "key user/tests/glob/test3 not found"); metaKey = keyGetMeta (key, "testmetakey1"); exit_if_fail (metaKey, "testmetakey1 not found"); succeed_if (strcmp ("testvalue1", keyValue (metaKey)) == 0, "value of metakey testmetakey1 not correct"); metaKey = keyGetMeta (key, "testmetakey2"); exit_if_fail (metaKey, "testmetakey2 not found"); succeed_if (strcmp ("testvalue2", keyValue (metaKey)) == 0, "value of metakey testmetakey2 not correct"); }
static void test_commentIniRead (char * fileName) { Key * parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("ini"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if (output_error (parentKey), "error in kdbGet"); succeed_if (output_warnings (parentKey), "warnings in kdbGet"); Key * key = ksLookupByName (ks, "user/tests/ini-read/nosectionkey", KDB_O_NONE); exit_if_fail (key, "nosectionkey not found"); const Key * noSectionComment = keyGetMeta (key, "comments/#0"); exit_if_fail (noSectionComment, "nosectionkey contained no comment"); succeed_if (!strcmp (";nosection comment1", keyString (noSectionComment)), "nosectionkey contained an invalid comment"); noSectionComment = keyGetMeta (key, "comments/#1"); exit_if_fail (noSectionComment, "nosectionkey contained no comment"); succeed_if (!strcmp (";nosection comment2", keyString (noSectionComment)), "nosectionkey contained an invalid comment"); key = ksLookupByName (ks, "user/tests/ini-read/section1", KDB_O_NONE); exit_if_fail (key, "section1 not found"); const Key * sectionComment = keyGetMeta (key, "comments/#0"); exit_if_fail (sectionComment, "sectionkey contained no comment"); succeed_if (!strcmp (";section comment1", keyString (sectionComment)), "sectionkey contained an invalid comment"); sectionComment = keyGetMeta (key, "comments/#1"); exit_if_fail (sectionComment, "sectionkey contained no comment"); succeed_if (!strcmp (";section comment2", keyString (sectionComment)), "sectionkey contained an invalid comment"); key = ksLookupByName (ks, "user/tests/ini-read/section1/key1", KDB_O_NONE); exit_if_fail (key, "key1 not found"); const Key * keyComment_ = keyGetMeta (key, "comments/#0"); exit_if_fail (keyComment_, "key1 contained no comment"); succeed_if (!strcmp (";key comment1", keyString (keyComment_)), "key1 contained an invalid comment"); keyComment_ = keyGetMeta (key, "comments/#1"); exit_if_fail (keyComment_, "key1 contained no comment"); succeed_if (!strcmp (";key comment2", keyString (keyComment_)), "key1 contained an invalid comment"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
void test_keyset() { KeySet *ks; Key *cur; int counter; printf("Testing KeySet from xml\n"); ks = ksNew(0, KS_END); exit_if_fail( ksFromXMLfile(ks, srcdir_file("xmltool/keyset.xml")) == 0, "ksFromXMLfile(key.xml) failed."); counter = 0; ksRewind(ks); while ( (cur = ksNext(ks)) ) { counter ++; /* Make tests ... */ // printf ("counter: %d - %s\n", counter, keyName(cur)); switch (counter) { // <key type="43" basename="0-27042916" value="0 216905227"><comment>2551516588474823843</comment></key> case 1: succeed_if (strcmp (keyName(cur),"user/tests/filesys/0-27042916") == 0,"name of first key not correct"); succeed_if (strcmp (keyValue(cur),"0 216905227") == 0,"value of first key not correct"); succeed_if (strcmp (keyComment(cur),"2551516588474823843") == 0,"comment of first key not correct"); break; // <key type="253" basename="1-2449524622" value="1 1679328197"><comment>3246436893195629244</comment></key> case 2: succeed_if (strcmp (keyName(cur),"user/tests/filesys/1-2449524622") == 0,"name of 2. key not correct"); succeed_if (strcmp (keyValue(cur),"1 1679328197") == 0,"value of 2. key not correct"); succeed_if (strcmp (keyComment(cur),"3246436893195629244") == 0,"comment of 2. key not correct"); break; // <key type="string" basename="dir-1-0"> case 3: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0") == 0,"name of 3. key not correct"); break; // <key type="114" basename="0-294164813" value="0 216245011"><comment>18454108762891828026</comment></key> case 4: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/0-294164813") == 0,"name of 4. key not correct"); succeed_if (strcmp (keyValue(cur),"0 216245011") == 0,"value of 4. key not correct"); succeed_if (strcmp (keyComment(cur),"18454108762891828026") == 0,"comment of 4. key not correct"); break; // <key type="135" basename="1-1479930365" value="1 2732423037"><comment>24597295372375238</comment></key> case 5: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/1-1479930365") == 0,"name of 4. key not correct"); succeed_if (strcmp (keyValue(cur),"1 2732423037") == 0,"value of 4. key not correct"); succeed_if (strcmp (keyComment(cur),"24597295372375238") == 0,"comment of 4. key not correct"); break; // <key type="string" basename="dir-2-0"> case 6: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/dir-2-0") == 0,"name of 3. key not correct"); break; // <key type="144" basename="0-215571059" value="0 264857705"><comment>2188631490667217086</comment></key> case 7: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/dir-2-0/0-215571059") == 0,"name of 4. key not correct"); succeed_if (strcmp (keyValue(cur),"0 264857705") == 0,"value of 4. key not correct"); succeed_if (strcmp (keyComment(cur),"2188631490667217086") == 0,"comment of 4. key not correct"); break; } } ksDel(ks); }
static void test_umlauts() { printf ("Test umlauts trie\n"); Trie *trie = test_insert (0, "user/umlauts/test", "slash"); trie = test_insert (trie, "user/umlauts#test", "hash"); trie = test_insert (trie, "user/umlauts test", "space"); trie = test_insert (trie, "user/umlauts\200test", "umlauts"); exit_if_fail (trie, "trie was not build up successfully"); Key *searchKey = keyNew("user", KEY_END); Backend *backend = elektraTrieLookup(trie, searchKey); succeed_if (!backend, "there should be no backend"); Key *mp = keyNew("user/umlauts/test", KEY_VALUE, "slash", KEY_END); keySetName(searchKey, "user/umlauts/test"); backend = elektraTrieLookup(trie, searchKey); succeed_if (backend, "there should be a backend"); compare_key(backend->mountpoint, mp); keySetName(searchKey, "user/umlauts#test"); keySetName(mp, "user/umlauts#test"); keySetString(mp, "hash"); Backend *b2 = elektraTrieLookup(trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend != b2, "should be other backend"); compare_key(b2->mountpoint, mp); keySetName(searchKey, "user/umlauts test"); keySetName(mp, "user/umlauts test"); keySetString(mp, "space"); b2 = elektraTrieLookup(trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend != b2, "should be other backend"); compare_key(b2->mountpoint, mp); keySetName(searchKey, "user/umlauts\200test"); keySetName(mp, "user/umlauts\200test"); keySetString(mp, "umlauts"); b2 = elektraTrieLookup(trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend != b2, "should be other backend"); compare_key(b2->mountpoint, mp); // output_trie(trie); elektraTrieClose(trie, 0); keyDel (mp); keyDel (searchKey); }
/** * @internal * Send Elektra's D-Bus message. * * @param signalName signal name * @param keyName key name */ static void dbusSendMessage (const char * signalName, const char * keyName) { DBusMessage * message; const char * interface = "org.libelektra"; const char * path = "/org/libelektra/configuration"; DBusConnection * connection = getDbusConnection (testBusType); exit_if_fail (connection != NULL, "could not get bus connection"); message = dbus_message_new_signal (path, interface, signalName); exit_if_fail (message, "could not allocate dbus message"); exit_if_fail (dbus_message_append_args (message, DBUS_TYPE_STRING, &keyName, DBUS_TYPE_INVALID), "could not add message arguments"); dbus_connection_send (connection, message, NULL); dbus_message_unref (message); dbus_connection_unref (connection); return; }
static void testread (const char * file) { Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END); KeySet * conf = ksNew (20, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END), keyNew ("system/header", KEY_VALUE, "colname", KEY_END), KS_END); PLUGIN_OPEN ("csvstorage"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key * key; key = ksLookupByName (ks, "user/tests/csvstorage/#1/col1", 0); exit_if_fail (key, "key not found"); succeed_if (strcmp (keyString (key), "l1c1") == 0, "wrong key"); key = ksLookupByName (ks, "user/tests/csvstorage/#2/col2", 0); exit_if_fail (key, "key not found"); succeed_if (strcmp (keyString (key), "l2c2") == 0, "wrong key"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
void test_namedMatchFlags (void) { Key * parentKey = keyNew ("user/tests/glob", KEY_END); KeySet * conf = ksNew (20, keyNew ("user/glob/#1", KEY_VALUE, "user/tests/glob/*", KEY_META, "testmetakey1", "testvalue1", KEY_END), /* explicitly request pathname matching */ keyNew ("user/glob/#1/flags", KEY_VALUE, "pathname", KEY_END), KS_END); PLUGIN_OPEN ("glob"); KeySet * ks = createKeys (); succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful"); succeed_if (output_error (parentKey), "error in kdbSet"); succeed_if (output_warnings (parentKey), "warnings in kdbSet"); Key * key = ksLookupByName (ks, "user/tests/glob/test1", 0); exit_if_fail (key, "key user/tests/glob/test1 not found"); const Key * metaKey1 = keyGetMeta (key, "testmetakey1"); exit_if_fail (metaKey1, "testmetakey1 not found"); key = ksLookupByName (ks, "user/tests/glob/test3", 0); exit_if_fail (key, "user/tests/glob/test3 not found"); const Key * metaKey2 = keyGetMeta (key, "testmetakey1"); exit_if_fail (metaKey2, "testmetakey1 not found"); key = ksLookupByName (ks, "user/tests/glob/test2/subtest1", 0); exit_if_fail (key, "user/tests/glob/test2/subtest1 not found"); const Key * metaKey3 = keyGetMeta (key, "testmetakey1"); exit_if_fail (!metaKey3, "testmetakey1 was copied to subtest1, but subtest1 should not be matched with pathname flag"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_simple() { printf ("Test simple mount\n"); KDB *kdb = kdb_new(); Key *errorKey = keyNew(0); KeySet *modules = modules_config(); Key *mp = keyNew("user/tests/simple", KEY_VALUE, "simple", KEY_END); succeed_if (elektraMountOpen(kdb, simple_config(), modules, errorKey) == 0, "could not open trie"); succeed_if (kdb->split->size == 1, "size of split not correct"); compare_key(mp, kdb->split->parents[0]); succeed_if(output_warnings (errorKey), "warnings found"); succeed_if(output_error (errorKey), "error found"); exit_if_fail (kdb->trie, "kdb->trie was not build up successfully"); Key *searchKey = keyNew("user", KEY_END); Backend *backend = elektraTrieLookup(kdb->trie, searchKey); succeed_if (!backend, "there should be no backend"); keySetName(searchKey, "user/tests/simple"); backend = elektraTrieLookup(kdb->trie, searchKey); succeed_if (backend, "there should be a backend"); compare_key(backend->mountpoint, mp); keySetName(searchKey, "user/tests/simple/below"); Backend *b2 = elektraTrieLookup(kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key(b2->mountpoint, mp); keySetName(searchKey, "user/tests/simple/deep/below"); b2 = elektraTrieLookup(kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key(b2->mountpoint, mp); keyDel (errorKey); ksDel (modules); keyDel (mp); keyDel (searchKey); kdb_del (kdb); }
static void _cursor_window_destroy_row_chunk(CursorWindow* thiz,CursorWindowRowChunk* chunk,INT32 col_num) { INT32 row=0; INT32 col=0; CursorWindowFieldSlot* field =NULL; for (;row<chunk->row_num;row++) { for (col=0;col<col_num;col++) { field=_cursor_window_locate_field_in_chunk(thiz,chunk,row,col); exit_if_fail(field); _cursor_window_destroy_field(field); } } TG_FREE(chunk); }
void test_onlyFirstMatchIsApplied (void) { Key * parentKey = keyNew ("user/tests/glob", KEY_END); // clang-format off KeySet * conf = ksNew (20, keyNew ("user/glob/#1", KEY_VALUE, "user/tests/glob/test1*", KEY_META, "testmetakey1", "testvalue1", KEY_END), keyNew ("user/glob/#2", KEY_VALUE, "user/tests/glob/*", KEY_META, "testmetakey2", "testvalue2", KEY_END), /* disable all flags */ keyNew ("user/glob/#1/flags", KEY_VALUE, "", KEY_END), keyNew ("user/glob/#2/flags", KEY_VALUE, "", KEY_END), KS_END); // clang-format on PLUGIN_OPEN ("glob"); KeySet * ks = createKeys (); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful"); succeed_if (output_error (parentKey), "error in kdbSet"); succeed_if (output_warnings (parentKey), "warnings in kdbSet"); Key * key = ksLookupByName (ks, "user/tests/glob/test1", 0); exit_if_fail (key, "key user/tests/glob/test1 not found"); const Key * firstMatchKey = keyGetMeta (key, "testmetakey1"); exit_if_fail (firstMatchKey, "testmetakey1 not found"); const Key * secondMatchKey = keyGetMeta (key, "testmetakey2"); exit_if_fail (!secondMatchKey, "testmetakey2 was applied to testmetakey1 although another match was already applied") key = ksLookupByName (ks, "user/tests/glob/test2/subtest1", 0); exit_if_fail (key, "user/tests/glob/test2/subtest1 not found"); exit_if_fail (keyGetMeta (key, "testmetakey2"), "testmetakey2 not found"); key = ksLookupByName (ks, "user/tests/glob/test3", 0); exit_if_fail (key, "user/tests/glob/test3 not found"); exit_if_fail (keyGetMeta (key, "testmetakey2"), "testmetakey2 not found"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_emptyvalues() { printf ("Test empty values in trie\n"); Trie *trie = 0; trie = test_insert (trie, "user/umlauts/b/", "b"); trie = test_insert (trie, "user/umlauts/a/", "a"); trie = test_insert (trie, "user/umlauts/", "/"); trie = test_insert (trie, "user/umlauts/c/", "c"); trie = test_insert (trie, "user/", "u"); exit_if_fail (trie, "trie was not build up successfully"); // output_trie(trie); elektraTrieClose(trie, 0); }
void test_kdbGetSet() { cout << "testing kdbSet() and kdbGet()" << endl; { KeySet ks_set (5, *Key ("user/tests/key3", KEY_DIR, KEY_END), *Key ("user/tests/key3/1", KEY_END), *Key ("user/tests/key3/2", KEY_END), *Key ("user/tests/key3/3", KEY_VALUE, "value", KEY_END), KS_END); KeySet ks; KDB kdb; kdb.get (ks, "user/tests/key3"); ks.append(ks_set); kdb.set (ks, "user/tests/key3"); } // check if they were written { KDB kdb; KeySet ks; kdb.get (ks, "user/tests/key3"); exit_if_fail(ks.lookup("user/tests/key3/3"), "could not find previously written key"); succeed_if(ks.lookup("user/tests/key3/3").get<std::string>() == "value", "could not get value"); succeed_if(!ks.lookup("user/tests/key3/3").needSync(), "should not need sync"); } // now remove keys (cleanup) { KeySet ks; KDB kdb; kdb.get (ks, "user/tests/key3"); ks.cut(Key("user/tests/key3", KEY_END)); kdb.set (ks, "user/tests/key3"); } // check if its gone now { KDB kdb; KeySet ks; kdb.get (ks, "user/tests/key3"); succeed_if(!ks.lookup("user/tests/key3/3"), "key was not removed"); } }
/** * @brief Compare two files line by line * * @param filename first file * @param genfilename file to compare with * * @retval 0 on errors (succeed_if already executed) * @retval 1 on success */ int compare_line_files (const char *filename, const char *genfilename) { FILE *forg, *fgen; char bufferorg [BUFFER_LENGTH + 1]; char buffergen [BUFFER_LENGTH + 1]; char *org = 0; char *gen = 0; int line = 0; forg = fopen (filename, "r"); fgen = fopen (genfilename, "r"); strncpy(bufferorg, "could not open file, orig: ", BUFFER_LENGTH); strncat(bufferorg, filename, BUFFER_LENGTH); strncat(bufferorg, " gen: ", BUFFER_LENGTH); strncat(bufferorg, genfilename, BUFFER_LENGTH); exit_if_fail (forg && fgen, bufferorg); while ( (org = fgets (bufferorg, BUFFER_LENGTH, forg)) && (gen = fgets (buffergen, BUFFER_LENGTH, fgen))) { line ++; if (strncmp (bufferorg, buffergen, BUFFER_LENGTH)) { printf ("Compare <%s>, with <%s>\n", bufferorg, buffergen); printf ("in file %s, line %d.\n", filename, line); succeed_if (0, "comparing lines failed"); goto error; } } if ( org || fgets (buffergen, BUFFER_LENGTH, fgen)) { printf ("The files do not have same number of lines (%d): %s.\n", line, filename); succeed_if (0, "comparing files failed"); goto error; } fclose (forg); fclose (fgen); return 1; error: fclose (forg); fclose (fgen); return 0; }
static void test_sectionRead (char * fileName) { Key * parentKey = keyNew ("user/tests/ini-section-read", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("ini"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if (output_error (parentKey), "error in kdbGet"); succeed_if (output_warnings (parentKey), "warnings in kdbGet"); Key * key = ksLookupByName (ks, "user/tests/ini-section-read/akey/looking/like/sections", KDB_O_NONE); exit_if_fail (key, "section like key not found not found"); succeed_if (!strcmp ("value", keyString (key)), "section like key contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-section-read/emptysection", KDB_O_NONE); exit_if_fail (key, "empty section key not found"); succeed_if (keyIsBinary (key), "empty section key is not a binary key"); succeed_if (!keyValue (key), "section key contains non null data"); key = ksLookupByName (ks, "user/tests/ini-section-read/section1", KDB_O_NONE); exit_if_fail (key, "section1 key not found"); succeed_if (keyIsBinary (key), "section1 key is not a binary key"); succeed_if (!keyValue (key), "section1 contains non null data"); key = ksLookupByName (ks, "user/tests/ini-section-read/section1/key1", KDB_O_NONE); exit_if_fail (key, "key1 not found not found"); succeed_if (!strcmp ("value1", keyString (key)), "key1 contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-section-read/section1/key/with/subkey", KDB_O_NONE); exit_if_fail (key, "key with subkey not found not found"); succeed_if (!strcmp ("value2", keyString (key)), "key with subkey contained invalid data"); key = ksLookupByName (ks, "user/tests/ini-section-read/section2/with/subkey", KDB_O_NONE); exit_if_fail (key, "section2 key not found"); succeed_if (keyIsBinary (key), "section2 key is not a binary key"); succeed_if (!keyValue (key), "section2 contains non null data"); key = ksLookupByName (ks, "user/tests/ini-section-read/section2/with/subkey/key2", KDB_O_NONE); exit_if_fail (key, "key2 not found not found"); succeed_if (!strcmp ("value2", keyString (key)), "key2 contained invalid data"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }