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: 2017-03-02 17:11:10 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13