1 /**
2 * collectd - src/curl_json.c
3 * Copyright (C) 2017 Florian octo Forster
4 *
5 * Licensed under the same terms and conditions as src/curl_json.c.
6 *
7 * Authors:
8 * Florian octo Forster <octo at collectd.org>
9 **/
11 #include "curl_json.c"
13 #include "testing.h"
15 static void test_submit(cj_t *db, cj_key_t *key, value_t *value) {
16 /* hack: we repurpose db->curl to store received values. */
17 c_avl_tree_t *values = (void *)db->curl;
19 value_t *value_copy = calloc(1, sizeof(*value_copy));
20 memmove(value_copy, value, sizeof(*value_copy));
22 assert(c_avl_insert(values, key->path, value_copy) == 0);
23 }
25 static derive_t test_metric(cj_t *db, char const *path) {
26 c_avl_tree_t *values = (void *)db->curl;
28 value_t *ret = NULL;
29 if (c_avl_get(values, path, (void *)&ret) == 0) {
30 return ret->derive;
31 }
33 return -1;
34 }
36 static cj_t *test_setup(char *json, char *key_path) {
37 cj_t *db = calloc(1, sizeof(*db));
38 db->yajl = yajl_alloc(&ycallbacks,
39 #if HAVE_YAJL_V2
40 /* alloc funcs = */ NULL,
41 #else
42 /* alloc funcs = */ NULL, NULL,
43 #endif
44 /* context = */ (void *)db);
46 /* hack; see above. */
47 db->curl = (void *)cj_avl_create();
49 cj_key_t *key = calloc(1, sizeof(*key));
50 key->path = strdup(key_path);
51 key->type = strdup("MAGIC");
53 assert(cj_append_key(db, key) == 0);
55 cj_tree_entry_t root = {0};
56 root.type = TREE;
57 root.tree = db->tree;
58 db->state[0].entry = &root;
60 cj_curl_callback(json, strlen(json), 1, db);
61 #if HAVE_YAJL_V2
62 yajl_complete_parse(db->yajl);
63 #else
64 yajl_parse_complete(db->yajl);
65 #endif
67 db->state[0].entry = NULL;
69 return db;
70 }
72 static void test_teardown(cj_t *db) {
73 c_avl_tree_t *values = (void *)db->curl;
74 db->curl = NULL;
76 void *key;
77 void *value;
78 while (c_avl_pick(values, &key, &value) == 0) {
79 /* key will be freed by cj_free. */
80 free(value);
81 }
82 c_avl_destroy(values);
84 yajl_free(db->yajl);
85 db->yajl = NULL;
87 cj_free(db);
88 }
90 DEF_TEST(parse) {
91 struct {
92 char *json;
93 char *key_path;
94 derive_t want;
95 } cases[] = {
96 /* simple map */
97 {"{\"foo\":42,\"bar\":23}", "foo", 42},
98 {"{\"foo\":42,\"bar\":23}", "bar", 23},
99 /* nested map */
100 {"{\"a\":{\"b\":{\"c\":123}}", "a/b/c", 123},
101 {"{\"x\":{\"y\":{\"z\":789}}", "x/*/z", 789},
102 /* simple array */
103 {"[10,11,12,13]", "0", 10},
104 {"[10,11,12,13]", "1", 11},
105 {"[10,11,12,13]", "2", 12},
106 {"[10,11,12,13]", "3", 13},
107 /* array index after non-numeric entry */
108 {"[true,11]", "1", 11},
109 {"[null,11]", "1", 11},
110 {"[\"s\",11]", "1", 11},
111 {"[{\"k\":\"v\"},11]", "1", 11},
112 {"[[0,1,2],11]", "1", 11},
113 /* nested array */
114 {"[[0,1,2],[3,4,5],[6,7,8]]", "0/0", 0},
115 {"[[0,1,2],[3,4,5],[6,7,8]]", "0/1", 1},
116 {"[[0,1,2],[3,4,5],[6,7,8]]", "0/2", 2},
117 {"[[0,1,2],[3,4,5],[6,7,8]]", "1/0", 3},
118 {"[[0,1,2],[3,4,5],[6,7,8]]", "1/1", 4},
119 {"[[0,1,2],[3,4,5],[6,7,8]]", "1/2", 5},
120 {"[[0,1,2],[3,4,5],[6,7,8]]", "2/0", 6},
121 {"[[0,1,2],[3,4,5],[6,7,8]]", "2/1", 7},
122 {"[[0,1,2],[3,4,5],[6,7,8]]", "2/2", 8},
123 /* testcase from #2266 */
124 {"{\"a\":[[10,11,12,13,14]]}", "a/0/0", 10},
125 {"{\"a\":[[10,11,12,13,14]]}", "a/0/1", 11},
126 {"{\"a\":[[10,11,12,13,14]]}", "a/0/2", 12},
127 {"{\"a\":[[10,11,12,13,14]]}", "a/0/3", 13},
128 {"{\"a\":[[10,11,12,13,14]]}", "a/0/4", 14},
129 };
131 for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
132 cj_t *db = test_setup(cases[i].json, cases[i].key_path);
134 EXPECT_EQ_INT(cases[i].want, test_metric(db, cases[i].key_path));
136 test_teardown(db);
137 }
139 return 0;
140 }
142 int main(int argc, char **argv) {
143 cj_submit = test_submit;
145 RUN_TEST(parse);
147 END_TEST;
148 }