LCOV - code coverage report
Current view: top level - tests/gpg - t-edit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 61 63 96.8 %
Date: 2018-11-15 08:49:49 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* t-edit.c - Regression test.
       2             :    Copyright (C) 2000 Werner Koch (dd9jn)
       3             :    Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
       4             : 
       5             :    This file is part of GPGME.
       6             : 
       7             :    GPGME is free software; you can redistribute it and/or modify it
       8             :    under the terms of the GNU Lesser General Public License as
       9             :    published by the Free Software Foundation; either version 2.1 of
      10             :    the License, or (at your option) any later version.
      11             : 
      12             :    GPGME is distributed in the hope that it will be useful, but
      13             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :    Lesser General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU Lesser General Public
      18             :    License along with this program; if not, write to the Free Software
      19             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      20             :    02111-1307, USA.  */
      21             : 
      22             : /* We need to include config.h so that we know whether we are building
      23             :    with large file system (LFS) support. */
      24             : #ifdef HAVE_CONFIG_H
      25             : #include <config.h>
      26             : #endif
      27             : 
      28             : #include <stdio.h>
      29             : #include <stdlib.h>
      30             : #include <string.h>
      31             : #include <assert.h>
      32             : #include <errno.h>
      33             : #include <unistd.h>
      34             : #include <errno.h>
      35             : 
      36             : #include <gpgme.h>
      37             : 
      38             : #include "t-support.h"
      39             : 
      40             : 
      41             : static void
      42          17 : flush_data (gpgme_data_t dh)
      43             : {
      44             :   char buf[100];
      45             :   int ret;
      46             : 
      47          17 :   ret = gpgme_data_seek (dh, 0, SEEK_SET);
      48          17 :   if (ret)
      49           0 :     fail_if_err (gpgme_error_from_errno (errno));
      50         226 :   while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
      51         192 :     fwrite (buf, ret, 1, stdout);
      52          17 :   if (ret < 0)
      53           0 :     fail_if_err (gpgme_error_from_errno (errno));
      54          17 : }
      55             : 
      56             : 
      57             : gpgme_error_t
      58          16 : interact_fnc (void *opaque, const char *status, const char *args, int fd)
      59             : {
      60          16 :   const char *result = NULL;
      61          16 :   gpgme_data_t out = (gpgme_data_t) opaque;
      62             : 
      63          16 :   fputs ("[-- Response --]\n", stdout);
      64          16 :   flush_data (out);
      65             : 
      66          16 :   fprintf (stdout, "[-- Code: %s, %s --]\n", status, args);
      67             : 
      68          16 :   if (fd >= 0)
      69             :     {
      70           7 :       if (!strcmp (args, "keyedit.prompt"))
      71             :         {
      72             :           static int step = 0;
      73             : 
      74           5 :           switch (step)
      75             :             {
      76             :             case 0:
      77           1 :               result = "fpr";
      78           1 :               break;
      79             :             case 1:
      80           1 :               result = "expire";
      81           1 :               break;
      82             : 
      83             :               /* This fixes the primary user ID so the keylisting
      84             :                  tests will have predictable output.  */
      85             :             case 2:
      86           1 :               result = "1";
      87           1 :               break;
      88             :             case 3:
      89           1 :               result = "primary";
      90           1 :               break;
      91             : 
      92             :             default:
      93           1 :               result = "quit";
      94           1 :               break;
      95             :             }
      96           5 :           step++;
      97             :         }
      98           2 :       else if (!strcmp (args, "keyedit.save.okay"))
      99           1 :         result = "Y";
     100           1 :       else if (!strcmp (args, "keygen.valid"))
     101           1 :         result = "0";
     102             :     }
     103             : 
     104          16 :   if (result)
     105             :     {
     106           7 :       gpgme_io_writen (fd, result, strlen (result));
     107           7 :       gpgme_io_writen (fd, "\n", 1);
     108             :     }
     109          16 :   return 0;
     110             : }
     111             : 
     112             : 
     113             : int
     114           1 : main (int argc, char **argv)
     115             : {
     116             :   gpgme_ctx_t ctx;
     117             :   gpgme_error_t err;
     118           1 :   gpgme_data_t out = NULL;
     119           1 :   gpgme_key_t key = NULL;
     120           1 :   const char *pattern = "Alpha";
     121             :   char *agent_info;
     122             : 
     123             :   (void)argc;
     124             :   (void)argv;
     125             : 
     126           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
     127             : 
     128           1 :   err = gpgme_new (&ctx);
     129           1 :   fail_if_err (err);
     130           1 :   err = gpgme_data_new (&out);
     131           1 :   fail_if_err (err);
     132             : 
     133           1 :   agent_info = getenv("GPG_AGENT_INFO");
     134           1 :   if (!(agent_info && strchr (agent_info, ':')))
     135           1 :     gpgme_set_passphrase_cb (ctx, passphrase_cb, 0);
     136             : 
     137           1 :   err = gpgme_op_keylist_start (ctx, pattern, 0);
     138           1 :   fail_if_err (err);
     139           1 :   err = gpgme_op_keylist_next (ctx, &key);
     140           1 :   fail_if_err (err);
     141           1 :   err = gpgme_op_keylist_end (ctx);
     142           1 :   fail_if_err (err);
     143             : 
     144           1 :   err = gpgme_op_interact (ctx, key, 0, interact_fnc, out, out);
     145           1 :   fail_if_err (err);
     146             : 
     147           1 :   fputs ("[-- Last response --]\n", stdout);
     148           1 :   flush_data (out);
     149             : 
     150           1 :   gpgme_data_release (out);
     151           1 :   gpgme_key_unref (key);
     152           1 :   gpgme_release (ctx);
     153             : 
     154           1 :   return 0;
     155             : }

Generated by: LCOV version 1.13