// 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_file_faulty_signature (void) { Plugin * plugin = NULL; Key * parentKey = keyNew ("system", KEY_END); KeySet * modules = ksNew (0, KS_END); KeySet * config = newPluginConfigurationWithTextmodeEnabled (); elektraModulesInit (modules, 0); plugin = elektraPluginOpen (PLUGIN_NAME, modules, config, 0); succeed_if (plugin, "failed to open plugin handle"); if (plugin) { KeySet * data = ksNew (0, KS_END); const char * tmpFile = elektraFilename (); if (tmpFile) { // prepare test file to be encrypted writeFaultySignatureFile (tmpFile); keySetString (parentKey, tmpFile); // try to decrypt/verify the file -- should fail succeed_if (plugin->kdbGet (plugin, data, parentKey) == -1, "kdb get succeeded on a faulty signature"); remove (tmpFile); } ksDel (data); elektraPluginClose (plugin, 0); } elektraModulesClose (modules, 0); ksDel (modules); keyDel (parentKey); }
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); }
static void test_init (void) { Plugin * plugin = NULL; Key * parentKey = keyNew ("system", KEY_END); KeySet * modules = ksNew (0, KS_END); KeySet * configKs = newPluginConfiguration (); elektraModulesInit (modules, 0); plugin = elektraPluginOpen (PLUGIN_NAME, modules, configKs, 0); succeed_if (plugin != 0, "failed to open the plugin"); if (plugin) { succeed_if (!strcmp (plugin->name, PLUGIN_NAME), "got wrong name"); KeySet * config = elektraPluginGetConfig (plugin); succeed_if (config != 0, "there should be a config"); succeed_if (plugin->kdbOpen != 0, "no open pointer"); succeed_if (plugin->kdbClose != 0, "no close pointer"); succeed_if (plugin->kdbGet != 0, "no get pointer"); succeed_if (plugin->kdbSet != 0, "no set pointer"); elektraPluginClose (plugin, 0); } elektraModulesClose (modules, 0); ksDel (modules); keyDel (parentKey); }
int main (int argc, char ** argv) { printf ("YAJL TESTS\n"); printf ("==================\n\n"); modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); init (argc, argv); test_nextNotBelow (); test_reverseLevel (); test_countLevel (); test_writing (); test_json ("yajl/testdata_null.json", getNullKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_boolean.json", getBooleanKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_number.json", getNumberKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_string.json", getStringKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_maps.json", getMapKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_array.json", getArrayKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_below.json", getBelowKeys (), ksNew (0, KS_END)); test_json ("yajl/OpenICC_device_config_DB.json", getOpenICCKeys (), ksNew (0, KS_END)); // TODO currently do not have a KeySet, wait for C-plugin to make // it easy to generate it.. test_readWrite ("yajl/empty_object.json", ksNew (0, KS_END)); test_readWrite ("yajl/empty_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/top_level_string.json", ksNew (0, KS_END)); test_readWrite ("yajl/top_level_integer.json", ksNew (0, KS_END)); test_readWrite ("yajl/rfc_object.json", ksNew (0, KS_END)); test_readWrite ("yajl/rfc_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_mixed.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_in_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_in_array_anon_map.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_nested.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_broken.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_special_ending.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_outside.json", ksNew (0, KS_END)); test_readWrite ("yajl/keyframes_complex.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_mixed2.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_special_start.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_mixed3.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_map.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_array1.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_map2.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_map1.json", ksNew (0, KS_END)); elektraModulesClose (modules, 0); ksDel (modules); print_result ("test_yajl"); return nbError; }
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); }
static void test_us () { printf ("Test mounting of user and system backends\n"); KDB * kdb = kdb_new (); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); Key * mp; KeySet * config = set_us (); ksAppendKey (config, keyNew ("system/elektra/mountpoints", KEY_END)); succeed_if (mountOpen (kdb, config, modules, 0) == 0, "could not open mount"); succeed_if (mountDefault (kdb, modules, 1, 0) == 0, "could not mount default backend"); succeed_if (kdb->split->size == 5, "size of split not correct"); mp = keyNew ("system", KEY_VALUE, "system", KEY_END); compare_key (mp, kdb->split->parents[0]); keySetName (mp, "user"); keySetString (mp, "user"); compare_key (mp, kdb->split->parents[1]); keySetName (mp, "system/elektra"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[4]); keyDel (mp); Key * key = keyNew ("user/anywhere/backend/simple", KEY_END); Backend * backend = trieLookup (kdb->trie, key); keyAddBaseName (key, "somewhere"); keyAddBaseName (key, "deep"); keyAddBaseName (key, "below"); Backend * backend2 = trieLookup (kdb->trie, key); succeed_if (backend == backend2, "should be same backend"); succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName (mp), "user"); succeed_if_same_string (keyString (mp), "user"); keySetName (key, "system/anywhere/tests/backend/two"); Backend * two = trieLookup (kdb->trie, key); succeed_if (two != backend, "should be differnt backend"); succeed_if ((mp = two->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName (mp), "system"); succeed_if_same_string (keyString (mp), "system"); keyDel (key); kdb_del (kdb); 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); }
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_file_crypto_operations (void) { Plugin * plugin = NULL; Key * parentKey = keyNew ("system", KEY_END); KeySet * modules = ksNew (0, KS_END); KeySet * config = newPluginConfiguration (); elektraModulesInit (modules, 0); plugin = elektraPluginOpen (PLUGIN_NAME, modules, config, 0); succeed_if (plugin, "failed to open plugin handle"); if (plugin) { KeySet * data = ksNew (0, KS_END); const char * tmpFile = elektraFilename (); if (tmpFile) { // prepare test file to be encrypted writeTestFile (tmpFile); keySetString (parentKey, tmpFile); // try to encrypt the file succeed_if (plugin->kdbSet (plugin, data, parentKey) == 1, "kdb set failed"); succeed_if (isTestFileCorrect (tmpFile) == -1, "file content did not change during encryption"); // try to decrypt the file again (simulating the pregetstorage call) succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get (pregetstorage) failed"); succeed_if (isTestFileCorrect (keyString (parentKey)) == 1, "file content could not be restored during decryption"); // a second call to kdb get (the postgetstorage call) should re-encrypt the file again succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get (postgetstorage) failed"); succeed_if (isTestFileCorrect (tmpFile) == -1, "postgetstorage did not encrypt the file again"); remove (tmpFile); } ksDel (data); elektraPluginClose (plugin, 0); } elektraModulesClose (modules, 0); ksDel (modules); keyDel (parentKey); }
void test_tempname() { printf ("Resolve Tempname\n"); KeySet *modules = ksNew(0); elektraModulesInit (modules, 0); 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 (!strcmp(plugin->name, "resolver"), "got wrong name"); resolverHandles *h = elektraPluginGetData(plugin); succeed_if (h != 0, "no plugin handle"); Key *parentKey= keyNew("system", KEY_END); plugin->kdbGet(plugin, 0, parentKey); succeed_if (!strncmp(h->system.tempfile, KDB_DB_SYSTEM "/elektra.ecf", sizeof(KDB_DB_SYSTEM)), "resulting filename not correct"); keySetName(parentKey, "user"); plugin->kdbGet(plugin, 0, parentKey); succeed_if (!strncmp(h->user.tempfile, KDB_DB_HOME "/" KDB_DB_USER "/elektra.ecf.tmp", sizeof(KDB_DB_HOME "/" KDB_DB_USER)), "resulting filename not correct"); keyDel (parentKey); elektraPluginClose(plugin, 0); elektraModulesClose(modules, 0); ksDel (modules); }
// test script with syntax error static void test_wrong (void) { printf ("Testing lua script with syntax error...\n"); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin_wrong.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), "we expect some warnings"); succeed_if (!output_error (errorKey), "we expect some errors"); succeed_if (plugin == NULL, "lua plugin shouldn't be loadable"); keyDel (errorKey); elektraModulesClose (modules, 0); ksDel (modules); }
static void test_file_signature_operations (void) { Plugin * plugin = NULL; Key * parentKey = keyNew ("system", KEY_END); KeySet * modules = ksNew (0, KS_END); KeySet * config = newPluginConfiguration (); elektraModulesInit (modules, 0); plugin = elektraPluginOpen (PLUGIN_NAME, modules, config, 0); succeed_if (plugin, "failed to open plugin handle"); if (plugin) { KeySet * data = ksNew (0, KS_END); const char * tmpFile = elektraFilename (); if (tmpFile) { // prepare test file to be encrypted writeTestFile (tmpFile); keySetString (parentKey, tmpFile); // try to encrypt the file succeed_if (plugin->kdbSet (plugin, data, parentKey) == 1, "kdb set failed"); succeed_if (isTestFileCorrect (tmpFile) == -1, "file content did not change during encryption"); // try to decrypt/verify the file succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get failed"); remove (tmpFile); } ksDel (data); elektraPluginClose (plugin, 0); } elektraModulesClose (modules, 0); ksDel (modules); keyDel (parentKey); }
static void test_BlockresolverWrite (char * fileName, char * compareName) { FILE * fin = fopen (srcdir_file (fileName), "r"); char buffer[1024]; const char * foutname = elektraFilename (); FILE * fout = fopen (foutname, "w"); while (fgets (buffer, sizeof (buffer), fin)) { fputs (buffer, fout); } fclose (fin); fclose (fout); Key * parentKey = keyNew ("system/test/blockresolver-write", KEY_VALUE, foutname, KEY_END); KeySet * conf = ksNew (10, keyNew ("system/path", KEY_VALUE, foutname, KEY_END), keyNew ("system/identifier", KEY_VALUE, "### block config", KEY_END), KS_END); KeySet * modules = ksNew (0, KS_END); KeySet * ks = ksNew (0, KS_END); elektraModulesInit (modules, 0); Plugin * resolver = elektraPluginOpen ("blockresolver", modules, ksDup (conf), 0); succeed_if (resolver->kdbGet (resolver, ks, parentKey) >= 0, "blockresolver->kdbGet failed"); Plugin * storage = elektraPluginOpen ("ini", modules, ksNew (0, KS_END), 0); succeed_if (storage->kdbGet (storage, ks, parentKey) >= 0, "storage->kdbGet failed"); keySetString (ksLookupByName (ks, "system/test/blockresolver-write/section/key", 0), "only the inside has changed"); succeed_if (storage->kdbSet (storage, ks, parentKey) >= 0, "storage->kdbSet failed"); succeed_if (resolver->kdbSet (resolver, ks, parentKey) >= 0, "blockresolver->kdbSet failed"); succeed_if (resolver->kdbSet (resolver, ks, parentKey) >= 0, "blockresolver->kdbSet failed"); succeed_if (compare_line_files (srcdir_file (compareName), foutname), "files do not match as expected"); elektraPluginClose (storage, 0); elektraPluginClose (resolver, 0); ksDel (conf); ksDel (ks); elektraModulesClose (modules, 0); ksDel (modules); keyDel (parentKey); }
static void test_simple () { printf ("Test plugin\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); elektraModulesClose (modules, 0); ksDel (modules); }
static void check_xdg () { 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"); int abort = 0; if (strchr (plugin->name, 'x') != NULL) { printf ("Will abort successfully because default resolver is an XDG resolver (%s)\n", plugin->name); abort = 1; } elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); if (abort) { exit (0); } }
void test_name () { printf ("Resolve Name\n"); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); 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); succeed_if (h != 0, "no plugin handle"); Key * parentKey = keyNew ("system", KEY_END); plugin->kdbGet (plugin, 0, parentKey); succeed_if_same_string (keyString (parentKey), KDB_DB_SYSTEM "/elektra.ecf"); keyDel (parentKey); elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); }
static int openStoragePlugin (const size_t storagePlugin) { modules[storagePlugin] = ksNew (0, KS_END); elektraModulesInit (modules[storagePlugin], 0); KeySet * conf = ksNew (0, KS_END); Key * errorKey = keyNew ("", KEY_END); Plugin * plugin = elektraPluginOpen (pluginNames[storagePlugin], modules[storagePlugin], conf, errorKey); const Key * metaWarnings = keyGetMeta (errorKey, "warnings"); if (metaWarnings) printf ("There are warnings for plugin: %s\n", pluginNames[storagePlugin]); const Key * metaError = keyGetMeta (errorKey, "error"); if (metaError) printf ("There are errors for plugin: %s\n", pluginNames[storagePlugin]); if (plugin == 0) { printf ("Could not open plugin: %s\n", pluginNames[storagePlugin]); return -1; } plugins[storagePlugin] = plugin; keyDel (errorKey); return 0; }
static void test_BlockresolverRead (char * fileName) { Key * parentKey = keyNew ("system/test/blockresolver-read", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (10, keyNew ("system/path", KEY_VALUE, srcdir_file (fileName), KEY_END), keyNew ("system/identifier", KEY_VALUE, "### block config", KEY_END), KS_END); KeySet * modules = ksNew (0, KS_END); KeySet * ks = ksNew (0, KS_END); elektraModulesInit (modules, 0); Plugin * resolver = elektraPluginOpen ("blockresolver", modules, ksDup (conf), 0); succeed_if (resolver->kdbGet (resolver, ks, parentKey) >= 0, "blockresolver->kdbGet failed"); output_warnings (parentKey); output_error (parentKey); Plugin * storage = elektraPluginOpen ("ini", modules, ksNew (0, KS_END), 0); succeed_if (storage->kdbGet (storage, ks, parentKey) >= 0, "storage->kdbGet failed"); succeed_if (!strcmp (keyString (ksLookupByName (ks, "system/test/blockresolver-read/section/key", 0)), "inside block"), "blockresolver failed to resolve requested block"); elektraPluginClose (storage, 0); elektraPluginClose (resolver, 0); ksDel (conf); ksDel (ks); elektraModulesClose (modules, 0); ksDel (modules); keyDel (parentKey); }
/** * @brief Opens the session with the Key database. * * @pre errorKey must be a valid key, e.g. created with keyNew() * * The method will bootstrap itself the following way. * The first step is to open the default backend. With it * system/elektra/mountpoints will be loaded and all needed * libraries and mountpoints will be determined. * These libraries for backends will be loaded and with it the * @p KDB data structure will be initialized. * * You must always call this method before retrieving or committing any * keys to the database. In the end of the program, * after using the key database, you must not forget to kdbClose(). * * The pointer to the @p KDB structure returned will be initialized * like described above, and it must be passed along on any kdb*() * method your application calls. * * Get a @p KDB handle for every thread using elektra. Don't share the * handle across threads, and also not the pointer accessing it: * * @snippet kdbopen.c open * * You don't need kdbOpen() if you only want to * manipulate plain in-memory Key or KeySet objects. * * @pre errorKey must be a valid key, e.g. created with keyNew() * * @param errorKey the key which holds errors and warnings which were issued * @see kdbGet(), kdbClose() to end all affairs to the key database. * @retval handle on success * @retval NULL on failure * @ingroup kdb */ KDB * kdbOpen (Key * errorKey) { if (!errorKey) { ELEKTRA_LOG ("no error key passed"); return 0; } ELEKTRA_LOG ("called with %s", keyName (errorKey)); int errnosave = errno; KDB * handle = elektraCalloc (sizeof (struct _KDB)); Key * initialParent = keyDup (errorKey); handle->modules = ksNew (0, KS_END); if (elektraModulesInit (handle->modules, errorKey) == -1) { ksDel (handle->modules); elektraFree (handle); ELEKTRA_SET_ERROR (94, errorKey, "elektraModulesInit returned with -1"); keySetName (errorKey, keyName (initialParent)); keySetString (errorKey, keyString (initialParent)); keyDel (initialParent); errno = errnosave; return 0; } KeySet * keys = ksNew (0, KS_END); int inFallback = 0; switch (elektraOpenBootstrap (handle, keys, errorKey)) { case -1: ksDel (handle->modules); elektraFree (handle); ELEKTRA_SET_ERROR (40, errorKey, "could not open default backend"); keySetName (errorKey, keyName (initialParent)); keySetString (errorKey, keyString (initialParent)); keyDel (initialParent); errno = errnosave; return 0; case 0: ELEKTRA_ADD_WARNING (17, errorKey, "Initial kdbGet() failed, you should either fix " KDB_DB_INIT " or the fallback " KDB_DB_FILE); break; case 2: ELEKTRA_LOG ("entered fallback code for bootstrapping"); inFallback = 1; break; } keySetString (errorKey, "kdbOpen(): mountGlobals"); if (mountGlobals (handle, ksDup (keys), handle->modules, errorKey) == -1) { // mountGlobals also sets a warning containing the name of the plugin that failed to load ELEKTRA_ADD_WARNING (139, errorKey, "Mounting global plugins failed"); } keySetName (errorKey, keyName (initialParent)); keySetString (errorKey, "kdbOpen(): backendClose"); backendClose (handle->defaultBackend, errorKey); splitDel (handle->split); handle->defaultBackend = 0; handle->trie = 0; #ifdef HAVE_LOGGER if (inFallback) ELEKTRA_LOG_WARNING ("fallback for bootstrapping: you might want to run `kdb upgrade-bootstrap`"); Key * key; ksRewind (keys); for (key = ksNext (keys); key; key = ksNext (keys)) { ELEKTRA_LOG_DEBUG ("config for createTrie name: %s value: %s", keyName (key), keyString (key)); } #endif handle->split = splitNew (); keySetString (errorKey, "kdbOpen(): mountOpen"); // Open the trie, keys will be deleted within mountOpen if (mountOpen (handle, keys, handle->modules, errorKey) == -1) { ELEKTRA_ADD_WARNING (93, errorKey, "Initial loading of trie did not work"); } keySetString (errorKey, "kdbOpen(): mountDefault"); if (mountDefault (handle, handle->modules, inFallback, errorKey) == -1) { ELEKTRA_SET_ERROR (40, errorKey, "could not reopen and mount default backend"); keySetString (errorKey, "kdbOpen(): close"); kdbClose (handle, errorKey); keySetName (errorKey, keyName (initialParent)); keySetString (errorKey, keyString (initialParent)); keyDel (initialParent); errno = errnosave; return 0; } keySetString (errorKey, "kdbOpen(): mountVersion"); mountVersion (handle, errorKey); keySetString (errorKey, "kdbOpen(): mountModules"); if (mountModules (handle, handle->modules, errorKey) == -1) { ELEKTRA_ADD_WARNING (92, errorKey, "Mounting modules did not work"); } keySetName (errorKey, keyName (initialParent)); keySetString (errorKey, keyString (initialParent)); keyDel (initialParent); errno = errnosave; return handle; }
int main (int argc, char ** argv) { if (argc < 4 || argc > 5 || (argc == 5 && elektraStrCmp (argv[4], "get") != 0)) { fprintf (stderr, "Usage: %s <path> <parent> <plugin> [get]\n", argv[0]); return 1; } typedef enum { BOTH, GET, Default = BOTH } Direction; Direction direction; if (argc == 5) direction = GET; const char * path = argv[1]; const char * parent = argv[2]; const char * pluginname = argv[3]; KeySet * ks = ksNew (0, KS_END); char * infile = elektraFormat ("%s/test.%s.in", path, pluginname); char * outfile = elektraFormat ("%s/test.%s.out", path, pluginname); { Key * getKey = keyNew (parent, KEY_VALUE, infile, KEY_END); KeySet * conf = ksNew (0, KS_END); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); Key * errorKey = keyNew ("", KEY_END); Plugin * plugin = elektraPluginOpen (pluginname, modules, conf, errorKey); keyDel (errorKey); plugin->kdbGet (plugin, ks, getKey); keyDel (getKey); elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); } if (ksGetSize (ks) <= 0) { return 1; } if (direction == BOTH) { Key * setKey = keyNew (parent, KEY_VALUE, outfile, KEY_END); KeySet * conf = ksNew (0, KS_END); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); Key * errorKey = keyNew ("", KEY_END); Plugin * plugin = elektraPluginOpen (pluginname, modules, conf, errorKey); keyDel (errorKey); plugin->kdbSet (plugin, ks, setKey); keyDel (setKey); elektraPluginClose (plugin, 0); elektraModulesClose (modules, 0); ksDel (modules); } elektraFree (infile); elektraFree (outfile); ksDel (ks); return 0; }
static void test_simple() { printf ("Test simple building of backend\n"); KeySet *modules = ksNew(0, KS_END); elektraModulesInit(modules, 0); Key *errorKey = 0; Backend *backend = elektraBackendOpen(set_simple(), modules, errorKey); succeed_if (backend->errorplugins[0] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[2] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[3] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[4] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[5] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[6] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[7] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[8] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[9] == 0, "there should be no plugin"); exit_if_fail (backend->errorplugins[1] != 0, "there should be a plugin"); succeed_if (backend->getplugins[0] == 0, "there should be no plugin"); succeed_if (backend->getplugins[2] == 0, "there should be no plugin"); succeed_if (backend->getplugins[3] == 0, "there should be no plugin"); succeed_if (backend->getplugins[4] == 0, "there should be no plugin"); succeed_if (backend->getplugins[5] == 0, "there should be no plugin"); succeed_if (backend->getplugins[6] == 0, "there should be no plugin"); succeed_if (backend->getplugins[7] == 0, "there should be no plugin"); succeed_if (backend->getplugins[8] == 0, "there should be no plugin"); succeed_if (backend->getplugins[9] == 0, "there should be no plugin"); exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin"); succeed_if (backend->setplugins[0] == 0, "there should be no plugin"); succeed_if (backend->setplugins[2] == 0, "there should be no plugin"); succeed_if (backend->setplugins[3] == 0, "there should be no plugin"); succeed_if (backend->setplugins[4] == 0, "there should be no plugin"); succeed_if (backend->setplugins[5] == 0, "there should be no plugin"); succeed_if (backend->setplugins[6] == 0, "there should be no plugin"); succeed_if (backend->setplugins[7] == 0, "there should be no plugin"); succeed_if (backend->setplugins[8] == 0, "there should be no plugin"); succeed_if (backend->setplugins[9] == 0, "there should be no plugin"); exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin"); Key *mp; succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName(mp), "user/tests/backend/simple"); succeed_if_same_string (keyString(mp), "simple"); Plugin *plugin = backend->getplugins[1]; 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"); elektraBackendClose (backend, errorKey); elektraModulesClose (modules, 0); ksDel (modules); }
void test_resolve() { char *path; int pathLen; printf ("Resolve Filename\n"); KeySet *modules = ksNew(0); elektraModulesInit (modules, 0); 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 (!strcmp(plugin->name, "resolver"), "got wrong name"); resolverHandles *h = elektraPluginGetData(plugin); succeed_if (h != 0, "no plugin handle"); resolverClose(&h->system); resolverClose(&h->user); Key *forKey = keyNew("system", KEY_END); succeed_if (resolveFilename(forKey, &h->system, forKey) != -1, "could not resolve filename"); 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"); resolverClose(&h->system); keySetName(forKey, "user"); succeed_if (resolveFilename(forKey, &h->user, forKey) != -1, "could not resolve filename"); pathLen = tempHomeLen + 1 + strlen (KDB_DB_USER) + 12 + 1; path = malloc (pathLen); succeed_if (path != 0, "malloc failed"); snprintf (path, pathLen, "%s/%s/elektra.ecf", tempHome, KDB_DB_USER); succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp(h->user.filename, path), "filename not set correctly"); resolverClose(&h->user); #ifdef HAVE_SETENV unsetenv("USER"); unsetenv("HOME"); keySetName(forKey, "system"); succeed_if (resolveFilename(forKey, &h->system, forKey) != -1, "could not resolve filename"); 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"); resolverClose(&h->system); keySetName(forKey, "user"); succeed_if (resolveFilename(forKey, &h->user, forKey) != -1, "could not resolve filename"); succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp(h->user.filename, KDB_DB_HOME "/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly"); resolverClose(&h->user); setenv("USER","other",1); succeed_if (resolveFilename(forKey, &h->user, forKey) != -1, "could not resolve filename"); succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp(h->user.filename, KDB_DB_HOME "/other/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly"); resolverClose(&h->user); setenv("HOME","/nfshome//max//",1); succeed_if (resolveFilename(forKey, &h->user, forKey) != -1, "could not resolve filename"); succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp(h->user.filename, "/nfshome/max/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly"); resolverClose(&h->user); unsetenv("HOME"); unsetenv("USER"); #endif keySetName(forKey, "user"); succeed_if (resolveFilename(forKey, &h->user, forKey) != -1, "could not resolve filename"); succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly"); succeed_if (!strcmp(h->user.filename, KDB_DB_HOME "/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly"); resolverClose(&h->user); keyDel (forKey); elektraPluginClose(plugin, 0); elektraModulesClose(modules, 0); ksDel (modules); free (path); }