static void test_ksDupFun (const size_t storagePlugin, const char * tmpFile, KeySet * copyFunction (const KeySet * source)) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = simpleTestKeySet (); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); KeySet * dupKs = copyFunction (ks); compare_keyset (dupKs, ks); compare_keyset (ks, dupKs); ksDel (dupKs); keyDel (parentKey); ksDel (ks); closeStoragePlugin (storagePlugin); }
void test_json (const char * fileName, KeySet * compareKeySet, KeySet * conf) { printf ("Test json 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); output_keyset(compareKeySet); */ compare_keyset (keys, compareKeySet); keySetString (parentKey, elektraFilename ()); // 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)); /* printf ("The keys we read out are:\n"); output_keyset(keys); printf ("The keys we compared it with:\n"); output_keyset(compareKeySet); */ keyDel (parentKey); ksDel (keys); ksDel (compareKeySet); elektraPluginClose (plugin, 0); }
void test_hostLensDelete(char *sourceFile, char *compFile) { Key *parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (sourceFile), KEY_END); KeySet *conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END); PLUGIN_OPEN("augeas"); KeySet *ks = ksNew(0, KS_END); succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if(output_error (parentKey), "error in kdbGet"); succeed_if(output_warnings (parentKey), "warnings in kdbGet"); Key *key = ksLookupByName (ks, "user/tests/augeas-hosts/1", 0); exit_if_fail(key, "localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); key = ksLookupByName (ks, "user/tests/augeas-hosts/1/ipaddr", 0); exit_if_fail(key, "ip address of localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); key = ksLookupByName (ks, "user/tests/augeas-hosts/1/canonical", 0); exit_if_fail(key, "canonical of localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); key = ksLookupByName (ks, "user/tests/augeas-hosts/1/#comment", 0); exit_if_fail(key, "comment of localhost not found"); ksPopAtCursor(ks, ksGetCursor(ks)); keyDel (key); keySetString (parentKey, elektraFilename()); succeed_if(plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if(output_error (parentKey), "error in kdbSet"); succeed_if(output_warnings (parentKey), "warnings in kdbSet"); succeed_if( compare_line_files (srcdir_file (compFile), keyString (parentKey)), "files do not match as expected"); ksDel (ks); elektraUnlink(keyString (parentKey)); keyDel (parentKey); PLUGIN_CLOSE () ; }
/** * Updates registrations with current data from storage. * Part of elektra plugin contract. * * @param handle plugin handle * @param returned key set containing current data from storage * @param parentKey key for errors * * @retval 1 on success * @retval -1 on failure */ int elektraInternalnotificationGet (Plugin * handle, KeySet * returned, Key * parentKey) { if (!elektraStrCmp (keyName (parentKey), "system/elektra/modules/internalnotification")) { KeySet * contract = ksNew ( 30, keyNew ("system/elektra/modules/internalnotification", KEY_VALUE, "internalnotification plugin waits for your orders", KEY_END), keyNew ("system/elektra/modules/internalnotification/exports", KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/get", KEY_FUNC, elektraInternalnotificationGet, KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/set", KEY_FUNC, elektraInternalnotificationSet, KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/open", KEY_FUNC, elektraInternalnotificationOpen, KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/close", KEY_FUNC, elektraInternalnotificationClose, KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/notificationCallback", KEY_FUNC, elektraInternalnotificationDoUpdate, KEY_END), // Export register* functions INTERNALNOTIFICATION_EXPORT_FUNCTION (Int), INTERNALNOTIFICATION_EXPORT_FUNCTION (UnsignedInt), INTERNALNOTIFICATION_EXPORT_FUNCTION (Long), INTERNALNOTIFICATION_EXPORT_FUNCTION (UnsignedLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (LongLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (UnsignedLongLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (Float), INTERNALNOTIFICATION_EXPORT_FUNCTION (Double), // Export register* functions for kdb_*_t types INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbBoolean), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbChar), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbOctet), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbShort), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbUnsignedShort), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbUnsignedLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbLongLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbUnsignedLongLong), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbFloat), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbDouble), INTERNALNOTIFICATION_EXPORT_FUNCTION (KdbLongDouble), keyNew ("system/elektra/modules/internalnotification/exports/registerCallback", KEY_FUNC, elektraInternalnotificationRegisterCallback, KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/registerCallbackSameOrBelow", KEY_FUNC, elektraInternalnotificationRegisterCallbackSameOrBelow, KEY_END), keyNew ("system/elektra/modules/internalnotification/exports/setConversionErrorCallback", KEY_FUNC, elektraInternalnotificationSetConversionErrorCallback, KEY_END), #include ELEKTRA_README keyNew ("system/elektra/modules/internalnotification/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END); ksAppend (returned, contract); ksDel (contract); return 1; } elektraInternalnotificationUpdateRegisteredKeys (handle, returned); return 1; }
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_mmap_ksDupFun (const char * tmpFile, KeySet * copyFunction (const KeySet * source)) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("mmapstorage"); KeySet * ks = simpleTestKeySet (); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); succeed_if ((ks->flags & KS_FLAG_MMAP_ARRAY) == KS_FLAG_MMAP_ARRAY, "KeySet array not in mmap"); KeySet * dupKs = copyFunction (ks); compare_keyset (dupKs, ks); compare_keyset (ks, dupKs); ksDel (dupKs); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
void test_keyset() { KeySet *ks; Key *cur; int counter; printf("Testing KeySet from xml\n"); ks = ksNew(0, KS_END); exit_if_fail( ksFromXMLfile(ks, srcdir_file("xmltool/keyset.xml")) == 0, "ksFromXMLfile(key.xml) failed."); counter = 0; ksRewind(ks); while ( (cur = ksNext(ks)) ) { counter ++; /* Make tests ... */ // printf ("counter: %d - %s\n", counter, keyName(cur)); switch (counter) { // <key type="43" basename="0-27042916" value="0 216905227"><comment>2551516588474823843</comment></key> case 1: succeed_if (strcmp (keyName(cur),"user/tests/filesys/0-27042916") == 0,"name of first key not correct"); succeed_if (strcmp (keyValue(cur),"0 216905227") == 0,"value of first key not correct"); succeed_if (strcmp (keyComment(cur),"2551516588474823843") == 0,"comment of first key not correct"); break; // <key type="253" basename="1-2449524622" value="1 1679328197"><comment>3246436893195629244</comment></key> case 2: succeed_if (strcmp (keyName(cur),"user/tests/filesys/1-2449524622") == 0,"name of 2. key not correct"); succeed_if (strcmp (keyValue(cur),"1 1679328197") == 0,"value of 2. key not correct"); succeed_if (strcmp (keyComment(cur),"3246436893195629244") == 0,"comment of 2. key not correct"); break; // <key type="string" basename="dir-1-0"> case 3: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0") == 0,"name of 3. key not correct"); break; // <key type="114" basename="0-294164813" value="0 216245011"><comment>18454108762891828026</comment></key> case 4: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/0-294164813") == 0,"name of 4. key not correct"); succeed_if (strcmp (keyValue(cur),"0 216245011") == 0,"value of 4. key not correct"); succeed_if (strcmp (keyComment(cur),"18454108762891828026") == 0,"comment of 4. key not correct"); break; // <key type="135" basename="1-1479930365" value="1 2732423037"><comment>24597295372375238</comment></key> case 5: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/1-1479930365") == 0,"name of 4. key not correct"); succeed_if (strcmp (keyValue(cur),"1 2732423037") == 0,"value of 4. key not correct"); succeed_if (strcmp (keyComment(cur),"24597295372375238") == 0,"comment of 4. key not correct"); break; // <key type="string" basename="dir-2-0"> case 6: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/dir-2-0") == 0,"name of 3. key not correct"); break; // <key type="144" basename="0-215571059" value="0 264857705"><comment>2188631490667217086</comment></key> case 7: succeed_if (strcmp (keyName(cur),"user/tests/filesys/dir-1-0/dir-2-0/0-215571059") == 0,"name of 4. key not correct"); succeed_if (strcmp (keyValue(cur),"0 264857705") == 0,"value of 4. key not correct"); succeed_if (strcmp (keyComment(cur),"2188631490667217086") == 0,"comment of 4. key not correct"); break; } } ksDel(ks); }
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/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); printf ("\ntest_yajl RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError); return nbError; }
/** * @brief Bootstrap, first phase with fallback * @internal * * @param handle already allocated, but without defaultBackend * @param [out] keys for bootstrapping * @param errorKey key to add errors too * * @retval -1 failure: cannot initialize defaultBackend * @retval 0 warning: could not get initial config * @retval 1 success * @retval 2 success in fallback mode */ int elektraOpenBootstrap (KDB * handle, KeySet * keys, Key * errorKey) { handle->defaultBackend = backendOpenDefault (handle->modules, KDB_DB_INIT, errorKey); if (!handle->defaultBackend) return -1; handle->split = splitNew (); splitAppend (handle->split, handle->defaultBackend, keyNew (KDB_SYSTEM_ELEKTRA, KEY_END), 2); keySetName (errorKey, KDB_SYSTEM_ELEKTRA); keySetString (errorKey, "kdbOpen(): get"); int funret = 1; int ret = kdbGet (handle, keys, errorKey); int fallbackret = 0; if (ret == 0 || ret == -1) { // could not get KDB_DB_INIT, try KDB_DB_FILE // first cleanup: ksClear (keys); backendClose (handle->defaultBackend, errorKey); splitDel (handle->split); // then create new setup: handle->defaultBackend = backendOpenDefault (handle->modules, KDB_DB_FILE, errorKey); if (!handle->defaultBackend) { elektraRemoveMetaData (errorKey, "error"); // fix errors from kdbGet() return -1; } handle->split = splitNew (); splitAppend (handle->split, handle->defaultBackend, keyNew (KDB_SYSTEM_ELEKTRA, KEY_END), 2); keySetName (errorKey, KDB_SYSTEM_ELEKTRA); keySetString (errorKey, "kdbOpen(): get fallback"); fallbackret = kdbGet (handle, keys, errorKey); keySetName (errorKey, "system/elektra/mountpoints"); KeySet * cutKeys = ksCut (keys, errorKey); if (fallbackret == 1 && ksGetSize (cutKeys) != 0) { funret = 2; } ksAppend (keys, cutKeys); ksDel (cutKeys); } if (ret == -1 && fallbackret == -1) { funret = 0; } elektraRemoveMetaData (errorKey, "error"); // fix errors from kdbGet() return funret; }
static void test_ksGetSize (const size_t storagePlugin, const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = simpleTestKeySet (); ssize_t origSize = ksGetSize (ks); succeed_if (origSize > 0, "ks was empty before kdbSet"); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); ksDel (ks); ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); ssize_t returnedSize = ksGetSize (ks); succeed_if (origSize == returnedSize, "ksGetSize before and after kdbSet, kdbGet differ"); keyDel (parentKey); ksDel (ks); closeStoragePlugin (storagePlugin); }
void testvalid (const char * file) { Key * parentKey = keyNew ("user/tests/lineendings", KEY_VALUE, srcdir_file (file), KEY_END); KeySet * conf = 0; PLUGIN_OPEN ("lineendings"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbget failed"); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbset failed"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
/** * Writes to @p stream an XML version of the @p ks object. * * String generated is of the form: * @verbatim <keyset> <key name=...>...</key> <key name=...>...</key> <key name=...>...</key> </keyset> * @endverbatim * * or if KDB_O_HIER is used, the form will be: * @verbatim <keyset parent="user/smallest/parent/name"> <key basename=...>...</key> <key name=...>...</key> <!-- a key thats not under this keyset's parent --> <key basename=...>...</key> </keyset> * @endverbatim * * KDB_O_HEADER will additionally generate a header like: * @verbatim <?xml version="1.0" encoding="UTF-8"?> <!-- Generated by Elektra API. Total of n keys. --> <keyset xmlns="http://www.libelektra.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.libelektra.org elektra.xsd"> * @endverbatim * * @param ks the KeySet to serialise * @param stream where to write output: a file or stdout * @param options accepted #option_t ORed: * - @p option_t::KDB_O_NUMBERS \n * Do not convert UID and GID into user and group names. * - @p option_t::KDB_O_FULLNAME \n * The @c user keys are exported with their full names (including * user domains) * - @p option_t::KDB_O_CONDENSED \n * Less human readable, more condensed output. * - @p option_t::KDB_O_XMLHEADERS \n * Exclude the correct XML headers in the output. If not used, the * <?xml?> and schema info inside the <keyset> object will not be generated. * - @p option_t::KDB_O_HIER \n * Will generate a <keyset> node containing a @c parent attribute, and * <key> nodes with a @c basename relative to that @c parent. The @c parent * is calculated by taking the smallest key name in the keyset, so it is a * good idea to have only related keys on the keyset. Otherwise, a valid * consistent XML document still will be generated with regular absolute * @c name attribute for the <key> nodes, due to a * clever keyToStreamBasename() implementation. * * @see keyToStream() * @see commandList() for usage example * @return number of bytes written to output, or -1 if some error occurs * @param ks The keyset to output * @param stream the file pointer where to send the stream * @param options see above text */ ssize_t ksToStream(const KeySet *ks, FILE* stream, option_t options) { size_t written=0; Key *key=0; char *codeset = "UTF-8"; KeySet *cks = ksDup (ks); ksRewind (cks); if (options & KDB_O_HEADER) { written+=fprintf(stream,"<?xml version=\"1.0\" encoding=\"%s\"?>", codeset); if (~options & KDB_O_CONDENSED) written+=fprintf(stream, "\n<!-- Generated by Elektra API. Total of %d keys. -->\n",(int)cks->size); if (~options & KDB_O_CONDENSED) written+=fprintf(stream,"<keyset xmlns=\"http://www.libelektra.org\"\n" "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" "\txsi:schemaLocation=\"http://www.libelektra.org elektra.xsd\"\n"); else written+=fprintf(stream,"<keyset xmlns=\"http://www.libelektra.org\"" " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" " xsi:schemaLocation=\"http://www.libelektra.org elektra.xsd\""); } else written+=fprintf(stream,"<keyset"); if (options & KDB_O_HIER) { char commonParent[KDB_MAX_PATH_LENGTH]; ksGetCommonParentName(cks,commonParent,sizeof(commonParent)); if (commonParent[0]) { written+=fprintf(stream," parent=\"%s\">\n", commonParent); ksRewind (cks); while ((key=ksNext (cks)) != 0) written+=keyToStreamBasename(key,stream,commonParent,0,options); } else { written+=fprintf(stream,">\n"); ksRewind (cks); while ((key=ksNext (cks)) != 0) written+=keyToStream(key,stream,options); } } else { /* No KDB_O_HIER*/ written+=fprintf(stream,">\n"); ksRewind (cks); while ((key=ksNext (cks)) != 0) written+=keyToStream(key,stream,options); } written+=fprintf(stream,"</keyset>\n"); ksDel (cks); return written; }
int main() { Key * k = keyNew("user/key", KEY_END); c = keyNew("user/copy", KEY_END); l(k); keyDel(k); keyDel(c); KeySet *ks = ksNew(20, KS_END); o(ks); ksDel(ks); }
static void test_getArray (void) { printf ("Test get array"); KeySet * keys = ksNew (10, keyNew ("user/test/key1", KEY_END), keyNew ("user/test/key2", KEY_END), keyNew ("user/test/array", KEY_END), keyNew ("user/test/array/#0", KEY_END), keyNew ("user/test/array/#0/below", KEY_END), keyNew ("user/test/array/#1", KEY_END), keyNew ("user/test/yetanotherkey", KEY_END), KS_END); Key * arrayParent = keyNew ("user/test/array", KEY_END); KeySet * array = elektraArrayGet (arrayParent, keys); succeed_if (array, "The getarray function did not return a proper keyset"); succeed_if (ksGetSize (array) == 2, "the array contains a wrong number of elements"); succeed_if (ksLookupByName (array, "user/test/array/#0", KDB_O_NONE), "the array does not contain #0"); succeed_if (ksLookupByName (array, "user/test/array/#1", KDB_O_NONE), "the array does not contain #1"); keyDel (arrayParent); ksDel (array); ksDel (keys); }
void testinvalid (const char * file) { Key * parentKey = keyNew ("user/tests/lineendings", KEY_VALUE, srcdir_file (file), KEY_END); KeySet * conf = ksNew (20, keyNew ("system/valid", KEY_VALUE, "CRLF", KEY_END), KS_END); PLUGIN_OPEN ("lineendings"); KeySet * ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbget failed"); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "should have failed"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_mmap_set_get (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 = simpleTestKeySet (); 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 = simpleTestKeySet (); compare_keyset (expected, returned); ksDel (expected); ksDel (returned); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
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 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_lookupIndirect () { printf ("Test lookup by indirect spec\n"); Key * s; Key * p; Key * d; Key * u; Key * y; Key * e; KeySet * ks = ksNew (20, s = keyNew ("spec/abc", KEY_END), p = keyNew ("proc/abc", KEY_END), d = keyNew ("dir/abc", KEY_END), u = keyNew ("user/abc", KEY_END), y = keyNew ("system/abc", KEY_END), e = keyNew ("system/else", KEY_END), KS_END); succeed_if (ksGetSize (ks) == 6, "wrong size"); Key * k = ksLookupByName (ks, "/abc", 0); succeed_if (k == p, "did not find proc key"); keySetMeta (s, "namespace/#0", "no"); keySetMeta (s, "default", "80"); k = ksLookupByName (ks, "/abc", 0); succeed_if (k != 0, "should find default"); succeed_if (ksGetSize (ks) == 7, "default key not added"); succeed_if_same_string (keyString (k), "80"); Key * k2 = ksLookupByName (ks, "/abc", 0); succeed_if (k == k2, "did not get same default"); keySetMeta (s, "fallback/#0", "/else"); k = ksLookupByName (ks, "/abc", 0); succeed_if (k == e, "did not find else"); keySetMeta (s, "namespace/#0", "system"); k = ksLookupByName (ks, "/abc", 0); succeed_if (k == y, "did not find system key"); keySetMeta (s, "namespace/#0", "system"); keySetMeta (s, "namespace/#1", "user"); k = ksLookupByName (ks, "/abc", 0); succeed_if (k == y, "did not find system key"); keySetMeta (s, "namespace/#0", "proc"); keySetMeta (s, "namespace/#1", "user"); k = ksLookupByName (ks, "/abc", 0); succeed_if (k == p, "did not find proc key"); keySetMeta (s, "override/#0", "/else"); k = ksLookupByName (ks, "/abc", 0); succeed_if (k == e, "did not find override key"); ksDel (ks); }
void test_metaMerging () { Key *parentKey = keyNew ("user/tests/keytometa", KEY_END); KeySet *conf = ksNew(0, KS_END); PLUGIN_OPEN("keytometa"); KeySet* ks = createMergeTestkeys (); 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/normalkey1", 0); succeed_if (key, "normalkey1 was removed"); const Key *metaKey1 = keyGetMeta(key, "testmeta"); succeed_if (metaKey1, "normalkey1 contained no metakey"); const char *expected1 = "meta line 1\nmeta line 2\nmeta line 3"; succeed_if (!strcmp (keyString(metaKey1), expected1), "metakey of normalkey1 contained incorrect data"); key = ksLookupByName(ks, "user/normalkey2", 0); succeed_if (key, "normalkey2 was removed"); const Key *metaKey2 = keyGetMeta(key, "testmeta"); succeed_if (metaKey2, "normalkey2 contained no metakey"); const char *expected2 = "meta line 30\nmeta line 31\nmeta line 32"; succeed_if (!strcmp (keyString(metaKey2), expected2), "metakey of normalkey2 contained incorrect data"); /* change the value of the middle key */ keySetMeta(key, "testmeta", "meta line 30\nchanged meta line\nmeta line 32"); 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 = ksLookupByName(ks, "user/convertkey30", 0); succeed_if (key, "convertkey30 was not restored"); succeed_if (!strcmp (keyString(key), "meta line 30"), "value of convertkey30 was modified"); key = ksLookupByName (ks, "user/convertkey31", 0); succeed_if (key, "convertkey31 was not restored"); succeed_if (!strcmp (keyString(key), "changed meta line"), "meta information was not written back to convertkey31"); key = ksLookupByName (ks, "user/convertkey32", 0); succeed_if (key, "convertkey32 was not restored"); succeed_if (!strcmp (keyString(key), "meta line 32"), "value of convertkey32 was modified"); keyDel (parentKey); ksDel(ks); PLUGIN_CLOSE (); }
static void clearStorage (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 = ksNew (0, KS_END); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
static KeySet * readFile (Key * parentKey) { FILE * fp = fopen (keyString (parentKey), "r"); KeySet * result = ksNew (0, KS_END); if (!fp) return result; while (!feof (fp)) { KeySet * package = nextPackage (fp, parentKey); ksAppend (result, package); ksDel (package); } return result; }
int main() { KeySet *out; KeySet *ks = ksNew (64, keyNew ("user/a/1", KEY_END), keyNew ("user/a/2", KEY_END), keyNew ("user/a/b/1", KEY_END), keyNew ("user/a/b/2", KEY_END), keyNew ("user/ab/2", KEY_END), keyNew ("user/b/1", KEY_END), keyNew ("user/b/2", KEY_END), KS_END); KeySet *values = 0; KeySet *values_below_30 = 0; global_a = keyNew ("user/a", KEY_END); ksForEach (ks, add_string); ksForEach (ks, add_comment); out = ksNew(0, KS_END); ksFilter (out, ks, has_a); ksDel (out); out = ksNew(0, KS_END); ksFilter (out, ks, below_a); ksDel (out); out = ksNew(0, KS_END); ksFilter (out, ks, direct_below_a); ksDel (out); ksDel (ks); keyDel (global_a); global_a = 0; values = ksNew (64, keyNew ("user/a", KEY_VALUE, "40", KEY_END), keyNew ("user/b", KEY_VALUE, "20", KEY_END), keyNew ("user/c", KEY_VALUE, "80", KEY_END), keyNew ("user/d", KEY_VALUE, "24", KEY_END), keyNew ("user/e", KEY_VALUE, "32", KEY_END), keyNew ("user/f", KEY_VALUE, "12", KEY_END), keyNew ("user/g", KEY_VALUE, "43", KEY_END), KS_END); /* add together */ ksForEach (values, sum_helper); values_below_30 = ksNew(0, KS_END); ksFilter (values_below_30, values, below_30); ksForEach (values_below_30, sum_helper); ksForEach (values, find_80); ksCurrent (values); /* here is user/c */ ksLookupByName (values, "user/c", 0); /* should find the same */ ksDel (values); ksDel (values_below_30); }
void test_hostLensWrite(char *fileName) { Key *parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, elektraFilename(), KEY_END); KeySet *conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END); PLUGIN_OPEN("augeas"); KeySet *ks = ksNew (30, keyNew ("user/tests/augeas-hosts/1", KEY_END), keyNew ("user/tests/augeas-hosts/1/ipaddr", KEY_VALUE, "127.0.0.1", KEY_META, "order", "10", KEY_END), keyNew ("user/tests/augeas-hosts/1/canonical", KEY_VALUE, "localhost", KEY_META, "order", "20", KEY_END), keyNew ("user/tests/augeas-hosts/1/#comment", KEY_VALUE, "hostcomment", KEY_META, "order", "21", KEY_END), keyNew ("user/tests/augeas-hosts/#comment", KEY_VALUE, "linecomment", KEY_META, "order", "22", KEY_END), keyNew ("user/tests/augeas-hosts/2/ipaddr", KEY_VALUE, "192.168.0.1", KEY_META, "order", "30", KEY_END), keyNew ("user/tests/augeas-hosts/2/canonical", KEY_VALUE, "host1", KEY_META, "order", "40", KEY_END), keyNew ("user/tests/augeas-hosts/2/alias[1]", KEY_VALUE, "host1alias1", KEY_META, "order", "50", KEY_END), keyNew ("user/tests/augeas-hosts/2/alias[2]", KEY_VALUE, "host1alias2", KEY_META, "order", "60", KEY_END), keyNew ("user/tests/augeas-hosts/3/ipaddr", KEY_VALUE, "fd00::4711:4712:2::1", KEY_META, "order", "70", KEY_END), keyNew ("user/tests/augeas-hosts/3/canonical", KEY_VALUE, "host2", KEY_META, "order", "80", KEY_END), keyNew ("user/tests/augeas-hosts/3/alias[1]", KEY_VALUE, "host2alias1", KEY_META, "order", "90", KEY_END), keyNew ("user/tests/augeas-hosts/3/alias[2]", KEY_VALUE, "host2alias2", KEY_META, "order", "100", KEY_END), KS_END); ksAppendKey (ks, parentKey); succeed_if(plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if(output_error (parentKey), "error in kdbSet"); succeed_if(output_warnings (parentKey), "warnings in kdbSet"); succeed_if( compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected"); elektraUnlink(keyString (parentKey)); ksDel (ks); PLUGIN_CLOSE (); }
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_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_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_complexInsert (char * source, char * compare) { Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, srcdir_file (source), KEY_END); Key * writeParentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END); KeySet * conf = ksNew (0, KS_END); KeySet * ks = ksNew (30, KS_END); KeySet * appendKS = ksNew (10, keyNew ("user/tests/ini-write/section/subsection", KEY_BINARY, KEY_END), keyNew ("user/tests/ini-write/section/subsection/subkey", KEY_VALUE, "subval", KEY_END), keyNew ("user/tests/ini-write/section/zkey3", KEY_VALUE, "3", KEY_END), KS_END); PLUGIN_OPEN ("ini"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful"); keyDel (ksLookup (ks, parentKey, KDB_O_POP)); keyDel (parentKey); ksAppend (ks, appendKS); succeed_if (plugin->kdbSet (plugin, ks, writeParentKey) >= 1, "call to kdbSet was not successful"); succeed_if (compare_line_files (srcdir_file (compare), keyString (writeParentKey)), "files do not match as expected"); keyDel (ksLookup (ks, writeParentKey, KDB_O_POP)); keyDel (writeParentKey); ksDel (appendKS); ksDel (ks); PLUGIN_CLOSE (); }
/** * Delete a split object. * * Will free all allocated resources of a split keyset. * * @param keysets the split object to work with * @ingroup split */ void elektraSplitDel (Split * keysets) { for (size_t i = 0; i < keysets->size; ++i) { ksDel (keysets->keysets[i]); keyDecRef (keysets->parents[i]); keyDel (keysets->parents[i]); } elektraFree (keysets->keysets); elektraFree (keysets->handles); elektraFree (keysets->parents); elektraFree (keysets->syncbits); elektraFree (keysets); }
static CondResult parseCondition (Key * key, const char * condition, const Key * suffixList, KeySet * ks, Key * parentKey) { CondResult result = FALSE; const char * regexString = "((\\(([^\\(\\)]*)\\)))"; regex_t regex; if ((regcomp (®ex, regexString, REG_EXTENDED | REG_NEWLINE))) { ELEKTRA_SET_ERROR (87, parentKey, "Couldn't compile regex: most likely out of memory"); // the regex compiles so the only // possible error would be out of // memory ksDel (ks); return ERROR; } char * localCondition = elektraStrDup (condition); int subMatches = 4; regmatch_t m[subMatches]; char * ptr = localCondition; while (1) { int nomatch = regexec (®ex, ptr, subMatches, m, 0); if (nomatch) { break; } if (m[3].rm_so == -1) { result = -1; break; } int startPos; int endPos; startPos = m[3].rm_so + (ptr - localCondition); endPos = m[3].rm_eo + (ptr - localCondition); char * singleCondition = elektraMalloc (endPos - startPos + 1); strncpy (singleCondition, localCondition + startPos, endPos - startPos); singleCondition[endPos - startPos] = '\0'; result = parseSingleCondition (key, singleCondition, suffixList, ks, parentKey); for (int i = startPos - 1; i < endPos + 1; ++i) localCondition[i] = ' '; localCondition[startPos - 1] = '\''; localCondition[startPos] = (result == TRUE) ? '1' : '0'; localCondition[startPos + 1] = '\''; elektraFree (singleCondition); } elektraFree (localCondition); regfree (®ex); return result; }