diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 19 | ||||
-rw-r--r-- | src/timesync/meson.build | 16 | ||||
-rw-r--r-- | src/timesync/test-timesync.c | 51 | ||||
-rw-r--r-- | src/timesync/timesyncd-conf.c | 10 | ||||
-rw-r--r-- | src/timesync/timesyncd-conf.h | 1 | ||||
-rw-r--r-- | src/timesync/timesyncd-manager.c | 4 | ||||
-rw-r--r-- | src/timesync/timesyncd-manager.h | 2 | ||||
-rw-r--r-- | src/timesync/timesyncd.c | 2 |
9 files changed, 102 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore index f0485330b5..60eda2b8ce 100644 --- a/.gitignore +++ b/.gitignore @@ -293,6 +293,7 @@ /test-tables /test-terminal-util /test-time +/test-timesync /test-tmpfiles /test-udev /test-uid-range diff --git a/Makefile.am b/Makefile.am index e95cbe4b97..27659188c7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5223,6 +5223,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 diff --git a/src/timesync/meson.build b/src/timesync/meson.build index ee54c3e449..4391afa93a 100644 --- a/src/timesync/meson.build +++ b/src/timesync/meson.build @@ -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 index 0000000000..a5a3433022 --- /dev/null +++ b/src/timesync/test-timesync.c @@ -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; +} diff --git a/src/timesync/timesyncd-conf.c b/src/timesync/timesyncd-conf.c index 99bdb55e98..f394d0a2af 100644 --- a/src/timesync/timesyncd-conf.c +++ b/src/timesync/timesyncd-conf.c @@ -34,6 +34,9 @@ int manager_parse_server_string(Manager *m, ServerType type, const char *string) first = type == SERVER_FALLBACK ? m->fallback_servers : m->system_servers; + if (type == SERVER_FALLBACK) + m->have_fallbacks = true; + for (;;) { _cleanup_free_ char *word = NULL; bool found = false; @@ -63,6 +66,13 @@ int manager_parse_server_string(Manager *m, ServerType type, const char *string) return 0; } +int manager_parse_fallback_string(Manager *m, const char *string) { + if (m->have_fallbacks) + return 0; + + return manager_parse_server_string(m, SERVER_FALLBACK, string); +} + int config_parse_servers( const char *unit, const char *filename, diff --git a/src/timesync/timesyncd-conf.h b/src/timesync/timesyncd-conf.h index 0280697e9c..0c4b44e707 100644 --- a/src/timesync/timesyncd-conf.h +++ b/src/timesync/timesyncd-conf.h @@ -29,3 +29,4 @@ int manager_parse_server_string(Manager *m, ServerType type, const char *string) int config_parse_servers(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int manager_parse_config_file(Manager *m); +int manager_parse_fallback_string(Manager *m, const char *string); diff --git a/src/timesync/timesyncd-manager.c b/src/timesync/timesyncd-manager.c index ae8514550a..a24c821bdc 100644 --- a/src/timesync/timesyncd-manager.c +++ b/src/timesync/timesyncd-manager.c @@ -1124,10 +1124,6 @@ int manager_new(Manager **ret) { RATELIMIT_INIT(m->ratelimit, RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST); - r = manager_parse_server_string(m, SERVER_FALLBACK, NTP_SERVERS); - if (r < 0) - return r; - r = sd_event_default(&m->event); if (r < 0) return r; diff --git a/src/timesync/timesyncd-manager.h b/src/timesync/timesyncd-manager.h index efe3e60d3e..cf681f6098 100644 --- a/src/timesync/timesyncd-manager.h +++ b/src/timesync/timesyncd-manager.h @@ -38,6 +38,8 @@ struct Manager { LIST_HEAD(ServerName, link_servers); LIST_HEAD(ServerName, fallback_servers); + bool have_fallbacks:1; + RateLimit ratelimit; bool exhausted_servers; diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c index b67d672a6a..052329f61e 100644 --- a/src/timesync/timesyncd.c +++ b/src/timesync/timesyncd.c @@ -132,6 +132,8 @@ int main(int argc, char *argv[]) { if (r < 0) log_warning_errno(r, "Failed to parse configuration file: %m"); + assert_se(manager_parse_fallback_string(m, NTP_SERVERS) >= 0); + log_debug("systemd-timesyncd running as pid " PID_FMT, getpid()); sd_notify(false, "READY=1\n" |