Merge pull request #1490 from rubenk/uuid
[collectd.git] / src / utils_format_json_test.c
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  */
26
27 #include "testing.h"
28 #include "collectd.h"
29 #include "utils_format_json.h"
30 #include "common.h" /* for STATIC_ARRAY_SIZE */
31
32 #include <yajl/yajl_common.h>
33 #include <yajl/yajl_parse.h>
34 #if HAVE_YAJL_YAJL_VERSION_H
35 # include <yajl/yajl_version.h>
36 #endif
37 #if YAJL_MAJOR > 1
38 # define HAVE_YAJL_V2 1
39 #endif
40
41 typedef struct
42 {
43   char const *key;
44   char const *value;
45 } label_t;
46
47 typedef struct
48 {
49   label_t *expected_labels;
50   size_t   expected_labels_num;
51
52   label_t *current_label;
53 } test_case_t;
54
55 #if HAVE_YAJL_V2
56 static int test_map_key (void *ctx, unsigned char const *key, size_t key_len)
57 #else
58 static int test_map_key (void *ctx, unsigned char const *key, unsigned int key_len)
59 #endif
60 {
61   test_case_t *c = ctx;
62   size_t i;
63
64   c->current_label = NULL;
65   for (i = 0; i < c->expected_labels_num; i++)
66   {
67     label_t *l = c->expected_labels + i;
68     if ((strlen (l->key) == key_len)
69         && (strncmp (l->key, (char const *) key, key_len) == 0))
70     {
71       c->current_label = l;
72       break;
73     }
74   }
75
76   return 1; /* continue */
77 }
78
79 static int expect_label (char const *name, char const *got, char const *want)
80 {
81   _Bool ok = (strcmp (got, want) == 0);
82   char msg[1024];
83
84   if (ok)
85     snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\"", name, got);
86   else
87     snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, want);
88
89   OK1 (ok, msg);
90   return 0;
91 }
92
93 #if HAVE_YAJL_V2
94 static int test_string (void *ctx, unsigned char const *value, size_t value_len)
95 #else
96 static int test_string (void *ctx, unsigned char const *value, unsigned int value_len)
97 #endif
98 {
99   test_case_t *c = ctx;
100
101   if (c->current_label != NULL)
102   {
103     label_t *l = c->current_label;
104     char *got;
105     int status;
106
107     got = malloc (value_len + 1);
108     memmove (got, value, value_len);
109     got[value_len] = 0;
110
111     status = expect_label (l->key, got, l->value);
112
113     free (got);
114
115     if (status != 0)
116       return 0; /* abort */
117   }
118
119   return 1; /* continue */
120 }
121
122 static int expect_json_labels (char *json, label_t *labels, size_t labels_num)
123 {
124   yajl_callbacks funcs = {
125     .yajl_string = test_string,
126     .yajl_map_key = test_map_key,
127   };
128
129   test_case_t c = { labels, labels_num, NULL };
130
131   yajl_handle hndl;
132 #if HAVE_YAJL_V2
133   CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* alloc = */ NULL, &c));
134 #else
135   CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* config = */ NULL, /* alloc = */ NULL, &c));
136 #endif
137   OK (yajl_parse (hndl, (unsigned char *) json, strlen (json)) == yajl_status_ok);
138
139   yajl_free (hndl);
140   return 0;
141 }
142
143 DEF_TEST(notification)
144 {
145   label_t labels[] = {
146     {"summary", "this is a message"},
147     {"alertname", "collectd_unit_test"},
148     {"instance", "example.com"},
149     {"service", "collectd"},
150     {"unit", "case"},
151   };
152
153   /* 1448284606.125 ^= 1555083754651779072 */
154   notification_t n = { NOTIF_WARNING, 1555083754651779072ULL, "this is a message",
155     "example.com", "unit", "", "test", "case", NULL };
156
157   char got[1024];
158   CHECK_ZERO (format_json_notification (got, sizeof (got), &n));
159   // printf ("got = \"%s\";\n", got);
160
161   return expect_json_labels (got, labels, STATIC_ARRAY_SIZE (labels));
162 }
163
164 int main (void)
165 {
166   RUN_TEST(notification);
167
168   END_TEST;
169 }