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(strunescape)
221 strncpy (buffer, "foo\\tbar", sizeof (buffer));
222 status = strunescape (buffer, sizeof (buffer));
224 STREQ ("foo\tbar", buffer);
226 strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
227 status = strunescape (buffer, sizeof (buffer));
229 STREQ ("\tfoo\r\n", buffer);
231 strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
232 status = strunescape (buffer, sizeof (buffer));
234 STREQ ("With \"quotes\"", buffer);
236 /* Backslash before null byte */
237 strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
238 status = strunescape (buffer, sizeof (buffer));
240 STREQ ("\tbackslash end", buffer);
243 /* Backslash at buffer end */
244 strncpy (buffer, "\\t3\\56", sizeof (buffer));
245 status = strunescape (buffer, 4);
247 OK(buffer[0] == '\t');
248 OK(buffer[1] == '3');
251 OK(buffer[4] == '5');
252 OK(buffer[5] == '6');
253 OK(buffer[6] == '7');
258 DEF_TEST(parse_values)
265 {"1435044576:42", 0, 42.0},
266 {"1435044576:42:23", -1, NAN},
267 {"1435044576:U", 0, NAN},
269 {"N:42.0:23", -1, NAN},
275 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++)
277 data_source_t dsrc = {
279 .type = DS_TYPE_GAUGE,
297 .host = "example.com",
298 .plugin = "common_test",
303 int status = parse_values (cases[i].buffer, &vl, &ds);
304 OK(status == cases[i].status);
308 OK(cases[i].value == vl.values[0].gauge);
321 RUN_TEST(escape_slashes);
322 RUN_TEST(strunescape);
323 RUN_TEST(parse_values);
328 /* vim: set sw=2 sts=2 et : */