Newer
Older
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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <json-c/json.h>
#include <libwebsockets.h>
#include "api.h"
struct json_object *file_obj, *modify_obj;
static void test_cfg_parse_success(void **state)
{
(void) state; /* unused */
struct json_object *file = path_to_obj("/home/jakob/git/json-editor-api/test.json");
struct json_object *obj = json_object_new_object();
struct json_object *tot = json_object_new_object();
json_object_object_add(obj, "api", json_object_new_string("test2"));
json_object_object_add(tot, "nested", obj);
json_object_object_add(tot, "test", json_object_new_string("success"));
assert_int_equal(1, json_object_equal(file, tot));
}
static void test_cfg_parse_fail(void **state)
{
(void) state; /* unused */
struct json_object *obj = path_to_obj("NON_EXISTENT_FILE");
assert_null(obj);
if (obj)
json_object_put(obj);
}
static void test_build_from_scratch(void **state)
{
(void) state; /* unused */
struct json_object *file = path_to_obj("/home/jakob/git/json-editor-api/test.json");
struct json_object *jobj = NULL;
set_by_string(NULL, &jobj, "{ \"test\":\"success\", \"nested\": { \"api\":\"test2\"} }", json_type_object);
printf("file_obj=%s\n", json_object_get_string(file));
printf("modify_obj=%s\n", json_object_get_string(jobj));
assert_int_equal(1, json_object_equal(file, jobj));
}
static void test_json_add_object(void **state)
{
(void) state;
struct json_object *obj = json_object_new_object();
json_object_object_add(obj, "test2", json_object_new_string("success"));
json_object_object_add(file_obj, "string", obj);
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
//json_object_object_add(file_obj, "string", json_object_new_string("1"));
set_by_string("string", &modify_obj, "{\"test2\":\"success\"}", json_type_object);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_array(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
json_object_array_add(arr, json_object_new_int(1));
json_object_array_add(arr, json_object_new_int(2));
json_object_array_add(arr, json_object_new_int(3));
json_object_object_add(file_obj, "ints", arr);
//json_object_object_add(file_obj, "string", json_object_new_string("1"));
set_by_string("ints", &modify_obj, "[ 1, 2, 3 ]", json_type_array);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_multi_types(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
struct json_object *obj = json_object_new_object();
struct json_object *nested = json_object_new_object();
json_object_object_add(nested, "integer", json_object_new_int(1));
json_object_object_add(obj, "nested1", nested);
json_object_object_add(file_obj, "nested0", obj);
json_object_array_add(arr, json_object_new_int(1));
json_object_array_add(arr, json_object_new_int(2));
json_object_array_add(arr, json_object_new_int(3));
json_object_object_add(file_obj, "ints", arr);
json_object_object_add(file_obj, "string", json_object_new_string("1"));
json_object_object_add(file_obj, "integer", json_object_new_int(1));
set_by_string("nested0.nested1.integer", &modify_obj, "1", json_type_int);
set_by_string("ints", &modify_obj, "[ 1, 2, 3 ]", json_type_array);
set_by_string("string", &modify_obj, "1", json_type_string);
set_by_string("integer", &modify_obj, "1", json_type_int);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_multi_obj(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
struct json_object *obj = json_object_new_object();
struct json_object *nested = json_object_new_object();
json_object_object_add(nested, "integer", json_object_new_int(1));
json_object_object_add(obj, "nested1", nested);
json_object_object_add(file_obj, "nested0", obj);
json_object_array_add(arr, json_object_new_int(1));
json_object_array_add(arr, json_object_new_int(2));
json_object_array_add(arr, json_object_new_int(3));
json_object_object_add(file_obj, "ints", arr);
json_object_object_add(file_obj, "string", json_object_new_string("1"));
json_object_object_add(file_obj, "integer", json_object_new_int(1));
set_by_string(NULL, &modify_obj, "{ \"nested0\": {\"nested1\": {\"integer\": 1}}, \"ints\": [1, 2, 3], \"string\":\"1\", \"integer\": 1}", json_type_object);
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
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_string(void **state)
{
(void) state;
//struct json_object *obj = json_object_new_object();
json_object_object_add(file_obj, "string", json_object_new_string("1"));
set_by_string("string", &modify_obj, "1", json_type_string);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_overwrite_string(void **state)
{
(void) state;
//struct json_object *obj = json_object_new_object();
json_object_object_add(file_obj, "test", json_object_new_string("1"));
set_by_string("test", &modify_obj, "1", json_type_string);
printf("file_obj = %s\n", json_object_get_string(file_obj));
printf("modify_obj = %s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
//json_object_object_add(file_obj, "test", json_object_new_string("1"));
struct json_object *obj, *nested;
json_object_object_get_ex(file_obj, "nested", &nested);
json_object_object_get_ex(nested, "api", &obj);
json_object_set_string(obj, "2");
set_by_string("nested.api", &modify_obj, "2", json_type_string);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_int(void **state)
{
(void) state;
//struct json_object *obj = json_object_new_object();
json_object_object_add(file_obj, "integer", json_object_new_int(1));
set_by_string("integer", &modify_obj, "1", json_type_int);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_int_nested(void **state)
{
(void) state;
struct json_object *obj = json_object_new_object();
struct json_object *nested = json_object_new_object();
json_object_object_add(nested, "integer", json_object_new_int(1));
json_object_object_add(obj, "nested1", nested);
json_object_object_add(file_obj, "nested0", obj);
set_by_string("nested0.nested1.integer", &modify_obj, "1", json_type_int);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_array_int(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
struct json_object *obj = json_object_new_object();
json_object_array_add(arr, json_object_new_int(1));
json_object_object_add(file_obj, "array", arr);
json_object_object_add(obj, "array", json_object_get(arr));
json_object_object_add(file_obj, "inner", obj);
set_by_string("array[-1]", &modify_obj, "1", json_type_int);
set_by_string("inner.array[-1]", &modify_obj, "1", json_type_int);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
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
285
286
287
288
289
290
291
static void test_json_add_array_object(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
struct json_object *obj = json_object_new_object();
json_object_object_add(obj, "integer", json_object_new_int(1));
json_object_object_add(obj, "string", json_object_new_string("test"));
json_object_array_add(arr, obj);
json_object_array_add(arr, json_object_get(obj)); // array will now hold two references to the same object (will double free otherwise)
json_object_object_add(file_obj, "array", arr);
set_by_string("array[-1].integer", &modify_obj, "1", json_type_int);
set_by_string("array[0].string", &modify_obj, "test", json_type_string);
set_by_string("array[-1]", &modify_obj, "{\"integer\": 1, \"string\":\"test\"}", json_type_object);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_array_nested_object(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
struct json_object *obj = json_object_new_object();
struct json_object *nested = json_object_new_object();
struct json_object *nested1 = json_object_new_object();
json_object_object_add(nested, "integer", json_object_new_int(1));
json_object_object_add(nested1, "nested1", nested);
json_object_object_add(obj, "nested", nested1);
json_object_array_add(arr, obj);
json_object_array_add(arr, json_object_get(obj));
json_object_array_add(arr, json_object_get(obj));
json_object_array_add(arr, json_object_get(obj));
json_object_array_add(arr, json_object_get(obj));
json_object_object_add(file_obj, "array", arr);
set_by_string(NULL, &modify_obj, "{\"array\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object);
set_by_string("array[5].nested.nested1.integer", &modify_obj, "1", json_type_int);
set_by_string("array[-1].nested.nested1", &modify_obj, "{\"integer\": 1}", json_type_object);
set_by_string("array[-1].nested", &modify_obj, "{\"nested1\": {\"integer\": 1}}", json_type_object);
set_by_string("array[-1]", &modify_obj, "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static void test_json_add_array_nested_array(void **state)
{
(void) state;
struct json_object *arr = json_object_new_array();
struct json_object *nested_arr = json_object_new_array();
struct json_object *nested1_arr = json_object_new_array();
json_object_array_add(nested_arr, json_object_new_int(1));
json_object_array_add(nested_arr, json_object_new_int(2));
json_object_array_add(nested_arr, json_object_new_int(3));
json_object_array_add(nested1_arr, json_object_new_int(5));
json_object_array_add(nested1_arr, json_object_new_int(6));
json_object_array_add(nested1_arr, json_object_new_int(7));
json_object_array_add(arr, nested_arr);
json_object_array_add(arr, nested1_arr);
json_object_object_add(file_obj, "array", arr);
//set_by_string("array[0][0].test", &modify_obj, "1", json_type_int);
set_by_string("array[0][0]", &modify_obj, "1", json_type_int);
set_by_string("array[0][1]", &modify_obj, "2", json_type_int);
set_by_string("array[0][2]", &modify_obj, "3", json_type_int);
set_by_string("array[1][-1]", &modify_obj, "5", json_type_int);
set_by_string("array[1][-1]", &modify_obj, "6", json_type_int);
set_by_string("array[1][-1]", &modify_obj, "7", json_type_int);
printf("file_obj=%s\n", json_object_get_string(file_obj));
printf("modify_obj=%s\n", json_object_get_string(modify_obj));
assert_int_equal(1, json_object_equal(modify_obj, file_obj));
}
static int setup (void** state) {
file_obj = path_to_obj("/home/jakob/git/json-editor-api/test.json");
modify_obj = path_to_obj("/home/jakob/git/json-editor-api/test.json");
return 0;
}
static int teardown (void** state) {
json_object_put(file_obj);
json_object_put(modify_obj);
return 0;
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_cfg_parse_success),
cmocka_unit_test(test_cfg_parse_fail),
cmocka_unit_test(test_build_from_scratch),
cmocka_unit_test_setup_teardown(test_json_add_int, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_int_nested, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_string, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_object, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_array, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_multi_types, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_multi_obj, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_overwrite_string, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_array_int, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_array_object, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_array_nested_object, setup, teardown),
cmocka_unit_test_setup_teardown(test_json_add_array_nested_array, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}