diff options
author | Ingo Klöcker <[email protected]> | 2024-08-05 15:14:01 +0000 |
---|---|---|
committer | Ingo Klöcker <[email protected]> | 2024-08-06 10:43:28 +0000 |
commit | d804a7a4bc871f38f6cdc2cd63af3ef004580437 (patch) | |
tree | 0d2343771232ee2f761b19471837b1726bf4f341 /tests | |
parent | w32: Use UTF-8 for localized error descriptions for all threads (diff) | |
download | gpgme-d804a7a4bc871f38f6cdc2cd63af3ef004580437.tar.gz gpgme-d804a7a4bc871f38f6cdc2cd63af3ef004580437.zip |
core: New function gpgme_op_setownertrust
* src/Makefile.am (main_sources): Add new file.
* src/context.h (ctx_op_data_id_t): Add OPDATA_SETOWNERTRUST.
* src/engine.c, src/engine.h (_gpgme_engine_op_setownertrust): New.
* src/engine-backend.h (engine_ops): Add 'setownertrust' and adjust all
engine initializers.
* src/engine-gpg.c (gpg_setownertrust): New.
(_gpgme_engine_ops_gpg): Set setownertrust to gpg_setownertrust.
* src/gpgme.h.in (gpgme_op_setownertrust_start, gpgme_op_setownertrust):
New.
* src/gpgme.def, src/libgpgme.vers: Add new functions.
* src/setownertrust.c: New.
* doc/gpgme.texi: Document new functions.
* tests/Makefile.am (noinst_PROGRAMS): Add new test program.
* tests/run-setownertrust.c: New.
* tests/gpg/Makefile.am (c_tests): Add new file. (LDADD): Add
@GPG_ERROR_LIBS@.
* tests/gpg/t-setownertrust.c: New.
* tests/gpg/t-support.h (have_gpg_version): New.
--
This extends GPGME to support the --quick-set-ownertrust command added
by GnuPG 2.4.6. This allows changing the owner trust of keys and
enabling/disabling keys without using the editinteractor interface.
GnuPG-bug-id: 7239
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/gpg/Makefile.am | 3 | ||||
-rw-r--r-- | tests/gpg/t-setownertrust.c | 132 | ||||
-rw-r--r-- | tests/gpg/t-support.h | 24 | ||||
-rw-r--r-- | tests/run-setownertrust.c | 127 |
5 files changed, 284 insertions, 4 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 99b6fb46..0147b4b7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -42,7 +42,7 @@ noinst_HEADERS = run-support.h noinst_PROGRAMS = $(TESTS) run-keylist run-export run-import run-sign \ run-verify run-encrypt run-identify run-decrypt run-genkey \ run-keysign run-tofu run-swdb run-threaded \ - run-receive-keys + run-receive-keys run-setownertrust run_threaded_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_MT_CFLAGS@ run_threaded_LDADD = ../src/libgpgme.la \ diff --git a/tests/gpg/Makefile.am b/tests/gpg/Makefile.am index dc3642a6..a70c7a7b 100644 --- a/tests/gpg/Makefile.am +++ b/tests/gpg/Makefile.am @@ -40,6 +40,7 @@ c_tests = \ t-decrypt t-verify t-decrypt-verify t-sig-notation t-export \ t-import t-edit t-keylist t-keylist-sig t-keylist-secret-sig t-wait \ t-encrypt-large t-file-name t-gpgconf t-encrypt-mixed t-edit-sign \ + t-setownertrust \ $(tests_unix) TESTS = initial.test $(c_tests) final.test @@ -67,7 +68,7 @@ BUILT_SOURCES = gpg.conf gpg-agent.conf pubring-stamp \ gpg-sample.stamp AM_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_CFLAGS@ AM_LDFLAGS = -no-install -LDADD = ../../src/libgpgme.la @LDADD_FOR_TESTS_KLUDGE@ +LDADD = ../../src/libgpgme.la @GPG_ERROR_LIBS@ @LDADD_FOR_TESTS_KLUDGE@ WITH_THREAD_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_MT_CFLAGS@ WITH_THREAD_LDADD = ../../src/libgpgme.la \ diff --git a/tests/gpg/t-setownertrust.c b/tests/gpg/t-setownertrust.c new file mode 100644 index 00000000..dd2c723d --- /dev/null +++ b/tests/gpg/t-setownertrust.c @@ -0,0 +1,132 @@ +/* t-setownertrust.c - Regression test. + * Copyright (C) 2024 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see <https://gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +/* We need to include config.h so that we know whether we are building + with large file system (LFS) support. */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#define PGM "t-setownertrust" +#include "t-support.h" + +#include <gpgme.h> + +#include <stdio.h> +#include <stdlib.h> + + +static gpgme_key_t +list_one_key (gpgme_ctx_t ctx, const char *pattern, int secret_only) +{ + gpgme_error_t err; + gpgme_key_t key = NULL; + + err = gpgme_op_keylist_start (ctx, pattern, secret_only); + fail_if_err (err); + err = gpgme_op_keylist_next (ctx, &key); + fail_if_err (err); + err = gpgme_op_keylist_end (ctx); + fail_if_err (err); + + return key; +} + + +int +main (int argc, char **argv) +{ + gpgme_ctx_t ctx; + gpgme_error_t err; + gpgme_key_t key = NULL; + const char *pattern = "Alpha"; + + (void)argc; + (void)argv; + + if (!have_gpg_version ("2.4.6")) + { + printf ("Testsuite skipped. Minimum GnuPG version (2.4.6) " + "not found.\n"); + exit(0); + } + + init_gpgme (GPGME_PROTOCOL_OpenPGP); + + err = gpgme_new (&ctx); + fail_if_err (err); + + key = list_one_key (ctx, pattern, 0); + err = gpgme_op_setownertrust (ctx, key, "disable"); + fail_if_err (err); + gpgme_key_unref (key); + + key = list_one_key (ctx, pattern, 0); + if (!key->disabled) + { + fprintf (stderr, "%s:%i: Key is unexpectedly not disabled\n", + PGM, __LINE__); + exit (1); + } + gpgme_key_unref (key); + + key = list_one_key (ctx, pattern, 0); + err = gpgme_op_setownertrust (ctx, key, "enable"); + fail_if_err (err); + gpgme_key_unref (key); + + key = list_one_key (ctx, pattern, 0); + if (key->disabled) + { + fprintf (stderr, "%s:%i: Key is unexpectedly disabled\n", + PGM, __LINE__); + exit (1); + } + gpgme_key_unref (key); + + /* Check error handling */ + err = gpgme_op_setownertrust (ctx, NULL, "ultimate"); + if (gpgme_err_code (err) != GPG_ERR_INV_VALUE) + { + fprintf (stderr, "%s:%i: Unexpected error code: %s\n", + PGM, __LINE__, gpgme_strerror (err)); + exit (1); + } + key = list_one_key (ctx, pattern, 0); + err = gpgme_op_setownertrust (ctx, key, NULL); + if (gpgme_err_code (err) != GPG_ERR_INV_VALUE) + { + fprintf (stderr, "%s:%i: Unexpected error code: %s\n", + PGM, __LINE__, gpgme_strerror (err)); + exit (1); + } + err = gpgme_op_setownertrust (ctx, key, ""); + if (gpgme_err_code (err) != GPG_ERR_INV_VALUE) + { + fprintf (stderr, "%s:%i: Unexpected error code: %s\n", + PGM, __LINE__, gpgme_strerror (err)); + exit (1); + } + gpgme_key_unref (key); + + gpgme_release (ctx); + + return 0; +} diff --git a/tests/gpg/t-support.h b/tests/gpg/t-support.h index ae70cecc..40b03b7f 100644 --- a/tests/gpg/t-support.h +++ b/tests/gpg/t-support.h @@ -19,19 +19,21 @@ * SPDX-License-Identifier: LGPL-2.1-or-later */ +#include <gpgme.h> +#include <gpg-error.h> + #include <unistd.h> #include <errno.h> #include <stdlib.h> #include <locale.h> #include <limits.h> #include <ctype.h> +#include <string.h> #ifdef HAVE_W32_SYSTEM #include <windows.h> #endif -#include <gpgme.h> - #ifndef PGM #define PGM "unknown program; define PGM before including t-support.h" #endif @@ -271,3 +273,21 @@ print_import_result (gpgme_import_result_t r) r->not_imported, r->skipped_v3_keys); } + + +/* Return true if the gpg engine's version is at least REQ_VERSION. */ +int +have_gpg_version (const char *req_version) +{ + gpgme_engine_info_t engine_info; + init_gpgme (GPGME_PROTOCOL_OpenPGP); + + fail_if_err (gpgme_get_engine_info (&engine_info)); + for (; engine_info; engine_info = engine_info->next) + if (engine_info->protocol == GPGME_PROTOCOL_OpenPGP) + break; + + test (engine_info); + + return gpgrt_cmp_version (engine_info->version, req_version, 3) >= 0; +} diff --git a/tests/run-setownertrust.c b/tests/run-setownertrust.c new file mode 100644 index 00000000..4ed2bf98 --- /dev/null +++ b/tests/run-setownertrust.c @@ -0,0 +1,127 @@ +/* run-setownertrust.c - Test tool to perform ownertrust changes + * Copyright (C) 2024 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see <https://gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +/* We need to include config.h so that we know whether we are building + with large file system (LFS) support. */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <gpgme.h> + +#define PGM "run-setownertrust" + +#include "run-support.h" + + +static gpg_error_t +status_cb (void *opaque, const char *keyword, const char *value) +{ + (void)opaque; + fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value)); + return 0; +} + + +static int +show_usage (int ex) +{ + fputs ("usage: " PGM " [options] USERID VALUE\n" + "Options:\n" + " --status print status lines from the backend\n" + , stderr); + exit (ex); +} + + +int +main (int argc, char **argv) +{ + int last_argc = -1; + gpgme_error_t err; + gpgme_ctx_t ctx; + gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; + int print_status = 0; + const char *userid; + const char *value; + gpgme_key_t key; + + if (argc) + { argc--; argv++; } + + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--")) + { + argc--; argv++; + break; + } + else if (!strcmp (*argv, "--help")) + show_usage (0); + else if (!strcmp (*argv, "--status")) + { + print_status = 1; + argc--; argv++; + } + else if (!strncmp (*argv, "--", 2)) + show_usage (1); + } + + if (argc != 2) + show_usage (1); + userid = argv[0]; + value = argv[1]; + + init_gpgme (protocol); + + err = gpgme_new (&ctx); + fail_if_err (err); + gpgme_set_protocol (ctx, protocol); + gpgme_set_armor (ctx, 1); + if (print_status) + { + gpgme_set_status_cb (ctx, status_cb, NULL); + gpgme_set_ctx_flag (ctx, "full-status", "1"); + } + + err = gpgme_get_key (ctx, userid, &key, 0); + if (err) + { + fprintf (stderr, PGM ": error getting public key for '%s': %s\n", + userid, gpg_strerror (err)); + exit (1); + } + err = gpgme_op_setownertrust (ctx, key, value); + if (err) + { + fprintf (stderr, PGM ": gpgme_op_setownertrust failed: %s\n", + gpg_strerror (err)); + exit (1); + } + gpgme_key_unref (key); + + gpgme_release (ctx); + return 0; +} |