diff options
author | Justus Winter <[email protected]> | 2017-01-12 13:01:00 +0000 |
---|---|---|
committer | Justus Winter <[email protected]> | 2017-01-12 13:18:15 +0000 |
commit | 186dcd3494eda7383de57a530fd15776410531a5 (patch) | |
tree | 56a2ca667d016fa641daa01f065a03b672f1e5c5 /tests | |
parent | tests: Enable gpgconf test. (diff) | |
download | gpgme-186dcd3494eda7383de57a530fd15776410531a5.tar.gz gpgme-186dcd3494eda7383de57a530fd15776410531a5.zip |
tests: Improve the gpgconf test.
* tests/gpg/t-gpgconf.c: Include support functions.
(fail_if_err): Remove macro.
(init_gpgme): Remove function.
(lookup): New function.
(main): Update some values and verify that the changes are applied.
* tests/gpg/t-support.h (test): New assert-like macro.
GnuPG-bug-id: 2881
Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/gpg/t-gpgconf.c | 154 | ||||
-rw-r--r-- | tests/gpg/t-support.h | 28 |
2 files changed, 129 insertions, 53 deletions
diff --git a/tests/gpg/t-gpgconf.c b/tests/gpg/t-gpgconf.c index 1074eef3..d199d2eb 100644 --- a/tests/gpg/t-gpgconf.c +++ b/tests/gpg/t-gpgconf.c @@ -34,37 +34,7 @@ #include <gpgme.h> - -#define fail_if_err(err) \ - do \ - { \ - if (err) \ - { \ - fprintf (stderr, "%s:%d: %s: %s\n", \ - __FILE__, __LINE__, gpgme_strsource (err), \ - gpgme_strerror (err)); \ - exit (1); \ - } \ - } \ - while (0) - - -void -init_gpgme (gpgme_protocol_t proto) -{ - gpgme_error_t err; - - gpgme_check_version (NULL); - setlocale (LC_ALL, ""); - gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); -#ifndef HAVE_W32_SYSTEM - gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); -#endif - - err = gpgme_engine_check_version (proto); - fail_if_err (err); -} - +#include "t-support.h" static char * spaces (char *str, int extra) @@ -251,6 +221,34 @@ dump_comp (gpgme_conf_comp_t comp) int +lookup (gpgme_conf_comp_t conf, + const char *component, + const char *option, + gpgme_conf_comp_t *comp, + gpgme_conf_opt_t *opt) +{ + *comp = conf; + while (*comp && strcmp ((*comp)->name, component)) + *comp = (*comp)->next; + + if (*comp) + { + *opt = (*comp)->options; + while (*opt && strcmp ((*opt)->name, option)) + *opt = (*opt)->next; + + /* Allow for the option not to be there. */ + if (*opt) + return 1; /* Found. */ + } + + return 0; /* Not found. */ +} + +#include <assert.h> + + +int main (void) { gpgme_ctx_t ctx; @@ -258,6 +256,7 @@ main (void) gpgme_conf_comp_t conf; gpgme_conf_comp_t comp; int first; + int i, N = 100; init_gpgme (GPGME_PROTOCOL_GPGCONF); @@ -279,40 +278,89 @@ main (void) comp = comp->next; } -#if 1 /* Now change something. */ - { - unsigned int count = 1; + fprintf (stderr, " dirmngr.verbose "); + for (i = 0; i < N; i++) { + unsigned int count = i % 4 + 1; /* counts must not be zero */ gpgme_conf_arg_t arg; gpgme_conf_opt_t opt; err = gpgme_conf_arg_new (&arg, GPGME_CONF_NONE, &count); fail_if_err (err); - comp = conf; - while (comp && strcmp (comp->name, "dirmngr")) - comp = comp->next; + if (lookup (conf, "dirmngr", "verbose", &comp, &opt)) + { + /* Found. */ + err = gpgme_conf_opt_change (opt, 0, arg); + fail_if_err (err); - if (comp) + err = gpgme_op_conf_save (ctx, comp); + fail_if_err (err); + } + else { - opt = comp->options; - while (opt && strcmp (opt->name, "verbose")) - opt = opt->next; - - /* Allow for the verbose option not to be there. */ - if (opt) - { - err = gpgme_conf_opt_change (opt, 0, arg); - fail_if_err (err); - - err = gpgme_op_conf_save (ctx, comp); - fail_if_err (err); - } + fprintf (stderr, "Skipping test, option dirmngr.verbose not found.\n"); + break; } + + /* Reload config and verify that the value was updated. */ + gpgme_conf_release (conf); + err = gpgme_op_conf_load (ctx, &conf); + fail_if_err (err); + if (lookup (conf, "dirmngr", "verbose", &comp, &opt)) + { + /* Found. */ + test (opt->alt_type == GPGME_CONF_NONE); + test ((unsigned long) opt->value->value.count == count); + } + + fprintf (stderr, "."); + fflush (stderr); } -#endif - gpgme_conf_release (conf); + /* Now change something else. */ + fprintf (stderr, " gpg.keyserver "); + for (i = 0; i < N; i++) { + const char *values[2] = { "hkp://foo.bar", "hkps://bar.foo" }; + gpgme_conf_arg_t arg; + gpgme_conf_opt_t opt; + err = gpgme_conf_arg_new (&arg, GPGME_CONF_STRING, values[i%2]); + fail_if_err (err); + + if (lookup (conf, "gpg", "keyserver", &comp, &opt)) + { + /* Found. */ + test (opt->alt_type == GPGME_CONF_STRING); + err = gpgme_conf_opt_change (opt, 0, arg); + fail_if_err (err); + + err = gpgme_op_conf_save (ctx, comp); + fail_if_err (err); + } + else + { + fprintf (stderr, "Skipping test, option gpg.keyserver not found.\n"); + break; + } + + /* Reload config and verify that the value was updated. */ + gpgme_conf_release (conf); + err = gpgme_op_conf_load (ctx, &conf); + fail_if_err (err); + if (lookup (conf, "gpg", "keyserver", &comp, &opt)) + { + /* Found. */ + test (opt->alt_type == GPGME_CONF_STRING); + test (strcmp (opt->value->value.string, values[i%2]) == 0); + } + + fprintf (stderr, "."); + fflush (stderr); + } + fprintf (stderr, "\n"); + + gpgme_conf_release (conf); + gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-support.h b/tests/gpg/t-support.h index db69dd0e..f6dec682 100644 --- a/tests/gpg/t-support.h +++ b/tests/gpg/t-support.h @@ -48,6 +48,34 @@ while (0) +#ifdef GPGRT_HAVE_MACRO_FUNCTION +void GPGRT_ATTR_NORETURN +_test (const char *expr, const char *file, int line, + const char *func) +{ + fprintf (stderr, "Test \"%s\" in %s failed (%s:%d)\n", + expr, func, file, line); + exit (1); +} +# define test(expr) \ + ((expr) \ + ? (void) 0 \ + : _test (#expr, __FILE__, __LINE__, __FUNCTION__)) +#else /*!GPGRT_HAVE_MACRO_FUNCTION*/ +void +_test (const char *expr, const char *file, int line) +{ + fprintf (stderr, "Test \"%s\" failed (%s:%d)\n", + expr, file, line); + exit (1); +} +# define test(expr) \ + ((expr) \ + ? (void) 0 \ + : _test (#expr, __FILE__, __LINE__)) +#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ + + static const char * nonnull (const char *s) { |