Line data Source code
1 : /* t-convert.c - Module test for convert.c
2 : * Copyright (C) 2006, 2008 Free Software Foundation, Inc.
3 : *
4 : * This file is part of GnuPG.
5 : *
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.
10 : *
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.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <config.h>
21 : #include <stdio.h>
22 : #include <stdlib.h>
23 : #include <assert.h>
24 :
25 : #include "util.h"
26 :
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)
32 :
33 :
34 : static void
35 1 : test_hex2bin (void)
36 : {
37 : static const char *valid[] = {
38 : "00112233445566778899aabbccddeeff11223344",
39 : "00112233445566778899AABBCCDDEEFF11223344",
40 : "00112233445566778899AABBCCDDEEFF11223344 blah",
41 : "00112233445566778899AABBCCDDEEFF11223344\tblah",
42 : "00112233445566778899AABBCCDDEEFF11223344\nblah",
43 : NULL
44 : };
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
58 : };
59 : static const char *valid2[] = {
60 : "00",
61 : "00 x",
62 : NULL
63 : };
64 : static const char *invalid2[] = {
65 : "",
66 : "0",
67 : "00:",
68 : "00x",
69 : " 00",
70 : NULL
71 : };
72 : unsigned char buffer[20];
73 : int len;
74 : int i;
75 :
76 :
77 6 : for (i=0; valid[i]; i++)
78 : {
79 5 : len = hex2bin (valid[i], buffer, sizeof buffer);
80 5 : if (len < 0)
81 0 : fail (i);
82 5 : 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 0 : fail (i);
85 : }
86 1 : if (hex2bin (valid[0], buffer, sizeof buffer) != 40)
87 0 : fail (0);
88 1 : if (hex2bin (valid[2], buffer, sizeof buffer) != 41)
89 0 : fail (0);
90 :
91 12 : for (i=0; invalid[i]; i++)
92 : {
93 11 : len = hex2bin (invalid[i], buffer, sizeof buffer);
94 11 : if (!(len < 0))
95 0 : fail (i);
96 : }
97 :
98 3 : for (i=0; valid2[i]; i++)
99 : {
100 2 : len = hex2bin (valid2[i], buffer, 1);
101 2 : if (len < 0)
102 0 : fail (i);
103 2 : if (memcmp (buffer, "\x00", 1))
104 0 : fail (i);
105 : }
106 1 : if (hex2bin (valid2[0], buffer, 1) != 2)
107 0 : fail (0);
108 1 : if (hex2bin (valid2[1], buffer, 1) != 3)
109 0 : fail (0);
110 :
111 6 : for (i=0; invalid2[i]; i++)
112 : {
113 5 : len = hex2bin (invalid2[i], buffer, 1);
114 5 : if (!(len < 0))
115 0 : fail (i);
116 : }
117 1 : }
118 :
119 :
120 :
121 : static void
122 1 : test_hexcolon2bin (void)
123 : {
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
132 : };
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
148 : };
149 : static const char *valid2[] = {
150 : "00",
151 : "00 x",
152 : NULL
153 : };
154 : static const char *invalid2[] = {
155 : "",
156 : "0",
157 : "00:",
158 : ":00",
159 : "0:0",
160 : "00x",
161 : " 00",
162 : NULL
163 : };
164 : unsigned char buffer[20];
165 : int len;
166 : int i;
167 :
168 :
169 7 : for (i=0; valid[i]; i++)
170 : {
171 6 : len = hexcolon2bin (valid[i], buffer, sizeof buffer);
172 6 : if (len < 0)
173 0 : fail (i);
174 6 : 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 0 : fail (i);
177 : }
178 1 : if (hexcolon2bin (valid[0], buffer, sizeof buffer) != 40)
179 0 : fail (0);
180 1 : if (hexcolon2bin (valid[3], buffer, sizeof buffer) != 41)
181 0 : fail (0);
182 :
183 14 : for (i=0; invalid[i]; i++)
184 : {
185 13 : len = hexcolon2bin (invalid[i], buffer, sizeof buffer);
186 13 : if (!(len < 0))
187 0 : fail (i);
188 : }
189 :
190 3 : for (i=0; valid2[i]; i++)
191 : {
192 2 : len = hexcolon2bin (valid2[i], buffer, 1);
193 2 : if (len < 0)
194 0 : fail (i);
195 2 : if (memcmp (buffer, "\x00", 1))
196 0 : fail (i);
197 : }
198 1 : if (hexcolon2bin (valid2[0], buffer, 1) != 2)
199 0 : fail (0);
200 1 : if (hexcolon2bin (valid2[1], buffer, 1) != 3)
201 0 : fail (0);
202 :
203 8 : for (i=0; invalid2[i]; i++)
204 : {
205 7 : len = hexcolon2bin (invalid2[i], buffer, 1);
206 7 : if (!(len < 0))
207 0 : fail (i);
208 : }
209 :
210 :
211 1 : }
212 :
213 :
214 :
215 : static void
216 1 : test_bin2hex (void)
217 : {
218 1 : 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 1 : char hexstuff[] = "00112233445566778899AABBCCDDEEFF011002A3";
221 : char buffer[2*20+1];
222 : char *p;
223 :
224 1 : p = bin2hex (stuff, 20, buffer);
225 1 : if (!p)
226 0 : fail (0);
227 1 : if (p != buffer)
228 0 : fail (0);
229 1 : if (strcmp (buffer, hexstuff))
230 0 : fail (0);
231 :
232 1 : p = bin2hex (stuff, 20, NULL);
233 1 : if (!p)
234 0 : fail (0);
235 1 : else if (strcmp (p, hexstuff))
236 0 : fail (0);
237 1 : xfree (p);
238 :
239 1 : p = bin2hex (stuff, (size_t)(-1), NULL);
240 1 : if (p)
241 0 : fail (0);
242 1 : else if (errno != ENOMEM)
243 0 : fail (1);
244 1 : }
245 :
246 :
247 : static void
248 1 : test_bin2hexcolon (void)
249 : {
250 1 : char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
251 : "\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
252 1 : char hexstuff[] = ("00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF"
253 : ":01:10:02:A3");
254 : char buffer[3*20+1];
255 : char *p;
256 :
257 1 : p = bin2hexcolon (stuff, 20, buffer);
258 1 : if (!p)
259 0 : fail (0);
260 1 : if (p != buffer)
261 0 : fail (0);
262 1 : if (strcmp (buffer, hexstuff))
263 0 : fail (0);
264 :
265 1 : p = bin2hexcolon (stuff, 20, NULL);
266 1 : if (!p)
267 0 : fail (0);
268 1 : else if (strcmp (p, hexstuff))
269 0 : fail (0);
270 1 : xfree (p);
271 :
272 1 : p = bin2hexcolon (stuff, (size_t)(-1), NULL);
273 1 : if (p)
274 0 : fail (0);
275 1 : else if (errno != ENOMEM)
276 0 : fail (1);
277 1 : }
278 :
279 :
280 :
281 : static void
282 1 : test_hex2str (void)
283 : {
284 : static struct {
285 : const char *hex;
286 : const char *str;
287 : int len; /* Length of STR. This may included embedded nuls. */
288 : int off;
289 : int no_alloc_test;
290 : } tests[] = {
291 : /* Simple tests. */
292 : { "112233445566778899aabbccddeeff1122",
293 : "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
294 : 17, 34 },
295 : { "112233445566778899aabbccddeeff1122 blah",
296 : "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
297 : 17, 34 },
298 : { "112233445566778899aabbccddeeff1122\tblah",
299 : "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
300 : 17, 34 },
301 : { "112233445566778899aabbccddeeff1122\nblah",
302 : "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
303 : 17, 34 },
304 : /* Valid tests yielding an empty string. */
305 : { "00",
306 : "",
307 : 1, 2 },
308 : { "00 x",
309 : "",
310 : 1, 2 },
311 : { "",
312 : "",
313 : 0, 0 },
314 : { " ",
315 : "",
316 : 0, 0 },
317 : /* Test trailing Nul feature. */
318 : { "112233445566778899aabbccddeeff1100",
319 : "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00",
320 : 17, 34 },
321 : { "112233445566778899aabbccddeeff1100 ",
322 : "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00",
323 : 17, 34 },
324 : /* Test buffer size. (buffer is of length 20) */
325 : { "6162636465666768696A6b6c6D6e6f70717273",
326 : "abcdefghijklmnopqrs",
327 : 19, 38 },
328 : { "6162636465666768696A6b6c6D6e6f7071727300",
329 : "abcdefghijklmnopqrs",
330 : 20, 40 },
331 : { "6162636465666768696A6b6c6D6e6f7071727374",
332 : NULL,
333 : 0, 0, 1 },
334 : { "6162636465666768696A6b6c6D6e6f707172737400",
335 : NULL,
336 : 0, 0, 1 },
337 : { "6162636465666768696A6b6c6D6e6f707172737475",
338 : NULL,
339 : 0, 0, 1 },
340 :
341 : /* Invalid tests. */
342 : { "112233445566778899aabbccddeeff1122334", NULL, 0, 0 },
343 : { "112233445566778899AABBCCDDEEFF1122334", NULL, 0, 0 },
344 : { "112233445566778899AABBCCDDEEFG11223344", NULL, 0, 0 },
345 : { "0:0112233445566778899aabbccddeeff11223344", NULL, 0, 0 },
346 : { "112233445566778899aabbccddeeff11223344:", NULL, 0, 0 },
347 : { "112233445566778899aabbccddeeff112233445", NULL, 0, 0 },
348 : { "112233445566778899aabbccddeeff1122334455", NULL, 0, 0, 1 },
349 : { "112233445566778899aabbccddeeff11223344blah", NULL, 0, 0 },
350 : { "0", NULL, 0, 0 },
351 : { "00:", NULL, 0, 0 },
352 : { "00x", NULL, 0, 0 },
353 :
354 : { NULL, NULL, 0, 0 }
355 : };
356 :
357 : int idx;
358 : char buffer[20];
359 : const char *tail;
360 : size_t count;
361 : char *result;
362 :
363 27 : for (idx=0; tests[idx].hex; idx++)
364 : {
365 26 : tail = hex2str (tests[idx].hex, buffer, sizeof buffer, &count);
366 26 : if (tests[idx].str)
367 : {
368 : /* Good case test. */
369 12 : if (!tail)
370 0 : fail (idx);
371 12 : else if (strcmp (tests[idx].str, buffer))
372 0 : fail (idx);
373 12 : else if (tail - tests[idx].hex != tests[idx].off)
374 0 : fail (idx);
375 12 : else if (tests[idx].len != count)
376 0 : fail (idx);
377 : }
378 : else
379 : {
380 : /* Bad case test. */
381 14 : if (tail)
382 0 : fail (idx);
383 : }
384 : }
385 :
386 : /* Same tests again using in-place conversion. */
387 27 : for (idx=0; tests[idx].hex; idx++)
388 : {
389 : char tmpbuf[100];
390 :
391 26 : assert (strlen (tests[idx].hex)+1 < sizeof tmpbuf);
392 26 : strcpy (tmpbuf, tests[idx].hex);
393 :
394 : /* Note: we still need to use 20 as buffer length because our
395 : tests assume that. */
396 26 : tail = hex2str (tmpbuf, tmpbuf, 20, &count);
397 26 : if (tests[idx].str)
398 : {
399 : /* Good case test. */
400 12 : if (!tail)
401 0 : fail (idx);
402 12 : else if (strcmp (tests[idx].str, tmpbuf))
403 0 : fail (idx);
404 12 : else if (tail - tmpbuf != tests[idx].off)
405 0 : fail (idx);
406 12 : else if (tests[idx].len != count)
407 0 : fail (idx);
408 : }
409 : else
410 : {
411 : /* Bad case test. */
412 14 : if (tail)
413 0 : fail (idx);
414 14 : if (strcmp (tmpbuf, tests[idx].hex))
415 0 : fail (idx); /* Buffer was modified. */
416 : }
417 : }
418 :
419 : /* Test the allocation variant. */
420 27 : for (idx=0; tests[idx].hex; idx++)
421 : {
422 26 : if (tests[idx].no_alloc_test)
423 4 : continue;
424 :
425 22 : result = hex2str_alloc (tests[idx].hex, &count);
426 22 : if (tests[idx].str)
427 : {
428 : /* Good case test. */
429 12 : if (!result)
430 0 : fail (idx);
431 12 : else if (strcmp (tests[idx].str, result))
432 0 : fail (idx);
433 12 : else if (count != tests[idx].off)
434 0 : fail (idx);
435 : }
436 : else
437 : {
438 : /* Bad case test. */
439 10 : if (result)
440 0 : fail (idx);
441 : }
442 22 : xfree (result);
443 : }
444 1 : }
445 :
446 :
447 :
448 :
449 :
450 : int
451 1 : main (int argc, char **argv)
452 : {
453 : (void)argc;
454 : (void)argv;
455 :
456 1 : test_hex2bin ();
457 1 : test_hexcolon2bin ();
458 1 : test_bin2hex ();
459 1 : test_bin2hexcolon ();
460 1 : test_hex2str ();
461 :
462 1 : return 0;
463 : }
|