-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_json_utils.c
87 lines (69 loc) · 3.31 KB
/
test_json_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include "json_utils.h"
#define IPV6_REGEX "^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|(([0-9A-Fa-f]{1,4}:){0,5}:((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|(::([0-9A-Fa-f]{1,4}:){0,5}((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$"
#define IPV4_REGEX "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
int main()
{
/*
const char *to_test[] = {
"{ \"key\": \"labbcc\", \"key2\": [ \"cc\", \"aa\", \"bb\" ]}",
"{ \"key\": \"pabbcc\", \"key2\": [ 2 ]}",
"{ \"key\": \"babbcc\", \"key2\": [ ]}",
"{ \"key\": \"aabbcc\", \"key2\": [ { \"key3\": \"zz\" ]}",
"{ \"not_the_key\": \"aabbcc\" }",
"{ \"key\": 3 }",
"",
NULL,
};
const char *trusted = "{\"key\": \"[[:alpha:]]\", \"key2\": [\"[[:alpha:]]\"]}";
*/
const char *to_test[] = {
"{ \"options\": { \"IPv4\": { \"Address\": \"12.12.12.12\" } } }",
"{ \"options\": { \"IPv4\": { \"Address\": \"999.12.12.12\" } } }",
"{ \"options\": { \"IPv4\": { \"Address\": \"...\" } } }",
"{ \"options\": { \"IPv6\": { \"Address\": \"2001:41D0:1:2E4e::1\" } } }",
"{ \"options\": { \"IPv6\": { \"Address\": \"2001::1:2E4e::1\" } } }",
"{ \"options\": { \"IPv6\": { \"Address\": \"::1:::2\" } } }",
NULL,
};
/*
const char *trusted = "{ \"service\": \"wifi_8888_8888_none\", \
\"options\": { \
\"IPv4\": { \
\"Address\": \"([[:digit:]]{1,3}\\.){3}\\.[[:digit:]]{1,3}\" \
} \
}";
*/
struct json_object *jtmp, *jtrusted, *jsubtrusted, *jsubtrusted2, *jsubtrusted3;
int i;
printf("\n[*] start\n");
//jtrusted = json_tokener_parse_ex(tok, trusted, strlen(trusted));
//printf("\n[*] jtok res: %s\n", json_tokener_error_desc(json_tokener_get_error(tok)));
jtrusted = json_object_new_object();
jsubtrusted = json_object_new_object();
jsubtrusted2 = json_object_new_object();
jsubtrusted3 = json_object_new_object();
json_object_object_add(jsubtrusted2, "Address", json_object_new_string(IPV4_REGEX));
json_object_object_add(jsubtrusted, "IPv4", jsubtrusted2);
json_object_object_add(jsubtrusted3, "Address", json_object_new_string(IPV6_REGEX));
json_object_object_add(jsubtrusted, "IPv6", jsubtrusted3);
json_object_object_add(jtrusted, key_options, jsubtrusted);
for (i = 0; to_test[i]; i++) {
jtmp = json_tokener_parse(to_test[i]);
printf("\n[*] test %d ... ", i);
if (__json_type_dispatch(jtmp, jtrusted))
printf("PASSED");
else
printf("FAILED");
printf("\n---\n%s\n---\n", to_test[i]);
json_object_put(jtmp);
jtmp = NULL;
}
printf("\n[*] the end.\n");
json_object_put(jtrusted);
return 0;
}