Merge remote-tracking branch 'github/pr/2105'
[collectd.git] / src / curl_json_test.c
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  **/
10
11 #include "curl_json.c"
12
13 #include "testing.h"
14
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;
18
19   value_t *value_copy = calloc(1, sizeof(*value_copy));
20   memmove(value_copy, value, sizeof(*value_copy));
21
22   assert(c_avl_insert(values, key->path, value_copy) == 0);
23 }
24
25 static derive_t test_metric(cj_t *db, char const *path) {
26   c_avl_tree_t *values = (void *)db->curl;
27
28   value_t *ret = NULL;
29   if (c_avl_get(values, path, (void *)&ret) == 0) {
30     return ret->derive;
31   }
32
33   return -1;
34 }
35
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);
45
46   /* hack; see above. */
47   db->curl = (void *)cj_avl_create();
48
49   cj_key_t *key = calloc(1, sizeof(*key));
50   key->magic = CJ_KEY_MAGIC;
51   key->path = strdup(key_path);
52   key->type = strdup("MAGIC");
53
54   assert(cj_append_key(db, key) == 0);
55
56   db->state[0].tree = db->tree;
57
58   cj_curl_callback(json, strlen(json), 1, db);
59 #if HAVE_YAJL_V2
60   yajl_complete_parse(db->yajl);
61 #else
62   yajl_parse_complete(db->yajl);
63 #endif
64
65   return db;
66 }
67
68 static void test_teardown(cj_t *db) {
69   c_avl_tree_t *values = (void *)db->curl;
70   db->curl = NULL;
71
72   void *key;
73   void *value;
74   while (c_avl_pick(values, &key, &value) == 0) {
75     /* key will be freed by cj_free. */
76     free(value);
77   }
78   c_avl_destroy(values);
79
80   yajl_free(db->yajl);
81   db->yajl = NULL;
82
83   cj_free(db);
84 }
85
86 DEF_TEST(parse) {
87   struct {
88     char *json;
89     char *key_path;
90     derive_t want;
91   } cases[] = {
92       /* simple map */
93       {"{\"foo\":42,\"bar\":23}", "foo", 42},
94       {"{\"foo\":42,\"bar\":23}", "bar", 23},
95       /* nested map */
96       {"{\"a\":{\"b\":{\"c\":123}}", "a/b/c", 123},
97       {"{\"x\":{\"y\":{\"z\":789}}", "x/*/z", 789},
98       /* simple array */
99       {"[10,11,12,13]", "0", 10},
100       {"[10,11,12,13]", "1", 11},
101       {"[10,11,12,13]", "2", 12},
102       {"[10,11,12,13]", "3", 13},
103       /* array index after non-numeric entry */
104       {"[true,11]", "1", 11},
105       {"[null,11]", "1", 11},
106       {"[\"s\",11]", "1", 11},
107       {"[{\"k\":\"v\"},11]", "1", 11},
108       {"[[0,1,2],11]", "1", 11},
109       /* nested array */
110       {"[[0,1,2],[3,4,5],[6,7,8]]", "0/0", 0},
111       {"[[0,1,2],[3,4,5],[6,7,8]]", "0/1", 1},
112       {"[[0,1,2],[3,4,5],[6,7,8]]", "0/2", 2},
113       {"[[0,1,2],[3,4,5],[6,7,8]]", "1/0", 3},
114       {"[[0,1,2],[3,4,5],[6,7,8]]", "1/1", 4},
115       {"[[0,1,2],[3,4,5],[6,7,8]]", "1/2", 5},
116       {"[[0,1,2],[3,4,5],[6,7,8]]", "2/0", 6},
117       {"[[0,1,2],[3,4,5],[6,7,8]]", "2/1", 7},
118       {"[[0,1,2],[3,4,5],[6,7,8]]", "2/2", 8},
119       /* testcase from #2266 */
120       {"{\"a\":[[10,11,12,13,14]]}", "a/0/0", 10},
121       {"{\"a\":[[10,11,12,13,14]]}", "a/0/1", 11},
122       {"{\"a\":[[10,11,12,13,14]]}", "a/0/2", 12},
123       {"{\"a\":[[10,11,12,13,14]]}", "a/0/3", 13},
124       {"{\"a\":[[10,11,12,13,14]]}", "a/0/4", 14},
125   };
126
127   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
128     cj_t *db = test_setup(cases[i].json, cases[i].key_path);
129
130     EXPECT_EQ_INT(cases[i].want, test_metric(db, cases[i].key_path));
131
132     test_teardown(db);
133   }
134
135   return 0;
136 }
137
138 int main(int argc, char **argv) {
139   cj_submit = test_submit;
140
141   RUN_TEST(parse);
142
143   END_TEST;
144 }