From: Florian Forster Date: Sun, 19 May 2013 06:59:04 +0000 (+0200) Subject: src/common_test.c: Add some tests for files in src/common.c. X-Git-Tag: collectd-5.5.0~277 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=855ba3880b50bc4a774a8c8d23612bba3550b3e7;p=collectd.git src/common_test.c: Add some tests for files in src/common.c. --- diff --git a/src/Makefile.am b/src/Makefile.am index 1ffd7854..520a6288 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1565,15 +1565,24 @@ uninstall-hook: rm -f $(DESTDIR)$(sysconfdir)/collectd.conf rm -f $(DESTDIR)$(pkgdatadir)/postgresql_default.conf; -if BUILD_FEATURE_DEBUG -bin_PROGRAMS += utils_vl_lookup_test +check_PROGRAMS = common_test utils_vl_lookup_test + +common_test_SOURCES = common_test.c \ + common.h common.c \ + mock/plugin.c \ + mock/utils_cache.c \ + mock/utils_time.c +common_test_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) +common_test_LDFLAGS = -export-dynamic +common_test_LDADD = + utils_vl_lookup_test_SOURCES = utils_vl_lookup_test.c \ utils_vl_lookup.h utils_vl_lookup.c \ utils_avltree.c utils_avltree.h \ common.h - -utils_vl_lookup_test_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) -DBUILD_TEST=1 +utils_vl_lookup_test_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) utils_vl_lookup_test_CFLAGS = $(AM_CFLAGS) utils_vl_lookup_test_LDFLAGS = -export-dynamic utils_vl_lookup_test_LDADD = -endif + +TESTS = common_test utils_vl_lookup_test diff --git a/src/common.c b/src/common.c index 18b5c432..1608d520 100644 --- a/src/common.c +++ b/src/common.c @@ -377,8 +377,10 @@ int strunescape (char *buf, size_t buf_len) if (buf[i] != '\\') continue; - if ((i >= buf_len) || (buf[i + 1] == '\0')) { + if (((i + 1) >= buf_len) || (buf[i + 1] == 0)) { ERROR ("string unescape: backslash found at end of string."); + /* Ensure null-byte at the end of the buffer. */ + buf[i] = 0; return (-1); } @@ -397,7 +399,10 @@ int strunescape (char *buf, size_t buf_len) break; } + /* Move everything after the position one position to the left. + * Add a null-byte as last character in the buffer. */ memmove (buf + i + 1, buf + i + 2, buf_len - i - 2); + buf[buf_len - 1] = 0; } return (0); } /* int strunescape */ diff --git a/src/common_test.c b/src/common_test.c new file mode 100644 index 00000000..a0d282cd --- /dev/null +++ b/src/common_test.c @@ -0,0 +1,276 @@ +/** + * collectd - src/common_test.c + * + * Copyright (C) 2013 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "common.h" + +static int fail_count = 0; +static int check_count = 0; + +#define TEST(func) do { \ + int status; \ + printf ("Testing %s ...\n", #func); \ + status = test_ ## func (); \ + printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \ + if (status != 0) { fail_count++; } \ +} while (0); + +#define OK1(cond, text) do { \ + _Bool result = (cond); \ + printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count, text); \ + if (!result) { return (-1); } \ +} while (0); + +#define STREQ(expect, actual) do { \ + if (strcmp (expect, actual) != 0) { \ + printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \ + ++check_count, #actual, expect, actual); \ + return (-1); \ + } \ + printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \ +} while (0) + +#define OK(cond) OK1(cond, #cond) + +static int test_sstrncpy (void) +{ + char buffer[16] = ""; + char *ptr = &buffer[4]; + char *ret; + + buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff; + buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff; + + ret = sstrncpy (ptr, "foobar", 8); + OK(ret == ptr); + STREQ ("foobar", ptr); + OK(buffer[3] == buffer[12]); + + ret = sstrncpy (ptr, "abc", 8); + OK(ret == ptr); + STREQ ("abc", ptr); + OK(buffer[3] == buffer[12]); + + ret = sstrncpy (ptr, "collectd", 8); + OK(ret == ptr); + OK(ptr[7] == 0); + STREQ ("collect", ptr); + OK(buffer[3] == buffer[12]); + + return (0); +} + +static int test_ssnprintf (void) +{ + char buffer[16] = ""; + char *ptr = &buffer[4]; + int status; + + buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff; + buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff; + + status = ssnprintf (ptr, 8, "%i", 1337); + OK(status == 4); + STREQ ("1337", ptr); + + status = ssnprintf (ptr, 8, "%s", "collectd"); + OK(status == 8); + OK(ptr[7] == 0); + STREQ ("collect", ptr); + OK(buffer[3] == buffer[12]); + + return (0); +} + +static int test_sstrdup (void) +{ + char *ptr; + + ptr = sstrdup ("collectd"); + OK(ptr != NULL); + STREQ ("collectd", ptr); + + sfree(ptr); + OK(ptr == NULL); + + ptr = sstrdup (NULL); + OK(ptr == NULL); + + return (0); +} + +static int test_strsplit (void) +{ + char buffer[32]; + char *fields[8]; + int status; + + strncpy (buffer, "foo bar", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 2); + STREQ ("foo", fields[0]); + STREQ ("bar", fields[1]); + + strncpy (buffer, "foo \t bar", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 2); + STREQ ("foo", fields[0]); + STREQ ("bar", fields[1]); + + strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 5); + STREQ ("one", fields[0]); + STREQ ("two", fields[1]); + STREQ ("three", fields[2]); + STREQ ("four", fields[3]); + STREQ ("five", fields[4]); + + strncpy (buffer, "\twith trailing\n", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 2); + STREQ ("with", fields[0]); + STREQ ("trailing", fields[1]); + + strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 8); + STREQ ("7", fields[6]); + STREQ ("8", fields[7]); + + strncpy (buffer, "single", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 1); + STREQ ("single", fields[0]); + + strncpy (buffer, "", sizeof (buffer)); + status = strsplit (buffer, fields, 8); + OK(status == 0); + + return (0); +} + +int test_strjoin (void) +{ + char buffer[16]; + char *fields[4]; + int status; + + fields[0] = "foo"; + fields[1] = "bar"; + fields[2] = "baz"; + fields[3] = "qux"; + + status = strjoin (buffer, sizeof (buffer), fields, 2, "!"); + OK(status == 7); + STREQ ("foo!bar", buffer); + + status = strjoin (buffer, sizeof (buffer), fields, 1, "!"); + OK(status == 3); + STREQ ("foo", buffer); + + status = strjoin (buffer, sizeof (buffer), fields, 0, "!"); + OK(status < 0); + + status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht"); + OK(status == 10); + STREQ ("foorchtbar", buffer); + + status = strjoin (buffer, sizeof (buffer), fields, 4, ""); + OK(status == 12); + STREQ ("foobarbazqux", buffer); + + status = strjoin (buffer, sizeof (buffer), fields, 4, "!"); + OK(status == 15); + STREQ ("foo!bar!baz!qux", buffer); + + fields[0] = "0123"; + fields[1] = "4567"; + fields[2] = "8901"; + fields[3] = "2345"; + status = strjoin (buffer, sizeof (buffer), fields, 4, "-"); + OK(status < 0); + + return (0); +} + +static int test_strunescape () +{ + char buffer[16]; + int status; + + strncpy (buffer, "foo\\tbar", sizeof (buffer)); + status = strunescape (buffer, sizeof (buffer)); + OK(status == 0); + STREQ ("foo\tbar", buffer); + + strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer)); + status = strunescape (buffer, sizeof (buffer)); + OK(status == 0); + STREQ ("\tfoo\r\n", buffer); + + strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer)); + status = strunescape (buffer, sizeof (buffer)); + OK(status == 0); + STREQ ("With \"quotes\"", buffer); + + /* Backslash before null byte */ + strncpy (buffer, "\\tbackslash end\\", sizeof (buffer)); + status = strunescape (buffer, sizeof (buffer)); + OK(status != 0); + STREQ ("\tbackslash end", buffer); + return (0); + + /* Backslash at buffer end */ + strncpy (buffer, "\\t3\\56", sizeof (buffer)); + status = strunescape (buffer, 4); + OK(status != 0); + OK(buffer[0] == '\t'); + OK(buffer[1] == '3'); + OK(buffer[2] == 0); + OK(buffer[3] == 0); + OK(buffer[4] == '5'); + OK(buffer[5] == '6'); + OK(buffer[6] == '7'); + + return (0); +} + +int main (void) +{ + TEST(sstrncpy); + TEST(ssnprintf); + TEST(sstrdup); + TEST(strsplit); + TEST(strjoin); + TEST(strunescape); + + if (fail_count != 0) + exit (EXIT_FAILURE); + exit (EXIT_SUCCESS); +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/mock/plugin.c b/src/mock/plugin.c new file mode 100644 index 00000000..cb3c65af --- /dev/null +++ b/src/mock/plugin.c @@ -0,0 +1,42 @@ +/** + * collectd - src/mock/plugin.c + * + * Copyright (C) 2013 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "plugin.h" + +void plugin_log (int level, char const *format, ...) +{ + char buffer[1024]; + va_list ap; + + va_start (ap, format); + vsnprintf (buffer, sizeof (buffer), format, ap); + va_end (ap); + + printf ("plugin_log (%i, \"%s\");\n", level, buffer); +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/mock/utils_cache.c b/src/mock/utils_cache.c new file mode 100644 index 00000000..8c8b55b8 --- /dev/null +++ b/src/mock/utils_cache.c @@ -0,0 +1,33 @@ +/** + * collectd - src/mock/utils_cache.c + * + * Copyright (C) 2013 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "utils_cache.h" + +gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl) +{ + return (NULL); +} diff --git a/src/mock/utils_time.c b/src/mock/utils_time.c new file mode 100644 index 00000000..38b59caa --- /dev/null +++ b/src/mock/utils_time.c @@ -0,0 +1,34 @@ +/** + * collectd - src/mock/utils_time.c + * + * Copyright (C) 2013 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "utils_time.h" + +cdtime_t cdtime (void) +{ + return (0); +} +