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/>.
27 #define pass() do { ; } while(0)
28 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
29 __FILE__,__LINE__, (a)); \
37 static const char *valid
[] = {
38 "00112233445566778899aabbccddeeff11223344",
39 "00112233445566778899AABBCCDDEEFF11223344",
40 "00112233445566778899AABBCCDDEEFF11223344 blah",
41 "00112233445566778899AABBCCDDEEFF11223344\tblah",
42 "00112233445566778899AABBCCDDEEFF11223344\nblah",
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",
59 static const char *valid2
[] = {
64 static const char *invalid2
[] = {
72 unsigned char buffer
[20];
77 for (i
=0; valid
[i
]; i
++)
79 len
= hex2bin (valid
[i
], buffer
, sizeof buffer
);
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))
86 if (hex2bin (valid
[0], buffer
, sizeof buffer
) != 40)
88 if (hex2bin (valid
[2], buffer
, sizeof buffer
) != 41)
91 for (i
=0; invalid
[i
]; i
++)
93 len
= hex2bin (invalid
[i
], buffer
, sizeof buffer
);
98 for (i
=0; valid2
[i
]; i
++)
100 len
= hex2bin (valid2
[i
], buffer
, 1);
103 if (memcmp (buffer
, "\x00", 1))
106 if (hex2bin (valid2
[0], buffer
, 1) != 2)
108 if (hex2bin (valid2
[1], buffer
, 1) != 3)
111 for (i
=0; invalid2
[i
]; i
++)
113 len
= hex2bin (invalid2
[i
], buffer
, 1);
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",
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",
149 static const char *valid2
[] = {
154 static const char *invalid2
[] = {
164 unsigned char buffer
[20];
169 for (i
=0; valid
[i
]; i
++)
171 len
= hexcolon2bin (valid
[i
], buffer
, sizeof buffer
);
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))
178 if (hexcolon2bin (valid
[0], buffer
, sizeof buffer
) != 40)
180 if (hexcolon2bin (valid
[3], buffer
, sizeof buffer
) != 41)
183 for (i
=0; invalid
[i
]; i
++)
185 len
= hexcolon2bin (invalid
[i
], buffer
, sizeof buffer
);
190 for (i
=0; valid2
[i
]; i
++)
192 len
= hexcolon2bin (valid2
[i
], buffer
, 1);
195 if (memcmp (buffer
, "\x00", 1))
198 if (hexcolon2bin (valid2
[0], buffer
, 1) != 2)
200 if (hexcolon2bin (valid2
[1], buffer
, 1) != 3)
203 for (i
=0; invalid2
[i
]; i
++)
205 len
= hexcolon2bin (invalid2
[i
], buffer
, 1);
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";
224 p
= bin2hex (stuff
, 20, buffer
);
229 if (strcmp (buffer
, hexstuff
))
232 p
= bin2hex (stuff
, 20, NULL
);
235 if (strcmp (p
, hexstuff
))
238 p
= bin2hex (stuff
, (size_t)(-1), NULL
);
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"
256 p
= bin2hexcolon (stuff
, 20, buffer
);
261 if (strcmp (buffer
, hexstuff
))
264 p
= bin2hexcolon (stuff
, 20, NULL
);
267 if (strcmp (p
, hexstuff
))
270 p
= bin2hexcolon (stuff
, (size_t)(-1), NULL
);
289 { "112233445566778899aabbccddeeff1122",
290 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
292 { "112233445566778899aabbccddeeff1122 blah",
293 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
295 { "112233445566778899aabbccddeeff1122\tblah",
296 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
298 { "112233445566778899aabbccddeeff1122\nblah",
299 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
301 /* Valid tests yielding an empty string. */
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",
318 { "112233445566778899aabbccddeeff112200 ",
319 "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
321 /* Test buffer size. (buffer is of length 20) */
322 { "6162636465666768696A6b6c6D6e6f70717273",
323 "abcdefghijklmnopqrs",
325 { "6162636465666768696A6b6c6D6e6f7071727300",
326 "abcdefghijklmnopqrs",
328 { "6162636465666768696A6b6c6D6e6f7071727374",
331 { "6162636465666768696A6b6c6D6e6f707172737400",
334 { "6162636465666768696A6b6c6D6e6f707172737475",
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 },
360 for (idx
=0; tests
[idx
].hex
; idx
++)
362 tail
= hex2str (tests
[idx
].hex
, buffer
, sizeof buffer
, &count
);
365 /* Good case test. */
368 else if (strcmp (tests
[idx
].str
, buffer
))
370 else if (tail
- tests
[idx
].hex
!= tests
[idx
].off
)
372 else if (strlen (buffer
) != count
)
383 /* Same tests again using in-place conversion. */
384 for (idx
=0; tests
[idx
].hex
; idx
++)
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
);
396 /* Good case test. */
399 else if (strcmp (tests
[idx
].str
, tmpbuf
))
401 else if (tail
- tmpbuf
!= tests
[idx
].off
)
403 else if (strlen (tmpbuf
) != count
)
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
)
422 result
= hex2str_alloc (tests
[idx
].hex
, &count
);
425 /* Good case test. */
428 else if (strcmp (tests
[idx
].str
, result
))
430 else if (count
!= tests
[idx
].off
)
448 main (int argc
, char **argv
)
452 test_hexcolon2bin ();
454 test_bin2hexcolon ();