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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/***
This file is part of PulseAudio.
Copyright 2016 Arun Raghavan <mail@arunraghavan.net>
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <check.h>
#include <pulse/json.h>
#include <pulsecore/core-util.h>
START_TEST (string_test) {
pa_json_object *o;
unsigned int i;
const char *strings_parse[] = {
"\"\"", "\"test\"", "\"test123\"", "\"123\"", "\"newline\\n\"", "\" spaces \"",
" \"lots of spaces\" ", "\"esc\\nape\"", "\"escape a \\\" quote\"",
};
const char *strings_compare[] = {
"", "test", "test123", "123", "newline\n", " spaces ",
"lots of spaces", "esc\nape", "escape a \" quote",
};
for (i = 0; i < PA_ELEMENTSOF(strings_parse); i++) {
o = pa_json_parse(strings_parse[i]);
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_STRING);
fail_unless(pa_streq(pa_json_object_get_string(o), strings_compare[i]));
pa_json_object_free(o);
}
}
END_TEST
START_TEST(int_test) {
pa_json_object *o;
unsigned int i;
const char *ints_parse[] = { "1", "-1", "1234", "0" };
const int ints_compare[] = { 1, -1, 1234, 0 };
for (i = 0; i < PA_ELEMENTSOF(ints_parse); i++) {
o = pa_json_parse(ints_parse[i]);
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_INT);
fail_unless(pa_json_object_get_int(o) == ints_compare[i]);
pa_json_object_free(o);
}
}
END_TEST
START_TEST(double_test) {
pa_json_object *o;
unsigned int i;
const char *doubles_parse[] = {
"1.0", "-1.1", "1234e2", "1234e0", "0.1234", "-0.1234", "1234e-1", "1234.5e-1", "1234.5e+2",
};
const double doubles_compare[] = {
1.0, -1.1, 123400.0, 1234.0, 0.1234, -0.1234, 123.4, 123.45, 123450.0,
};
for (i = 0; i < PA_ELEMENTSOF(doubles_parse); i++) {
o = pa_json_parse(doubles_parse[i]);
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_DOUBLE);
fail_unless(PA_DOUBLE_IS_EQUAL(pa_json_object_get_double(o), doubles_compare[i]));
pa_json_object_free(o);
}
}
END_TEST
START_TEST(null_test) {
pa_json_object *o;
o = pa_json_parse("null");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_NULL);
pa_json_object_free(o);
}
END_TEST
START_TEST(bool_test) {
pa_json_object *o;
o = pa_json_parse("true");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_BOOL);
fail_unless(pa_json_object_get_bool(o) == true);
pa_json_object_free(o);
o = pa_json_parse("false");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_BOOL);
fail_unless(pa_json_object_get_bool(o) == false);
pa_json_object_free(o);
}
END_TEST
START_TEST(object_test) {
pa_json_object *o;
const pa_json_object *v;
o = pa_json_parse(" { \"name\" : \"A Person\" } ");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_OBJECT);
v = pa_json_object_get_object_member(o, "name");
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_STRING);
fail_unless(pa_streq(pa_json_object_get_string(v), "A Person"));
pa_json_object_free(o);
o = pa_json_parse(" { \"age\" : -45.3e-0 } ");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_OBJECT);
v = pa_json_object_get_object_member(o, "age");
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_DOUBLE);
fail_unless(PA_DOUBLE_IS_EQUAL(pa_json_object_get_double(v), -45.3));
pa_json_object_free(o);
o = pa_json_parse("{\"person\":true}");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_OBJECT);
v = pa_json_object_get_object_member(o, "person");
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_BOOL);
fail_unless(pa_json_object_get_bool(v) == true);
pa_json_object_free(o);
o = pa_json_parse("{ \"parent\": { \"child\": false } }");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_OBJECT);
v = pa_json_object_get_object_member(o, "parent");
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_OBJECT);
v = pa_json_object_get_object_member(v, "child");
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_BOOL);
fail_unless(pa_json_object_get_bool(v) == false);
pa_json_object_free(o);
}
END_TEST
START_TEST(array_test) {
pa_json_object *o;
const pa_json_object *v, *v2;
o = pa_json_parse(" [ ] ");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_ARRAY);
fail_unless(pa_json_object_get_array_length(o) == 0);
pa_json_object_free(o);
o = pa_json_parse("[\"a member\"]");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_ARRAY);
fail_unless(pa_json_object_get_array_length(o) == 1);
v = pa_json_object_get_array_member(o, 0);
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_STRING);
fail_unless(pa_streq(pa_json_object_get_string(v), "a member"));
pa_json_object_free(o);
o = pa_json_parse("[\"a member\", 1234.5, { \"another\": true } ]");
fail_unless(o != NULL);
fail_unless(pa_json_object_get_type(o) == PA_JSON_TYPE_ARRAY);
fail_unless(pa_json_object_get_array_length(o) == 3);
v = pa_json_object_get_array_member(o, 0);
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_STRING);
fail_unless(pa_streq(pa_json_object_get_string(v), "a member"));
v = pa_json_object_get_array_member(o, 1);
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_DOUBLE);
fail_unless(PA_DOUBLE_IS_EQUAL(pa_json_object_get_double(v), 1234.5));
v = pa_json_object_get_array_member(o, 2);
fail_unless(v != NULL);
fail_unless(pa_json_object_get_type(v) == PA_JSON_TYPE_OBJECT);
v2 =pa_json_object_get_object_member(v, "another");
fail_unless(v2 != NULL);
fail_unless(pa_json_object_get_type(v2) == PA_JSON_TYPE_BOOL);
fail_unless(pa_json_object_get_bool(v2) == true);
pa_json_object_free(o);
}
END_TEST
START_TEST(bad_test) {
unsigned int i;
const char *bad_parse[] = {
"\"" /* Quote not closed */,
"123456789012345678901234567890" /* Overflow */,
"0.123456789012345678901234567890" /* Overflow */,
"1e123456789012345678901234567890" /* Overflow */,
"1e" /* Bad number string */,
"1." /* Bad number string */,
"1.e3" /* Bad number string */,
"-" /* Bad number string */,
"{ \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { \"a\": { } } } } } } } } } } } } } } } } } } } } } }" /* Nested too deep */,
"[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ { \"a\": \"b\" } ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ]" /* Nested too deep */,
"asdf" /* Unquoted string */,
"{ a: true }" /* Unquoted key in object */,
"\" \a\"" /* Alarm is not a valid character */
};
for (i = 0; i < PA_ELEMENTSOF(bad_parse); i++) {
pa_json_object *obj;
fail_unless((obj = pa_json_parse(bad_parse[i])) == NULL);
if (obj)
pa_json_object_free(obj);
}
}
END_TEST
int main(int argc, char *argv[]) {
int failed = 0;
Suite *s;
TCase *tc;
SRunner *sr;
s = suite_create("JSON");
tc = tcase_create("json");
tcase_add_test(tc, string_test);
tcase_add_test(tc, int_test);
tcase_add_test(tc, double_test);
tcase_add_test(tc, null_test);
tcase_add_test(tc, bool_test);
tcase_add_test(tc, object_test);
tcase_add_test(tc, array_test);
tcase_add_test(tc, bad_test);
suite_add_tcase(s, tc);
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|