aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c
diff options
context:
space:
mode:
authorJay Threeth2011-04-04 11:48:26 -0700
committerJay Threeth2011-04-04 11:48:26 -0700
commit3c9cc506f741b980565ff5b3b001cd8b6ee36b12 (patch)
treecb862c57b3d5f74177cde3bd962a53fc377166f6 /linden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c
parentbuild fixes, might build on linux now (diff)
downloadmeta-impy-3c9cc506f741b980565ff5b3b001cd8b6ee36b12.zip
meta-impy-3c9cc506f741b980565ff5b3b001cd8b6ee36b12.tar.gz
meta-impy-3c9cc506f741b980565ff5b3b001cd8b6ee36b12.tar.bz2
meta-impy-3c9cc506f741b980565ff5b3b001cd8b6ee36b12.tar.xz
add source to libraries, and cruft for building under windows
Diffstat (limited to 'linden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c')
-rwxr-xr-xlinden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c396
1 files changed, 396 insertions, 0 deletions
diff --git a/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c b/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c
new file mode 100755
index 0000000..551ab68
--- /dev/null
+++ b/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/tsexp.c
@@ -0,0 +1,396 @@
1/* tsexp.c - S-expression regression tests
2 * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it 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 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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 02111-1307, USA
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdarg.h>
25#include "../src/gcrypt.h"
26
27#define PGMNAME "tsexp"
28
29static int verbose;
30static int error_count;
31
32static void
33info (const char *format, ...)
34{
35 va_list arg_ptr;
36
37 if (verbose)
38 {
39 va_start( arg_ptr, format ) ;
40 vfprintf (stderr, format, arg_ptr );
41 va_end(arg_ptr);
42 }
43}
44
45static void
46fail ( const char *format, ... )
47{
48 va_list arg_ptr ;
49
50 fputs (PGMNAME ": ", stderr);
51 va_start( arg_ptr, format ) ;
52 vfprintf (stderr, format, arg_ptr );
53 va_end(arg_ptr);
54 error_count++;
55}
56
57
58/* fixme: we need better tests */
59static void
60basic (void)
61{
62 int pass;
63 gcry_sexp_t sexp;
64 int idx;
65 char *secure_buffer;
66 size_t secure_buffer_len;
67 const char *string;
68 static struct {
69 const char *token;
70 const char *parm;
71 } values[] = {
72 { "public-key", NULL },
73 { "dsa", NULL },
74 { "dsa", "p" },
75 { "dsa", "y" },
76 { "dsa", "q" },
77 { "dsa", "g" },
78 { NULL }
79 };
80
81 info ("doing some pretty pointless tests\n");
82
83 secure_buffer_len = 99;
84 secure_buffer = gcry_xmalloc_secure (secure_buffer_len);
85 memset (secure_buffer, 'G', secure_buffer_len);
86
87 for (pass=0;;pass++)
88 {
89 switch (pass)
90 {
91 case 0:
92 string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
93 "(q #61626364656667#) (g %m)))");
94
95 if ( gcry_sexp_build (&sexp, NULL, string,
96 gcry_mpi_set_ui (NULL, 42)) )
97 {
98 fail (" scanning `%s' failed\n", string);
99 return;
100 }
101 break;
102
103 case 1:
104 string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
105 "(q %b) (g %m)))");
106
107 if ( gcry_sexp_build (&sexp, NULL, string,
108 15, "foo\0\x01\0x02789012345",
109 gcry_mpi_set_ui (NULL, 42)) )
110 {
111 fail (" scanning `%s' failed\n", string);
112 return;
113 }
114 break;
115
116 case 2:
117 string = ("(public-key (dsa (p #41424344#) (y silly_y_value) "
118 "(q %b) (g %m)))");
119
120 if ( gcry_sexp_build (&sexp, NULL, string,
121 secure_buffer_len, secure_buffer,
122 gcry_mpi_set_ui (NULL, 17)) )
123 {
124 fail (" scanning `%s' failed\n", string);
125 return;
126 }
127 if (!gcry_is_secure (sexp))
128 fail ("gcry_sexp_build did not switch to secure memory\n");
129 break;
130
131 default:
132 return; /* Ready. */
133 }
134
135
136 /* now find something */
137 for (idx=0; values[idx].token; idx++)
138 {
139 const char *token = values[idx].token;
140 const char *parm = values[idx].parm;
141 gcry_sexp_t s1, s2;
142 gcry_mpi_t a;
143 const char *p;
144 size_t n;
145
146 s1 = gcry_sexp_find_token (sexp, token, strlen(token) );
147 if (!s1)
148 {
149 fail ("didn't found `%s'\n", token);
150 continue;
151 }
152
153 p = gcry_sexp_nth_data (s1, 0, &n);
154 if (!p)
155 {
156 fail ("no car for `%s'\n", token);
157 continue;
158 }
159 info ("car=`%.*s'\n", (int)n, p);
160
161 s2 = gcry_sexp_cdr (s1);
162 if (!s2)
163 {
164 fail ("no cdr for `%s'\n", token);
165 continue;
166 }
167
168 p = gcry_sexp_nth_data (s2, 0, &n);
169 if (p)
170 {
171 fail ("data at car of `%s'\n", token);
172 continue;
173 }
174
175 if (parm)
176 {
177 s2 = gcry_sexp_find_token (s1, parm, strlen (parm));
178 if (!s2)
179 {
180 fail ("didn't found `%s'\n", parm);
181 continue;
182 }
183 p = gcry_sexp_nth_data (s2, 0, &n);
184 if (!p)
185 {
186 fail("no car for `%s'\n", parm );
187 continue;
188 }
189 info ("car=`%.*s'\n", (int)n, p);
190 p = gcry_sexp_nth_data (s2, 1, &n);
191 if (!p)
192 {
193 fail("no cdr for `%s'\n", parm );
194 continue;
195 }
196 info ("cdr=`%.*s'\n", (int)n, p);
197
198 a = gcry_sexp_nth_mpi (s2, 0, GCRYMPI_FMT_USG);
199 if (!a)
200 {
201 fail("failed to cdr the mpi for `%s'\n", parm);
202 continue;
203 }
204 }
205 }
206
207 gcry_sexp_release (sexp);
208 sexp = NULL;
209 }
210 gcry_free (secure_buffer);
211}
212
213
214static void
215canon_len (void)
216{
217 static struct {
218 size_t textlen; /* length of the buffer */
219 size_t expected;/* expected length or 0 on error and then ... */
220 size_t erroff; /* ... and at this offset */
221 gcry_error_t errcode; /* ... with this error code */
222 char *text;
223 } values[] = {
224 { 14, 13, 0, GPG_ERR_NO_ERROR, "(9:abcdefghi) " },
225 { 16, 15, 0, GPG_ERR_NO_ERROR, "(10:abcdefghix)" },
226 { 14, 0,14, GPG_ERR_SEXP_STRING_TOO_LONG, "(10:abcdefghi)" },
227 { 15, 0, 1, GPG_ERR_SEXP_ZERO_PREFIX, "(010:abcdefghi)" },
228 { 2, 0, 0, GPG_ERR_SEXP_NOT_CANONICAL, "1:"},
229 { 4, 0, 4, GPG_ERR_SEXP_STRING_TOO_LONG, "(1:)"},
230 { 5, 5, 0, GPG_ERR_NO_ERROR, "(1:x)"},
231 { 2, 2, 0, GPG_ERR_NO_ERROR, "()"},
232 { 4, 2, 0, GPG_ERR_NO_ERROR, "()()"},
233 { 4, 4, 0, GPG_ERR_NO_ERROR, "(())"},
234 { 3, 0, 3, GPG_ERR_SEXP_STRING_TOO_LONG, "(()"},
235 { 3, 0, 1, GPG_ERR_SEXP_BAD_CHARACTER, "( )"},
236 { 9, 9, 0, GPG_ERR_NO_ERROR, "(3:abc())"},
237 { 10, 0, 6, GPG_ERR_SEXP_BAD_CHARACTER, "(3:abc ())"},
238 /* fixme: we need much more cases */
239 { 0 },
240 };
241 int idx;
242 gcry_error_t errcode;
243 size_t n, erroff;
244
245 info ("checking canoncial length test function\n");
246 for (idx=0; values[idx].text; idx++)
247 {
248 n = gcry_sexp_canon_len ((const unsigned char*)values[idx].text,
249 values[idx].textlen,
250 &erroff, &errcode);
251
252 if (n && n == values[idx].expected)
253 ; /* success */
254 else if (!n && !values[idx].expected)
255 { /* we expected an error - check that this is the right one */
256 if (values[idx].erroff != erroff)
257 fail ("canonical length test %d - wrong error offset %u\n",
258 idx, (unsigned int)erroff);
259 if (gcry_err_code (errcode) != values[idx].errcode)
260 fail ("canonical length test %d - wrong error code %d\n",
261 idx, errcode);
262 }
263 else
264 fail ("canonical length test %d failed - n=%u, off=%u, err=%d\n",
265 idx, (unsigned int)n, (unsigned int)erroff, errcode);
266 }
267}
268
269
270static void
271back_and_forth_one (int testno, const char *buffer, size_t length)
272{
273 gcry_error_t rc;
274 gcry_sexp_t se, se1;
275 size_t n, n1;
276 char *p1;
277
278 rc = gcry_sexp_new (&se, buffer, length, 1);
279 if (rc)
280 {
281 fail ("baf %d: gcry_sexp_new failed: %s\n", testno, gpg_strerror (rc));
282 return;
283 }
284 n1 = gcry_sexp_sprint (se, GCRYSEXP_FMT_CANON, NULL, 0);
285 if (!n1)
286 {
287 fail ("baf %d: get required length for canon failed\n", testno);
288 return;
289 }
290 p1 = gcry_xmalloc (n1);
291 n = gcry_sexp_sprint (se, GCRYSEXP_FMT_CANON, p1, n1);
292 if (n1 != n+1) /* sprints adds an extra 0 but dies not return it */
293 {
294 fail ("baf %d: length mismatch for canon\n", testno);
295 return;
296 }
297 rc = gcry_sexp_create (&se1, p1, n, 0, gcry_free);
298 if (rc)
299 {
300 fail ("baf %d: gcry_sexp_create failed: %s\n",
301 testno, gpg_strerror (rc));
302 return;
303 }
304 gcry_sexp_release (se1);
305
306 /* FIXME: we need a lot more tests */
307
308 gcry_sexp_release (se);
309}
310
311
312
313static void
314back_and_forth (void)
315{
316 static struct { char *buf; int len; } tests[] = {
317 { "(7:g34:fgh1::2:())", 0 },
318 { "(7:g34:fgh1::2:())", 18 },
319 { NULL, 0 }
320 };
321 int idx;
322
323 for (idx=0; tests[idx].buf; idx++)
324 back_and_forth_one (idx, tests[idx].buf, tests[idx].len);
325}
326
327
328static void
329check_sscan (void)
330{
331 static struct {
332 const char *text;
333 gcry_error_t expected_err;
334 } values[] = {
335 /* Bug reported by Olivier L'Heureux 2003-10-07 */
336 { "(7:sig-val(3:dsa"
337 "(1:r20:\x7e\xff\xd5\xba\xc9\xc9\xa4\x9b\xd4\x26\x8b\x64"
338 "\x06\x7a\xcf\x42\x7b\x6c\x51\xfb)"
339 "(1:s21:\x01\x8c\x6c\x6f\x37\x1a\x8d\xfd\x5a\xb3\x2a\x3d"
340 "\xc5\xae\x23\xed\x32\x62\x30\x62\x3e)))",
341 GPG_ERR_NO_ERROR },
342 { "(7:sig-val(3:dsa"
343 "(1:r20:\x7e\xff\xd5\xba\xc9\xc9\xa4\x9b\xd4\x26\x8b\x64"
344 "\x06\x7a\xcf\x42\x7b\x6c\x51\xfb)"
345 "(1:s21:\x01\x8c\x6c\x6f\x37\x1a\x8d\xfd\x5a\xb3\x2a\x3d"
346 "\xc5\xae\x23\xed\x32\x62\x30\x62\x3e))",
347 GPG_ERR_SEXP_UNMATCHED_PAREN },
348 { "(7:sig-val(3:dsa"
349 "(1:r20:\x7e\xff\xd5\xba\xc9\xc9\xa4\x9b\xd4\x26\x8b\x64"
350 "\x06\x7a\xcf\x42\x7b\x6c\x51\xfb)"
351 "(1:s21:\x01\x8c\x6c\x6f\x37\x1a\x8d\xfd\x5a\xb3\x2a\x3d"
352 "\xc5\xae\x23\xed\x32\x62\x30\x62\x3e))))",
353 GPG_ERR_SEXP_UNMATCHED_PAREN },
354 { NULL, 0 }
355 };
356 int idx;
357 gcry_error_t err;
358 gcry_sexp_t s;
359
360 info ("checking gcry_sexp_sscan\n");
361 for (idx=0; values[idx].text; idx++)
362 {
363 err = gcry_sexp_sscan (&s, NULL,
364 values[idx].text,
365 strlen (values[idx].text));
366 if (gpg_err_code (err) != values[idx].expected_err)
367 fail ("gcry_sexp_sscan test %d failed: %s\n", idx, gpg_strerror (err));
368 gcry_sexp_release (s);
369 }
370}
371
372
373
374
375int
376main (int argc, char **argv)
377{
378 if (argc > 1 && !strcmp (argv[1], "-v"))
379 verbose = 1;
380
381 gcry_control (GCRYCTL_DISABLE_SECMEM_WARN);
382 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
383
384 basic ();
385 canon_len ();
386 back_and_forth ();
387 check_sscan ();
388
389 return error_count? 1:0;
390}
391
392
393
394
395
396