]>
Commit | Line | Data |
---|---|---|
79fa1b45 JM |
1 | /* |
2 | * JSON parser - test program | |
3 | * Copyright (c) 2019, 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" | |
f3e67159 | 10 | #include "utils/common.h" |
79fa1b45 JM |
11 | #include "utils/os.h" |
12 | #include "utils/json.h" | |
f3e67159 | 13 | #include "utils/wpa_debug.h" |
79fa1b45 JM |
14 | |
15 | ||
f3e67159 JM |
16 | void run_test(const char *buf, size_t len) |
17 | { | |
18 | struct json_token *root; | |
19 | char *txt; | |
20 | size_t buflen = 10000; | |
21 | ||
22 | root = json_parse(buf, len); | |
23 | if (!root) { | |
24 | wpa_printf(MSG_DEBUG, "JSON parsing failed"); | |
25 | return; | |
26 | } | |
27 | ||
28 | txt = os_zalloc(buflen); | |
29 | if (txt) { | |
30 | json_print_tree(root, txt, buflen); | |
31 | wpa_printf(MSG_DEBUG, "%s", txt); | |
32 | os_free(txt); | |
33 | } | |
34 | json_free(root); | |
35 | } | |
36 | ||
37 | ||
38 | #ifdef TEST_LIBFUZZER | |
39 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) | |
40 | { | |
41 | run_test((const char *) data, size); | |
42 | return 0; | |
43 | } | |
44 | #else /* TEST_LIBFUZZER */ | |
79fa1b45 JM |
45 | int main(int argc, char *argv[]) |
46 | { | |
47 | char *buf; | |
48 | size_t len; | |
f3e67159 JM |
49 | |
50 | wpa_debug_level = 0; | |
79fa1b45 JM |
51 | |
52 | if (argc < 2) | |
53 | return -1; | |
54 | ||
55 | buf = os_readfile(argv[1], &len); | |
56 | if (!buf) | |
57 | return -1; | |
58 | ||
f3e67159 | 59 | run_test(buf, len); |
79fa1b45 | 60 | os_free(buf); |
79fa1b45 JM |
61 | |
62 | return 0; | |
63 | } | |
f3e67159 | 64 | #endif /* TEST_LIBFUZZER */ |