2 * collectd - src/tests/test_common.c
3 * Copyright (C) 2013 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
33 char *ptr = &buffer[4];
36 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
37 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
39 ret = sstrncpy (ptr, "foobar", 8);
41 STREQ ("foobar", ptr);
42 OK(buffer[3] == buffer[12]);
44 ret = sstrncpy (ptr, "abc", 8);
47 OK(buffer[3] == buffer[12]);
49 ret = sstrncpy (ptr, "collectd", 8);
52 STREQ ("collect", ptr);
53 OK(buffer[3] == buffer[12]);
61 char *ptr = &buffer[4];
64 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
65 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
67 status = ssnprintf (ptr, 8, "%i", 1337);
71 status = ssnprintf (ptr, 8, "%s", "collectd");
74 STREQ ("collect", ptr);
75 OK(buffer[3] == buffer[12]);
84 ptr = sstrdup ("collectd");
86 STREQ ("collectd", ptr);
103 strncpy (buffer, "foo bar", sizeof (buffer));
104 status = strsplit (buffer, fields, 8);
106 STREQ ("foo", fields[0]);
107 STREQ ("bar", fields[1]);
109 strncpy (buffer, "foo \t bar", sizeof (buffer));
110 status = strsplit (buffer, fields, 8);
112 STREQ ("foo", fields[0]);
113 STREQ ("bar", fields[1]);
115 strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer));
116 status = strsplit (buffer, fields, 8);
118 STREQ ("one", fields[0]);
119 STREQ ("two", fields[1]);
120 STREQ ("three", fields[2]);
121 STREQ ("four", fields[3]);
122 STREQ ("five", fields[4]);
124 strncpy (buffer, "\twith trailing\n", sizeof (buffer));
125 status = strsplit (buffer, fields, 8);
127 STREQ ("with", fields[0]);
128 STREQ ("trailing", fields[1]);
130 strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer));
131 status = strsplit (buffer, fields, 8);
133 STREQ ("7", fields[6]);
134 STREQ ("8", fields[7]);
136 strncpy (buffer, "single", sizeof (buffer));
137 status = strsplit (buffer, fields, 8);
139 STREQ ("single", fields[0]);
141 strncpy (buffer, "", sizeof (buffer));
142 status = strsplit (buffer, fields, 8);
159 status = strjoin (buffer, sizeof (buffer), fields, 2, "!");
161 STREQ ("foo!bar", buffer);
163 status = strjoin (buffer, sizeof (buffer), fields, 1, "!");
165 STREQ ("foo", buffer);
167 status = strjoin (buffer, sizeof (buffer), fields, 0, "!");
170 status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht");
172 STREQ ("foorchtbar", buffer);
174 status = strjoin (buffer, sizeof (buffer), fields, 4, "");
176 STREQ ("foobarbazqux", buffer);
178 status = strjoin (buffer, sizeof (buffer), fields, 4, "!");
180 STREQ ("foo!bar!baz!qux", buffer);
186 status = strjoin (buffer, sizeof (buffer), fields, 4, "-");
192 DEF_TEST(escape_slashes)
198 {"foo/bar/baz", "foo_bar_baz"},
199 {"/like/a/path", "like_a_path"},
200 {"trailing/slash/", "trailing_slash_"},
201 {"foo//bar", "foo__bar"},
205 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
208 strncpy (buffer, cases[i].str, sizeof (buffer));
209 OK(escape_slashes (buffer, sizeof (buffer)) == 0);
210 STREQ(cases[i].want, buffer);
216 DEF_TEST(escape_string)
222 {"foobar", "foobar"},
223 {"f00bar", "f00bar"},
224 {"foo bar", "\"foo bar\""},
225 {"foo \"bar\"", "\"foo \\\"bar\\\"\""},
226 {"012345678901234", "012345678901234"},
227 {"012345 78901234", "\"012345 789012\""},
228 {"012345 78901\"34", "\"012345 78901\""},
232 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
235 strncpy (buffer, cases[i].str, sizeof (buffer));
236 OK(escape_string (buffer, sizeof (buffer)) == 0);
237 STREQ(cases[i].want, buffer);
243 DEF_TEST(strunescape)
248 strncpy (buffer, "foo\\tbar", sizeof (buffer));
249 status = strunescape (buffer, sizeof (buffer));
251 STREQ ("foo\tbar", buffer);
253 strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
254 status = strunescape (buffer, sizeof (buffer));
256 STREQ ("\tfoo\r\n", buffer);
258 strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
259 status = strunescape (buffer, sizeof (buffer));
261 STREQ ("With \"quotes\"", buffer);
263 /* Backslash before null byte */
264 strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
265 status = strunescape (buffer, sizeof (buffer));
267 STREQ ("\tbackslash end", buffer);
270 /* Backslash at buffer end */
271 strncpy (buffer, "\\t3\\56", sizeof (buffer));
272 status = strunescape (buffer, 4);
274 OK(buffer[0] == '\t');
275 OK(buffer[1] == '3');
278 OK(buffer[4] == '5');
279 OK(buffer[5] == '6');
280 OK(buffer[6] == '7');
285 DEF_TEST(parse_values)
292 {"1435044576:42", 0, 42.0},
293 {"1435044576:42:23", -1, NAN},
294 {"1435044576:U", 0, NAN},
296 {"N:42.0:23", -1, NAN},
302 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++)
304 data_source_t dsrc = {
306 .type = DS_TYPE_GAUGE,
324 .host = "example.com",
325 .plugin = "common_test",
330 int status = parse_values (cases[i].buffer, &vl, &ds);
331 OK(status == cases[i].status);
335 OK(cases[i].value == vl.values[0].gauge);
341 DEF_TEST(value_to_rate)
351 { 0, 10, DS_TYPE_DERIVE, {.derive = 0}, {.derive = 1000}, NAN},
352 {10, 20, DS_TYPE_DERIVE, {.derive = 1000}, {.derive = 2000}, 100.0},
353 {20, 30, DS_TYPE_DERIVE, {.derive = 2000}, {.derive = 1800}, -20.0},
354 { 0, 10, DS_TYPE_COUNTER, {.counter = 0}, {.counter = 1000}, NAN},
355 {10, 20, DS_TYPE_COUNTER, {.counter = 1000}, {.counter = 5000}, 400.0},
356 /* 32bit wrap-around. */
357 {20, 30, DS_TYPE_COUNTER, {.counter = 4294967238}, {.counter = 42}, 10.0},
358 {30, 40, DS_TYPE_COUNTER, {.counter = 18446744073709551558ULL}, {.counter = 42}, 10.0},
362 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
363 value_to_rate_state_t state = { cases[i].v0, TIME_T_TO_CDTIME_T (cases[i].t0) };
366 if (cases[i].t0 == 0) {
367 OK(value_to_rate (&got, cases[i].v1.derive, &state, cases[i].ds_type, TIME_T_TO_CDTIME_T (cases[i].t1)) == EAGAIN);
371 OK(value_to_rate (&got, cases[i].v1.derive, &state, cases[i].ds_type, TIME_T_TO_CDTIME_T (cases[i].t1)) == 0);
372 DBLEQ(cases[i].want, got.gauge);
385 RUN_TEST(escape_slashes);
386 RUN_TEST(escape_string);
387 RUN_TEST(strunescape);
388 RUN_TEST(parse_values);
389 RUN_TEST(value_to_rate);
394 /* vim: set sw=2 sts=2 et : */