LCOV - code coverage report
Current view: top level - lang/cpp/tests - run-keylist.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 73 0.0 %
Date: 2018-11-15 08:49:49 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /*
       2             :     run-keylist.cpp
       3             : 
       4             :     This file is part of GpgMEpp's test suite.
       5             :     Copyright (c) 2018 Intevation GmbH
       6             : 
       7             :     QGpgME is free software; you can redistribute it and/or
       8             :     modify it under the terms of the GNU General Public License,
       9             :     version 2, as published by the Free Software Foundation.
      10             : 
      11             :     QGpgME is distributed in the hope that it will be useful,
      12             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :     General Public License for more details.
      15             : 
      16             :     You should have received a copy of the GNU General Public License
      17             :     along with this program; if not, write to the Free Software
      18             :     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      19             : 
      20             :     In addition, as a special exception, the copyright holders give
      21             :     permission to link the code of this program with any edition of
      22             :     the Qt library by Trolltech AS, Norway (or with modified versions
      23             :     of Qt that use the same license as Qt), and distribute linked
      24             :     combinations including the two.  You must obey the GNU General
      25             :     Public License in all respects for all of the code used other than
      26             :     Qt.  If you modify this file, you may extend this exception to
      27             :     your version of the file, but you are not obligated to do so.  If
      28             :     you do not wish to do so, delete this exception statement from
      29             :     your version.
      30             : */
      31             : 
      32             : #ifdef HAVE_CONFIG_H
      33             :  #include "config.h"
      34             : #endif
      35             : 
      36             : #include "context.h"
      37             : #include "key.h"
      38             : #include "keylistresult.h"
      39             : 
      40             : #include <memory>
      41             : #include <sstream>
      42             : #include <iostream>
      43             : 
      44             : using namespace GpgME;
      45             : 
      46             : static int
      47           0 : show_usage (int ex)
      48             : {
      49             :   fputs ("usage: run-keylist [options] [pattern]\n\n"
      50             :          "Options:\n"
      51             :          "  --verbose        run in verbose mode\n"
      52             :          "  --openpgp        use the OpenPGP protocol (default)\n"
      53             :          "  --cms            use the CMS protocol\n"
      54             :          "  --secret         list only secret keys\n"
      55             :          "  --with-secret    list pubkeys with secret info filled\n"
      56             :          "  --local          use GPGME_KEYLIST_MODE_LOCAL\n"
      57             :          "  --extern         use GPGME_KEYLIST_MODE_EXTERN\n"
      58             :          "  --sigs           use GPGME_KEYLIST_MODE_SIGS\n"
      59             :          "  --tofu           use GPGME_KEYLIST_MODE_TOFU\n"
      60             :          "  --sig-notations  use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
      61             :          "  --ephemeral      use GPGME_KEYLIST_MODE_EPHEMERAL\n"
      62             :          "  --validate       use GPGME_KEYLIST_MODE_VALIDATE\n"
      63             :          "  --locate         use GPGME_KEYLIST_MODE_LOCATE\n"
      64           0 :          , stderr);
      65           0 :   exit (ex);
      66             : }
      67             : 
      68             : int
      69           0 : main (int argc, char **argv)
      70             : {
      71           0 :     int last_argc = -1;
      72           0 :     Protocol protocol = OpenPGP;
      73           0 :     unsigned int mode = 0;
      74           0 :     bool only_secret = false;
      75             : 
      76           0 :     if (argc) {
      77           0 :         argc--; argv++;
      78             :     }
      79             : 
      80           0 :     while (argc && last_argc != argc ) {
      81           0 :         last_argc = argc;
      82           0 :         if (!strcmp (*argv, "--")) {
      83           0 :             argc--; argv++;
      84           0 :             break;
      85           0 :         } else if (!strcmp (*argv, "--help")) {
      86           0 :             show_usage (0);
      87           0 :         } else if (!strcmp (*argv, "--openpgp")) {
      88           0 :             protocol = OpenPGP;
      89           0 :             argc--; argv++;
      90           0 :         } else if (!strcmp (*argv, "--cms")) {
      91           0 :             protocol = CMS;
      92           0 :             argc--; argv++;
      93           0 :         } else if (!strcmp (*argv, "--secret")) {
      94           0 :             only_secret = true;
      95           0 :             argc--; argv++;
      96           0 :         } else if (!strcmp (*argv, "--local")) {
      97           0 :             mode |= KeyListMode::Local;
      98           0 :             argc--; argv++;
      99           0 :         } else if (!strcmp (*argv, "--extern")) {
     100           0 :             mode |= KeyListMode::Extern;
     101           0 :             argc--; argv++;
     102           0 :         }else if (!strcmp (*argv, "--tofu")) {
     103           0 :             mode |= KeyListMode::WithTofu;
     104           0 :             argc--; argv++;
     105           0 :         } else if (!strcmp (*argv, "--sigs")) {
     106           0 :             mode |= KeyListMode::Signatures;
     107           0 :             argc--; argv++;
     108           0 :         } else if (!strcmp (*argv, "--sig-notations")) {
     109           0 :             mode |= KeyListMode::SignatureNotations;
     110           0 :             argc--; argv++;
     111           0 :         } else if (!strcmp (*argv, "--ephemeral")) {
     112           0 :             mode |= KeyListMode::Ephemeral;
     113           0 :             argc--; argv++;
     114           0 :         } else if (!strcmp (*argv, "--validate")) {
     115           0 :             mode |= KeyListMode::Validate;
     116           0 :             argc--; argv++;
     117           0 :         } else if (!strcmp (*argv, "--locate")) {
     118           0 :             argc--; argv++;
     119           0 :             mode |= KeyListMode::Locate;
     120           0 :         } else if (!strncmp (*argv, "--", 2)) {
     121           0 :             show_usage (1);
     122             :         }
     123             :     }
     124             : 
     125           0 :     if (argc > 1) {
     126           0 :         show_usage (1);
     127             :     }
     128             : 
     129           0 :     GpgME::initializeLibrary();
     130           0 :     auto ctx = std::unique_ptr<Context> (Context::createForProtocol(protocol));
     131           0 :     if (!ctx) {
     132           0 :         std::cerr << "Failed to get Context";
     133           0 :         return -1;
     134             :     }
     135           0 :     ctx->setKeyListMode (mode);
     136           0 :     Error err = ctx->startKeyListing (*argv, only_secret);
     137           0 :     if (err) {
     138           0 :         std::cout << "Error: " << err.asString() << "\n";
     139           0 :         return -1;
     140             :     }
     141           0 :     GpgME::Key key;
     142           0 :     std::stringstream ss;
     143           0 :     do {
     144           0 :         key = ctx->nextKey(err);
     145           0 :         ss << key << "\n\n";
     146           0 :     } while (!err && !key.isNull());
     147             : 
     148           0 :     std::cout << ss.str();
     149             : 
     150           0 :     return 0;
     151           0 : }

Generated by: LCOV version 1.13