LCOV - code coverage report
Current view: top level - tests - run-tofu.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 84 0.0 %
Date: 2016-12-01 18:45:36 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* run-tofu.c  - Test tool for Tofu functions
       2             :  * Copyright (C) 2016 g10 Code GmbH
       3             :  *
       4             :  * This file is part of GPGME.
       5             :  *
       6             :  * GPGME is free software; you can redistribute it and/or modify it
       7             :  * under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * GPGME is distributed in the hope that it will be useful, but
      12             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, see <https://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : /* We need to include config.h so that we know whether we are building
      21             :    with large file system (LFS) support. */
      22             : #ifdef HAVE_CONFIG_H
      23             : #include <config.h>
      24             : #endif
      25             : 
      26             : #include <stdlib.h>
      27             : #include <stdio.h>
      28             : #include <string.h>
      29             : #include <assert.h>
      30             : 
      31             : #include <gpgme.h>
      32             : 
      33             : #define PGM "run-tofu"
      34             : 
      35             : #include "run-support.h"
      36             : 
      37             : 
      38             : static int verbose;
      39             : 
      40             : 
      41             : static gpg_error_t
      42           0 : status_cb (void *opaque, const char *keyword, const char *value)
      43             : {
      44             :   (void)opaque;
      45           0 :   fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value));
      46           0 :   return 0;
      47             : }
      48             : 
      49             : 
      50             : 
      51             : static gpgme_tofu_policy_t
      52           0 : parse_policy_string (const char *string)
      53             : {
      54             :   gpgme_tofu_policy_t policy;
      55             : 
      56           0 :   if (!strcmp (string, "auto"))
      57           0 :     policy = GPGME_TOFU_POLICY_AUTO;
      58           0 :   else if (!strcmp (string, "good"))
      59           0 :     policy = GPGME_TOFU_POLICY_GOOD;
      60           0 :   else if (!strcmp (string, "bad"))
      61           0 :     policy = GPGME_TOFU_POLICY_BAD;
      62           0 :   else if (!strcmp (string, "ask"))
      63           0 :     policy = GPGME_TOFU_POLICY_ASK;
      64           0 :   else if (!strcmp (string, "unknown"))
      65           0 :     policy = GPGME_TOFU_POLICY_UNKNOWN;
      66             :   else
      67             :     {
      68           0 :       fprintf (stderr, PGM ": invalid policy value '%s'\n", string);
      69           0 :       exit (1);
      70             :     }
      71             : 
      72           0 :   return policy;
      73             : }
      74             : 
      75             : 
      76             : 
      77             : static int
      78           0 : show_usage (int ex)
      79             : {
      80           0 :   fputs ("usage: " PGM " [options] FPR\n\n"
      81             :          "Options:\n"
      82             :          "  --policy NAME    Set tofu policy for key to NAME\n"
      83             :          "  --verbose        run in verbose mode\n"
      84             :          "  --status         print status lines from the backend\n"
      85             :          , stderr);
      86           0 :   exit (ex);
      87             : }
      88             : 
      89             : 
      90             : int
      91           0 : main (int argc, char **argv)
      92             : {
      93           0 :   int last_argc = -1;
      94             :   gpgme_error_t err;
      95             :   gpgme_ctx_t ctx;
      96           0 :   gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
      97           0 :   int print_status = 0;
      98             :   gpgme_key_t thekey;
      99             :   const char *fpr;
     100           0 :   const char *policystr = NULL;
     101             :   gpgme_tofu_policy_t policy;
     102             :   const char *s;
     103             : 
     104           0 :   if (argc)
     105           0 :     { argc--; argv++; }
     106             : 
     107           0 :   while (argc && last_argc != argc )
     108             :     {
     109           0 :       last_argc = argc;
     110           0 :       if (!strcmp (*argv, "--"))
     111             :         {
     112           0 :           argc--; argv++;
     113           0 :           break;
     114             :         }
     115           0 :       else if (!strcmp (*argv, "--help"))
     116           0 :         show_usage (0);
     117           0 :       else if (!strcmp (*argv, "--verbose"))
     118             :         {
     119           0 :           verbose = 1;
     120           0 :           argc--; argv++;
     121             :         }
     122           0 :       else if (!strcmp (*argv, "--status"))
     123             :         {
     124           0 :           print_status = 1;
     125           0 :           argc--; argv++;
     126             :         }
     127           0 :       else if (!strcmp (*argv, "--policy"))
     128             :         {
     129           0 :           argc--; argv++;
     130           0 :           if (!argc)
     131           0 :             show_usage (1);
     132           0 :           policystr = *argv;
     133           0 :           argc--; argv++;
     134             :         }
     135           0 :       else if (!strncmp (*argv, "--", 2))
     136           0 :         show_usage (1);
     137             :     }
     138             : 
     139           0 :   if (argc != 1)
     140           0 :     show_usage (1);
     141           0 :   fpr = argv[0];
     142             : 
     143           0 :   init_gpgme (protocol);
     144             : 
     145           0 :   err = gpgme_new (&ctx);
     146           0 :   fail_if_err (err);
     147           0 :   gpgme_set_protocol (ctx, protocol);
     148           0 :   gpgme_set_armor (ctx, 1);
     149             : 
     150             : 
     151           0 :   s = gpgme_get_ctx_flag (ctx, "no_such-flag");
     152           0 :   if (s)
     153             :     {
     154           0 :       fprintf (stderr, PGM ": gpgme_get_ctx_flag failed "
     155             :                "(bad name not detected)\n");
     156           0 :       exit (1);
     157             :     }
     158           0 :   s = gpgme_get_ctx_flag (ctx, "full-status");
     159           0 :   if (!s || *s)
     160             :     {
     161           0 :       fprintf (stderr, PGM ": gpgme_get_ctx_flag failed (wrong false)\n");
     162           0 :       exit (1);
     163             :     }
     164           0 :   if (print_status)
     165             :     {
     166           0 :       gpgme_set_status_cb (ctx, status_cb, NULL);
     167           0 :       gpgme_set_ctx_flag (ctx, "full-status", "1");
     168           0 :       s = gpgme_get_ctx_flag (ctx, "full-status");
     169           0 :       if (!s || strcmp (s, "1"))
     170             :         {
     171           0 :           fprintf (stderr, PGM ": gpgme_get_ctx_flag fauled (wrong true)\n");
     172           0 :           exit (1);
     173             :         }
     174             :     }
     175             : 
     176           0 :   err = gpgme_get_key (ctx, fpr, &thekey, 0);
     177           0 :   if (err)
     178             :     {
     179           0 :       fprintf (stderr, PGM ": error getting key '%s': %s\n",
     180             :                fpr, gpg_strerror (err));
     181           0 :       exit (1);
     182             :     }
     183             : 
     184           0 :   if (policystr)
     185             :     {
     186           0 :       policy = parse_policy_string (policystr);
     187             : 
     188           0 :       err = gpgme_op_tofu_policy (ctx, thekey, policy);
     189           0 :       if (err)
     190             :         {
     191           0 :           fprintf (stderr, PGM ": gpgme_op_tofu_polciy failed: %s\n",
     192             :                    gpg_strerror (err));
     193           0 :           exit (1);
     194             :         }
     195             :     }
     196             : 
     197           0 :   gpgme_key_unref (thekey);
     198           0 :   gpgme_release (ctx);
     199           0 :   return 0;
     200             : }

Generated by: LCOV version 1.11