1 /**
2 * collectd - src/utils_format_json_test.c
3 * Copyright (C) 2015 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 #include "collectd.h"
29 #include "testing.h"
30 #include "utils_format_json.h"
31 #include "common.h" /* for STATIC_ARRAY_SIZE */
33 #include <yajl/yajl_common.h>
34 #include <yajl/yajl_parse.h>
35 #if HAVE_YAJL_YAJL_VERSION_H
36 # include <yajl/yajl_version.h>
37 #endif
38 #if YAJL_MAJOR > 1
39 # define HAVE_YAJL_V2 1
40 #endif
42 typedef struct
43 {
44 char const *key;
45 char const *value;
46 } label_t;
48 typedef struct
49 {
50 label_t *expected_labels;
51 size_t expected_labels_num;
53 label_t *current_label;
54 } test_case_t;
56 #if HAVE_YAJL_V2
57 static int test_map_key (void *ctx, unsigned char const *key, size_t key_len)
58 #else
59 static int test_map_key (void *ctx, unsigned char const *key, unsigned int key_len)
60 #endif
61 {
62 test_case_t *c = ctx;
63 size_t i;
65 c->current_label = NULL;
66 for (i = 0; i < c->expected_labels_num; i++)
67 {
68 label_t *l = c->expected_labels + i;
69 if ((strlen (l->key) == key_len)
70 && (strncmp (l->key, (char const *) key, key_len) == 0))
71 {
72 c->current_label = l;
73 break;
74 }
75 }
77 return 1; /* continue */
78 }
80 static int expect_label (char const *name, char const *got, char const *want)
81 {
82 _Bool ok = (strcmp (got, want) == 0);
83 char msg[1024];
85 if (ok)
86 snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\"", name, got);
87 else
88 snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, want);
90 OK1 (ok, msg);
91 return 0;
92 }
94 #if HAVE_YAJL_V2
95 static int test_string (void *ctx, unsigned char const *value, size_t value_len)
96 #else
97 static int test_string (void *ctx, unsigned char const *value, unsigned int value_len)
98 #endif
99 {
100 test_case_t *c = ctx;
102 if (c->current_label != NULL)
103 {
104 label_t *l = c->current_label;
105 char *got;
106 int status;
108 got = malloc (value_len + 1);
109 memmove (got, value, value_len);
110 got[value_len] = 0;
112 status = expect_label (l->key, got, l->value);
114 free (got);
116 if (status != 0)
117 return 0; /* abort */
118 }
120 return 1; /* continue */
121 }
123 static int expect_json_labels (char *json, label_t *labels, size_t labels_num)
124 {
125 yajl_callbacks funcs = {
126 .yajl_string = test_string,
127 .yajl_map_key = test_map_key,
128 };
130 test_case_t c = { labels, labels_num, NULL };
132 yajl_handle hndl;
133 #if HAVE_YAJL_V2
134 CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* alloc = */ NULL, &c));
135 #else
136 CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* config = */ NULL, /* alloc = */ NULL, &c));
137 #endif
138 OK (yajl_parse (hndl, (unsigned char *) json, strlen (json)) == yajl_status_ok);
140 yajl_free (hndl);
141 return 0;
142 }
144 DEF_TEST(notification)
145 {
146 label_t labels[] = {
147 {"summary", "this is a message"},
148 {"alertname", "collectd_unit_test"},
149 {"instance", "example.com"},
150 {"service", "collectd"},
151 {"unit", "case"},
152 };
154 /* 1448284606.125 ^= 1555083754651779072 */
155 notification_t n = { NOTIF_WARNING, 1555083754651779072ULL, "this is a message",
156 "example.com", "unit", "", "test", "case", NULL };
158 char got[1024];
159 CHECK_ZERO (format_json_notification (got, sizeof (got), &n));
160 // printf ("got = \"%s\";\n", got);
162 return expect_json_labels (got, labels, STATIC_ARRAY_SIZE (labels));
163 }
165 int main (void)
166 {
167 RUN_TEST(notification);
169 END_TEST;
170 }