1 /**
2  * collectd - src/tests/test_common.c
3  * Copyright (C) 2013       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 // clang-format off
28 /*
29  * Explicit order is required or _FILE_OFFSET_BITS will have definition mismatches on Solaris
30  * See Github Issue #3193 for details
31  */
32 #include "utils/common/common.h"
33 #include "testing.h"
34 // clang-format on
35 
36 #if HAVE_KSTAT_H
37 #include <kstat.h>
38 #endif
39 
40 #if HAVE_LIBKSTAT
41 kstat_ctl_t *kc;
42 #endif /* HAVE_LIBKSTAT */
43 
DEF_TEST(sstrncpy)44 DEF_TEST(sstrncpy) {
45   char buffer[16] = "";
46   char *ptr = &buffer[4];
47   char *ret;
48 
49   buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
50   buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
51 
52   ret = sstrncpy(ptr, "foobar", 8);
53   OK(ret == ptr);
54   EXPECT_EQ_STR("foobar", ptr);
55   OK(buffer[3] == buffer[12]);
56 
57   ret = sstrncpy(ptr, "abc", 8);
58   OK(ret == ptr);
59   EXPECT_EQ_STR("abc", ptr);
60   OK(buffer[3] == buffer[12]);
61 
62   ret = sstrncpy(ptr, "collectd", 8);
63   OK(ret == ptr);
64   OK(ptr[7] == 0);
65   EXPECT_EQ_STR("collect", ptr);
66   OK(buffer[3] == buffer[12]);
67 
68   return 0;
69 }
70 
DEF_TEST(sstrdup)71 DEF_TEST(sstrdup) {
72   char *ptr;
73 
74   ptr = sstrdup("collectd");
75   OK(ptr != NULL);
76   EXPECT_EQ_STR("collectd", ptr);
77 
78   sfree(ptr);
79 
80   ptr = sstrdup(NULL);
81   OK(ptr == NULL);
82 
83   return 0;
84 }
85 
DEF_TEST(strsplit)86 DEF_TEST(strsplit) {
87   char buffer[32];
88   char *fields[8];
89   int status;
90 
91   strncpy(buffer, "foo bar", sizeof(buffer));
92   status = strsplit(buffer, fields, 8);
93   OK(status == 2);
94   EXPECT_EQ_STR("foo", fields[0]);
95   EXPECT_EQ_STR("bar", fields[1]);
96 
97   strncpy(buffer, "foo \t bar", sizeof(buffer));
98   status = strsplit(buffer, fields, 8);
99   OK(status == 2);
100   EXPECT_EQ_STR("foo", fields[0]);
101   EXPECT_EQ_STR("bar", fields[1]);
102 
103   strncpy(buffer, "one two\tthree\rfour\nfive", sizeof(buffer));
104   status = strsplit(buffer, fields, 8);
105   OK(status == 5);
106   EXPECT_EQ_STR("one", fields[0]);
107   EXPECT_EQ_STR("two", fields[1]);
108   EXPECT_EQ_STR("three", fields[2]);
109   EXPECT_EQ_STR("four", fields[3]);
110   EXPECT_EQ_STR("five", fields[4]);
111 
112   strncpy(buffer, "\twith trailing\n", sizeof(buffer));
113   status = strsplit(buffer, fields, 8);
114   OK(status == 2);
115   EXPECT_EQ_STR("with", fields[0]);
116   EXPECT_EQ_STR("trailing", fields[1]);
117 
118   strncpy(buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof(buffer));
119   status = strsplit(buffer, fields, 8);
120   OK(status == 8);
121   EXPECT_EQ_STR("7", fields[6]);
122   EXPECT_EQ_STR("8", fields[7]);
123 
124   strncpy(buffer, "single", sizeof(buffer));
125   status = strsplit(buffer, fields, 8);
126   OK(status == 1);
127   EXPECT_EQ_STR("single", fields[0]);
128 
129   strncpy(buffer, "", sizeof(buffer));
130   status = strsplit(buffer, fields, 8);
131   OK(status == 0);
132 
133   return 0;
134 }
135 
DEF_TEST(strjoin)136 DEF_TEST(strjoin) {
137   struct {
138     char **fields;
139     size_t fields_num;
140     char *separator;
141 
142     int want_return;
143     char *want_buffer;
144   } cases[] = {
145       /* Normal case. */
146       {(char *[]){"foo", "bar"}, 2, "!", 7, "foo!bar"},
147       /* One field only. */
148       {(char *[]){"foo"}, 1, "!", 3, "foo"},
149       /* No fields at all. */
150       {NULL, 0, "!", 0, ""},
151       /* Longer separator. */
152       {(char *[]){"foo", "bar"}, 2, "rcht", 10, "foorchtbar"},
153       /* Empty separator. */
154       {(char *[]){"foo", "bar"}, 2, "", 6, "foobar"},
155       /* NULL separator. */
156       {(char *[]){"foo", "bar"}, 2, NULL, 6, "foobar"},
157       /* buffer not large enough -> string is truncated. */
158       {(char *[]){"aaaaaa", "bbbbbb", "c!"}, 3, "-", 16, "aaaaaa-bbbbbb-c"},
159       /* buffer not large enough -> last field fills buffer completely. */
160       {(char *[]){"aaaaaaa", "bbbbbbb", "!"}, 3, "-", 17, "aaaaaaa-bbbbbbb"},
161       /* buffer not large enough -> string does *not* end in separator. */
162       {(char *[]){"aaaa", "bbbb", "cccc", "!"}, 4, "-", 16, "aaaa-bbbb-cccc"},
163       /* buffer not large enough -> string does not end with partial
164          separator. */
165       {(char *[]){"aaaaaa", "bbbbbb", "!"}, 3, "+-", 17, "aaaaaa+-bbbbbb"},
166   };
167 
168   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
169     char buffer[16];
170     int status;
171 
172     memset(buffer, 0xFF, sizeof(buffer));
173     status = strjoin(buffer, sizeof(buffer), cases[i].fields,
174                      cases[i].fields_num, cases[i].separator);
175     EXPECT_EQ_INT(cases[i].want_return, status);
176     EXPECT_EQ_STR(cases[i].want_buffer, buffer);
177 
178     /* use (NULL, 0) to determine required buffer size. */
179     EXPECT_EQ_INT(cases[i].want_return,
180                   strjoin(NULL, 0, cases[i].fields, cases[i].fields_num,
181                           cases[i].separator));
182   }
183 
184   return 0;
185 }
186 
DEF_TEST(escape_slashes)187 DEF_TEST(escape_slashes) {
188   struct {
189     char *str;
190     char *want;
191   } cases[] = {
192       {"foo/bar/baz", "foo_bar_baz"},
193       {"/like/a/path", "like_a_path"},
194       {"trailing/slash/", "trailing_slash_"},
195       {"foo//bar", "foo__bar"},
196   };
197 
198   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
199     char buffer[32] = {0};
200 
201     strncpy(buffer, cases[i].str, sizeof(buffer) - 1);
202     OK(escape_slashes(buffer, sizeof(buffer)) == 0);
203     EXPECT_EQ_STR(cases[i].want, buffer);
204   }
205 
206   return 0;
207 }
208 
DEF_TEST(escape_string)209 DEF_TEST(escape_string) {
210   struct {
211     char *str;
212     char *want;
213   } cases[] = {
214       {"foobar", "foobar"},
215       {"f00bar", "f00bar"},
216       {"foo bar", "\"foo bar\""},
217       {"foo \"bar\"", "\"foo \\\"bar\\\"\""},
218       {"012345678901234", "012345678901234"},
219       {"012345 78901234", "\"012345 789012\""},
220       {"012345 78901\"34", "\"012345 78901\""},
221   };
222 
223   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
224     char buffer[16] = {0};
225 
226     strncpy(buffer, cases[i].str, sizeof(buffer) - 1);
227     OK(escape_string(buffer, sizeof(buffer)) == 0);
228     EXPECT_EQ_STR(cases[i].want, buffer);
229   }
230 
231   return 0;
232 }
233 
DEF_TEST(strunescape)234 DEF_TEST(strunescape) {
235   char buffer[32] = {0};
236   int status;
237 
238   strncpy(buffer, "foo\\tbar", sizeof(buffer) - 1);
239   status = strunescape(buffer, sizeof(buffer));
240   OK(status == 0);
241   EXPECT_EQ_STR("foo\tbar", buffer);
242 
243   strncpy(buffer, "\\tfoo\\r\\n", sizeof(buffer) - 1);
244   status = strunescape(buffer, sizeof(buffer));
245   OK(status == 0);
246   EXPECT_EQ_STR("\tfoo\r\n", buffer);
247 
248   strncpy(buffer, "With \\\"quotes\\\"", sizeof(buffer) - 1);
249   status = strunescape(buffer, sizeof(buffer));
250   OK(status == 0);
251   EXPECT_EQ_STR("With \"quotes\"", buffer);
252 
253   /* Backslash before null byte */
254   strncpy(buffer, "\\tbackslash end\\", sizeof(buffer) - 1);
255   status = strunescape(buffer, sizeof(buffer));
256   OK(status != 0);
257   EXPECT_EQ_STR("\tbackslash end", buffer);
258   return 0;
259 
260   /* Backslash at buffer end */
261   strncpy(buffer, "\\t3\\56", sizeof(buffer) - 1);
262   status = strunescape(buffer, 4);
263   OK(status != 0);
264   OK(buffer[0] == '\t');
265   OK(buffer[1] == '3');
266   OK(buffer[2] == 0);
267   OK(buffer[3] == 0);
268   OK(buffer[4] == '5');
269   OK(buffer[5] == '6');
270   OK(buffer[6] == '7');
271 
272   return 0;
273 }
274 
DEF_TEST(parse_values)275 DEF_TEST(parse_values) {
276   struct {
277     char buffer[64];
278     int status;
279     gauge_t value;
280   } cases[] = {
281       {"1435044576:42", 0, 42.0}, {"1435044576:42:23", -1, NAN},
282       {"1435044576:U", 0, NAN},   {"N:12.3", 0, 12.3},
283       {"N:42.0:23", -1, NAN},     {"N:U", 0, NAN},
284       {"T:42.0", -1, NAN},
285   };
286 
287   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
288     data_source_t dsrc = {
289         .name = "value",
290         .type = DS_TYPE_GAUGE,
291         .min = 0.0,
292         .max = NAN,
293     };
294     data_set_t ds = {
295         .type = "example",
296         .ds_num = 1,
297         .ds = &dsrc,
298     };
299 
300     value_t v = {
301         .gauge = NAN,
302     };
303     value_list_t vl = {
304         .values = &v,
305         .values_len = 1,
306         .time = 0,
307         .interval = 0,
308         .host = "example.com",
309         .plugin = "common_test",
310         .type = "example",
311         .meta = NULL,
312     };
313 
314     int status = parse_values(cases[i].buffer, &vl, &ds);
315     EXPECT_EQ_INT(cases[i].status, status);
316     if (status != 0)
317       continue;
318 
319     EXPECT_EQ_DOUBLE(cases[i].value, vl.values[0].gauge);
320   }
321 
322   return 0;
323 }
324 
DEF_TEST(value_to_rate)325 DEF_TEST(value_to_rate) {
326   struct {
327     time_t t0;
328     time_t t1;
329     int ds_type;
330     value_t v0;
331     value_t v1;
332     gauge_t want;
333   } cases[] = {
334       {0, 10, DS_TYPE_DERIVE, {.derive = 0}, {.derive = 1000}, NAN},
335       {10, 20, DS_TYPE_DERIVE, {.derive = 1000}, {.derive = 2000}, 100.0},
336       {20, 30, DS_TYPE_DERIVE, {.derive = 2000}, {.derive = 1800}, -20.0},
337       {0, 10, DS_TYPE_COUNTER, {.counter = 0}, {.counter = 1000}, NAN},
338       {10, 20, DS_TYPE_COUNTER, {.counter = 1000}, {.counter = 5000}, 400.0},
339       /* 32bit wrap-around. */
340       {20,
341        30,
342        DS_TYPE_COUNTER,
343        {.counter = 4294967238ULL},
344        {.counter = 42},
345        10.0},
346       /* 64bit wrap-around. */
347       {30,
348        40,
349        DS_TYPE_COUNTER,
350        {.counter = 18446744073709551558ULL},
351        {.counter = 42},
352        10.0},
353   };
354 
355   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
356     cdtime_t t0 = TIME_T_TO_CDTIME_T(cases[i].t0);
357     value_to_rate_state_t state = {
358         .last_value = cases[i].v0,
359         .last_time = t0,
360     };
361     gauge_t got;
362 
363     if (cases[i].t0 == 0) {
364       EXPECT_EQ_INT(EAGAIN,
365                     value_to_rate(&got, cases[i].v1, cases[i].ds_type,
366                                   TIME_T_TO_CDTIME_T(cases[i].t1), &state));
367       continue;
368     }
369 
370     EXPECT_EQ_INT(0, value_to_rate(&got, cases[i].v1, cases[i].ds_type,
371                                    TIME_T_TO_CDTIME_T(cases[i].t1), &state));
372     EXPECT_EQ_DOUBLE(cases[i].want, got);
373   }
374 
375   return 0;
376 }
377 
main(void)378 int main(void) {
379   RUN_TEST(sstrncpy);
380   RUN_TEST(sstrdup);
381   RUN_TEST(strsplit);
382   RUN_TEST(strjoin);
383   RUN_TEST(escape_slashes);
384   RUN_TEST(escape_string);
385   RUN_TEST(strunescape);
386   RUN_TEST(parse_values);
387   RUN_TEST(value_to_rate);
388 
389   END_TEST;
390 }
391