// 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); }
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); }
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); }
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); }
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 int closeStoragePlugin (const size_t storagePlugin) { elektraPluginClose (plugins[storagePlugin], 0); elektraModulesClose (modules[storagePlugin], 0); ksDel (modules[storagePlugin]); return 0; }
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_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); }
/** * Closes the session with the Key database. * * @pre The handle must be a valid handle as returned from kdbOpen() * * @pre errorKey must be a valid key, e.g. created with keyNew() * * This is the counterpart of kdbOpen(). * * You must call this method when you finished your affairs with the key * database. You can manipulate Key and KeySet objects also after * kdbClose(), but you must not use any kdb*() call afterwards. * * The @p handle parameter will be finalized and all resources associated to it * will be freed. After a kdbClose(), the @p handle cannot be used anymore. * * @param handle contains internal information of * @link kdbOpen() opened @endlink key database * @param errorKey the key which holds error/warning information * @retval 0 on success * @retval -1 on NULL pointer * @ingroup kdb */ int kdbClose (KDB * handle, Key * errorKey) { if (!handle) { return -1; } Key * initialParent = keyDup (errorKey); int errnosave = errno; splitDel (handle->split); trieClose (handle->trie, errorKey); backendClose (handle->defaultBackend, errorKey); handle->defaultBackend = 0; // not set in fallback mode, so lets check: if (handle->initBackend) { backendClose (handle->initBackend, errorKey); handle->initBackend = 0; } for (int i = 0; i < NR_GLOBAL_POSITIONS; ++i) { for (int j = 0; j < NR_GLOBAL_SUBPOSITIONS; ++j) { elektraPluginClose (handle->globalPlugins[i][j], errorKey); } } if (handle->modules) { elektraModulesClose (handle->modules, errorKey); ksDel (handle->modules); } else { ELEKTRA_ADD_WARNING (47, errorKey, "modules were not open"); } elektraFree (handle); keySetName (errorKey, keyName (initialParent)); keySetString (errorKey, keyString (initialParent)); keyDel (initialParent); errno = errnosave; 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); }
/** * Opens a default backend using the plugin named KDB_DEFAULT_RESOLVER * and KDB_DEFAULT_STORAGE. * * @param modules the modules to work with * @param errorKey the key to issue warnings and errors to * @return the fresh allocated default backend or 0 if it failed */ Backend* elektraBackendOpenDefault(KeySet *modules, Key *errorKey) { Backend *backend = elektraBackendAllocate(); KeySet *resolverConfig = ksNew(5, keyNew("system/path", KEY_VALUE, KDB_DB_FILE, KEY_END), KS_END); Plugin *resolver = elektraPluginOpen(KDB_DEFAULT_RESOLVER, modules, resolverConfig, errorKey); if (!resolver) { elektraFree(backend); /* error already set in elektraPluginOpen */ return 0; } backend->getplugins[RESOLVER_PLUGIN] = resolver; backend->setplugins[RESOLVER_PLUGIN] = resolver; backend->setplugins[COMMIT_PLUGIN] = resolver; backend->errorplugins[STORAGE_PLUGIN] = resolver; resolver->refcounter = 4; KeySet *storageConfig = ksNew(5, KS_END); Plugin *storage = elektraPluginOpen(KDB_DEFAULT_STORAGE, modules, storageConfig, errorKey); if (!storage) { elektraPluginClose(resolver, errorKey); elektraFree(backend); /* error already set in elektraPluginOpen */ return 0; } backend->getplugins[STORAGE_PLUGIN] = storage; backend->setplugins[STORAGE_PLUGIN] = storage; storage->refcounter = 2; Key *mp = keyNew ("", KEY_VALUE, "default", KEY_END); backend->mountpoint = mp; keyIncRef(backend->mountpoint); return backend; }
/** * @internal * Unload plugin by plugin handle. * * @param plugin Plugin handle * @retval 0 on error * @retval 1 on success */ static int unloadPlugin (Plugin * plugin) { ELEKTRA_NOT_NULL (plugin); Key * errorKey = keyNew (0); int result = elektraPluginClose (plugin, errorKey); int hasError = keyGetMeta (errorKey, "error") != NULL; keyDel (errorKey); if (!result || hasError) { ELEKTRA_LOG_WARNING ("elektraPluginClose failed: result=%d", result); return 0; } else { return 1; } }
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); }
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 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); } }
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); }
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); }
/** * Opens a plugin. * * The config will be used as is. So be sure to transfer ownership * of the config to it, with e.g. ksDup(). * elektraPluginClose() will delete the config. * * @return a pointer to a new created plugin or 0 on error */ Plugin * elektraPluginOpen (const char * name, KeySet * modules, KeySet * config, Key * errorKey) { Plugin * handle = 0; const char * n; elektraPluginFactory pluginFactory = 0; if (!name || name[0] == '\0') { ELEKTRA_ADD_WARNING (39, errorKey, "name is null or empty"); goto err_clup; } n = name; while (*n != '\0') { if (*n == '/') ++n; else break; } if (*n == '\0') { ELEKTRA_ADD_WARNING (39, errorKey, "name contained slashes only"); goto err_clup; } pluginFactory = elektraModulesLoad (modules, name, errorKey); if (pluginFactory == 0) { /* warning already set by elektraModulesLoad */ goto err_clup; } handle = pluginFactory (); if (handle == 0) { ELEKTRA_ADD_WARNING (6, errorKey, name); goto err_clup; } /* init reference counting */ handle->refcounter = 1; handle->config = config; config = 0; // for err_clup case /* let the plugin initialize itself */ if (handle->kdbOpen) { if ((handle->kdbOpen (handle, errorKey)) == -1) { ELEKTRA_ADD_WARNING (11, errorKey, name); elektraPluginClose (handle, errorKey); goto err_clup; } } #if DEBUG && VERBOSE printf ("Finished loading plugin %s\n", name); #endif return handle; err_clup: #if DEBUG printf ("Failed to load plugin %s\n", name); #endif ksDel (config); return 0; }
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); }
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; }
/** * Opens a default backend using the plugin named KDB_DEFAULT_RESOLVER * and KDB_DEFAULT_STORAGE. * * @param modules the modules to work with * @param errorKey the key to issue warnings and errors to * @return the fresh allocated default backend or 0 if it failed */ Backend* elektraBackendOpenDefault(KeySet *modules, const char * file, Key *errorKey) { Backend *backend = elektraBackendAllocate(); KeySet *resolverConfig = ksNew(5, keyNew("system/path", KEY_VALUE, file, KEY_END), KS_END); elektraKeySetName(errorKey, "", KEY_CASCADING_NAME | KEY_EMPTY_NAME); Plugin *resolver = elektraPluginOpen(KDB_DEFAULT_RESOLVER, modules, resolverConfig, errorKey); if (!resolver) { elektraFree(backend); /* error already set in elektraPluginOpen */ return 0; } #if DEBUG && VERBOSE KeySet *tracerConfig = ksNew(5, // does not matter because it is mounted differently in system/elektra/modules: // keyNew("system/logmodule", KEY_VALUE, "1", KEY_END), KS_END); Plugin *tracer = elektraPluginOpen("tracer", modules, tracerConfig, errorKey); if (tracer) { backend->getplugins[RESOLVER_PLUGIN+1] = tracer; backend->setplugins[RESOLVER_PLUGIN+1] = tracer; backend->errorplugins[RESOLVER_PLUGIN+1] = tracer; tracer->refcounter = 3; } #endif backend->getplugins[RESOLVER_PLUGIN] = resolver; backend->setplugins[RESOLVER_PLUGIN] = resolver; backend->setplugins[COMMIT_PLUGIN] = resolver; backend->errorplugins[STORAGE_PLUGIN] = resolver; resolver->refcounter = 4; KeySet *storageConfig = ksNew(5, KS_END); Plugin *storage = elektraPluginOpen(KDB_DEFAULT_STORAGE, modules, storageConfig, errorKey); if (!storage) { elektraPluginClose(resolver, errorKey); elektraFree(backend); /* error already set in elektraPluginOpen */ return 0; } backend->getplugins[STORAGE_PLUGIN] = storage; backend->setplugins[STORAGE_PLUGIN] = storage; storage->refcounter = 2; Key *mp = keyNew ("", KEY_VALUE, "default", KEY_END); backend->mountpoint = mp; keyIncRef(backend->mountpoint); return backend; }
int elektraNotificationOpen (KDB * kdb) { // Make sure kdb is not null if (!kdb) { ELEKTRA_LOG_WARNING ("kdb was not set"); return 0; } // Allow open only once if (kdb->notificationPlugin) { ELEKTRA_LOG_WARNING ("elektraNotificationOpen already called for kdb"); return 0; } Plugin * notificationPlugin = loadPlugin (kdb, "internalnotification", NULL); if (!notificationPlugin) { return 0; } int mountResult = mountGlobalPlugin (kdb, notificationPlugin); if (!mountResult) { Key * errorKey = keyNew (0); elektraPluginClose (notificationPlugin, errorKey); keyDel (errorKey); return 0; } // Create context for notification callback ElektraNotificationCallbackContext * context = elektraMalloc (sizeof (*context)); if (context == NULL) { unmountGlobalPlugin (kdb, notificationPlugin); Key * errorKey = keyNew (0); elektraPluginClose (notificationPlugin, errorKey); keyDel (errorKey); return 0; } context->kdb = kdb; context->kdbUpdate = &elektraNotificationKdbUpdate; context->notificationPlugin = notificationPlugin; // Get notification callback from notification plugin size_t func = elektraPluginGetFunction (notificationPlugin, "notificationCallback"); if (!func) { unmountGlobalPlugin (kdb, notificationPlugin); Key * errorKey = keyNew (0); elektraPluginClose (notificationPlugin, errorKey); keyDel (errorKey); return 0; } ElektraNotificationCallback notificationCallback = (ElektraNotificationCallback) func; // Open notification for plugins pluginsOpenNotification (kdb, notificationCallback, context); kdb->notificationPlugin = notificationPlugin; kdb->notificationCallbackContext = context; return 1; }