2 * collectd - src/curl_json.c
3 * Copyright (C) 2017 Florian octo Forster
5 * Licensed under the same terms and conditions as src/curl_json.c.
8 * Florian octo Forster <octo at collectd.org>
11 #include "curl_json.c"
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);
25 static derive_t test_metric(cj_t *db, char const *path) {
26 c_avl_tree_t *values = (void *)db->curl;
29 if (c_avl_get(values, path, (void *)&ret) == 0) {
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,
40 /* alloc funcs = */ NULL,
42 /* alloc funcs = */ NULL, NULL,
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};
58 db->state[0].entry = &root;
60 cj_curl_callback(json, strlen(json), 1, db);
62 yajl_complete_parse(db->yajl);
64 yajl_parse_complete(db->yajl);
67 db->state[0].entry = NULL;
72 static void test_teardown(cj_t *db) {
73 c_avl_tree_t *values = (void *)db->curl;
78 while (c_avl_pick(values, &key, &value) == 0) {
79 /* key will be freed by cj_free. */
82 c_avl_destroy(values);
97 {"{\"foo\":42,\"bar\":23}", "foo", 42},
98 {"{\"foo\":42,\"bar\":23}", "bar", 23},
100 {"{\"a\":{\"b\":{\"c\":123}}", "a/b/c", 123},
101 {"{\"x\":{\"y\":{\"z\":789}}", "x/*/z", 789},
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},
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},
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));
142 int main(int argc, char **argv) {
143 cj_submit = test_submit;