Line data Source code
1 : /* t-encrypt-large.c - Regression test for large amounts of data.
2 : Copyright (C) 2005 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, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : /* We need to include config.h so that we know whether we are building
22 : with large file system (LFS) support. */
23 : #ifdef HAVE_CONFIG_H
24 : #include <config.h>
25 : #endif
26 :
27 : #include <stdlib.h>
28 : #include <stdio.h>
29 : #include <string.h>
30 :
31 : #include <gpgme.h>
32 :
33 : #include "t-support.h"
34 :
35 :
36 : struct cb_parms
37 : {
38 : size_t bytes_to_send;
39 : size_t bytes_received;
40 : };
41 :
42 :
43 :
44 : /* The read callback used by GPGME to read data. */
45 : static ssize_t
46 26 : read_cb (void *handle, void *buffer, size_t size)
47 : {
48 26 : struct cb_parms *parms = handle;
49 26 : char *p = buffer;
50 :
51 100026 : for (; size && parms->bytes_to_send; size--, parms->bytes_to_send--)
52 100000 : *p++ = rand ();
53 :
54 26 : return (p - (char*)buffer);
55 : }
56 :
57 : /* The write callback used by GPGME to write data. */
58 : static ssize_t
59 26 : write_cb (void *handle, const void *buffer, size_t size)
60 : {
61 26 : struct cb_parms *parms = handle;
62 :
63 26 : parms->bytes_received += size;
64 :
65 26 : return size;
66 : }
67 :
68 :
69 : static void
70 2 : progress_cb (void *opaque, const char *what, int type, int current, int total)
71 : {
72 : /* This is just a dummy. */
73 2 : }
74 :
75 :
76 :
77 :
78 :
79 : int
80 1 : main (int argc, char *argv[])
81 : {
82 : gpgme_ctx_t ctx;
83 : gpgme_error_t err;
84 : struct gpgme_data_cbs cbs;
85 : gpgme_data_t in, out;
86 1 : gpgme_key_t key[3] = { NULL, NULL, NULL };
87 : gpgme_encrypt_result_t result;
88 : size_t nbytes;
89 : struct cb_parms parms;
90 :
91 1 : if (argc > 1)
92 0 : nbytes = atoi (argv[1]);
93 : else
94 1 : nbytes = 100000;
95 :
96 1 : init_gpgme (GPGME_PROTOCOL_OpenPGP);
97 :
98 1 : memset (&cbs, 0, sizeof cbs);
99 1 : cbs.read = read_cb;
100 1 : cbs.write = write_cb;
101 1 : memset (&parms, 0, sizeof parms);
102 1 : parms.bytes_to_send = nbytes;
103 :
104 1 : err = gpgme_new (&ctx);
105 1 : fail_if_err (err);
106 1 : gpgme_set_armor (ctx, 0);
107 :
108 : /* Install a progress handler to enforce a bit of more work to the
109 : gpgme i/o system. */
110 1 : gpgme_set_progress_cb (ctx, progress_cb, NULL);
111 :
112 1 : err = gpgme_data_new_from_cbs (&in, &cbs, &parms);
113 1 : fail_if_err (err);
114 :
115 1 : err = gpgme_data_new_from_cbs (&out, &cbs, &parms);
116 1 : fail_if_err (err);
117 :
118 1 : err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
119 : &key[0], 0);
120 1 : fail_if_err (err);
121 1 : err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
122 : &key[1], 0);
123 1 : fail_if_err (err);
124 :
125 1 : err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
126 1 : fail_if_err (err);
127 1 : result = gpgme_op_encrypt_result (ctx);
128 1 : if (result->invalid_recipients)
129 : {
130 0 : fprintf (stderr, "Invalid recipient encountered: %s\n",
131 0 : result->invalid_recipients->fpr);
132 0 : exit (1);
133 : }
134 1 : printf ("plaintext=%u bytes, ciphertext=%u bytes\n",
135 1 : (unsigned int)nbytes, (unsigned int)parms.bytes_received);
136 :
137 1 : gpgme_key_unref (key[0]);
138 1 : gpgme_key_unref (key[1]);
139 1 : gpgme_data_release (in);
140 1 : gpgme_data_release (out);
141 1 : gpgme_release (ctx);
142 1 : return 0;
143 : }
|