diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/Makefile.am | 8 | ||||
-rw-r--r-- | src/tests/Makefile.in | 24 | ||||
-rw-r--r-- | src/tests/README | 4 | ||||
-rw-r--r-- | src/tests/common/fdset_tests.c | 59 | ||||
-rw-r--r-- | src/tests/xfr_tests.c | 384 | ||||
-rw-r--r-- | src/tests/xfr_tests.h | 10 |
6 files changed, 31 insertions, 458 deletions
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 29733b7..bb381dc 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -3,8 +3,7 @@ ACLOCAL_AMFLAGS = -I $(top_srcdir)/m4 AM_CPPFLAGS = -I$(top_srcdir)/src/libknot -I$(top_srcdir)/src -DSYSCONFDIR='"$(sysconfdir)"' -DSBINDIR='"$(sbindir)"' check_PROGRAMS = \ - unittests \ - unittests_xfr + unittests TESTS = unittests @@ -63,12 +62,7 @@ unittests_SOURCES = \ libknot/sign_tests.h \ unittests_main.c -unittests_xfr_SOURCES = \ - xfr_tests.c \ - xfr_tests.h - unittests_LDADD = ../libknotd.la ../libknots.la @LIBOBJS@ -unittests_xfr_LDADD = ../libknotd.la ../libknot.la ../libknots.la @LIBOBJS@ sample_conf.rc: files/sample_conf $(top_srcdir)/resource.sh $(srcdir)/files/sample_conf >$@ diff --git a/src/tests/Makefile.in b/src/tests/Makefile.in index df605b8..fefb224 100644 --- a/src/tests/Makefile.in +++ b/src/tests/Makefile.in @@ -50,10 +50,10 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ -check_PROGRAMS = unittests$(EXEEXT) unittests_xfr$(EXEEXT) +check_PROGRAMS = unittests$(EXEEXT) TESTS = unittests$(EXEEXT) subdir = src/tests -DIST_COMMON = README $(srcdir)/Makefile.am $(srcdir)/Makefile.in +DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_compile_flag.m4 \ $(top_srcdir)/m4/ax_ext.m4 \ @@ -88,10 +88,6 @@ unittests_DEPENDENCIES = ../libknotd.la ../libknots.la @LIBOBJS@ AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_0 = --silent -am_unittests_xfr_OBJECTS = xfr_tests.$(OBJEXT) -unittests_xfr_OBJECTS = $(am_unittests_xfr_OBJECTS) -unittests_xfr_DEPENDENCIES = ../libknotd.la ../libknot.la \ - ../libknots.la @LIBOBJS@ DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp am__depfiles_maybe = depfiles @@ -118,9 +114,8 @@ am__v_CCLD_0 = @echo " CCLD " $@; AM_V_GEN = $(am__v_GEN_@AM_V@) am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) am__v_GEN_0 = @echo " GEN " $@; -SOURCES = $(unittests_SOURCES) $(nodist_unittests_SOURCES) \ - $(unittests_xfr_SOURCES) -DIST_SOURCES = $(unittests_SOURCES) $(unittests_xfr_SOURCES) +SOURCES = $(unittests_SOURCES) $(nodist_unittests_SOURCES) +DIST_SOURCES = $(unittests_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -198,6 +193,7 @@ PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ RAGEL = @RAGEL@ RANLIB = @RANLIB@ +RELEASE_DATE = @RELEASE_DATE@ SED = @SED@ SET_MAKE = @SET_MAKE@ SHELL = @SHELL@ @@ -225,6 +221,7 @@ build_cpu = @build_cpu@ build_os = @build_os@ build_vendor = @build_vendor@ builddir = @builddir@ +config_dir = @config_dir@ datadir = @datadir@ datarootdir = @datarootdir@ docdir = @docdir@ @@ -321,12 +318,7 @@ unittests_SOURCES = \ libknot/sign_tests.h \ unittests_main.c -unittests_xfr_SOURCES = \ - xfr_tests.c \ - xfr_tests.h - unittests_LDADD = ../libknotd.la ../libknots.la @LIBOBJS@ -unittests_xfr_LDADD = ../libknotd.la ../libknot.la ../libknots.la @LIBOBJS@ all: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) all-am @@ -438,9 +430,6 @@ libknot/sign_tests.$(OBJEXT): libknot/$(am__dirstamp) \ unittests$(EXEEXT): $(unittests_OBJECTS) $(unittests_DEPENDENCIES) $(EXTRA_unittests_DEPENDENCIES) @rm -f unittests$(EXEEXT) $(AM_V_CCLD)$(LINK) $(unittests_OBJECTS) $(unittests_LDADD) $(LIBS) -unittests_xfr$(EXEEXT): $(unittests_xfr_OBJECTS) $(unittests_xfr_DEPENDENCIES) $(EXTRA_unittests_xfr_DEPENDENCIES) - @rm -f unittests_xfr$(EXEEXT) - $(AM_V_CCLD)$(LINK) $(unittests_xfr_OBJECTS) $(unittests_xfr_LDADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -469,7 +458,6 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unittests_main.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xfr_tests.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@common/$(DEPDIR)/acl_tests.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@common/$(DEPDIR)/base32hex_tests.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@common/$(DEPDIR)/base64_tests.Po@am__quote@ diff --git a/src/tests/README b/src/tests/README deleted file mode 100644 index 1d9748b..0000000 --- a/src/tests/README +++ /dev/null @@ -1,4 +0,0 @@ -Unit testing ------------- - -Make assembles "unittest" binary with all of the planned tests included. diff --git a/src/tests/common/fdset_tests.c b/src/tests/common/fdset_tests.c index d7b29fa..e8b9d73 100644 --- a/src/tests/common/fdset_tests.c +++ b/src/tests/common/fdset_tests.c @@ -98,22 +98,21 @@ static int fdset_tests_count(int argc, char *argv[]) static int fdset_tests_run(int argc, char *argv[]) { - diag("fdset: implements '%s'", fdset_method()); - /* 1. Create fdset. */ - fdset_t *set = fdset_new(); - ok(set != 0, "fdset: new"); + fdset_t set; + int ret = fdset_init(&set, 32); + ok(ret == 0, "fdset: init"); /* 2. Create pipe. */ int fds[2], tmpfds[2]; - int ret = pipe(fds); + ret = pipe(fds); ok(ret >= 0, "fdset: pipe() works"); ret = pipe(tmpfds); /* 3. Add fd to set. */ - ret = fdset_add(set, fds[0], OS_EV_READ); + ret = fdset_add(&set, fds[0], POLLIN, NULL); ok(ret == 0, "fdset: add to set works"); - fdset_add(set, tmpfds[0], OS_EV_READ); + fdset_add(&set, tmpfds[0], POLLIN, NULL); /* Schedule write. */ struct timeval ts, te; @@ -122,54 +121,44 @@ static int fdset_tests_run(int argc, char *argv[]) pthread_create(&t, 0, thr_action, &fds[1]); /* 4. Watch fdset. */ - ret = fdset_wait(set, OS_EV_FOREVER); + int nfds = poll(set.pfd, set.n, 2000); gettimeofday(&te, 0); size_t diff = timeval_diff(&ts, &te); - ok(ret > 0 && diff > 99 && diff < 10000, - "fdset: poll returned events in %zu ms", diff); + ok(nfds > 0 && diff > 99 && diff < 10000, + "fdset: poll returned %d events in %zu ms", nfds, diff); /* 5. Prepare event set. */ - fdset_it_t it; - ret = fdset_begin(set, &it); - ok(ret == 0 && it.fd == fds[0], "fdset: begin is valid, ret=%d", ret); + ok(set.pfd[0].revents & POLLIN, "fdset: pipe is active"); /* 6. Receive data. */ char buf = 0x00; - ret = read(it.fd, &buf, WRITE_PATTERN_LEN); - ok(ret >= 0 && buf == WRITE_PATTERN, "fdset: contains valid data, fd=%d", it.fd); - - /* 7. Iterate event set. */ - ret = fdset_next(set, &it); - ok(ret < 0, "fdset: boundary check works"); + ret = read(set.pfd[0].fd, &buf, WRITE_PATTERN_LEN); + ok(ret >= 0 && buf == WRITE_PATTERN, "fdset: contains valid data"); - /* 8. Remove from event set. */ - ret = fdset_remove(set, fds[0]); + /* 7-9. Remove from event set. */ + ret = fdset_remove(&set, 0); ok(ret == 0, "fdset: remove from fdset works"); close(fds[0]); close(fds[1]); - ret = fdset_remove(set, tmpfds[0]); + ret = fdset_remove(&set, 0); close(tmpfds[1]); close(tmpfds[1]); - - /* 9. Poll empty fdset. */ - ret = fdset_wait(set, OS_EV_FOREVER); - ok(ret <= 0, "fdset: polling empty fdset returns -1 (ret=%d)", ret); + ok(ret == 0, "fdset: remove from fdset works (2)"); + ret = fdset_remove(&set, 0); + ok(ret != 0, "fdset: removing nonexistent item"); /* 10. Crash test. */ lives_ok({ - fdset_destroy(0); - fdset_add(0, -1, 0); - fdset_remove(0, -1); - fdset_wait(0, OS_EV_NOWAIT); - fdset_begin(0, 0); - fdset_end(0, 0); - fdset_next(0, 0); - fdset_method(); + fdset_init(0, 0); + fdset_add(0, 1, 1, 0); + fdset_add(0, 0, 1, 0); + fdset_remove(0, 1); + fdset_remove(0, 0); }, "fdset: crash test successful"); /* 11. Destroy fdset. */ - ret = fdset_destroy(set); + ret = fdset_clear(&set); ok(ret == 0, "fdset: destroyed"); /* Cleanup. */ diff --git a/src/tests/xfr_tests.c b/src/tests/xfr_tests.c deleted file mode 100644 index fee09f2..0000000 --- a/src/tests/xfr_tests.c +++ /dev/null @@ -1,384 +0,0 @@ -/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -/* - * This test is basically a copy of the whole server, with following exceptions: - * - signal handler now handles one more signal - * SIGUSR1 is used to signal this - * binary that an integrity check should be done -*/ - -#include <config.h> -#include <time.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <getopt.h> -#include <assert.h> - -#include "knot/knot.h" -#include "knot/server/server.h" -#include "knot/ctl/process.h" -#include "knot/conf/conf.h" -#include "knot/conf/logconf.h" -#include "common/evqueue.h" -#include "knot/server/zones.h" - -/*----------------------------------------------------------------------------*/ - -/* Signal flags. */ -static volatile short sig_req_stop = 0; -static volatile short sig_req_reload = 0; -static volatile short sig_stopping = 0; -static volatile short sig_integrity_check = 0; - - -// SIGINT signal handler -void interrupt_handle(int s) -{ - // Reload configuration - if (s == SIGHUP) { - sig_req_reload = 1; - return; - } - - // Stop server - if (s == SIGINT || s == SIGTERM) { - if (sig_stopping == 0) { - sig_req_stop = 1; - sig_stopping = 1; - } else { - log_server_notice("OK! Exiting immediately.\n"); - exit(1); - } - } - - // Start zone integrity check - if (s == SIGUSR1) { - sig_integrity_check = 1; - return; - } -} - -void help(int argc, char **argv) -{ - printf("Usage: %sd [parameters]\n", - PACKAGE_NAME); - printf("Parameters:\n" - " -c, --config [file] Select configuration file.\n" - " -z, --zone [origin] Inspected zone origin.\n" - " -d, --daemonize Run server as a daemon.\n" - " -v, --verbose Verbose mode - additional runtime information.\n" - " -V, --version Print version of the server.\n" - " -h, --help Print help and usage.\n" - "Send SIGUSR1 to trigger integrity check.\n"); -} - -int main(int argc, char **argv) -{ - // Parse command line arguments - int check_iteration = 0; - int c = 0, li = 0; - int verbose = 0; - int daemonize = 0; - char *config_fn = NULL; - char *zone = NULL; - - /* Long options. */ - struct option opts[] = { - {"config", required_argument, 0, 'c'}, - {"zone", required_argument, 0, 'z'}, - {"daemonize", no_argument, 0, 'd'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0} - }; - - while ((c = getopt_long(argc, argv, "c:z:dvVh", opts, &li)) != -1) { - switch (c) - { - case 'c': - config_fn = strdup(optarg); - break; - case 'd': - daemonize = 1; - break; - case 'v': - verbose = 1; - break; - case 'V': - printf("%s, version %s\n", "Knot DNS", PACKAGE_VERSION); - return 0; - case 'z': - if (optarg[strlen(optarg) - 1] != '.') { - zone = strcdup(optarg, "."); - } else { - zone = strdup(optarg); - } - break; - case 'h': - case '?': - default: - help(argc, argv); - return 1; - } - } - - // Now check if we want to daemonize - if (daemonize) { - if (daemon(1, 0) != 0) { - free(zone); - free(config_fn); - fprintf(stderr, "Daemonization failed, " - "shutting down...\n"); - return 1; - } - } - - // Register service and signal handler - struct sigaction emptyset; - memset(&emptyset, 0, sizeof(struct sigaction)); - emptyset.sa_handler = interrupt_handle; - sigemptyset(&emptyset.sa_mask); - emptyset.sa_flags = 0; - sigaction(SIGALRM, &emptyset, NULL); // Interrupt - - // Setup event queue - evqueue_set(evqueue_new()); - - // Initialize log - log_init(); - - // Verbose mode - if (verbose) { - int mask = LOG_MASK(LOG_INFO)|LOG_MASK(LOG_DEBUG); - log_levels_add(LOGT_STDOUT, LOG_ANY, mask); - } - - // Initialize pseudorandom number generator - srand(time(0)); - - // Create server - server_t *server = server_create(); - - // Initialize configuration - rcu_read_lock(); - conf_add_hook(conf(), CONF_LOG, log_conf_hook, 0); - conf_add_hook(conf(), CONF_LOG, zones_ns_conf_hook, server->nameserver); - conf_add_hook(conf(), CONF_LOG, server_conf_hook, server); - rcu_read_unlock(); - - // Find implicit configuration file - if (!config_fn) { - config_fn = conf_find_default(); - } - - // Find absolute path for config file - if (config_fn[0] != '/') - { - // Get absolute path to cwd - size_t cwbuflen = 64; - char *cwbuf = malloc((cwbuflen + 2) * sizeof(char)); - while (getcwd(cwbuf, cwbuflen) == 0) { - cwbuflen *= 2; - cwbuf = realloc(cwbuf, (cwbuflen + 2) * sizeof(char)); - } - cwbuflen = strlen(cwbuf); - - // Append ending slash - if (cwbuf[cwbuflen - 1] != '/') { - cwbuf = strncat(cwbuf, "/", 1); - } - - // Assemble path to config file - char *abs_cfg = strcdup(cwbuf, config_fn); - free(config_fn); - free(cwbuf); - config_fn = abs_cfg; - } - - // Open configuration - log_server_info("Reading configuration '%s' ...\n", config_fn); - int conf_ret = conf_open(config_fn); - if (conf_ret != KNOT_EOK) { - if (conf_ret == KNOT_ENOENT) { - log_server_error("Couldn't open configuration file " - "'%s'.\n", config_fn); - } else { - log_server_error("Failed to load configuration '%s'.\n", - config_fn); - } - server_destroy(&server); - free(config_fn); - return 1; - } else { - log_server_info("Configured %d interfaces and %d zones.\n", - conf()->ifaces_count, conf()->zones_count); - } - log_server_info("\n"); - - // Create server instance - char* pidfile = pid_filename(); - - // Run server - int res = 0; - log_server_info("Starting server...\n"); - if ((res = server_start(server)) == KNOT_EOK) { - - // Save PID - int has_pid = 1; - int rc = pid_write(pidfile); - if (rc < 0) { - has_pid = 0; - log_server_warning("Failed to create " - "PID file '%s'.\n", pidfile); - } - - // Change directory if daemonized - if (daemonize) { - log_server_info("Server started as a daemon, " - "PID = %ld\n", (long)getpid()); - res = chdir("/"); - } else { - log_server_info("Server started in foreground, " - "PID = %ld\n", (long)getpid()); - } - if (has_pid) { - log_server_info("PID stored in %s\n", pidfile); - } else { - log_server_warning("Server running without PID file.\n"); - } - size_t zcount = server->nameserver->zone_db->zone_count; - if (!zcount) { - log_server_warning("Server started, but no zones served.\n"); - } - - // Setup signal handler - struct sigaction sa; - memset(&sa, 0, sizeof(sa)); - sa.sa_handler = interrupt_handle; - sigemptyset(&sa.sa_mask); - sigaction(SIGINT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); - sigaction(SIGHUP, &sa, NULL); - sigaction(SIGUSR1, &sa, NULL); - sa.sa_flags = 0; - pthread_sigmask(SIG_BLOCK, &sa.sa_mask, NULL); - - /* Run event loop. */ - for(;;) { - pthread_sigmask(SIG_UNBLOCK, &sa.sa_mask, NULL); - int ret = evqueue_poll(evqueue(), 0, 0); - pthread_sigmask(SIG_BLOCK, &sa.sa_mask, NULL); - - /* Interrupts. */ - /*! \todo More robust way to exit evloop. - * Event loop should exit with a special - * event. - */ - if (sig_req_stop) { - sig_req_stop = 0; - server_stop(server); - break; - } - if (sig_req_reload) { - log_server_info("Reloading configuration...\n"); - sig_req_reload = 0; - int cf_ret = conf_open(config_fn); - switch (cf_ret) { - case KNOT_EOK: - log_server_info("Configuration " - "reloaded.\n"); - break; - case KNOT_ENOENT: - log_server_error("Configuration " - "file '%s' " - "not found.\n", - conf()->filename); - break; - default: - log_server_error("Configuration " - "reload failed.\n"); - break; - } - } - if (zone == NULL) sig_integrity_check = 0; - if (sig_integrity_check) { - log_server_info("Starting integrity check of zone: %s\n", zone); - knot_dname_t* zdn = knot_dname_new_from_str(zone, strlen(zone), NULL); - assert(zdn); - knot_zone_t *z = knot_zonedb_find_zone(server->nameserver->zone_db, zdn); - int ic_ret = knot_zone_contents_integrity_check(z->contents); - log_server_info("Integrity check: %d errors discovered.\n", ic_ret); - knot_dname_free(&zdn); - log_server_info("Integrity check %d finished.\n", check_iteration); - ++check_iteration; - } - - /* Events. */ - if (ret > 0) { - event_t ev; - if (evqueue_get(evqueue(), &ev) == 0) { - dbg_server_verb("Event: " - "received new event.\n"); - if (ev.cb) { - ev.cb(&ev); - } - } - } - } - pthread_sigmask(SIG_UNBLOCK, &sa.sa_mask, NULL); - - if ((res = server_wait(server)) != KNOT_EOK) { - log_server_error("An error occured while " - "waiting for server to finish.\n"); - } else { - log_server_info("Server finished.\n"); - } - - } else { - log_server_fatal("An error occured while " - "starting the server.\n"); - } - - // Stop server and close log - server_destroy(&server); - - // Remove PID file - if (pid_remove(pidfile) < 0) { - log_server_warning("Failed to remove PID file.\n"); - } - - log_server_info("Shut down.\n"); - log_close(); - free(pidfile); - - // Destroy event loop - evqueue_t *q = evqueue(); - evqueue_free(&q); - - // Free default config filename if exists - free(zone); - free(config_fn); - - if (!daemonize) { - fflush(stdout); - fflush(stderr); - } - - return res; -} diff --git a/src/tests/xfr_tests.h b/src/tests/xfr_tests.h deleted file mode 100644 index 29de11d..0000000 --- a/src/tests/xfr_tests.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef XFR_TESTS_H -#define XFR_TESTS_H - -class xfr_tests -{ -public: - xfr_tests(); -}; - -#endif // XFR_TESTS_H |