]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
clock-util: make clock_is_localtime() testable and add initial tests
authorMartin Pitt <martin.pitt@ubuntu.com>
Fri, 26 Feb 2016 10:25:22 +0000 (11:25 +0100)
committerMartin Pitt <martin.pitt@ubuntu.com>
Fri, 26 Feb 2016 10:28:49 +0000 (11:28 +0100)
Add path argument to clock_is_localtime() and default to "/etc/adjtime" if it's
NULL. This makes the function testable.

Add test-clock: initial test cases for some scenarios, using a temporary file.
This also checks the behaviour with a NULL (i. e. the system's /etc/adjtime)
file.

.gitignore
Makefile.am
src/basic/clock-util.c
src/basic/clock-util.h
src/core/dbus-manager.c
src/core/main.c
src/test/test-clock.c [new file with mode: 0644]
src/timedate/timedated.c
src/timesync/timesyncd.c

index eab660e859bc16421983f36449ef23689e3ea1c0..56a60ba7266a47d7f91057f3e22b65bf6bf11288 100644 (file)
 /test-cgroup
 /test-cgroup-mask
 /test-cgroup-util
+/test-clock
 /test-compress
 /test-compress-benchmark
 /test-condition
index 7bd98dddf65e97e4c2354de3671a56d7b271e006..4f9072c0ff807760b74e3bc3e2a62edd2ec7ed1b 100644 (file)
@@ -1448,6 +1448,7 @@ tests += \
        test-prioq \
        test-fileio \
        test-time \
+       test-clock \
        test-hashmap \
        test-set \
        test-bitmap \
@@ -1961,6 +1962,12 @@ test_time_SOURCES = \
 test_time_LDADD = \
        libshared.la
 
+test_clock_SOURCES = \
+       src/test/test-clock.c
+
+test_clock_LDADD = \
+       libshared.la
+
 test_architecture_SOURCES = \
        src/test/test-architecture.c
 
index 507e757ff0806af3a0f29baec414abe98c098d5a..dd6c043af99a39c4432ae3e136f6edfe2667a8f5 100644 (file)
@@ -69,9 +69,12 @@ int clock_set_hwclock(const struct tm *tm) {
         return 0;
 }
 
-int clock_is_localtime(void) {
+int clock_is_localtime(const char* adjtime_path) {
         _cleanup_fclose_ FILE *f;
 
+        if (adjtime_path == NULL)
+                adjtime_path = "/etc/adjtime";
+
         /*
          * The third line of adjtime is "UTC" or "LOCAL" or nothing.
          *   # /etc/adjtime
@@ -79,7 +82,7 @@ int clock_is_localtime(void) {
          *   0
          *   UTC
          */
-        f = fopen("/etc/adjtime", "re");
+        f = fopen(adjtime_path, "re");
         if (f) {
                 char line[LINE_MAX];
                 bool b;
index f471f2abcfa95c1d062ae20b2c8c50cfd4b2ea41..8830cd2f381026ef5ef2177f9b9935b2c1ad7397 100644 (file)
@@ -21,7 +21,7 @@
 
 #include <time.h>
 
-int clock_is_localtime(void);
+int clock_is_localtime(const char* adjtime_path);
 int clock_set_timezone(int *min);
 int clock_reset_timewarp(void);
 int clock_get_hwclock(struct tm *tm);
index f939196397bb69bf49233e262b510da30e88eb94..00372b92b4bb9cee0e66da95f429bc2ee8e50827 100644 (file)
@@ -139,7 +139,7 @@ static int property_get_tainted(
         if (access("/proc/cgroups", F_OK) < 0)
                 e = stpcpy(e, "cgroups-missing:");
 
-        if (clock_is_localtime() > 0)
+        if (clock_is_localtime(NULL) > 0)
                 e = stpcpy(e, "local-hwclock:");
 
         /* remove the last ':' */
index b4e96fd6f427a16340e7e5829044b420c92c20ae..2c315930ed04b9a1781ae95aabfe27a097f7b910 100644 (file)
@@ -1375,7 +1375,7 @@ int main(int argc, char *argv[]) {
                 }
 
                 if (!skip_setup) {
-                        if (clock_is_localtime() > 0) {
+                        if (clock_is_localtime(NULL) > 0) {
                                 int min;
 
                                 /*
diff --git a/src/test/test-clock.c b/src/test/test-clock.c
new file mode 100644 (file)
index 0000000..27f6b8c
--- /dev/null
@@ -0,0 +1,93 @@
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2016 Canonical Ltd.
+
+  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/>.
+***/
+
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "macro.h"
+#include "fileio.h"
+#include "log.h"
+#include "clock-util.h"
+
+static void test_clock_is_localtime(void) {
+        char adjtime[] = "/tmp/test-adjtime.XXXXXX";
+        int fd;
+        FILE* f;
+
+        const struct scenario {
+                const char* contents;
+                int expected_result;
+        } scenarios[] = {
+                /* adjtime configures UTC */
+                {"0.0 0 0\n0\nUTC\n", 0},
+                /* adjtime configures local time */
+                {"0.0 0 0\n0\nLOCAL\n", 1},
+                /* no final EOL */
+                {"0.0 0 0\n0\nUTC", 0},
+                {"0.0 0 0\n0\nLOCAL", 1},
+                /* unknown value -> defaults to UTC */
+                {"0.0 0 0\n0\nFOO\n", 0},
+                /* gibberish */
+                {"br0ken", -EIO},
+        };
+
+        /* without an adjtime file we default to UTC */
+        assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
+
+        fd = mkostemp_safe(adjtime, O_WRONLY|O_CLOEXEC);
+        assert(fd > 0);
+        log_info("adjtime test file: %s", adjtime);
+        f = fdopen(fd, "w");
+        assert(f);
+
+        for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) {
+                log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result);
+                log_info("%s", scenarios[i].contents);
+                rewind(f);
+                ftruncate(fd, 0);
+                assert_se(write_string_stream(f, scenarios[i].contents, false) == 0);
+                assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result);
+        }
+
+        unlink(adjtime);
+}
+
+/* Test with the real /etc/adjtime */
+static void test_clock_is_localtime_system(void) {
+        int r;
+        r = clock_is_localtime(NULL);
+
+        if (access("/etc/adjtime", F_OK) == 0) {
+                log_info("/etc/adjtime exists, clock_is_localtime() == %i", r);
+                /* we cannot assert much if /etc/adjtime exists, just that we
+                 * expect either an answer, or an EIO if the local file really
+                 * is badly malformed. I. e. we don't expect any other error
+                 * code or crash. */
+                assert(r == 0 || r == 1 || r == -EIO);
+        } else
+                /* default is UTC if there is no /etc/adjtime */
+                assert(r == 0);
+}
+
+int main(int argc, char *argv[]) {
+        test_clock_is_localtime();
+        test_clock_is_localtime_system();
+
+        return 0;
+}
index 2a10135fbabced59cdcf38a92c8badef8a616e16..55c24ac4f09bf0f352d2e21e756e585e9689c164 100644 (file)
@@ -78,7 +78,7 @@ static int context_read_data(Context *c) {
         c->zone = t;
         t = NULL;
 
-        c->local_rtc = clock_is_localtime() > 0;
+        c->local_rtc = clock_is_localtime(NULL) > 0;
 
         return 0;
 }
index 23e19159e0bef9805dd246a21a63c0611af204a0..b67d672a6ade4c15384888bf0768994df4a28667 100644 (file)
@@ -122,7 +122,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (clock_is_localtime() > 0) {
+        if (clock_is_localtime(NULL) > 0) {
                 log_info("The system is configured to read the RTC time in the local time zone. "
                          "This mode can not be fully supported. All system time to RTC updates are disabled.");
                 m->rtc_local_time = true;