test-timesync: add first test for timesyncd conf parsing
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 25 May 2017 14:26:29 +0000 (10:26 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 25 May 2017 14:59:35 +0000 (10:59 -0400)
We parse the string supplied in NTP_SERVERS during configuration under an
assert_se(). Right now we will accept pretty much anything there, but in case
we are more picky in the future, add a simple test which checks that we can
actually parse whatever is in NTP_SERVERS so that we don't fail the assertion
at runtime.

.gitignore
Makefile.am
src/timesync/meson.build
src/timesync/test-timesync.c [new file with mode: 0644]

index f048533..60eda2b 100644 (file)
 /test-tables
 /test-terminal-util
 /test-time
+/test-timesync
 /test-tmpfiles
 /test-udev
 /test-uid-range
index 6b418dc..9d8f281 100644 (file)
@@ -5221,6 +5221,25 @@ systemd_timesyncd_LDADD = \
        libsystemd-shared.la \
        -lm
 
+test_timesync_SOURCES = \
+       src/timesync/test-timesync.c \
+       src/timesync/timesyncd-manager.c \
+       src/timesync/timesyncd-manager.h \
+       src/timesync/timesyncd-conf.c \
+       src/timesync/timesyncd-conf.h \
+       src/timesync/timesyncd-server.c \
+       src/timesync/timesyncd-server.h
+
+nodist_test_timesync_SOURCES = \
+       src/timesync/timesyncd-gperf.c
+
+test_timesync_LDADD = \
+       libsystemd-shared.la \
+       -lm
+
+tests += \
+       test-timesync
+
 rootlibexec_PROGRAMS += \
        systemd-timesyncd
 
index ee54c3e..4391afa 100644 (file)
@@ -24,3 +24,19 @@ if conf.get('ENABLE_TIMESYNCD', false)
         install_data(timesyncd_conf,
                      install_dir : pkgsysconfdir)
 endif
+
+############################################################
+
+tests += [
+        [['src/timesync/test-timesync.c',
+          'src/timesync/timesyncd-manager.c',
+          'src/timesync/timesyncd-manager.h',
+          'src/timesync/timesyncd-conf.c',
+          'src/timesync/timesyncd-conf.h',
+          'src/timesync/timesyncd-server.c',
+          'src/timesync/timesyncd-server.h',
+          timesyncd_gperf_c],
+         [libshared],
+         [libm],
+         'ENABLE_TIMESYNCD'],
+]
diff --git a/src/timesync/test-timesync.c b/src/timesync/test-timesync.c
new file mode 100644 (file)
index 0000000..a5a3433
--- /dev/null
@@ -0,0 +1,51 @@
+/***
+  This file is part of systemd.
+
+  Copyright 2017 Zbigniew Jędrzejewski-Szmek
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd 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
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+/* Some unit tests for the helper functions in timesyncd. */
+
+#include "log.h"
+#include "macro.h"
+#include "timesyncd-conf.h"
+
+static void test_manager_parse_string(void) {
+        /* Make sure that NTP_SERVERS is configured to something
+         * that we can actually parse successfully. */
+
+        _cleanup_(manager_freep) Manager *m = NULL;
+
+        assert_se(manager_new(&m) == 0);
+
+        assert_se(!m->have_fallbacks);
+        assert_se(manager_parse_server_string(m, SERVER_FALLBACK, NTP_SERVERS) == 0);
+        assert_se(m->have_fallbacks);
+        assert_se(manager_parse_fallback_string(m, NTP_SERVERS) == 0);
+
+        assert_se(manager_parse_server_string(m, SERVER_SYSTEM, "time1.foobar.com time2.foobar.com") == 0);
+        assert_se(manager_parse_server_string(m, SERVER_FALLBACK, "time1.foobar.com time2.foobar.com") == 0);
+        assert_se(manager_parse_server_string(m, SERVER_LINK, "time1.foobar.com time2.foobar.com") == 0);
+}
+
+int main(int argc, char **argv) {
+        log_set_max_level(LOG_DEBUG);
+        log_parse_environment();
+
+        test_manager_parse_string();
+
+        return 0;
+}