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>
32 #endif /* HAVE_LIBKSTAT */
37 char *ptr = &buffer[4];
40 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
41 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
43 ret = sstrncpy (ptr, "foobar", 8);
45 EXPECT_EQ_STR ("foobar", ptr);
46 OK(buffer[3] == buffer[12]);
48 ret = sstrncpy (ptr, "abc", 8);
50 EXPECT_EQ_STR ("abc", ptr);
51 OK(buffer[3] == buffer[12]);
53 ret = sstrncpy (ptr, "collectd", 8);
56 EXPECT_EQ_STR ("collect", ptr);
57 OK(buffer[3] == buffer[12]);
65 char *ptr = &buffer[4];
68 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
69 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
71 status = ssnprintf (ptr, 8, "%i", 1337);
73 EXPECT_EQ_STR ("1337", ptr);
75 status = ssnprintf (ptr, 8, "%s", "collectd");
78 EXPECT_EQ_STR ("collect", ptr);
79 OK(buffer[3] == buffer[12]);
88 ptr = sstrdup ("collectd");
90 EXPECT_EQ_STR ("collectd", ptr);
107 strncpy (buffer, "foo bar", sizeof (buffer));
108 status = strsplit (buffer, fields, 8);
110 EXPECT_EQ_STR ("foo", fields[0]);
111 EXPECT_EQ_STR ("bar", fields[1]);
113 strncpy (buffer, "foo \t bar", sizeof (buffer));
114 status = strsplit (buffer, fields, 8);
116 EXPECT_EQ_STR ("foo", fields[0]);
117 EXPECT_EQ_STR ("bar", fields[1]);
119 strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer));
120 status = strsplit (buffer, fields, 8);
122 EXPECT_EQ_STR ("one", fields[0]);
123 EXPECT_EQ_STR ("two", fields[1]);
124 EXPECT_EQ_STR ("three", fields[2]);
125 EXPECT_EQ_STR ("four", fields[3]);
126 EXPECT_EQ_STR ("five", fields[4]);
128 strncpy (buffer, "\twith trailing\n", sizeof (buffer));
129 status = strsplit (buffer, fields, 8);
131 EXPECT_EQ_STR ("with", fields[0]);
132 EXPECT_EQ_STR ("trailing", fields[1]);
134 strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer));
135 status = strsplit (buffer, fields, 8);
137 EXPECT_EQ_STR ("7", fields[6]);
138 EXPECT_EQ_STR ("8", fields[7]);
140 strncpy (buffer, "single", sizeof (buffer));
141 status = strsplit (buffer, fields, 8);
143 EXPECT_EQ_STR ("single", fields[0]);
145 strncpy (buffer, "", sizeof (buffer));
146 status = strsplit (buffer, fields, 8);
163 status = strjoin (buffer, sizeof (buffer), fields, 2, "!");
165 EXPECT_EQ_STR ("foo!bar", buffer);
167 status = strjoin (buffer, sizeof (buffer), fields, 1, "!");
169 EXPECT_EQ_STR ("foo", buffer);
171 status = strjoin (buffer, sizeof (buffer), fields, 0, "!");
174 status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht");
176 EXPECT_EQ_STR ("foorchtbar", buffer);
178 status = strjoin (buffer, sizeof (buffer), fields, 4, "");
180 EXPECT_EQ_STR ("foobarbazqux", buffer);
182 status = strjoin (buffer, sizeof (buffer), fields, 4, "!");
184 EXPECT_EQ_STR ("foo!bar!baz!qux", buffer);
190 status = strjoin (buffer, sizeof (buffer), fields, 4, "-");
196 DEF_TEST(escape_slashes)
202 {"foo/bar/baz", "foo_bar_baz"},
203 {"/like/a/path", "like_a_path"},
204 {"trailing/slash/", "trailing_slash_"},
205 {"foo//bar", "foo__bar"},
209 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
212 strncpy (buffer, cases[i].str, sizeof (buffer));
213 OK(escape_slashes (buffer, sizeof (buffer)) == 0);
214 EXPECT_EQ_STR(cases[i].want, buffer);
220 DEF_TEST(escape_string)
226 {"foobar", "foobar"},
227 {"f00bar", "f00bar"},
228 {"foo bar", "\"foo bar\""},
229 {"foo \"bar\"", "\"foo \\\"bar\\\"\""},
230 {"012345678901234", "012345678901234"},
231 {"012345 78901234", "\"012345 789012\""},
232 {"012345 78901\"34", "\"012345 78901\""},
236 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
239 strncpy (buffer, cases[i].str, sizeof (buffer));
240 OK(escape_string (buffer, sizeof (buffer)) == 0);
241 EXPECT_EQ_STR(cases[i].want, buffer);
247 DEF_TEST(strunescape)
252 strncpy (buffer, "foo\\tbar", sizeof (buffer));
253 status = strunescape (buffer, sizeof (buffer));
255 EXPECT_EQ_STR ("foo\tbar", buffer);
257 strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
258 status = strunescape (buffer, sizeof (buffer));
260 EXPECT_EQ_STR ("\tfoo\r\n", buffer);
262 strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
263 status = strunescape (buffer, sizeof (buffer));
265 EXPECT_EQ_STR ("With \"quotes\"", buffer);
267 /* Backslash before null byte */
268 strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
269 status = strunescape (buffer, sizeof (buffer));
271 EXPECT_EQ_STR ("\tbackslash end", buffer);
274 /* Backslash at buffer end */
275 strncpy (buffer, "\\t3\\56", sizeof (buffer));
276 status = strunescape (buffer, 4);
278 OK(buffer[0] == '\t');
279 OK(buffer[1] == '3');
282 OK(buffer[4] == '5');
283 OK(buffer[5] == '6');
284 OK(buffer[6] == '7');
289 DEF_TEST(parse_values)
296 {"1435044576:42", 0, 42.0},
297 {"1435044576:42:23", -1, NAN},
298 {"1435044576:U", 0, NAN},
300 {"N:42.0:23", -1, NAN},
306 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++)
308 data_source_t dsrc = {
310 .type = DS_TYPE_GAUGE,
328 .host = "example.com",
329 .plugin = "common_test",
334 int status = parse_values (cases[i].buffer, &vl, &ds);
335 EXPECT_EQ_INT (cases[i].status, status);
339 EXPECT_EQ_DOUBLE (cases[i].value, vl.values[0].gauge);
345 DEF_TEST(value_to_rate)
355 { 0, 10, DS_TYPE_DERIVE, {.derive = 0}, {.derive = 1000}, NAN},
356 {10, 20, DS_TYPE_DERIVE, {.derive = 1000}, {.derive = 2000}, 100.0},
357 {20, 30, DS_TYPE_DERIVE, {.derive = 2000}, {.derive = 1800}, -20.0},
358 { 0, 10, DS_TYPE_COUNTER, {.counter = 0}, {.counter = 1000}, NAN},
359 {10, 20, DS_TYPE_COUNTER, {.counter = 1000}, {.counter = 5000}, 400.0},
360 /* 32bit wrap-around. */
361 {20, 30, DS_TYPE_COUNTER, {.counter = 4294967238ULL}, {.counter = 42}, 10.0},
362 /* 64bit wrap-around. */
363 {30, 40, DS_TYPE_COUNTER, {.counter = 18446744073709551558ULL}, {.counter = 42}, 10.0},
367 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
368 value_to_rate_state_t state = { cases[i].v0, TIME_T_TO_CDTIME_T (cases[i].t0) };
371 if (cases[i].t0 == 0) {
372 OK(value_to_rate (&got, cases[i].v1, cases[i].ds_type, TIME_T_TO_CDTIME_T(cases[i].t1), &state) == EAGAIN);
376 OK(value_to_rate (&got, cases[i].v1, cases[i].ds_type, TIME_T_TO_CDTIME_T(cases[i].t1), &state) == 0);
377 EXPECT_EQ_DOUBLE(cases[i].want, got);
390 RUN_TEST(escape_slashes);
391 RUN_TEST(escape_string);
392 RUN_TEST(strunescape);
393 RUN_TEST(parse_values);
394 RUN_TEST(value_to_rate);
399 /* vim: set sw=2 sts=2 et : */