1 /* t-encrypt-sym.c - Regression test.
2  * Copyright (C) 2000 Werner Koch (dd9jn)
3  * Copyright (C) 2001, 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  */
18 
19 /* We need to include config.h so that we know whether we are building
20    with large file system (LFS) support. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <unistd.h>
30 
31 #include <gpgme.h>
32 
33 #include "t-support.h"
34 
35 
36 int
main(int argc,char * argv[])37 main (int argc, char *argv[])
38 {
39   gpgme_ctx_t ctx;
40   gpgme_error_t err;
41   gpgme_data_t plain, cipher;
42   const char *text = "Hallo Leute\n";
43   char *text2;
44   char *p;
45   size_t len;
46 
47   (void)argc;
48   (void)argv;
49 
50   init_gpgme (GPGME_PROTOCOL_OpenPGP);
51 
52   err = gpgme_new (&ctx);
53   fail_if_err (err);
54   gpgme_set_armor (ctx, 1);
55 
56   p = getenv("GPG_AGENT_INFO");
57   if (!(p && strchr (p, ':')))
58     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
59 
60   err = gpgme_data_new_from_mem (&plain, text, strlen (text), 0);
61   fail_if_err (err);
62 
63   err = gpgme_data_new (&cipher);
64   fail_if_err (err);
65 
66   err = gpgme_op_encrypt (ctx, 0, 0, plain, cipher);
67   fail_if_err (err);
68 
69   fflush (NULL);
70   fputs ("Begin Result Encryption:\n", stdout);
71   print_data (cipher);
72   fputs ("End Result.\n", stdout);
73 
74   gpgme_data_seek (cipher, 0, SEEK_SET);
75 
76   gpgme_data_release (plain);
77   err = gpgme_data_new (&plain);
78   fail_if_err (err);
79 
80   err = gpgme_op_decrypt (ctx, cipher, plain);
81   fail_if_err (err);
82 
83   fputs ("Begin Result Decryption:\n", stdout);
84   print_data (plain);
85   fputs ("End Result.\n", stdout);
86 
87   text2 = gpgme_data_release_and_get_mem (plain, &len);
88   if (strncmp (text, text2, len))
89     {
90       fprintf (stderr, "%s:%d: Wrong plaintext\n", __FILE__, __LINE__);
91       exit (1);
92     }
93 
94   gpgme_data_release (cipher);
95   free (text2);
96   gpgme_release (ctx);
97 
98   return 0;
99 }
100