static void test_get (void) { char const * const fileName = "camel/simple.yaml"; printf ("• Parse file “%s”\n", fileName); char const * const prefix = "user/camel/tests/read"; Key * parentKey = keyNew (prefix, KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("camel"); KeySet * keySet = ksNew (0, KS_END); int status = plugin->kdbGet (plugin, keySet, parentKey); succeed_if (status == ELEKTRA_PLUGIN_STATUS_SUCCESS || status == ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "Unable to open or parse file"); succeed_if (output_error (parentKey), "Received unexpected error while reading the configuration"); char keyValues[][2][50] = { { "hello", "world" }, }; Key * key; char text[MAX_LENGTH_TEXT]; for (size_t pair = 0; pair < sizeof (keyValues) / sizeof (keyValues[0]); pair++) { char * name = keyValues[pair][0]; char * value = keyValues[pair][1]; snprintf (text, MAX_LENGTH_TEXT, "%s/%s", prefix, name); key = ksLookupByName (keySet, text, KDB_O_NONE); snprintf (text, MAX_LENGTH_TEXT, "Key “%s” not found", name); exit_if_fail (key, text); succeed_if_same_string (keyString (key), value); } keyDel (parentKey); ksDel (keySet); PLUGIN_CLOSE (); }
/** * Increases the size of split and appends a new empty keyset. * * Initializes the element with the given parameters * at size-1 to be used. * * Will automatically resize split if needed. * * @param split the split object to work with * @param backend the backend which should be appended * @param parentKey the parentKey which should be appended * @param syncbits the initial syncstate which should be appended * @ingroup split * @retval -1 if no split is found * @return the position of the new element: size-1 */ ssize_t elektraSplitAppend (Split * split, Backend * backend, Key * parentKey, int syncbits) { if (!split) { /* To make test cases work & valgrind clean */ keyDel (parentKey); return -1; } ++split->size; if (split->size > split->alloc) elektraSplitResize (split); // index of the new element const int n = split->size - 1; split->keysets[n] = ksNew (0, KS_END); split->handles[n] = backend; split->parents[n] = parentKey; split->syncbits[n] = syncbits; return n; }
static void test_commit (void) { printf ("test commit notification\n"); Key * parentKey = keyNew ("system/tests/foo", KEY_END); Key * toAdd = keyNew ("system/tests/foo/bar", KEY_END); KeySet * ks = ksNew (0, KS_END); KeySet * conf = ksNew (3, keyNew ("/endpoint", KEY_VALUE, TEST_ENDPOINT, KEY_END), keyNew ("/connectTimeout", KEY_VALUE, TESTCONFIG_CONNECT_TIMEOUT, KEY_END), keyNew ("/subscribeTimeout", KEY_VALUE, TESTCONFIG_SUBSCRIBE_TIMEOUT, KEY_END), KS_END); PLUGIN_OPEN ("zeromqsend"); // initial get to save current state plugin->kdbGet (plugin, ks, parentKey); // add key to keyset ksAppendKey (ks, toAdd); receiveTimeout = 0; receivedKeyName = NULL; receivedChangeType = NULL; pthread_t * thread = startNotificationReaderThread ("Commit"); plugin->kdbSet (plugin, ks, parentKey); pthread_join (*thread, NULL); succeed_if (receiveTimeout == 0, "receiving did time out"); succeed_if (!keyGetMeta (parentKey, "warnings"), "warning meta key was set"); succeed_if_same_string ("Commit", receivedChangeType); succeed_if_same_string (keyName (parentKey), receivedKeyName); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); elektraFree (receivedKeyName); elektraFree (receivedChangeType); elektraFree (thread); }
void testReadMultiLine (const char * fileName) { Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("file"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); const Key * key = ksLookupByName (ks, "user/tests/file", KDB_O_NONE); exit_if_fail (key, "key not found"); succeed_if (!strcmp ("\nthis\n\n\tis a\n multi line test-\nfile\n\n", keyString (key)), "read multiline data doesn't match expected string"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_openclose (void) { printf ("test open & close\n"); // TODO test with ASAN and with & without cascading key Key * key = keyNew ("system/sw/tests/testlib_notification", KEY_END); KDB * kdb = kdbOpen (key); exit_if_fail (kdb, "opening kdb failed"); succeed_if (!elektraNotificationClose (kdb), "could close notification system without open"); succeed_if (elektraNotificationOpen (kdb), "could not open notification system"); succeed_if (!elektraNotificationOpen (kdb), "could open notification system twice"); succeed_if (elektraNotificationClose (kdb), "could not close notification system"); succeed_if (elektraNotificationOpen (kdb), "could not re-open notification system"); // cleanup succeed_if (elektraNotificationClose (kdb), "could not close notification system"); succeed_if (kdbClose (kdb, key) == 0, "could not close kdb"); keyDel (key); }
void test_simpleAppendModes() { Key *parentKey = keyNew ("user/tests/keytometa", KEY_END); KeySet *conf = ksNew (0); PLUGIN_OPEN("keytometa"); KeySet *ks = createSimpleTestKeys (); 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"); /* converted keys must be removed from the result */ succeed_if (!ksLookupByName(ks, "user/convertkey1", 0), "convertkey1 was not converted"); succeed_if (!ksLookupByName(ks, "user/convertkey2", 0), "convertkey2 was not converted"); /* normalkey2 must contain meta information generated from convertkey1 (via next) */ Key *key = ksLookupByName(ks, "user/normalkey2", 0); succeed_if (key, "normalkey2 was removed"); const Key *metaKey1 = keyGetMeta(key, "testmeta"); succeed_if (metaKey1, "normalkey1 contained no metakey"); succeed_if (!strcmp (keyString(metaKey1), "testvalue1"), "metakey of normalkey1 contained incorrect data"); /* normalkey3 must contain meta information generated from convertkey2 (via previous) */ key = ksLookupByName (ks, "user/normalkey3", 0); succeed_if (key, "normalkey3 was removed"); const Key *metaKey2 = keyGetMeta(key, "testmeta"); succeed_if (metaKey2, "normalkey1 contained no metakey"); succeed_if (!strcmp (keyString(metaKey2), "testvalue2"), "metakey of normalkey2 contained incorrect data"); keyDel (parentKey); ksDel(ks); PLUGIN_CLOSE () ; }
static void test_basics (void) { printf ("test basics\n"); Key * parentKey = keyNew ("user/tests/internalnotification", KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("internalnotification"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbOpen (plugin, parentKey) == 1, "call to kdbOpen was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "call to kdbGet was not successful"); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful"); succeed_if (plugin->kdbClose (plugin, parentKey) == 1, "call to kdbClose was not successful"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
// simple return value test static void test_fail (void) { printf ("Testing return values from lua functions...\n"); KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, srcdir_file ("lua/lua_plugin_fail.lua"), KEY_END), keyNew ("user/print", KEY_END), KS_END); PLUGIN_OPEN ("lua"); Key * parentKey = keyNew ("user/tests/from_c", KEY_END); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == -1, "call to kdbGet didn't fail"); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == -1, "call to kdbSet didn't fail"); succeed_if (plugin->kdbError (plugin, ks, parentKey) == -1, "call to kdbError didn't fail"); succeed_if (output_warnings (parentKey), "warnings in kdbOpen"); succeed_if (output_error (parentKey), "errors in kdbOpen"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
void testRoundTrip (const char * fileName) { Key * parentKey = keyNew ("user/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("file"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); keySetString (parentKey, elektraFilename ()); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful"); succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_toUpper() { Key *parentKey = keyNew("user/tests/rename", KEY_END); KeySet *conf = ksNew(20, keyNew("system/toupper", KEY_VALUE, "0", KEY_END), KS_END); KeySet *ks = createSimpleTestKeys(); ksAppendKey(ks, parentKey); PLUGIN_OPEN("rename"); succeed_if(plugin->kdbGet(plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key *key = ksLookupByName(ks, "user/tests/rename/WILL/BE/STRIPPED/KEY1", KDB_O_NONE); succeed_if(key, "key1 was not correctly rename"); key = ksLookupByName(ks, "user/tests/rename/WILL/BE/STRIPPED/KEY2", KDB_O_NONE); succeed_if(key, "key2 was not correctly rename"); key = ksLookupByName(ks, "user/tests/rename/WILL/NOT/BE/STRIPPED/KEY4", KDB_O_NONE); succeed_if(key, "key4 was not correctly rename"); keyDel(parentKey); ksDel(ks); PLUGIN_CLOSE(); }
static void test_lookupCascading () { printf ("Test lookup cascading\n"); Key * specKey = keyNew ("/abc", KEY_CASCADING_NAME, KEY_META, "override/#0", "/something", KEY_END); Key * k = 0; KeySet * ks = ksNew (20, k = keyNew ("user/else", KEY_END), KS_END); succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == 0, "found wrong key"); keySetMeta (specKey, "fallback/#0", "/else"); succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k, "did not find fallback key"); keySetMeta (specKey, "fallback/#0", ""); succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == 0, "found wrong key"); keySetMeta (specKey, "override/#0", "/else"); succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k, "did not find override key"); keySetMeta (specKey, "override/#0", ""); succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == 0, "found wrong key"); elektraKeySetName (specKey, "/else", KEY_CASCADING_NAME); succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k, "did not find key itself"); keyDel (specKey); ksDel (ks); }
void test_commentIniRead(char *fileName) { Key *parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file(fileName), KEY_END); KeySet *conf = ksNew (0); PLUGIN_OPEN ("ini"); KeySet *ks = ksNew (0); 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, "comment"); exit_if_fail(noSectionComment, "nosectionkey contained no comment"); succeed_if (!strcmp ("nosection comment1\nnosection 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, "comment"); exit_if_fail(sectionComment, "nosectionkey contained no comment"); succeed_if (!strcmp ("section comment1\nsection comment2", keyString(sectionComment)), "section1 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, "comment"); exit_if_fail(keyComment, "key1 contained no comment"); succeed_if (!strcmp ("key comment1\nkey comment2", keyString(keyComment)), "key1 contained an invalid comment"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE () ; }
static void test_mmap_meta (const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("mmapstorage"); KeySet * ks = metaTestKeySet (); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); KeySet * returned = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, returned, parentKey) == 1, "kdbGet was not successful"); KeySet * expected = metaTestKeySet (); compare_keyset (expected, returned); ksDel (expected); ksDel (returned); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
int main() { KeySet *myConfig = ksNew(0, KS_END); Key *key = keyNew("system/test/myapp",KEY_END); KDB *handle = kdbOpen(key); kdbGet(handle, myConfig, key); keySetName(key, "user/test/myapp"); kdbGet(handle, myConfig, key); // check for errors in key keyDel(key); /* ksRewind(myConfig); while ((key = ksNext(myConfig))) { printf ("%s\n", keyName(key)); } */ key = ksLookupByName(myConfig,"/test/myapp/key", 0); // check if key is not 0 and work with it... if (key) { printf("%s\n", keyString(key)); } ksDel (myConfig); // delete the in-memory configuration // maybe you want kdbSet() myConfig here kdbClose(handle, 0); // no more affairs with the key database. return 0; }
int main (void) { KeySet * myConfig = ksNew (0, KS_END); Key * key = keyNew ("/sw/MyApp", KEY_CASCADING_NAME, KEY_END); KDB * handle = kdbOpen (key); if (!handle) printError (key); printWarnings (key); if (kdbGet (handle, myConfig, key) < 0) printError (key); printWarnings (key); keyDel (key); // lookup Key * result = ksLookupByName (myConfig, "/sw/MyApp/Tests/TestKey1", 0); if (!result) printf ("Key not found in KeySet\n"); else { // do something with the key const char * key_name = keyName (result); const char * key_value = keyString (result); const char * key_comment = keyString (keyGetMeta (result, "comment")); printf ("key: %s value: %s comment: %s\n", key_name, key_value, key_comment); } ksDel (myConfig); // delete the in-memory configuration // maybe you want kdbSet() myConfig here kdbClose (handle, 0); // no more affairs with the key database. }
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 validateArray (KeySet * ks, Key * arrayKey, Key * specKey) { Key * tmpArrayParent = keyDup (arrayKey); keySetBaseName (tmpArrayParent, 0); Key * arrayParent = ksLookup (ks, tmpArrayParent, KDB_O_NONE); keyDel (tmpArrayParent); if (arrayParent == NULL) return; KeySet * ksCopy = ksDup (ks); KeySet * subKeys = ksCut (ksCopy, arrayParent); Key * cur; long validCount = 0; while ((cur = ksNext (subKeys)) != NULL) { if (!keyIsDirectBelow (arrayParent, cur)) continue; if (keyBaseName (cur)[0] == '#') { if (elektraArrayValidateName (cur) == 1) { ++validCount; keySetMeta (cur, "spec/internal/valid", ""); } else { KeySet * invalidCutKS = ksCut (subKeys, cur); Key * toMark; while ((toMark = ksNext (invalidCutKS)) != NULL) { if (strcmp (keyName (cur), keyName (toMark))) keySetMeta (toMark, "conflict/invalid", ""); elektraMetaArrayAdd (arrayParent, "conflict/invalid/hasmember", keyName (toMark)); } ksDel (invalidCutKS); } } } ksDel (subKeys); ksDel (ksCopy); validateArrayRange (arrayParent, validCount, specKey); }
static void test_simpleCutRestoreOnSet () { Key * parentKey = keyNew ("user/tests/rename", KEY_END); Key * parentKeyCopy = keyDup (parentKey); KeySet * conf = ksNew (20, keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), KS_END); PLUGIN_OPEN ("rename"); KeySet * ks = createSimpleTestKeys (); ksAppendKey (ks, parentKey); 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"); 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"); /* test that the keys have been correctly restored */ KeySet * expected = createSimpleTestKeys (); /* the parent key is restored from user/tests/rename/will/be/stripped * and therefore will have its key value */ keySetString (parentKeyCopy, "value3"); ksAppendKey (expected, parentKeyCopy); compareKeySets (ks, expected); ksDel (expected); ksDel (ks); /* * this has to be done because the parentKey is not * part of ks anymore due to renaming */ keyDel (parentKey); PLUGIN_CLOSE (); }
static char * parseAlias (KeySet * append, const Key * hostParent, char * tokenPointer) { char * fieldBuffer; int sret = 0; sret = elektraParseToken (&fieldBuffer, tokenPointer); if (sret == 0) return 0; Key * alias = keyDup (hostParent); keyAddBaseName (alias, fieldBuffer); elektraFree (fieldBuffer); /* only add the alias if it does not exist already */ if (ksLookup (append, alias, KDB_O_NONE)) { keyDel (alias); } else { ksAppendKey (append, alias); } return tokenPointer + sret; }
static void test_addNewBaseToParentKey () { Key * parentKey = keyNew ("user/tests/rename", KEY_END); KeySet * conf = ksNew (20, keyNew ("system/cut", KEY_VALUE, "new/base", KEY_END), KS_END); PLUGIN_OPEN ("rename"); KeySet * ks = ksNew (0, KS_END); keyIncRef (parentKey); ksAppendKey (ks, parentKey); 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/rename/new/base", 0); succeed_if (key, "new base was not correctly appended to parent key"); ksDel (ks); keyDecRef (parentKey); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_replaceString () { Key * parentKey = keyNew ("user/tests/rename", KEY_END); KeySet * conf = ksNew (20, keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), keyNew ("system/replacewith", KEY_VALUE, "stripped/it/is", KEY_END), KS_END); KeySet * ks = createSimpleTestKeys (); ksAppendKey (ks, parentKey); PLUGIN_OPEN ("rename"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key * key = ksLookupByName (ks, "user/tests/rename/stripped/it/is/key1", KDB_O_NONE); succeed_if (key, "key1 was not correctly rename"); key = ksLookupByName (ks, "user/tests/rename/stripped/it/is/key2", KDB_O_NONE); succeed_if (key, "key2 was not correctly rename"); key = ksLookupByName (ks, "user/tests/rename/will/not/be/stripped/key4", KDB_O_NONE); succeed_if (key, "key4 was not correctly rename"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
static void test_toLower() { Key *parentKey = keyNew("user/tests/rename", KEY_END); KeySet *conf = ksNew(20, keyNew("system/tolower", KEY_VALUE, "0", KEY_END), KS_END); KeySet *ks = ksNew(20, keyNew("user/tests/rename/AM/I/LOWERCASE", KEY_VALUE, "val1", KEY_END), keyNew("user/tests/rename/I/HOPE/IM/LOWERCASE/TOO", KEY_VALUE, "val2", KEY_END), KS_END); ksAppendKey(ks, parentKey); PLUGIN_OPEN("rename"); succeed_if(plugin->kdbGet(plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key *key = ksLookupByName(ks, "user/tests/rename/am/i/lowercase", KDB_O_NONE); succeed_if(key, "key1 was not correctly rename"); key = ksLookupByName(ks, "user/tests/rename/i/hope/im/lowercase/too", KDB_O_NONE); succeed_if(key, "key2 was not correctly rename"); keyDel(parentKey); ksDel(ks); PLUGIN_CLOSE(); }
static void test_intUpdateWithCascadingKey (void) { printf ("test update with cascading key registered\n"); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("internalnotification"); Key * registeredKey = keyNew ("/test/internalnotification/value", KEY_END); int value = 0; succeed_if (internalnotificationRegisterInt (plugin, registeredKey, &value) == 1, "call to elektraInternalnotificationRegisterInt was not successful"); Key * valueKey = keyNew ("user/test/internalnotification/value", KEY_VALUE, "42", KEY_END); KeySet * ks = ksNew (1, valueKey, KS_END); elektraInternalnotificationUpdateRegisteredKeys (plugin, ks); succeed_if (value == 42, "registered value was not updated"); keyDel (registeredKey); ksDel (ks); PLUGIN_CLOSE (); }
static void test_updateOnKdbGet (void) { printf ("test update on kdbGet\n"); Key * parentKey = keyNew ("user/tests/internalnotification", KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("internalnotification"); Key * valueKey = keyNew ("user/test/internalnotification/value", KEY_VALUE, "42", KEY_END); KeySet * ks = ksNew (1, valueKey, KS_END); int value = 0; succeed_if (internalnotificationRegisterInt (plugin, valueKey, &value) == 1, "call to elektraInternalnotificationRegisterInt was not successful"); plugin->kdbGet (plugin, ks, parentKey); succeed_if (value == 42, "registered value was not updated"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
void test_plainIniRead(char *fileName) { Key *parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file(fileName), KEY_END); KeySet *conf = ksNew (0); PLUGIN_OPEN ("ini"); KeySet *ks = ksNew (0); 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 (keyIsDir(key), "section1 is not a directory key"); 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 () ; }
void test_commentIniWrite(char *fileName) { Key *parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename(), KEY_END); KeySet *conf = ksNew (0); PLUGIN_OPEN("ini"); KeySet *ks = ksNew (30, keyNew ("user/tests/ini-write/nosectionkey", KEY_VALUE, "nosectionvalue", KEY_COMMENT, "nosection comment1\nnosection comment2", KEY_END), keyNew ("user/tests/ini-write/section1", KEY_DIR, KEY_COMMENT, "section comment1\nsection comment2", KEY_END), keyNew ("user/tests/ini-write/section1/key1", KEY_VALUE, "value1", KEY_COMMENT, "key comment1\nkey comment2", KEY_END), KS_END); 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"); succeed_if( compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE () ; }
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 (!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 (!strcmp(keyString(parentKey), KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct"); keyDel (parentKey); elektraPluginClose(plugin, 0); elektraModulesClose(modules, 0); ksDel (modules); }
static void test_lookupChainRealWorld () { printf ("Test lookup chain real world\n"); Key * k1 = 0; Key * k2 = 0; Key * k3 = 0; Key * k4 = 0; // clang-format off KeySet *ks= ksNew(20, k1 = keyNew("spec/sw/P/current/editor", KEY_META, "example", "vim", KEY_META, "override/#0", "/sw/P/override/editor", KEY_META, "override/#1", "/sw/override/editor", KEY_META, "override/#2", "/sw/defaults/editor", KEY_END), k2 = keyNew("user/sw/defaults/editor", KEY_VALUE, "ok", KEY_END), k3 = keyNew("dir/sw/P/current/editor", KEY_VALUE, "wrong dir value", KEY_END), k4 = keyNew("user/sw/P/current/editor", KEY_VALUE, "wrong user value", KEY_END), KS_END); // clang-format on Key * found = ksLookupByName (ks, "/sw/P/current/editor", 0); succeed_if (found == k2, "found wrong key"); succeed_if_same_string (keyName (found), "user/sw/defaults/editor"); succeed_if_same_string (keyString (found), "ok"); Key * searchKey = keyNew ("/sw/P/current/editor", KEY_END); found = ksLookup (ks, searchKey, 0); succeed_if (found == k2, "found wrong key"); succeed_if_same_string (keyName (found), "user/sw/defaults/editor"); succeed_if_same_string (keyString (found), "ok"); keyDel (searchKey); ksDel (ks); }
static void test_lookupChainLast () { printf ("Test lookup chain last\n"); Key * k1 = 0; Key * k2 = 0; Key * k3 = 0; Key * k4 = 0; // clang-format off KeySet *ks= ksNew(20, k1 = keyNew("spec/key", KEY_VALUE, "spec value", KEY_META, "override/#0", "/something", KEY_META, "override/#1", "/something_else", KEY_META, "override/#2", "/override", KEY_END), k2 = keyNew("user/key", KEY_VALUE, "wrong user value", KEY_END), k3 = keyNew("dir/key", KEY_VALUE, "wrong dir value", KEY_END), k4 = keyNew("user/override", KEY_VALUE, "ok", KEY_END), KS_END); // clang-format on Key * found = ksLookupByName (ks, "/key", 0); succeed_if (found == k4, "found wrong key"); succeed_if_same_string (keyName (found), "user/override"); succeed_if_same_string (keyString (found), "ok"); Key * searchKey = keyNew ("/key", KEY_END); found = ksLookup (ks, searchKey, 0); succeed_if (found == k4, "found wrong key"); succeed_if_same_string (keyName (found), "user/override"); succeed_if_same_string (keyString (found), "ok"); keyDel (searchKey); ksDel (ks); }