diff options
Diffstat (limited to 'lang/cpp/tests')
-rw-r--r-- | lang/cpp/tests/Makefile.am | 11 | ||||
-rw-r--r-- | lang/cpp/tests/Makefile.in | 27 | ||||
-rw-r--r-- | lang/cpp/tests/run-wkdlookup.cpp | 156 |
3 files changed, 189 insertions, 5 deletions
diff --git a/lang/cpp/tests/Makefile.am b/lang/cpp/tests/Makefile.am index 0b27595..9e67dff 100644 --- a/lang/cpp/tests/Makefile.am +++ b/lang/cpp/tests/Makefile.am @@ -32,5 +32,14 @@ AM_CPPFLAGS = -I$(top_srcdir)/lang/cpp/src -I$(top_builddir)/src \ run_getkey_SOURCES = run-getkey.cpp run_keylist_SOURCES = run-keylist.cpp run_verify_SOURCES = run-verify.cpp +if !HAVE_W32_SYSTEM +run_wkdlookup_SOURCES = run-wkdlookup.cpp +endif -noinst_PROGRAMS = run-getkey run-keylist run-verify +if HAVE_W32_SYSTEM +programs_unix = +else +programs_unix = run-wkdlookup +endif + +noinst_PROGRAMS = run-getkey run-keylist run-verify $(programs_unix) diff --git a/lang/cpp/tests/Makefile.in b/lang/cpp/tests/Makefile.in index a675bf1..280ffec 100644 --- a/lang/cpp/tests/Makefile.in +++ b/lang/cpp/tests/Makefile.in @@ -107,7 +107,7 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ noinst_PROGRAMS = run-getkey$(EXEEXT) run-keylist$(EXEEXT) \ - run-verify$(EXEEXT) + run-verify$(EXEEXT) $(am__EXEEXT_1) subdir = lang/cpp/tests ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_cxx_compile_stdcxx.m4 \ @@ -128,6 +128,7 @@ mkinstalldirs = $(SHELL) $(top_srcdir)/build-aux/mkinstalldirs CONFIG_HEADER = $(top_builddir)/conf/config.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = +@HAVE_W32_SYSTEM_FALSE@am__EXEEXT_1 = run-wkdlookup$(EXEEXT) PROGRAMS = $(noinst_PROGRAMS) am_run_getkey_OBJECTS = run-getkey.$(OBJEXT) run_getkey_OBJECTS = $(am_run_getkey_OBJECTS) @@ -148,6 +149,13 @@ run_verify_OBJECTS = $(am_run_verify_OBJECTS) run_verify_LDADD = $(LDADD) run_verify_DEPENDENCIES = ../../cpp/src/libgpgmepp.la \ ../../../src/libgpgme.la +am__run_wkdlookup_SOURCES_DIST = run-wkdlookup.cpp +@HAVE_W32_SYSTEM_FALSE@am_run_wkdlookup_OBJECTS = \ +@HAVE_W32_SYSTEM_FALSE@ run-wkdlookup.$(OBJEXT) +run_wkdlookup_OBJECTS = $(am_run_wkdlookup_OBJECTS) +run_wkdlookup_LDADD = $(LDADD) +run_wkdlookup_DEPENDENCIES = ../../cpp/src/libgpgmepp.la \ + ../../../src/libgpgme.la AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false @@ -164,7 +172,8 @@ DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/conf depcomp = $(SHELL) $(top_srcdir)/build-aux/depcomp am__maybe_remake_depfiles = depfiles am__depfiles_remade = ./$(DEPDIR)/run-getkey.Po \ - ./$(DEPDIR)/run-keylist.Po ./$(DEPDIR)/run-verify.Po + ./$(DEPDIR)/run-keylist.Po ./$(DEPDIR)/run-verify.Po \ + ./$(DEPDIR)/run-wkdlookup.Po am__mv = mv -f CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) @@ -185,9 +194,9 @@ am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) am__v_CXXLD_0 = @echo " CXXLD " $@; am__v_CXXLD_1 = SOURCES = $(run_getkey_SOURCES) $(run_keylist_SOURCES) \ - $(run_verify_SOURCES) + $(run_verify_SOURCES) $(run_wkdlookup_SOURCES) DIST_SOURCES = $(run_getkey_SOURCES) $(run_keylist_SOURCES) \ - $(run_verify_SOURCES) + $(run_verify_SOURCES) $(am__run_wkdlookup_SOURCES_DIST) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -427,6 +436,9 @@ AM_CPPFLAGS = -I$(top_srcdir)/lang/cpp/src -I$(top_builddir)/src \ run_getkey_SOURCES = run-getkey.cpp run_keylist_SOURCES = run-keylist.cpp run_verify_SOURCES = run-verify.cpp +@HAVE_W32_SYSTEM_FALSE@run_wkdlookup_SOURCES = run-wkdlookup.cpp +@HAVE_W32_SYSTEM_FALSE@programs_unix = run-wkdlookup +@HAVE_W32_SYSTEM_TRUE@programs_unix = all: all-am .SUFFIXES: @@ -482,6 +494,10 @@ run-verify$(EXEEXT): $(run_verify_OBJECTS) $(run_verify_DEPENDENCIES) $(EXTRA_ru @rm -f run-verify$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(run_verify_OBJECTS) $(run_verify_LDADD) $(LIBS) +run-wkdlookup$(EXEEXT): $(run_wkdlookup_OBJECTS) $(run_wkdlookup_DEPENDENCIES) $(EXTRA_run_wkdlookup_DEPENDENCIES) + @rm -f run-wkdlookup$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(run_wkdlookup_OBJECTS) $(run_wkdlookup_LDADD) $(LIBS) + mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -491,6 +507,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/run-getkey.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/run-keylist.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/run-verify.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/run-wkdlookup.Po@am__quote@ # am--include-marker $(am__depfiles_remade): @$(MKDIR_P) $(@D) @@ -653,6 +670,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/run-getkey.Po -rm -f ./$(DEPDIR)/run-keylist.Po -rm -f ./$(DEPDIR)/run-verify.Po + -rm -f ./$(DEPDIR)/run-wkdlookup.Po -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-tags @@ -701,6 +719,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/run-getkey.Po -rm -f ./$(DEPDIR)/run-keylist.Po -rm -f ./$(DEPDIR)/run-verify.Po + -rm -f ./$(DEPDIR)/run-wkdlookup.Po -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/lang/cpp/tests/run-wkdlookup.cpp b/lang/cpp/tests/run-wkdlookup.cpp new file mode 100644 index 0000000..71c4e70 --- /dev/null +++ b/lang/cpp/tests/run-wkdlookup.cpp @@ -0,0 +1,156 @@ +/* + run-wkdlookup.cpp + + This file is part of GpgMEpp's test suite. + Copyright (c) 2021 g10 Code GmbH + Software engineering by Ingo Klöcker <dev@ingo-kloecker.de> + + GPGME++ is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 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 Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with GPGME++; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include "context.h" +#include "data.h" +#include "defaultassuantransaction.h" +#include "key.h" + +#include <memory> +#include <sstream> +#include <iostream> +#include <thread> + +using namespace GpgME; + +static int +show_usage (int ex) +{ + fputs ("usage: run-wkdlookup <email address>\n\n" + , stderr); + exit (ex); +} + +int +main (int argc, char **argv) +{ + int last_argc = -1; + + 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 (!strncmp (*argv, "--", 2)) { + show_usage (1); + } + } + + if (argc != 1) { + show_usage (1); + } + + const std::string email{*argv}; + + GpgME::initializeLibrary(); + Error err; + auto ctx = std::unique_ptr<Context>{Context::createForEngine(AssuanEngine, &err)}; + if (!ctx) { + std::cerr << "Failed to get context (Error: " << err.asString() << ")\n"; + return -1; + } + + const std::string dirmngrSocket = GpgME::dirInfo("dirmngr-socket"); + if ((err = ctx->setEngineFileName(dirmngrSocket.c_str()))) { + std::cerr << "Failed to set engine file name (Error: " << err.asString() << ")\n"; + return -1; + } + if ((err = ctx->setEngineHomeDirectory(""))) { + std::cerr << "Failed to set engine home directory (Error: " << err.asString() << ")\n"; + return -1; + } + + // try do connect to dirmngr + err = ctx->assuanTransact("GETINFO version"); + if (err && err.code() != GPG_ERR_ASS_CONNECT_FAILED) { + std::cerr << "Failed to start assuan transaction (Error: " << err.asString() << ")\n"; + return -1; + } + if (err.code() == GPG_ERR_ASS_CONNECT_FAILED) { + std::cerr << "Starting dirmngr ...\n"; + auto spawnCtx = std::unique_ptr<Context>{Context::createForEngine(SpawnEngine, &err)}; + if (!spawnCtx) { + std::cerr << "Failed to get context for spawn engine (Error: " << err.asString() << ")\n"; + return -1; + } + + const auto dirmngrProgram = GpgME::dirInfo("dirmngr-name"); + const auto homedir = GpgME::dirInfo("homedir"); + const char *argv[] = { + dirmngrProgram, + "--homedir", + homedir, + "--daemon", + NULL + }; + auto ignoreIO = Data{Data::null}; + err = spawnCtx->spawnAsync(dirmngrProgram, argv, + ignoreIO, ignoreIO, ignoreIO, + Context::SpawnDetached); + if (err) { + std::cerr << "Failed to start dirmngr (Error: " << err.asString() << ")\n"; + return -1; + } + + // wait for socket to become available + int cnt = 0; + do { + ++cnt; + std::cerr << "Waiting for dirmngr to start ...\n"; + std::this_thread::sleep_for(std::chrono::milliseconds{250 * cnt}); + err = ctx->assuanTransact("GETINFO version"); + } while (err.code() == GPG_ERR_ASS_CONNECT_FAILED && cnt < 5); + } + + const auto cmd = std::string{"WKD_GET "} + email; + err = ctx->assuanTransact(cmd.c_str()); + if (err && err.code() != GPG_ERR_NO_NAME && err.code() != GPG_ERR_NO_DATA) { + std::cerr << "Error: WKD_GET returned " << err.asString() << "\n"; + return -1; + } + + const auto transaction = std::unique_ptr<DefaultAssuanTransaction>(dynamic_cast<DefaultAssuanTransaction*>(ctx->takeLastAssuanTransaction().release())); + const auto source = transaction->firstStatusLine("SOURCE"); + const auto rawData = transaction->data(); + if (rawData.size() == 0) { + std::cout << "No key found for " << email << "\n"; + } else { + const auto data = GpgME::Data{rawData.c_str(), rawData.size()}; + const auto keys = data.toKeys(GpgME::OpenPGP); + for (const auto &key : keys) { + std::cout << "Found key for " << email << " at " << source << ":\n" << key << "\n"; + } + } + + return 0; +} |