From: Florian Forster Date: Tue, 26 May 2015 06:52:39 +0000 (+0200) Subject: Build system: Build tested units as libraries. X-Git-Tag: collectd-5.5.0~2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a644c803e14a19015acfbf5368e336c359b250e3;p=collectd.git Build system: Build tested units as libraries. This simplifies the build rules for the tests, aka. check programs. * test_foo.c have been renamed to foo_test.c. * foo_test.c now reside right next to foo.c and foo.h. * Build and refer to .la files, rather than depending on .c files from other directories. Fixes: #1042 --- diff --git a/src/Makefile.am b/src/Makefile.am index 08b8d525..37d9216f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,6 +23,14 @@ AM_CPPFLAGS += -DPKGDATADIR='"${pkgdatadir}"' AUTOMAKE_OPTIONS = subdir-objects +noinst_LTLIBRARIES = libmount.la liblookup.la + +libmount_la_SOURCES = utils_mount.c utils_mount.h +libmount_la_LIBADD = daemon/libcommon.la + +liblookup_la_SOURCES = utils_vl_lookup.c utils_vl_lookup.h +liblookup_la_LIBADD = daemon/libavltree.la daemon/libcommon.la + sbin_PROGRAMS = collectdmon bin_PROGRAMS = collectd-nagios collectdctl collectd-tg @@ -55,10 +63,9 @@ endif collectdctl_LDADD += libcollectdclient/libcollectdclient.la collectdctl_DEPENDENCIES = libcollectdclient/libcollectdclient.la -collectd_tg_SOURCES = collectd-tg.c \ - daemon/utils_heap.c daemon/utils_heap.h +collectd_tg_SOURCES = collectd-tg.c collectd_tg_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src/libcollectdclient/collectd -I$(top_builddir)/src/libcollectdclient/collectd -collectd_tg_LDADD = +collectd_tg_LDADD = daemon/libheap.a if BUILD_WITH_LIBSOCKET collectd_tg_LDADD += -lsocket endif @@ -183,9 +190,9 @@ endif if BUILD_PLUGIN_CGROUPS pkglib_LTLIBRARIES += cgroups.la cgroups_la_SOURCES = cgroups.c \ - utils_ignorelist.c utils_ignorelist.h \ - utils_mount.c utils_mount.h + utils_ignorelist.c utils_ignorelist.h cgroups_la_LDFLAGS = $(PLUGIN_LDFLAGS) +cgroups_la_LIBADD = libmount.la endif if BUILD_PLUGIN_CONNTRACK @@ -283,9 +290,9 @@ endif if BUILD_PLUGIN_DF pkglib_LTLIBRARIES += df.la df_la_SOURCES = df.c \ - utils_ignorelist.c utils_ignorelist.h \ - utils_mount.c utils_mount.h + utils_ignorelist.c utils_ignorelist.h df_la_LDFLAGS = $(PLUGIN_LDFLAGS) +df_la_LIBADD = libmount.la endif if BUILD_PLUGIN_DISK @@ -1373,49 +1380,12 @@ uninstall-hook: rm -f $(DESTDIR)$(sysconfdir)/collectd.conf rm -f $(DESTDIR)$(pkgdatadir)/postgresql_default.conf; -check_PROGRAMS = test_common test_utils_avltree test_utils_heap test_utils_mount test_utils_vl_lookup - -test_common_SOURCES = tests/test_common.c \ - daemon/common.h daemon/common.c \ - tests/macros.h \ - tests/mock/plugin.c \ - tests/mock/utils_cache.c \ - tests/mock/utils_time.c -test_common_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) -test_common_LDFLAGS = -export-dynamic -test_common_LDADD = - -test_utils_avltree_SOURCES = tests/test_utils_avltree.c \ - daemon/utils_avltree.c daemon/utils_avltree.h -test_utils_avltree_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) -test_utils_avltree_LDFLAGS = -export-dynamic -test_utils_avltree_LDADD = - -test_utils_heap_SOURCES = tests/test_utils_heap.c \ - daemon/utils_heap.c daemon/utils_heap.h -test_utils_heap_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) -test_utils_heap_LDFLAGS = -export-dynamic -test_utils_heap_LDADD = - -test_utils_mount_SOURCES = tests/test_utils_mount.c \ - utils_mount.c utils_mount.h \ - daemon/common.c daemon/common.h \ - tests/mock/plugin.c \ - tests/mock/utils_cache.c \ - tests/mock/utils_time.c -test_utils_mount_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) -test_utils_mount_LDFLAGS = -export-dynamic -test_utils_mount_LDADD = - -test_utils_vl_lookup_SOURCES = tests/test_utils_vl_lookup.c \ - utils_vl_lookup.h utils_vl_lookup.c \ - daemon/utils_avltree.c daemon/utils_avltree.h \ - daemon/common.c daemon/common.h \ - tests/mock/plugin.c \ - tests/mock/utils_cache.c \ - tests/mock/utils_time.c -test_utils_vl_lookup_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) -test_utils_vl_lookup_LDFLAGS = -export-dynamic -test_utils_vl_lookup_LDADD = - -TESTS = test_common test_utils_avltree test_utils_heap test_utils_mount test_utils_vl_lookup +check_PROGRAMS = test_utils_mount test_utils_vl_lookup + +test_utils_mount_SOURCES = utils_mount_test.c +test_utils_mount_LDADD = libmount.la daemon/libplugin_mock.la + +test_utils_vl_lookup_SOURCES = utils_vl_lookup_test.c +test_utils_vl_lookup_LDADD = liblookup.la daemon/libplugin_mock.la + +TESTS = test_utils_mount test_utils_vl_lookup diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index fc815543..06bc150f 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -17,16 +17,23 @@ AUTOMAKE_OPTIONS = subdir-objects sbin_PROGRAMS = collectd +noinst_LTLIBRARIES = libavltree.la libcommon.la libheap.la libplugin_mock.la + +libavltree_la_SOURCES = utils_avltree.c utils_avltree.h + +libcommon_la_SOURCES = common.c common.h + +libheap_la_SOURCES = utils_heap.c utils_heap.h + +libplugin_mock_la_SOURCES = plugin_mock.c utils_cache_mock.c utils_time_mock.c + collectd_SOURCES = collectd.c collectd.h \ - common.c common.h \ configfile.c configfile.h \ filter_chain.c filter_chain.h \ meta_data.c meta_data.h \ plugin.c plugin.h \ - utils_avltree.c utils_avltree.h \ utils_cache.c utils_cache.h \ utils_complain.c utils_complain.h \ - utils_heap.c utils_heap.h \ utils_llist.c utils_llist.h \ utils_random.c utils_random.h \ utils_tail_match.c utils_tail_match.h \ @@ -41,7 +48,7 @@ collectd_SOURCES = collectd.c collectd.h \ collectd_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) collectd_CFLAGS = $(AM_CFLAGS) collectd_LDFLAGS = -export-dynamic -collectd_LDADD = -lm +collectd_LDADD = libavltree.la libcommon.la libheap.la -lm collectd_DEPENDENCIES = # Link to these libraries.. @@ -83,3 +90,15 @@ collectd_DEPENDENCIES += $(top_builddir)/src/liboconfig/liboconfig.la else collectd_LDADD += -loconfig endif + +check_PROGRAMS = test_common test_utils_avltree test_utils_heap +TESTS = test_common test_utils_avltree test_utils_heap + +test_common_SOURCES = common_test.c +test_common_LDADD = libcommon.la libplugin_mock.la + +test_utils_avltree_SOURCES = utils_avltree_test.c +test_utils_avltree_LDADD = libavltree.la + +test_utils_heap_SOURCES = utils_heap_test.c +test_utils_heap_LDADD = libheap.la diff --git a/src/daemon/common_test.c b/src/daemon/common_test.c new file mode 100644 index 00000000..1fa8f324 --- /dev/null +++ b/src/daemon/common_test.c @@ -0,0 +1,246 @@ +/** + * collectd - src/tests/test_common.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 "testing.h" +#include "common.h" + +DEF_TEST(sstrncpy) +{ + 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); +} + +DEF_TEST(ssnprintf) +{ + 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); +} + +DEF_TEST(sstrdup) +{ + char *ptr; + + ptr = sstrdup ("collectd"); + OK(ptr != NULL); + STREQ ("collectd", ptr); + + sfree(ptr); + OK(ptr == NULL); + + ptr = sstrdup (NULL); + OK(ptr == NULL); + + return (0); +} + +DEF_TEST(strsplit) +{ + 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); +} + +DEF_TEST(strjoin) +{ + 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); +} + +DEF_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) +{ + RUN_TEST(sstrncpy); + RUN_TEST(ssnprintf); + RUN_TEST(sstrdup); + RUN_TEST(strsplit); + RUN_TEST(strjoin); + RUN_TEST(strunescape); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/daemon/plugin_mock.c b/src/daemon/plugin_mock.c new file mode 100644 index 00000000..86528809 --- /dev/null +++ b/src/daemon/plugin_mock.c @@ -0,0 +1,41 @@ +/** + * collectd - src/tests/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/daemon/utils_avltree_test.c b/src/daemon/utils_avltree_test.c new file mode 100644 index 00000000..2a8244c9 --- /dev/null +++ b/src/daemon/utils_avltree_test.c @@ -0,0 +1,82 @@ +/** + * collectd - src/tests/test_utils_avltree.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 "testing.h" +#include "collectd.h" +#include "utils_avltree.h" + +static int compare_total_count = 0; +#define RESET_COUNTS() do { compare_total_count = 0; } while (0) + +static int compare_callback (void const *v0, void const *v1) +{ + assert (v0 != NULL); + assert (v1 != NULL); + + compare_total_count++; + return (strcmp (v0, v1)); +} + +DEF_TEST(success) +{ + c_avl_tree_t *t; + char key_orig[] = "foo"; + char value_orig[] = "bar"; + char *key_ret = NULL; + char *value_ret = NULL; + + RESET_COUNTS (); + t = c_avl_create (compare_callback); + OK (t != NULL); + + OK (c_avl_insert (t, key_orig, value_orig) == 0); + OK (c_avl_size (t) == 1); + + /* Key already exists. */ + OK (c_avl_insert (t, "foo", "qux") > 0); + + OK (c_avl_get (t, "foo", (void *) &value_ret) == 0); + OK (value_ret == &value_orig[0]); + + key_ret = value_ret = NULL; + OK (c_avl_remove (t, "foo", (void *) &key_ret, (void *) &value_ret) == 0); + OK (key_ret == &key_orig[0]); + OK (value_ret == &value_orig[0]); + OK (c_avl_size (t) == 0); + + c_avl_destroy (t); + + return (0); +} + +int main (void) +{ + RUN_TEST(success); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/daemon/utils_cache_mock.c b/src/daemon/utils_cache_mock.c new file mode 100644 index 00000000..6c78d64d --- /dev/null +++ b/src/daemon/utils_cache_mock.c @@ -0,0 +1,32 @@ +/** + * collectd - src/tests/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/daemon/utils_heap_test.c b/src/daemon/utils_heap_test.c new file mode 100644 index 00000000..53d0fba8 --- /dev/null +++ b/src/daemon/utils_heap_test.c @@ -0,0 +1,85 @@ +/** + * collectd - src/tests/test_utils_heap.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 "testing.h" +#include "collectd.h" +#include "utils_heap.h" + +static int compare (void const *v0, void const *v1) +{ + int const *i0 = v0; + int const *i1 = v1; + + if ((*i0) < (*i1)) + return -1; + else if ((*i0) > (*i1)) + return 1; + else + return 0; +} + +DEF_TEST(simple) +{ + int values[] = { 9, 5, 6, 1, 3, 4, 0, 8, 2, 7 }; + int i; + c_heap_t *h; + + CHECK_NOT_NULL(h = c_heap_create (compare)); + for (i = 0; i < 10; i++) + CHECK_ZERO(c_heap_insert (h, &values[i])); + + for (i = 0; i < 5; i++) + { + int *ret = NULL; + CHECK_NOT_NULL(ret = c_heap_get_root(h)); + OK(*ret == i); + } + + CHECK_ZERO(c_heap_insert (h, &values[6] /* = 0 */)); + CHECK_ZERO(c_heap_insert (h, &values[3] /* = 1 */)); + CHECK_ZERO(c_heap_insert (h, &values[8] /* = 2 */)); + CHECK_ZERO(c_heap_insert (h, &values[4] /* = 3 */)); + CHECK_ZERO(c_heap_insert (h, &values[5] /* = 4 */)); + + for (i = 0; i < 10; i++) + { + int *ret = NULL; + CHECK_NOT_NULL(ret = c_heap_get_root(h)); + OK(*ret == i); + } + + c_heap_destroy(h); + return (0); +} + +int main (void) +{ + RUN_TEST(simple); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/daemon/utils_time_mock.c b/src/daemon/utils_time_mock.c new file mode 100644 index 00000000..5edfe6f9 --- /dev/null +++ b/src/daemon/utils_time_mock.c @@ -0,0 +1,33 @@ +/** + * collectd - src/tests/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); +} + diff --git a/src/testing.h b/src/testing.h new file mode 100644 index 00000000..5df1b83a --- /dev/null +++ b/src/testing.h @@ -0,0 +1,67 @@ +/** + * collectd - src/tests/macros.h + * 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 + */ + +static int fail_count__ = 0; +static int check_count__ = 0; + +#define DEF_TEST(func) static int test_##func () + +#define RUN_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 END_TEST exit ((fail_count__ == 0) ? 0 : 1); + +#define OK1(cond, text) do { \ + _Bool result = (cond); \ + printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count__, text); \ +} while (0) +#define OK(cond) OK1(cond, #cond) + +#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 CHECK_NOT_NULL(expr) do { \ + void *ptr_; \ + ptr_ = (expr); \ + OK1(ptr_ != NULL, #expr); \ +} while (0) + +#define CHECK_ZERO(expr) do { \ + long status_; \ + status_ = (long) (expr); \ + OK1(status_ == 0L, #expr); \ +} while (0) diff --git a/src/tests/common_test.c b/src/tests/common_test.c deleted file mode 100644 index f65fcab3..00000000 --- a/src/tests/common_test.c +++ /dev/null @@ -1,246 +0,0 @@ -/** - * collectd - src/tests/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 "tests/macros.h" -#include "common.h" - -DEF_TEST(sstrncpy) -{ - 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); -} - -DEF_TEST(ssnprintf) -{ - 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); -} - -DEF_TEST(sstrdup) -{ - char *ptr; - - ptr = sstrdup ("collectd"); - OK(ptr != NULL); - STREQ ("collectd", ptr); - - sfree(ptr); - OK(ptr == NULL); - - ptr = sstrdup (NULL); - OK(ptr == NULL); - - return (0); -} - -DEF_TEST(strsplit) -{ - 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); -} - -DEF_TEST(strjoin) -{ - 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); -} - -DEF_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) -{ - RUN_TEST(sstrncpy); - RUN_TEST(ssnprintf); - RUN_TEST(sstrdup); - RUN_TEST(strsplit); - RUN_TEST(strjoin); - RUN_TEST(strunescape); - - END_TEST; -} - -/* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/macros.h b/src/tests/macros.h deleted file mode 100644 index 5df1b83a..00000000 --- a/src/tests/macros.h +++ /dev/null @@ -1,67 +0,0 @@ -/** - * collectd - src/tests/macros.h - * 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 - */ - -static int fail_count__ = 0; -static int check_count__ = 0; - -#define DEF_TEST(func) static int test_##func () - -#define RUN_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 END_TEST exit ((fail_count__ == 0) ? 0 : 1); - -#define OK1(cond, text) do { \ - _Bool result = (cond); \ - printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count__, text); \ -} while (0) -#define OK(cond) OK1(cond, #cond) - -#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 CHECK_NOT_NULL(expr) do { \ - void *ptr_; \ - ptr_ = (expr); \ - OK1(ptr_ != NULL, #expr); \ -} while (0) - -#define CHECK_ZERO(expr) do { \ - long status_; \ - status_ = (long) (expr); \ - OK1(status_ == 0L, #expr); \ -} while (0) diff --git a/src/tests/mock/plugin.c b/src/tests/mock/plugin.c deleted file mode 100644 index 86528809..00000000 --- a/src/tests/mock/plugin.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * collectd - src/tests/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/tests/mock/utils_cache.c b/src/tests/mock/utils_cache.c deleted file mode 100644 index 6c78d64d..00000000 --- a/src/tests/mock/utils_cache.c +++ /dev/null @@ -1,32 +0,0 @@ -/** - * collectd - src/tests/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/tests/mock/utils_time.c b/src/tests/mock/utils_time.c deleted file mode 100644 index 5edfe6f9..00000000 --- a/src/tests/mock/utils_time.c +++ /dev/null @@ -1,33 +0,0 @@ -/** - * collectd - src/tests/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); -} - diff --git a/src/tests/test_common.c b/src/tests/test_common.c deleted file mode 100644 index c2eec953..00000000 --- a/src/tests/test_common.c +++ /dev/null @@ -1,246 +0,0 @@ -/** - * collectd - src/tests/test_common.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 "tests/macros.h" -#include "common.h" - -DEF_TEST(sstrncpy) -{ - 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); -} - -DEF_TEST(ssnprintf) -{ - 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); -} - -DEF_TEST(sstrdup) -{ - char *ptr; - - ptr = sstrdup ("collectd"); - OK(ptr != NULL); - STREQ ("collectd", ptr); - - sfree(ptr); - OK(ptr == NULL); - - ptr = sstrdup (NULL); - OK(ptr == NULL); - - return (0); -} - -DEF_TEST(strsplit) -{ - 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); -} - -DEF_TEST(strjoin) -{ - 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); -} - -DEF_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) -{ - RUN_TEST(sstrncpy); - RUN_TEST(ssnprintf); - RUN_TEST(sstrdup); - RUN_TEST(strsplit); - RUN_TEST(strjoin); - RUN_TEST(strunescape); - - END_TEST; -} - -/* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/test_utils_avltree.c b/src/tests/test_utils_avltree.c deleted file mode 100644 index 00d14e16..00000000 --- a/src/tests/test_utils_avltree.c +++ /dev/null @@ -1,82 +0,0 @@ -/** - * collectd - src/tests/test_utils_avltree.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 "tests/macros.h" -#include "collectd.h" -#include "utils_avltree.h" - -static int compare_total_count = 0; -#define RESET_COUNTS() do { compare_total_count = 0; } while (0) - -static int compare_callback (void const *v0, void const *v1) -{ - assert (v0 != NULL); - assert (v1 != NULL); - - compare_total_count++; - return (strcmp (v0, v1)); -} - -DEF_TEST(success) -{ - c_avl_tree_t *t; - char key_orig[] = "foo"; - char value_orig[] = "bar"; - char *key_ret = NULL; - char *value_ret = NULL; - - RESET_COUNTS (); - t = c_avl_create (compare_callback); - OK (t != NULL); - - OK (c_avl_insert (t, key_orig, value_orig) == 0); - OK (c_avl_size (t) == 1); - - /* Key already exists. */ - OK (c_avl_insert (t, "foo", "qux") > 0); - - OK (c_avl_get (t, "foo", (void *) &value_ret) == 0); - OK (value_ret == &value_orig[0]); - - key_ret = value_ret = NULL; - OK (c_avl_remove (t, "foo", (void *) &key_ret, (void *) &value_ret) == 0); - OK (key_ret == &key_orig[0]); - OK (value_ret == &value_orig[0]); - OK (c_avl_size (t) == 0); - - c_avl_destroy (t); - - return (0); -} - -int main (void) -{ - RUN_TEST(success); - - END_TEST; -} - -/* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/test_utils_heap.c b/src/tests/test_utils_heap.c deleted file mode 100644 index 91c90e5d..00000000 --- a/src/tests/test_utils_heap.c +++ /dev/null @@ -1,85 +0,0 @@ -/** - * collectd - src/tests/test_utils_heap.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 "collectd.h" -#include "tests/macros.h" -#include "utils_heap.h" - -static int compare (void const *v0, void const *v1) -{ - int const *i0 = v0; - int const *i1 = v1; - - if ((*i0) < (*i1)) - return -1; - else if ((*i0) > (*i1)) - return 1; - else - return 0; -} - -DEF_TEST(simple) -{ - int values[] = { 9, 5, 6, 1, 3, 4, 0, 8, 2, 7 }; - int i; - c_heap_t *h; - - CHECK_NOT_NULL(h = c_heap_create (compare)); - for (i = 0; i < 10; i++) - CHECK_ZERO(c_heap_insert (h, &values[i])); - - for (i = 0; i < 5; i++) - { - int *ret = NULL; - CHECK_NOT_NULL(ret = c_heap_get_root(h)); - OK(*ret == i); - } - - CHECK_ZERO(c_heap_insert (h, &values[6] /* = 0 */)); - CHECK_ZERO(c_heap_insert (h, &values[3] /* = 1 */)); - CHECK_ZERO(c_heap_insert (h, &values[8] /* = 2 */)); - CHECK_ZERO(c_heap_insert (h, &values[4] /* = 3 */)); - CHECK_ZERO(c_heap_insert (h, &values[5] /* = 4 */)); - - for (i = 0; i < 10; i++) - { - int *ret = NULL; - CHECK_NOT_NULL(ret = c_heap_get_root(h)); - OK(*ret == i); - } - - c_heap_destroy(h); - return (0); -} - -int main (void) -{ - RUN_TEST(simple); - - END_TEST; -} - -/* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/test_utils_mount.c b/src/tests/test_utils_mount.c deleted file mode 100644 index ba6b99bb..00000000 --- a/src/tests/test_utils_mount.c +++ /dev/null @@ -1,101 +0,0 @@ -/** - * collectd - src/tests/test_utils_mount.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 "tests/macros.h" -#include "collectd.h" -#include "utils_mount.h" - -DEF_TEST(cu_mount_checkoption) -{ - char line_opts[] = "foo=one,bar=two,qux=three"; - char *foo = strstr (line_opts, "foo"); - char *bar = strstr (line_opts, "bar"); - char *qux = strstr (line_opts, "qux"); - - char line_bool[] = "one,two,three"; - char *one = strstr (line_bool, "one"); - char *two = strstr (line_bool, "two"); - char *three = strstr (line_bool, "three"); - - /* Normal operation */ - OK (foo == cu_mount_checkoption (line_opts, "foo", 0)); - OK (bar == cu_mount_checkoption (line_opts, "bar", 0)); - OK (qux == cu_mount_checkoption (line_opts, "qux", 0)); - OK (NULL == cu_mount_checkoption (line_opts, "unknown", 0)); - - OK (one == cu_mount_checkoption (line_bool, "one", 0)); - OK (two == cu_mount_checkoption (line_bool, "two", 0)); - OK (three == cu_mount_checkoption (line_bool, "three", 0)); - OK (NULL == cu_mount_checkoption (line_bool, "four", 0)); - - /* Shorter and longer parts */ - OK (foo == cu_mount_checkoption (line_opts, "fo", 0)); - OK (bar == cu_mount_checkoption (line_opts, "bar=", 0)); - OK (qux == cu_mount_checkoption (line_opts, "qux=thr", 0)); - - OK (one == cu_mount_checkoption (line_bool, "o", 0)); - OK (two == cu_mount_checkoption (line_bool, "tw", 0)); - OK (three == cu_mount_checkoption (line_bool, "thr", 0)); - - /* "full" flag */ - OK (one == cu_mount_checkoption (line_bool, "one", 1)); - OK (two == cu_mount_checkoption (line_bool, "two", 1)); - OK (three == cu_mount_checkoption (line_bool, "three", 1)); - OK (NULL == cu_mount_checkoption (line_bool, "four", 1)); - - OK (NULL == cu_mount_checkoption (line_bool, "o", 1)); - OK (NULL == cu_mount_checkoption (line_bool, "tw", 1)); - OK (NULL == cu_mount_checkoption (line_bool, "thr", 1)); - - return (0); -} -DEF_TEST(cu_mount_getoptionvalue) -{ - char line_opts[] = "foo=one,bar=two,qux=three"; - char line_bool[] = "one,two,three"; - - STREQ ("one", cu_mount_getoptionvalue (line_opts, "foo=")); - STREQ ("two", cu_mount_getoptionvalue (line_opts, "bar=")); - STREQ ("three", cu_mount_getoptionvalue (line_opts, "qux=")); - OK (NULL == cu_mount_getoptionvalue (line_opts, "unknown=")); - - STREQ ("", cu_mount_getoptionvalue (line_bool, "one")); - STREQ ("", cu_mount_getoptionvalue (line_bool, "two")); - STREQ ("", cu_mount_getoptionvalue (line_bool, "three")); - OK (NULL == cu_mount_getoptionvalue (line_bool, "four")); - - return (0); -} - -int main (void) -{ - RUN_TEST(cu_mount_checkoption); - RUN_TEST(cu_mount_getoptionvalue); - - END_TEST; -} - -/* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/test_utils_vl_lookup.c b/src/tests/test_utils_vl_lookup.c deleted file mode 100644 index 842f3fd3..00000000 --- a/src/tests/test_utils_vl_lookup.c +++ /dev/null @@ -1,249 +0,0 @@ -/** - * collectd - src/tests/test_utils_vl_lookup.c - * Copyright (C) 2012 Florian 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 Forster - **/ - -#include "tests/macros.h" -#include "collectd.h" -#include "utils_vl_lookup.h" - -static _Bool expect_new_obj = 0; -static _Bool have_new_obj = 0; - -static identifier_t last_class_ident; -static identifier_t last_obj_ident; - -static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN }; -static data_set_t const ds_test = { "test", 1, &dsrc_test }; - -static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN }; -static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown }; - -static int lookup_obj_callback (data_set_t const *ds, - value_list_t const *vl, - void *user_class, void *user_obj) -{ - identifier_t *class = user_class; - identifier_t *obj = user_obj; - - OK1(expect_new_obj == have_new_obj, - (expect_new_obj ? "New obj is created." : "Updating existing obj.")); - - memcpy (&last_class_ident, class, sizeof (last_class_ident)); - memcpy (&last_obj_ident, obj, sizeof (last_obj_ident)); - - if (strcmp (obj->plugin_instance, "failure") == 0) - return (-1); - - return (0); -} - -static void *lookup_class_callback (data_set_t const *ds, - value_list_t const *vl, void *user_class) -{ - identifier_t *class = user_class; - identifier_t *obj; - - OK(expect_new_obj); - - memcpy (&last_class_ident, class, sizeof (last_class_ident)); - - obj = malloc (sizeof (*obj)); - strncpy (obj->host, vl->host, sizeof (obj->host)); - strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin)); - strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance)); - strncpy (obj->type, vl->type, sizeof (obj->type)); - strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance)); - - have_new_obj = 1; - - return ((void *) obj); -} - -static void checked_lookup_add (lookup_t *obj, /* {{{ */ - char const *host, - char const *plugin, char const *plugin_instance, - char const *type, char const *type_instance, - unsigned int group_by) -{ - identifier_t ident; - void *user_class; - - memset (&ident, 0, sizeof (ident)); - strncpy (ident.host, host, sizeof (ident.host)); - strncpy (ident.plugin, plugin, sizeof (ident.plugin)); - strncpy (ident.plugin_instance, plugin_instance, sizeof (ident.plugin_instance)); - strncpy (ident.type, type, sizeof (ident.type)); - strncpy (ident.type_instance, type_instance, sizeof (ident.type_instance)); - - user_class = malloc (sizeof (ident)); - memmove (user_class, &ident, sizeof (ident)); - - OK(lookup_add (obj, &ident, group_by, user_class) == 0); -} /* }}} void test_add */ - -static int checked_lookup_search (lookup_t *obj, - char const *host, - char const *plugin, char const *plugin_instance, - char const *type, char const *type_instance, - _Bool expect_new) -{ - int status; - value_list_t vl = VALUE_LIST_STATIC; - data_set_t const *ds = &ds_unknown; - - strncpy (vl.host, host, sizeof (vl.host)); - strncpy (vl.plugin, plugin, sizeof (vl.plugin)); - strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); - strncpy (vl.type, type, sizeof (vl.type)); - strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - - if (strcmp (vl.type, "test") == 0) - ds = &ds_test; - - expect_new_obj = expect_new; - have_new_obj = 0; - - status = lookup_search (obj, ds, &vl); - return (status); -} - -static lookup_t *checked_lookup_create (void) -{ - lookup_t *obj = lookup_create ( - lookup_class_callback, - lookup_obj_callback, - (void *) free, - (void *) free); - OK(obj != NULL); - return (obj); -} - -DEF_TEST(group_by_specific_host) -{ - lookup_t *obj = checked_lookup_create (); - - checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST); - checked_lookup_search (obj, "host0", "test", "", "test", "0", - /* expect new = */ 1); - checked_lookup_search (obj, "host0", "test", "", "test", "1", - /* expect new = */ 0); - checked_lookup_search (obj, "host1", "test", "", "test", "0", - /* expect new = */ 1); - checked_lookup_search (obj, "host1", "test", "", "test", "1", - /* expect new = */ 0); - - lookup_destroy (obj); - return (0); -} - -DEF_TEST(group_by_any_host) -{ - lookup_t *obj = checked_lookup_create (); - - checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST); - checked_lookup_search (obj, "host0", "plugin0", "", "test", "0", - /* expect new = */ 1); - checked_lookup_search (obj, "host0", "plugin0", "", "test", "1", - /* expect new = */ 0); - checked_lookup_search (obj, "host0", "plugin1", "", "test", "0", - /* expect new = */ 0); - checked_lookup_search (obj, "host0", "plugin1", "", "test", "1", - /* expect new = */ 0); - checked_lookup_search (obj, "host1", "plugin0", "", "test", "0", - /* expect new = */ 1); - checked_lookup_search (obj, "host1", "plugin0", "", "test", "1", - /* expect new = */ 0); - checked_lookup_search (obj, "host1", "plugin1", "", "test", "0", - /* expect new = */ 0); - checked_lookup_search (obj, "host1", "plugin1", "", "test", "1", - /* expect new = */ 0); - - lookup_destroy (obj); - return (0); -} - -DEF_TEST(multiple_lookups) -{ - lookup_t *obj = checked_lookup_create (); - int status; - - checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST); - checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST); - - status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "", - /* expect new = */ 0); - assert (status == 0); - status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "", - /* expect new = */ 1); - assert (status == 1); - status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "ti0", - /* expect new = */ 1); - assert (status == 1); - status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "ti0", - /* expect new = */ 0); - assert (status == 2); - - lookup_destroy (obj); - return (0); -} - -DEF_TEST(regex) -{ - lookup_t *obj = checked_lookup_create (); - - checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/", - LU_GROUP_BY_TYPE_INSTANCE); - checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "user", - /* expect new = */ 1); - checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "idle", - /* expect new = */ 1); - checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "user", - /* expect new = */ 0); - checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "idle", - /* expect new = */ 0); - checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "user", - /* expect new = */ 0); - checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "idle", - /* expect new = */ 0); - checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "user", - /* expect new = */ 0); - checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "idle", - /* expect new = */ 0); - checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "system", - /* expect new = */ 1); - - lookup_destroy (obj); - return (0); -} - -int main (int argc, char **argv) /* {{{ */ -{ - RUN_TEST(group_by_specific_host); - RUN_TEST(group_by_any_host); - RUN_TEST(multiple_lookups); - RUN_TEST(regex); - - END_TEST; -} /* }}} int main */ diff --git a/src/utils_mount.c b/src/utils_mount.c index 3cede018..b63a81ad 100644 --- a/src/utils_mount.c +++ b/src/utils_mount.c @@ -21,20 +21,18 @@ * Niki W. Waibel **/ -#if HAVE_CONFIG_H -# include "config.h" -#endif +#include "collectd.h" +#include "utils_mount.h" + +#include "common.h" /* sstrncpy() et alii */ +#include "plugin.h" /* ERROR() macro */ -#include "common.h" #if HAVE_XFS_XQM_H # include #define XFS_SUPER_MAGIC_STR "XFSB" #define XFS_SUPER_MAGIC2_STR "BSFX" #endif -#include "plugin.h" -#include "utils_mount.h" - #if HAVE_GETVFSSTAT # if HAVE_SYS_TYPES_H # include diff --git a/src/utils_mount_test.c b/src/utils_mount_test.c new file mode 100644 index 00000000..c5ffbfbc --- /dev/null +++ b/src/utils_mount_test.c @@ -0,0 +1,101 @@ +/** + * collectd - src/tests/test_utils_mount.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 "testing.h" +#include "collectd.h" +#include "utils_mount.h" + +DEF_TEST(cu_mount_checkoption) +{ + char line_opts[] = "foo=one,bar=two,qux=three"; + char *foo = strstr (line_opts, "foo"); + char *bar = strstr (line_opts, "bar"); + char *qux = strstr (line_opts, "qux"); + + char line_bool[] = "one,two,three"; + char *one = strstr (line_bool, "one"); + char *two = strstr (line_bool, "two"); + char *three = strstr (line_bool, "three"); + + /* Normal operation */ + OK (foo == cu_mount_checkoption (line_opts, "foo", 0)); + OK (bar == cu_mount_checkoption (line_opts, "bar", 0)); + OK (qux == cu_mount_checkoption (line_opts, "qux", 0)); + OK (NULL == cu_mount_checkoption (line_opts, "unknown", 0)); + + OK (one == cu_mount_checkoption (line_bool, "one", 0)); + OK (two == cu_mount_checkoption (line_bool, "two", 0)); + OK (three == cu_mount_checkoption (line_bool, "three", 0)); + OK (NULL == cu_mount_checkoption (line_bool, "four", 0)); + + /* Shorter and longer parts */ + OK (foo == cu_mount_checkoption (line_opts, "fo", 0)); + OK (bar == cu_mount_checkoption (line_opts, "bar=", 0)); + OK (qux == cu_mount_checkoption (line_opts, "qux=thr", 0)); + + OK (one == cu_mount_checkoption (line_bool, "o", 0)); + OK (two == cu_mount_checkoption (line_bool, "tw", 0)); + OK (three == cu_mount_checkoption (line_bool, "thr", 0)); + + /* "full" flag */ + OK (one == cu_mount_checkoption (line_bool, "one", 1)); + OK (two == cu_mount_checkoption (line_bool, "two", 1)); + OK (three == cu_mount_checkoption (line_bool, "three", 1)); + OK (NULL == cu_mount_checkoption (line_bool, "four", 1)); + + OK (NULL == cu_mount_checkoption (line_bool, "o", 1)); + OK (NULL == cu_mount_checkoption (line_bool, "tw", 1)); + OK (NULL == cu_mount_checkoption (line_bool, "thr", 1)); + + return (0); +} +DEF_TEST(cu_mount_getoptionvalue) +{ + char line_opts[] = "foo=one,bar=two,qux=three"; + char line_bool[] = "one,two,three"; + + STREQ ("one", cu_mount_getoptionvalue (line_opts, "foo=")); + STREQ ("two", cu_mount_getoptionvalue (line_opts, "bar=")); + STREQ ("three", cu_mount_getoptionvalue (line_opts, "qux=")); + OK (NULL == cu_mount_getoptionvalue (line_opts, "unknown=")); + + STREQ ("", cu_mount_getoptionvalue (line_bool, "one")); + STREQ ("", cu_mount_getoptionvalue (line_bool, "two")); + STREQ ("", cu_mount_getoptionvalue (line_bool, "three")); + OK (NULL == cu_mount_getoptionvalue (line_bool, "four")); + + return (0); +} + +int main (void) +{ + RUN_TEST(cu_mount_checkoption); + RUN_TEST(cu_mount_getoptionvalue); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/utils_vl_lookup_test.c b/src/utils_vl_lookup_test.c new file mode 100644 index 00000000..6a2676a8 --- /dev/null +++ b/src/utils_vl_lookup_test.c @@ -0,0 +1,249 @@ +/** + * collectd - src/tests/test_utils_vl_lookup.c + * Copyright (C) 2012 Florian 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 Forster + **/ + +#include "testing.h" +#include "collectd.h" +#include "utils_vl_lookup.h" + +static _Bool expect_new_obj = 0; +static _Bool have_new_obj = 0; + +static identifier_t last_class_ident; +static identifier_t last_obj_ident; + +static data_source_t dsrc_test = { "value", DS_TYPE_DERIVE, 0.0, NAN }; +static data_set_t const ds_test = { "test", 1, &dsrc_test }; + +static data_source_t dsrc_unknown = { "value", DS_TYPE_DERIVE, 0.0, NAN }; +static data_set_t const ds_unknown = { "unknown", 1, &dsrc_unknown }; + +static int lookup_obj_callback (data_set_t const *ds, + value_list_t const *vl, + void *user_class, void *user_obj) +{ + identifier_t *class = user_class; + identifier_t *obj = user_obj; + + OK1(expect_new_obj == have_new_obj, + (expect_new_obj ? "New obj is created." : "Updating existing obj.")); + + memcpy (&last_class_ident, class, sizeof (last_class_ident)); + memcpy (&last_obj_ident, obj, sizeof (last_obj_ident)); + + if (strcmp (obj->plugin_instance, "failure") == 0) + return (-1); + + return (0); +} + +static void *lookup_class_callback (data_set_t const *ds, + value_list_t const *vl, void *user_class) +{ + identifier_t *class = user_class; + identifier_t *obj; + + OK(expect_new_obj); + + memcpy (&last_class_ident, class, sizeof (last_class_ident)); + + obj = malloc (sizeof (*obj)); + strncpy (obj->host, vl->host, sizeof (obj->host)); + strncpy (obj->plugin, vl->plugin, sizeof (obj->plugin)); + strncpy (obj->plugin_instance, vl->plugin_instance, sizeof (obj->plugin_instance)); + strncpy (obj->type, vl->type, sizeof (obj->type)); + strncpy (obj->type_instance, vl->type_instance, sizeof (obj->type_instance)); + + have_new_obj = 1; + + return ((void *) obj); +} + +static void checked_lookup_add (lookup_t *obj, /* {{{ */ + char const *host, + char const *plugin, char const *plugin_instance, + char const *type, char const *type_instance, + unsigned int group_by) +{ + identifier_t ident; + void *user_class; + + memset (&ident, 0, sizeof (ident)); + strncpy (ident.host, host, sizeof (ident.host)); + strncpy (ident.plugin, plugin, sizeof (ident.plugin)); + strncpy (ident.plugin_instance, plugin_instance, sizeof (ident.plugin_instance)); + strncpy (ident.type, type, sizeof (ident.type)); + strncpy (ident.type_instance, type_instance, sizeof (ident.type_instance)); + + user_class = malloc (sizeof (ident)); + memmove (user_class, &ident, sizeof (ident)); + + OK(lookup_add (obj, &ident, group_by, user_class) == 0); +} /* }}} void test_add */ + +static int checked_lookup_search (lookup_t *obj, + char const *host, + char const *plugin, char const *plugin_instance, + char const *type, char const *type_instance, + _Bool expect_new) +{ + int status; + value_list_t vl = VALUE_LIST_STATIC; + data_set_t const *ds = &ds_unknown; + + strncpy (vl.host, host, sizeof (vl.host)); + strncpy (vl.plugin, plugin, sizeof (vl.plugin)); + strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); + strncpy (vl.type, type, sizeof (vl.type)); + strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + + if (strcmp (vl.type, "test") == 0) + ds = &ds_test; + + expect_new_obj = expect_new; + have_new_obj = 0; + + status = lookup_search (obj, ds, &vl); + return (status); +} + +static lookup_t *checked_lookup_create (void) +{ + lookup_t *obj = lookup_create ( + lookup_class_callback, + lookup_obj_callback, + (void *) free, + (void *) free); + OK(obj != NULL); + return (obj); +} + +DEF_TEST(group_by_specific_host) +{ + lookup_t *obj = checked_lookup_create (); + + checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST); + checked_lookup_search (obj, "host0", "test", "", "test", "0", + /* expect new = */ 1); + checked_lookup_search (obj, "host0", "test", "", "test", "1", + /* expect new = */ 0); + checked_lookup_search (obj, "host1", "test", "", "test", "0", + /* expect new = */ 1); + checked_lookup_search (obj, "host1", "test", "", "test", "1", + /* expect new = */ 0); + + lookup_destroy (obj); + return (0); +} + +DEF_TEST(group_by_any_host) +{ + lookup_t *obj = checked_lookup_create (); + + checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST); + checked_lookup_search (obj, "host0", "plugin0", "", "test", "0", + /* expect new = */ 1); + checked_lookup_search (obj, "host0", "plugin0", "", "test", "1", + /* expect new = */ 0); + checked_lookup_search (obj, "host0", "plugin1", "", "test", "0", + /* expect new = */ 0); + checked_lookup_search (obj, "host0", "plugin1", "", "test", "1", + /* expect new = */ 0); + checked_lookup_search (obj, "host1", "plugin0", "", "test", "0", + /* expect new = */ 1); + checked_lookup_search (obj, "host1", "plugin0", "", "test", "1", + /* expect new = */ 0); + checked_lookup_search (obj, "host1", "plugin1", "", "test", "0", + /* expect new = */ 0); + checked_lookup_search (obj, "host1", "plugin1", "", "test", "1", + /* expect new = */ 0); + + lookup_destroy (obj); + return (0); +} + +DEF_TEST(multiple_lookups) +{ + lookup_t *obj = checked_lookup_create (); + int status; + + checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST); + checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST); + + status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "", + /* expect new = */ 0); + assert (status == 0); + status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "", + /* expect new = */ 1); + assert (status == 1); + status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "ti0", + /* expect new = */ 1); + assert (status == 1); + status = checked_lookup_search (obj, "host0", "plugin0", "", "test", "ti0", + /* expect new = */ 0); + assert (status == 2); + + lookup_destroy (obj); + return (0); +} + +DEF_TEST(regex) +{ + lookup_t *obj = checked_lookup_create (); + + checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/", + LU_GROUP_BY_TYPE_INSTANCE); + checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "user", + /* expect new = */ 1); + checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "idle", + /* expect new = */ 1); + checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "user", + /* expect new = */ 0); + checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "idle", + /* expect new = */ 0); + checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "user", + /* expect new = */ 0); + checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "idle", + /* expect new = */ 0); + checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "user", + /* expect new = */ 0); + checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "idle", + /* expect new = */ 0); + checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "system", + /* expect new = */ 1); + + lookup_destroy (obj); + return (0); +} + +int main (int argc, char **argv) /* {{{ */ +{ + RUN_TEST(group_by_specific_host); + RUN_TEST(group_by_any_host); + RUN_TEST(multiple_lookups); + RUN_TEST(regex); + + END_TEST; +} /* }}} int main */