wpa_supplicant: Reschedule sched scan after network change
[mech_eap.git] / tests / test-printf.c
1 /*
2  * printf format routines - test program
3  * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10 #include "utils/os.h"
11 #include "utils/common.h"
12
13
14 struct test_data {
15         u8 *data;
16         size_t len;
17         char *encoded;
18 };
19
20 static const struct test_data tests[] = {
21         { (u8 *) "abcde", 5, "abcde" },
22         { (u8 *) "a\0b\nc\ed\re\tf", 11, "a\\0b\\nc\\ed\\re\\tf" },
23         { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
24         { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
25         { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
26           "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
27         { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
28           "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
29         { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
30           "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
31         { NULL, 0, NULL }
32 };
33
34
35 static void print_hex(const u8 *data, size_t len)
36 {
37         size_t i;
38         for (i = 0; i < len; i++)
39                 printf(" %02x", data[i]);
40 }
41
42
43 int main(int argc, char *argv[])
44 {
45         int i;
46         size_t binlen;
47         char buf[100];
48         u8 bin[100];
49         int errors = 0;
50
51         for (i = 0; tests[i].data; i++) {
52                 const struct test_data *test = &tests[i];
53                 printf("%d:", i);
54                 print_hex(test->data, test->len);
55                 printf_encode(buf, sizeof(buf), test->data, test->len);
56                 printf(" -> \"%s\"\n", buf);
57
58                 binlen = printf_decode(bin, sizeof(bin), buf);
59                 if (binlen != test->len ||
60                     os_memcmp(bin, test->data, binlen) != 0) {
61                         printf("Error in decoding#1:");
62                         print_hex(bin, binlen);
63                         printf("\n");
64                         errors++;
65                 }
66
67                 binlen = printf_decode(bin, sizeof(bin), test->encoded);
68                 if (binlen != test->len ||
69                     os_memcmp(bin, test->data, binlen) != 0) {
70                         printf("Error in decoding#2:");
71                         print_hex(bin, binlen);
72                         printf("\n");
73                         errors++;
74                 }
75         }
76
77         if (errors) {
78                 printf("%d test(s) failed\n", errors);
79                 return -1;
80         }
81
82         return 0;
83 }