LCOV - code coverage report
Current view: top level - tests/gpg - t-encrypt-mixed.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 40 51 78.4 %
Date: 2018-11-15 08:49:49 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* t-encrypt-mixed.c - Regression test.
       2             :    Copyright (C) 2016 by Bundesamt für Sicherheit in der Informationstechnik
       3             :    Software engineering by Intevation 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             : #ifdef HAVE_CONFIG_H
      23             : #include <config.h>
      24             : #endif
      25             : 
      26             : #include <stdlib.h>
      27             : #include <stdio.h>
      28             : #include <string.h>
      29             : 
      30             : #include <gpgme.h>
      31             : 
      32             : #include "t-support.h"
      33             : 
      34             : /* Tests mixed symmetric and asymmetric decryption. Verifies
      35             :    that an encrypted message can be decrypted without the
      36             :    secret key but that the recipient is also set correctly. */
      37             : int
      38           1 : main (void)
      39             : {
      40             :   gpgme_ctx_t ctx;
      41             :   gpgme_error_t err;
      42             :   gpgme_data_t in, out;
      43           1 :   gpgme_key_t key[2] = { NULL, NULL };
      44             :   gpgme_encrypt_result_t result;
      45             :   gpgme_decrypt_result_t dec_result;
      46             :   gpgme_recipient_t recipient;
      47           1 :   const char *text = "Hallo Leute\n";
      48             :   char *text2;
      49             :   size_t len;
      50             : 
      51           1 :   init_gpgme (GPGME_PROTOCOL_OpenPGP);
      52             : 
      53           1 :   err = gpgme_new (&ctx);
      54           1 :   fail_if_err (err);
      55           1 :   gpgme_set_armor (ctx, 1);
      56             : 
      57           1 :   err = gpgme_data_new_from_mem (&in, text, strlen (text), 0);
      58           1 :   fail_if_err (err);
      59             : 
      60           1 :   err = gpgme_data_new (&out);
      61           1 :   fail_if_err (err);
      62             : 
      63           1 :   gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
      64             : 
      65             :   /* A recipient for which we don't have a secret key */
      66           1 :   err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
      67             :                        &key[0], 0);
      68           1 :   fail_if_err (err);
      69             : 
      70           1 :   err = gpgme_op_encrypt (ctx, key,
      71             :                           GPGME_ENCRYPT_ALWAYS_TRUST | GPGME_ENCRYPT_SYMMETRIC,
      72             :                           in, out);
      73           1 :   fail_if_err (err);
      74           1 :   result = gpgme_op_encrypt_result (ctx);
      75           1 :   if (result->invalid_recipients)
      76             :     {
      77           0 :       fprintf (stderr, "Invalid recipient encountered: %s\n",
      78           0 :                result->invalid_recipients->fpr);
      79           0 :       exit (1);
      80             :     }
      81             : 
      82           1 :   print_data (out);
      83             : 
      84             :   /* Now try to decrypt */
      85           1 :   gpgme_data_seek (out, 0, SEEK_SET);
      86             : 
      87           1 :   gpgme_data_release (in);
      88           1 :   err = gpgme_data_new (&in);
      89           1 :   fail_if_err (err);
      90             : 
      91           1 :   err = gpgme_op_decrypt (ctx, out, in);
      92           1 :   fail_if_err (err);
      93             : 
      94           1 :   fputs ("Begin Result Decryption:\n", stdout);
      95           1 :   print_data (in);
      96           1 :   fputs ("End Result.\n", stdout);
      97             : 
      98           1 :   dec_result = gpgme_op_decrypt_result (ctx);
      99           1 :   if (dec_result->unsupported_algorithm || dec_result->wrong_key_usage)
     100             :     {
     101           0 :       fprintf (stderr, "%s:%d: Decryption failed\n", __FILE__, __LINE__);
     102           0 :       exit (1);
     103             :     }
     104             : 
     105           1 :   text2 = gpgme_data_release_and_get_mem (in, &len);
     106           1 :   if (strncmp (text, text2, len))
     107             :     {
     108           0 :       fprintf (stderr, "%s:%d: Wrong plaintext\n", __FILE__, __LINE__);
     109           0 :       exit (1);
     110             :     }
     111             : 
     112           1 :   recipient = dec_result->recipients;
     113           1 :   if (!recipient || recipient->next)
     114             :     {
     115           0 :       fprintf (stderr, "%s:%d: Invalid recipients \n", __FILE__, __LINE__);
     116           0 :       exit (1);
     117             :     }
     118             : 
     119           1 :   if (strncmp (recipient->keyid, "5381EA4EE29BA37F", 16))
     120             :     {
     121           0 :       fprintf (stderr, "%s:%d: Not encrypted to recipient's subkey \n", __FILE__, __LINE__);
     122           0 :       exit (1);
     123             :     }
     124             : 
     125           1 :   gpgme_key_unref (key[0]);
     126           1 :   free (text2);
     127           1 :   gpgme_data_release (out);
     128           1 :   gpgme_release (ctx);
     129           1 :   return 0;
     130             : }

Generated by: LCOV version 1.13