static void test_timeoutConnect (void) { printf ("test connect timeout\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); plugin->kdbSet (plugin, ks, parentKey); char * expectedWarningNumber = elektraFormat ("%d", ELEKTRA_WARNING_ZEROMQSEND_TIMEOUT); succeed_if (keyGetMeta (parentKey, "warnings"), "warning meta key was not set"); succeed_if_same_string (expectedWarningNumber, keyValue (keyGetMeta (parentKey, "warnings/#00/number"))); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); elektraFree (expectedWarningNumber); }
/** * @brief Builds together a format string by the plugin's configuration * * @param handle to plugin * @param first format string for key * @param second format string for value * * @return newly allocated format (to be freed with elektraFree); */ static char * getFormat (Plugin * handle, const char * first, const char * second) { char * format; Key * key = ksLookupByName (elektraPluginGetConfig (handle), "/format", 0); if (!key) { format = elektraStrDup ("%s = %s\n"); } else { const size_t maxFactor = 2; // at maximum every char is a %, %% -> %%%% const size_t newLineAtEnd = 2; const size_t userFormatSize = keyGetValueSize (key); format = elektraMalloc (userFormatSize * maxFactor + newLineAtEnd); const char * userFormat = keyString (key); int gotPercent = 0; size_t j = 0; for (size_t i = 0; i < userFormatSize; ++i, ++j) { const char c = userFormat[i]; if (gotPercent) { if (c == '%') { // escaped %% -> %%%% format[j++] = '%'; format[j++] = '%'; format[j] = '%'; } else { // single % -> %s format[j++] = 's'; format[j] = c; } gotPercent = 0; } else if (c == '%') { format[j] = c; gotPercent = 1; } else { format[j] = c; } } --j; // discard null byte that is already there ELEKTRA_ASSERT (format[j] == '\0', "should be null byte at end of string but was %c", format[j]); format[j++] = '\n'; format[j] = '\0'; } char * ret = elektraFormat (format, first, second); elektraFree (format); return ret; }
/** * Converts a long double to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraLongDoubleToString (kdb_long_double_t value) { return elektraFormat ("%Lf", value); }
/** * Converts a double to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraDoubleToString (kdb_double_t value) { return elektraFormat ("%f", value); }
/** * Converts a float to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraFloatToString (kdb_float_t value) { return elektraFormat ("%f", value); }
/** * Converts an unsigned long long to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraUnsignedLongLongToString (kdb_unsigned_long_long_t value) { return elektraFormat (ELEKTRA_UNSIGNED_LONG_LONG_F, value); }
/** * Converts a long long to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraLongLongToString (kdb_long_long_t value) { return elektraFormat (ELEKTRA_LONG_LONG_F, value); }
/** * Converts an unsigned short to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraUnsignedShortToString (kdb_unsigned_short_t value) { return elektraFormat ("%d", value); }
/** * Converts a short to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraShortToString (kdb_short_t value) { return elektraFormat ("%d", value); }
/** * Converts an octet to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraOctetToString (kdb_octet_t value) { return elektraFormat ("%d", value); }
/** * Converts a char to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the value to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraCharToString (kdb_char_t value) { return elektraFormat ("%c", value); }
/** * Converts a boolean to string * * The string is allocated with elektraMalloc() and must * be disposed of with elektraFree(). * * @param value the boolean to convert * @return a new string allocated with elektraMalloc, or 0 on error */ char * elektraBooleanToString (kdb_boolean_t value) { return elektraFormat ("%s", value ? "1" : "0"); }
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; }
int main (int argc, char ** argv) { printf ("GOPTS TESTS\n"); printf ("==================\n\n"); init (argc, argv); char * ldLibPath = elektraFormat ("LD_LIBRARY_PATH=%s", getenv ("LD_LIBRARY_PATH")); run_test (NO_ARGS (TEST_EMPTY), NO_ENVP (ldLibPath)); run_test (NO_ARGS (TEST_SINGLEOPT), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "-capple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "-capple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "-c", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "-c", "apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "--longopt=apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "--longopt=apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "--longopt", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "--longopt", "apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_SINGLEOPT, "noopt"), NO_ENVP (ldLibPath)); run_test (NO_ARGS (TEST_SINGLEENV), NO_ENVP (ldLibPath)); run_test (NO_ARGS (TEST_SINGLEENV), ENVP (ldLibPath, "ENV_VAR=apple")); run_test (NO_ARGS (TEST_SINGLEENV), ENVP (ldLibPath, "OTHER_ENV_VAR=apple")); run_test (NO_ARGS (TEST_TWOOPT), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-capple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-capple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-c", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-c", "apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt=apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt=apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt", "apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "noopt"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-bapple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-bapple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-b", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-b", "apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt2=apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt2=apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt2", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt2", "apple", "morearg"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-bapple", "-capple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "-bapple", "morearg", "-c", "apple"), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_TWOOPT, "--longopt2", "apple", "--longopt", "apple"), NO_ENVP (ldLibPath)); run_test (NO_ARGS (TEST_TWOENV), NO_ENVP (ldLibPath)); run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "ENV_VAR=apple")); run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "OTHER_ENV_VAR=apple")); run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "ENV_VAR=apple", "OTHER_ENV_VAR=apple")); run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "OTHER_OTHER_ENV_VAR=apple")); run_test (NO_ARGS (TEST_TWOENV), ENVP (ldLibPath, "ENV_VAR=apple", "OTHER_ENV_VAR=apple", "OTHER_OTHER_ENV_VAR=apple")); run_test (NO_ARGS (TEST_MIXED), NO_ENVP (ldLibPath)); run_test (ARGS (TEST_MIXED, "-capple"), ENVP (ldLibPath, "ENV_VAR=apple")); run_test (ARGS (TEST_MIXED, "-c", "apple"), ENVP (ldLibPath, "OTHER_ENV_VAR=apple")); run_test (ARGS (TEST_MIXED, "--longopt=apple"), ENVP (ldLibPath, "ENV_VAR=apple", "OTHER_ENV_VAR=apple", "OTHER_OTHER_ENV_VAR=apple")); run_test (ARGS (TEST_MIXED, "--longopt", "apple"), ENVP (ldLibPath, "OTHER_ENV_VAR=apple")); elektraFree (ldLibPath); print_result ("testmod_gopts"); return nbError; }