1 /* tsexp.c - S-expression regression tests
2 * Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
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.
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.
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
28 #include "../src/gcrypt.h"
30 #define PGMNAME "tsexp"
33 static int error_count;
36 info (const char *format, ...)
42 va_start( arg_ptr, format ) ;
43 vfprintf (stderr, format, arg_ptr );
49 fail ( const char *format, ... )
53 fputs (PGMNAME ": ", stderr);
54 va_start( arg_ptr, format ) ;
55 vfprintf (stderr, format, arg_ptr );
61 /* fixme: we need better tests */
69 size_t secure_buffer_len;
75 { "public-key", NULL },
84 info ("doing some pretty pointless tests\n");
86 secure_buffer_len = 99;
87 secure_buffer = gcry_xmalloc_secure (secure_buffer_len);
88 memset (secure_buffer, 'G', secure_buffer_len);
95 string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
96 "(q #61626364656667#) (g %m)))");
98 if ( gcry_sexp_build (&sexp, NULL, string,
99 gcry_mpi_set_ui (NULL, 42)) )
101 fail (" scanning `%s' failed\n", string);
107 string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
110 if ( gcry_sexp_build (&sexp, NULL, string,
111 15, "foo\0\x01\0x02789012345",
112 gcry_mpi_set_ui (NULL, 42)) )
114 fail (" scanning `%s' failed\n", string);
120 string = ("(public-key (dsa (p #41424344#) (y silly_y_value) "
123 if ( gcry_sexp_build (&sexp, NULL, string,
124 secure_buffer_len, secure_buffer,
125 gcry_mpi_set_ui (NULL, 17)) )
127 fail (" scanning `%s' failed\n", string);
130 if (!gcry_is_secure (sexp))
131 fail ("gcry_sexp_build did not switch to secure memory\n");
136 gcry_sexp_t help_sexp;
138 if (gcry_sexp_new (&help_sexp,
139 "(foobar-parms (xp #1234#)(xq #03#))", 0, 1))
141 fail (" scanning fixed string failed\n");
145 string = ("(public-key (dsa (p #41424344#) (parm %S) "
146 "(y dummy)(q %b) (g %m)))");
147 if ( gcry_sexp_build (&sexp, NULL, string, help_sexp,
148 secure_buffer_len, secure_buffer,
149 gcry_mpi_set_ui (NULL, 17)) )
151 fail (" scanning `%s' failed\n", string);
154 gcry_sexp_release (help_sexp);
164 /* now find something */
165 for (idx=0; values[idx].token; idx++)
167 const char *token = values[idx].token;
168 const char *parm = values[idx].parm;
174 s1 = gcry_sexp_find_token (sexp, token, strlen(token) );
177 fail ("didn't found `%s'\n", token);
181 p = gcry_sexp_nth_data (s1, 0, &n);
184 fail ("no car for `%s'\n", token);
187 info ("car=`%.*s'\n", (int)n, p);
189 s2 = gcry_sexp_cdr (s1);
192 fail ("no cdr for `%s'\n", token);
196 p = gcry_sexp_nth_data (s2, 0, &n);
199 fail ("data at car of `%s'\n", token);
205 s2 = gcry_sexp_find_token (s1, parm, strlen (parm));
208 fail ("didn't found `%s'\n", parm);
211 p = gcry_sexp_nth_data (s2, 0, &n);
214 fail("no car for `%s'\n", parm );
217 info ("car=`%.*s'\n", (int)n, p);
218 p = gcry_sexp_nth_data (s2, 1, &n);
221 fail("no cdr for `%s'\n", parm );
224 info ("cdr=`%.*s'\n", (int)n, p);
226 a = gcry_sexp_nth_mpi (s2, 0, GCRYMPI_FMT_USG);
229 fail("failed to cdr the mpi for `%s'\n", parm);
235 gcry_sexp_release (sexp);
238 gcry_free (secure_buffer);
246 size_t textlen; /* length of the buffer */
247 size_t expected;/* expected length or 0 on error and then ... */
248 size_t erroff; /* ... and at this offset */
249 gcry_error_t errcode; /* ... with this error code */
252 { 14, 13, 0, GPG_ERR_NO_ERROR, "(9:abcdefghi) " },
253 { 16, 15, 0, GPG_ERR_NO_ERROR, "(10:abcdefghix)" },
254 { 14, 0,14, GPG_ERR_SEXP_STRING_TOO_LONG, "(10:abcdefghi)" },
255 { 15, 0, 1, GPG_ERR_SEXP_ZERO_PREFIX, "(010:abcdefghi)" },
256 { 2, 0, 0, GPG_ERR_SEXP_NOT_CANONICAL, "1:"},
257 { 4, 0, 4, GPG_ERR_SEXP_STRING_TOO_LONG, "(1:)"},
258 { 5, 5, 0, GPG_ERR_NO_ERROR, "(1:x)"},
259 { 2, 2, 0, GPG_ERR_NO_ERROR, "()"},
260 { 4, 2, 0, GPG_ERR_NO_ERROR, "()()"},
261 { 4, 4, 0, GPG_ERR_NO_ERROR, "(())"},
262 { 3, 0, 3, GPG_ERR_SEXP_STRING_TOO_LONG, "(()"},
263 { 3, 0, 1, GPG_ERR_SEXP_BAD_CHARACTER, "( )"},
264 { 9, 9, 0, GPG_ERR_NO_ERROR, "(3:abc())"},
265 { 10, 0, 6, GPG_ERR_SEXP_BAD_CHARACTER, "(3:abc ())"},
266 /* fixme: we need much more cases */
270 gcry_error_t errcode;
273 info ("checking canoncial length test function\n");
274 for (idx=0; values[idx].text; idx++)
276 n = gcry_sexp_canon_len ((const unsigned char*)values[idx].text,
280 if (n && n == values[idx].expected)
282 else if (!n && !values[idx].expected)
283 { /* we expected an error - check that this is the right one */
284 if (values[idx].erroff != erroff)
285 fail ("canonical length test %d - wrong error offset %u\n",
286 idx, (unsigned int)erroff);
287 if (gcry_err_code (errcode) != values[idx].errcode)
288 fail ("canonical length test %d - wrong error code %d\n",
292 fail ("canonical length test %d failed - n=%u, off=%u, err=%d\n",
293 idx, (unsigned int)n, (unsigned int)erroff, errcode);
299 back_and_forth_one (int testno, const char *buffer, size_t length)
306 rc = gcry_sexp_new (&se, buffer, length, 1);
309 fail ("baf %d: gcry_sexp_new failed: %s\n", testno, gpg_strerror (rc));
312 n1 = gcry_sexp_sprint (se, GCRYSEXP_FMT_CANON, NULL, 0);
315 fail ("baf %d: get required length for canon failed\n", testno);
318 p1 = gcry_xmalloc (n1);
319 n = gcry_sexp_sprint (se, GCRYSEXP_FMT_CANON, p1, n1);
320 if (n1 != n+1) /* sprints adds an extra 0 but dies not return it */
322 fail ("baf %d: length mismatch for canon\n", testno);
325 rc = gcry_sexp_create (&se1, p1, n, 0, gcry_free);
328 fail ("baf %d: gcry_sexp_create failed: %s\n",
329 testno, gpg_strerror (rc));
332 gcry_sexp_release (se1);
334 /* Again but with memory checking. */
335 p1 = gcry_xmalloc (n1+2);
338 n = gcry_sexp_sprint (se, GCRYSEXP_FMT_CANON, p1+1, n1);
339 if (n1 != n+1) /* sprints adds an extra 0 but does not return it */
341 fail ("baf %d: length mismatch for canon\n", testno);
344 if (*p1 != '\x55' || p1[n1+1] != '\xaa')
345 fail ("baf %d: memory corrupted (1)\n", testno);
346 rc = gcry_sexp_create (&se1, p1+1, n, 0, NULL);
349 fail ("baf %d: gcry_sexp_create failed: %s\n",
350 testno, gpg_strerror (rc));
353 if (*p1 != '\x55' || p1[n1+1] != '\xaa')
354 fail ("baf %d: memory corrupted (2)\n", testno);
355 gcry_sexp_release (se1);
356 if (*p1 != '\x55' || p1[n1+1] != '\xaa')
357 fail ("baf %d: memory corrupted (3)\n", testno);
360 /* FIXME: we need a lot more tests */
362 gcry_sexp_release (se);
368 back_and_forth (void)
370 static struct { const char *buf; int len; } tests[] = {
371 { "(7:g34:fgh1::2:())", 0 },
372 { "(7:g34:fgh1::2:())", 18 },
374 "(protected-private-key \n"
376 " (n #00BE8A536204687149A48FF9F1715FF3530AD9A836D62102BF4065E5CF5953236DB94F1DF2FF4D525CD4CE7966DDC3C839968E8BAC2948934DF047CC65287CD79F6C23C93E55D7F9231E3942BD496DE383469977635A51ADF4AF747DB958CA02E9940DFC1DC0FC7FC755E7EB6618FEE6DA54B8A06E0CBF9D9257443F9992261435#)\n"
378 " (protected openpgp-s2k3-sha1-aes-cbc \n"
380 " (sha1 #C2A5673BD3882405# \"96\")\n"
381 " #8D08AAF6A9209ED69D71EB7E64D78715#)\n"
382 " #F7B0B535F8F8E22F4F3DA031224070303F82F9207D42952F1ACF21A4AB1C50304EBB25527992C7B265A9E9FF702826FB88759BDD55E4759E9FCA6C879538C9D043A9C60A326CB6681090BAA731289BD880A7D5774D9999F026E5E7963BFC8C0BDC9F061393CB734B4F259725C0A0A0B15BA39C39146EF6A1B3DC4DF30A22EBE09FD05AE6CB0C8C6532951A925F354F4E26A51964F5BBA50081690C421C8385C4074E9BAB9297D081B857756607EAE652415275A741C89E815558A50AC638EDC5F5030210B4395E3E1A40FF38DCCCB333A19EA88EFE7E4D51B54128C6DF27395646836679AC21B1B25C1DA6F0A7CE9F9BE078EFC7934FA9AE202CBB0AA06C20DFAF9A66FAB7E9073FBE96B9A7F25C3BA45EC3EECA65796AEE313BA148DE5314F30345B452B50B17C4D841A7F27397126E8C10BD0CE3B50A82C0425AAEE7798031671407B681F52916256F78CAF92A477AC27BCBE26DAFD1BCE386A853E2A036F8314BB2E8E5BB1F196434232EFB0288331C2AB16DBC5457CC295EB966CAC5CE73D5DA5D566E469F0EFA82F9A12B8693E0#)\n"
389 for (idx=0; tests[idx].buf; idx++)
390 back_and_forth_one (idx, tests[idx].buf, tests[idx].len);
399 gcry_error_t expected_err;
401 /* Bug reported by Olivier L'Heureux 2003-10-07 */
403 "(1:r20:\x7e\xff\xd5\xba\xc9\xc9\xa4\x9b\xd4\x26\x8b\x64"
404 "\x06\x7a\xcf\x42\x7b\x6c\x51\xfb)"
405 "(1:s21:\x01\x8c\x6c\x6f\x37\x1a\x8d\xfd\x5a\xb3\x2a\x3d"
406 "\xc5\xae\x23\xed\x32\x62\x30\x62\x3e)))",
409 "(1:r20:\x7e\xff\xd5\xba\xc9\xc9\xa4\x9b\xd4\x26\x8b\x64"
410 "\x06\x7a\xcf\x42\x7b\x6c\x51\xfb)"
411 "(1:s21:\x01\x8c\x6c\x6f\x37\x1a\x8d\xfd\x5a\xb3\x2a\x3d"
412 "\xc5\xae\x23\xed\x32\x62\x30\x62\x3e))",
413 GPG_ERR_SEXP_UNMATCHED_PAREN },
415 "(1:r20:\x7e\xff\xd5\xba\xc9\xc9\xa4\x9b\xd4\x26\x8b\x64"
416 "\x06\x7a\xcf\x42\x7b\x6c\x51\xfb)"
417 "(1:s21:\x01\x8c\x6c\x6f\x37\x1a\x8d\xfd\x5a\xb3\x2a\x3d"
418 "\xc5\xae\x23\xed\x32\x62\x30\x62\x3e))))",
419 GPG_ERR_SEXP_UNMATCHED_PAREN },
426 info ("checking gcry_sexp_sscan\n");
427 for (idx=0; values[idx].text; idx++)
429 err = gcry_sexp_sscan (&s, NULL,
431 strlen (values[idx].text));
432 if (gpg_err_code (err) != values[idx].expected_err)
433 fail ("gcry_sexp_sscan test %d failed: %s\n", idx, gpg_strerror (err));
434 gcry_sexp_release (s);
442 main (int argc, char **argv)
444 if (argc > 1 && !strcmp (argv[1], "--verbose"))
447 gcry_control (GCRYCTL_DISABLE_SECMEM_WARN);
448 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
455 return error_count? 1:0;