agent/
[gnupg.git] / common / t-convert.c
blob4b04f3a3297b9db2299f6b279b876ccfce44e7c1
1 /* t-convert.c - Module test for convert.c
2 * Copyright (C) 2006, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <assert.h>
25 #include "util.h"
27 #define pass() do { ; } while(0)
28 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
29 __FILE__,__LINE__, (a)); \
30 exit (1); \
31 } while(0)
34 static void
35 test_hex2bin (void)
37 static const char *valid[] = {
38 "00112233445566778899aabbccddeeff11223344",
39 "00112233445566778899AABBCCDDEEFF11223344",
40 "00112233445566778899AABBCCDDEEFF11223344 blah",
41 "00112233445566778899AABBCCDDEEFF11223344\tblah",
42 "00112233445566778899AABBCCDDEEFF11223344\nblah",
43 NULL
45 static const char *invalid[] = {
46 "00112233445566778899aabbccddeeff1122334",
47 "00112233445566778899AABBCCDDEEFF1122334",
48 "00112233445566778899AABBCCDDEEFG11223344",
49 "00 112233445566778899aabbccddeeff11223344",
50 "00:112233445566778899aabbccddeeff11223344",
51 ":00112233445566778899aabbccddeeff11223344",
52 "0:0112233445566778899aabbccddeeff11223344",
53 "00112233445566778899aabbccddeeff11223344:",
54 "00112233445566778899aabbccddeeff112233445",
55 "00112233445566778899aabbccddeeff1122334455",
56 "00112233445566778899aabbccddeeff11223344blah",
57 NULL
59 static const char *valid2[] = {
60 "00",
61 "00 x",
62 NULL
64 static const char *invalid2[] = {
65 "",
66 "0",
67 "00:",
68 "00x",
69 " 00",
70 NULL
72 unsigned char buffer[20];
73 int len;
74 int i;
77 for (i=0; valid[i]; i++)
79 len = hex2bin (valid[i], buffer, sizeof buffer);
80 if (len < 0)
81 fail (i);
82 if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
83 "\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
84 fail (i);
86 if (hex2bin (valid[0], buffer, sizeof buffer) != 40)
87 fail (0);
88 if (hex2bin (valid[2], buffer, sizeof buffer) != 41)
89 fail (0);
91 for (i=0; invalid[i]; i++)
93 len = hex2bin (invalid[i], buffer, sizeof buffer);
94 if (!(len < 0))
95 fail (i);
98 for (i=0; valid2[i]; i++)
100 len = hex2bin (valid2[i], buffer, 1);
101 if (len < 0)
102 fail (i);
103 if (memcmp (buffer, "\x00", 1))
104 fail (i);
106 if (hex2bin (valid2[0], buffer, 1) != 2)
107 fail (0);
108 if (hex2bin (valid2[1], buffer, 1) != 3)
109 fail (0);
111 for (i=0; invalid2[i]; i++)
113 len = hex2bin (invalid2[i], buffer, 1);
114 if (!(len < 0))
115 fail (i);
121 static void
122 test_hexcolon2bin (void)
124 static const char *valid[] = {
125 "00112233445566778899aabbccddeeff11223344",
126 "00112233445566778899AABBCCDDEEFF11223344",
127 "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
128 "00112233445566778899AABBCCDDEEFF11223344 blah",
129 "00112233445566778899AABBCCDDEEFF11223344\tblah",
130 "00112233445566778899AABBCCDDEEFF11223344\nblah",
131 NULL
133 static const char *invalid[] = {
134 "00112233445566778899aabbccddeeff1122334",
135 "00112233445566778899AABBCCDDEEFF1122334",
136 "00112233445566778899AABBCCDDEEFG11223344",
137 ":00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
138 "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44:",
139 "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:3344",
140 "00:1122:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
141 "0011:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
142 "00 11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
143 "00:11 22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
144 "00112233445566778899aabbccddeeff112233445",
145 "00112233445566778899aabbccddeeff1122334455",
146 "00112233445566778899aabbccddeeff11223344blah",
147 NULL
149 static const char *valid2[] = {
150 "00",
151 "00 x",
152 NULL
154 static const char *invalid2[] = {
156 "0",
157 "00:",
158 ":00",
159 "0:0",
160 "00x",
161 " 00",
162 NULL
164 unsigned char buffer[20];
165 int len;
166 int i;
169 for (i=0; valid[i]; i++)
171 len = hexcolon2bin (valid[i], buffer, sizeof buffer);
172 if (len < 0)
173 fail (i);
174 if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
175 "\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
176 fail (i);
178 if (hexcolon2bin (valid[0], buffer, sizeof buffer) != 40)
179 fail (0);
180 if (hexcolon2bin (valid[3], buffer, sizeof buffer) != 41)
181 fail (0);
183 for (i=0; invalid[i]; i++)
185 len = hexcolon2bin (invalid[i], buffer, sizeof buffer);
186 if (!(len < 0))
187 fail (i);
190 for (i=0; valid2[i]; i++)
192 len = hexcolon2bin (valid2[i], buffer, 1);
193 if (len < 0)
194 fail (i);
195 if (memcmp (buffer, "\x00", 1))
196 fail (i);
198 if (hexcolon2bin (valid2[0], buffer, 1) != 2)
199 fail (0);
200 if (hexcolon2bin (valid2[1], buffer, 1) != 3)
201 fail (0);
203 for (i=0; invalid2[i]; i++)
205 len = hexcolon2bin (invalid2[i], buffer, 1);
206 if (!(len < 0))
207 fail (i);
215 static void
216 test_bin2hex (void)
218 char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
219 "\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
220 char hexstuff[] = "00112233445566778899AABBCCDDEEFF011002A3";
221 char buffer[2*20+1];
222 char *p;
224 p = bin2hex (stuff, 20, buffer);
225 if (!p)
226 fail (0);
227 if (p != buffer)
228 fail (0);
229 if (strcmp (buffer, hexstuff))
230 fail (0);
232 p = bin2hex (stuff, 20, NULL);
233 if (!p)
234 fail (0);
235 if (strcmp (p, hexstuff))
236 fail (0);
238 p = bin2hex (stuff, (size_t)(-1), NULL);
239 if (p)
240 fail (0);
241 if (errno != ENOMEM)
242 fail (1);
246 static void
247 test_bin2hexcolon (void)
249 char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
250 "\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
251 char hexstuff[] = ("00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF"
252 ":01:10:02:A3");
253 char buffer[3*20+1];
254 char *p;
256 p = bin2hexcolon (stuff, 20, buffer);
257 if (!p)
258 fail (0);
259 if (p != buffer)
260 fail (0);
261 if (strcmp (buffer, hexstuff))
262 fail (0);
264 p = bin2hexcolon (stuff, 20, NULL);
265 if (!p)
266 fail (0);
267 if (strcmp (p, hexstuff))
268 fail (0);
270 p = bin2hexcolon (stuff, (size_t)(-1), NULL);
271 if (p)
272 fail (0);
273 if (errno != ENOMEM)
274 fail (1);
279 static void
280 test_hex2str (void)
282 static struct {
283 const char *hex;
284 const char *str;
285 int off;
286 int no_alloc_test;
287 } tests[] = {
288 /* Simple tests. */
289 { "112233445566778899aabbccddeeff1122",
290 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
291 34 },
292 { "112233445566778899aabbccddeeff1122 blah",
293 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
294 34 },
295 { "112233445566778899aabbccddeeff1122\tblah",
296 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
297 34 },
298 { "112233445566778899aabbccddeeff1122\nblah",
299 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
300 34 },
301 /* Valid tests yielding an empty string. */
302 { "00",
304 2 },
305 { "00 x",
307 2 },
308 { "",
310 0 },
311 { " ",
313 0 },
314 /* Test trailing Nul feature. */
315 { "112233445566778899aabbccddeeff112200",
316 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
317 36 },
318 { "112233445566778899aabbccddeeff112200 ",
319 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
320 36 },
321 /* Test buffer size. (buffer is of length 20) */
322 { "6162636465666768696A6b6c6D6e6f70717273",
323 "abcdefghijklmnopqrs",
324 38 },
325 { "6162636465666768696A6b6c6D6e6f7071727300",
326 "abcdefghijklmnopqrs",
327 40 },
328 { "6162636465666768696A6b6c6D6e6f7071727374",
329 NULL,
330 0, 1 },
331 { "6162636465666768696A6b6c6D6e6f707172737400",
332 NULL,
333 0, 1 },
334 { "6162636465666768696A6b6c6D6e6f707172737475",
335 NULL,
336 0, 1 },
338 /* Invalid tests. */
339 { "112233445566778899aabbccddeeff1122334", NULL, 0 },
340 { "112233445566778899AABBCCDDEEFF1122334", NULL, 0 },
341 { "112233445566778899AABBCCDDEEFG11223344", NULL, 0 },
342 { "0:0112233445566778899aabbccddeeff11223344", NULL, 0 },
343 { "112233445566778899aabbccddeeff11223344:", NULL, 0 },
344 { "112233445566778899aabbccddeeff112233445", NULL, 0 },
345 { "112233445566778899aabbccddeeff1122334455", NULL, 0, 1 },
346 { "112233445566778899aabbccddeeff11223344blah", NULL, 0 },
347 { "0", NULL, 0 },
348 { "00:", NULL, 0 },
349 { "00x", NULL, 0 },
351 { NULL, NULL, 0 }
354 int idx;
355 char buffer[20];
356 const char *tail;
357 size_t count;
358 char *result;
360 for (idx=0; tests[idx].hex; idx++)
362 tail = hex2str (tests[idx].hex, buffer, sizeof buffer, &count);
363 if (tests[idx].str)
365 /* Good case test. */
366 if (!tail)
367 fail (idx);
368 else if (strcmp (tests[idx].str, buffer))
369 fail (idx);
370 else if (tail - tests[idx].hex != tests[idx].off)
371 fail (idx);
372 else if (strlen (buffer) != count)
373 fail (idx);
375 else
377 /* Bad case test. */
378 if (tail)
379 fail (idx);
383 /* Same tests again using in-place conversion. */
384 for (idx=0; tests[idx].hex; idx++)
386 char tmpbuf[100];
388 assert (strlen (tests[idx].hex)+1 < sizeof tmpbuf);
389 strcpy (tmpbuf, tests[idx].hex);
391 /* Note: we still need to use 20 as buffer length because our
392 tests assume that. */
393 tail = hex2str (tmpbuf, tmpbuf, 20, &count);
394 if (tests[idx].str)
396 /* Good case test. */
397 if (!tail)
398 fail (idx);
399 else if (strcmp (tests[idx].str, tmpbuf))
400 fail (idx);
401 else if (tail - tmpbuf != tests[idx].off)
402 fail (idx);
403 else if (strlen (tmpbuf) != count)
404 fail (idx);
406 else
408 /* Bad case test. */
409 if (tail)
410 fail (idx);
411 if (strcmp (tmpbuf, tests[idx].hex))
412 fail (idx); /* Buffer was modified. */
416 /* Test the allocation variant. */
417 for (idx=0; tests[idx].hex; idx++)
419 if (tests[idx].no_alloc_test)
420 continue;
422 result = hex2str_alloc (tests[idx].hex, &count);
423 if (tests[idx].str)
425 /* Good case test. */
426 if (!result)
427 fail (idx);
428 else if (strcmp (tests[idx].str, result))
429 fail (idx);
430 else if (count != tests[idx].off)
431 fail (idx);
433 else
435 /* Bad case test. */
436 if (result)
437 fail (idx);
439 xfree (result);
448 main (int argc, char **argv)
450 (void)argc;
451 (void)argv;
453 test_hex2bin ();
454 test_hexcolon2bin ();
455 test_bin2hex ();
456 test_bin2hexcolon ();
457 test_hex2str ();
459 return 0;