dovecot-2.2: lib-master: Added unit test for master_service_sett...
dovecot at dovecot.org
dovecot at dovecot.org
Fri Jan 10 21:59:21 EET 2014
details: http://hg.dovecot.org/dovecot-2.2/rev/9360aeba0099
changeset: 17093:9360aeba0099
user: Timo Sirainen <tss at iki.fi>
date: Fri Jan 10 21:59:15 2014 +0200
description:
lib-master: Added unit test for master_service_settings_cache_read()
diffstat:
src/lib-master/Makefile.am | 22 +++
src/lib-master/test-master-service-settings-cache.c | 113 ++++++++++++++++++++
2 files changed, 135 insertions(+), 0 deletions(-)
diffs (153 lines):
diff -r 4dd86f75346b -r 9360aeba0099 src/lib-master/Makefile.am
--- a/src/lib-master/Makefile.am Fri Jan 10 13:00:39 2014 -0500
+++ b/src/lib-master/Makefile.am Fri Jan 10 21:59:15 2014 +0200
@@ -4,6 +4,7 @@
AM_CPPFLAGS = \
-I$(top_srcdir)/src/lib \
+ -I$(top_srcdir)/src/lib-test \
-I$(top_srcdir)/src/lib-settings \
-I$(top_srcdir)/src/lib-ssl-iostream \
-DPKG_RUNDIR=\""$(rundir)"\" \
@@ -48,3 +49,24 @@
pkginc_libdir=$(pkgincludedir)
pkginc_lib_HEADERS = $(headers)
+
+test_programs = \
+ test-master-service-settings-cache
+
+noinst_PROGRAMS = $(test_programs)
+
+test_libs = \
+ ../lib-test/libtest.la \
+ ../lib/liblib.la
+
+test_deps = $(noinst_LTLIBRARIES) $(test_libs)
+
+test_master_service_settings_cache_SOURCES = test-master-service-settings-cache.c
+test_master_service_settings_cache_LDADD = master-service-settings-cache.lo ../lib-settings/libsettings.la $(test_libs)
+test_master_service_settings_cache_DEPENDENCIES = $(test_deps) ../lib-settings/libsettings.la
+
+check: check-am check-test
+check-test: all-am
+ for bin in $(test_programs); do \
+ if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \
+ done
diff -r 4dd86f75346b -r 9360aeba0099 src/lib-master/test-master-service-settings-cache.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-master/test-master-service-settings-cache.c Fri Jan 10 21:59:15 2014 +0200
@@ -0,0 +1,113 @@
+/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "test-common.h"
+#include "settings-parser.h"
+#include "master-service-private.h"
+#include "master-service-settings.h"
+#include "master-service-settings-cache.h"
+
+#include <stdlib.h>
+
+struct master_service *master_service;
+
+static struct master_service test_master_service;
+static struct master_service_settings set;
+static struct master_service_settings_input input;
+static struct master_service_settings_output output;
+static struct master_service_settings_cache *cache;
+
+struct test_service_settings {
+ const char *foo;
+};
+
+#undef DEF
+#define DEF(type, name) \
+ { type, #name, offsetof(struct test_service_settings, name), NULL }
+
+static const struct setting_define test_setting_defines[] = {
+ DEF(SET_STR, foo),
+ SETTING_DEFINE_LIST_END
+};
+
+static const struct test_service_settings test_default_settings = {
+ .foo = ""
+};
+
+static const struct setting_parser_info test_setting_parser_info = {
+ .module_name = "module",
+ .defines = test_setting_defines,
+ .defaults = &test_default_settings,
+
+ .type_offset = (size_t)-1,
+ .struct_size = sizeof(struct test_service_settings),
+
+ .parent_offset = (size_t)-1
+};
+
+int master_service_settings_read(struct master_service *service ATTR_UNUSED,
+ const struct master_service_settings_input *input ATTR_UNUSED,
+ struct master_service_settings_output *output_r,
+ const char **error_r ATTR_UNUSED)
+{
+ *output_r = output;
+ return 0;
+}
+
+const struct master_service_settings *
+master_service_settings_get(struct master_service *service ATTR_UNUSED)
+{
+ return &set;
+}
+
+static void test_master_service_settings_cache_once(void)
+{
+ const struct setting_parser_context *parser;
+ const char *error;
+
+ output.used_local = output.service_uses_local && rand() % 2;
+ if (output.used_local) {
+ input.local_ip.family = AF_INET;
+ input.local_ip.u.ip4.s_addr = 100 + rand() % 100;
+ }
+ output.used_remote = output.service_uses_remote && rand() % 2;
+ if (output.used_remote) {
+ input.remote_ip.family = AF_INET;
+ input.remote_ip.u.ip4.s_addr = 100 + rand() % 100;
+ }
+ test_assert(master_service_settings_cache_read(cache, &input, NULL, &parser, &error) == 0);
+}
+
+static void test_master_service_settings_cache(void)
+{
+ int i, j;
+
+ for (i = 1; i < 4; i++) {
+ cache = master_service_settings_cache_init(master_service,
+ "module", "service_name");
+ output.service_uses_local = (i & 1) != 0;
+ output.service_uses_remote = (i & 2) != 0;
+ for (j = 0; j < 1000; j++)
+ test_master_service_settings_cache_once();
+ master_service_settings_cache_deinit(&cache);
+ }
+}
+
+int main(void)
+{
+ static void (*test_functions[])(void) = {
+ test_master_service_settings_cache,
+ NULL
+ };
+
+ memset(&input, 0, sizeof(input));
+ input.module = "module";
+ input.service = "service_name";
+
+ set.config_cache_size = 1024*4;
+ test_master_service.set_parser =
+ settings_parser_init(pool_alloconly_create("set pool", 1024),
+ &test_setting_parser_info, 0);
+ master_service = &test_master_service;
+ return test_run(test_functions);
+}
More information about the dovecot-cvs
mailing list