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 27 : write_cb (void *handle, const void *buffer, size_t size)
60 : {
61 27 : struct cb_parms *parms = handle;
62 :
63 : (void)buffer;
64 :
65 27 : parms->bytes_received += size;
66 :
67 27 : return size;
68 : }
69 :
70 :
71 : static void
72 2 : progress_cb (void *opaque, const char *what, int type, int current, int total)
73 : {
74 : /* This is just a dummy. */
75 : (void)opaque;
76 : (void)what;
77 : (void)type;
78 : (void)current;
79 : (void)total;
80 2 : }
81 :
82 :
83 :
84 :
85 :
86 : int
87 1 : main (int argc, char *argv[])
88 : {
89 : gpgme_ctx_t ctx;
90 : gpgme_error_t err;
91 : struct gpgme_data_cbs cbs;
92 : gpgme_data_t in, out;
93 1 : gpgme_key_t key[3] = { NULL, NULL, NULL };
94 : gpgme_encrypt_result_t result;
95 : size_t nbytes;
96 : struct cb_parms parms;
97 :
98 1 : if (argc > 1)
99 0 : nbytes = atoi (argv[1]);
100 : else
101 1 : nbytes = 100000;
102 :
103 1 : init_gpgme (GPGME_PROTOCOL_OpenPGP);
104 :
105 1 : memset (&cbs, 0, sizeof cbs);
106 1 : cbs.read = read_cb;
107 1 : cbs.write = write_cb;
108 1 : memset (&parms, 0, sizeof parms);
109 1 : parms.bytes_to_send = nbytes;
110 :
111 1 : err = gpgme_new (&ctx);
112 1 : fail_if_err (err);
113 1 : gpgme_set_armor (ctx, 0);
114 :
115 : /* Install a progress handler to enforce a bit of more work to the
116 : gpgme i/o system. */
117 1 : gpgme_set_progress_cb (ctx, progress_cb, NULL);
118 :
119 1 : err = gpgme_data_new_from_cbs (&in, &cbs, &parms);
120 1 : fail_if_err (err);
121 :
122 1 : err = gpgme_data_new_from_cbs (&out, &cbs, &parms);
123 1 : fail_if_err (err);
124 :
125 1 : err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
126 : &key[0], 0);
127 1 : fail_if_err (err);
128 1 : err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
129 : &key[1], 0);
130 1 : fail_if_err (err);
131 :
132 1 : err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
133 1 : fail_if_err (err);
134 1 : result = gpgme_op_encrypt_result (ctx);
135 1 : if (result->invalid_recipients)
136 : {
137 0 : fprintf (stderr, "Invalid recipient encountered: %s\n",
138 0 : result->invalid_recipients->fpr);
139 0 : exit (1);
140 : }
141 1 : printf ("plaintext=%u bytes, ciphertext=%u bytes\n",
142 1 : (unsigned int)nbytes, (unsigned int)parms.bytes_received);
143 :
144 1 : gpgme_key_unref (key[0]);
145 1 : gpgme_key_unref (key[1]);
146 1 : gpgme_data_release (in);
147 1 : gpgme_data_release (out);
148 1 : gpgme_release (ctx);
149 1 : return 0;
150 : }
|