1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright (C) 2016 Canonical Ltd.
9 #include "clock-util.h"
15 static void test_clock_is_localtime(void) {
16 char adjtime
[] = "/tmp/test-adjtime.XXXXXX";
18 _cleanup_fclose_
FILE* f
= NULL
;
20 static const struct scenario
{
24 /* adjtime configures UTC */
25 {"0.0 0 0\n0\nUTC\n", 0},
26 /* adjtime configures local time */
27 {"0.0 0 0\n0\nLOCAL\n", 1},
29 {"0.0 0 0\n0\nUTC", 0},
30 {"0.0 0 0\n0\nLOCAL", 1},
31 /* empty value -> defaults to UTC */
33 /* unknown value -> defaults to UTC */
34 {"0.0 0 0\n0\nFOO\n", 0},
41 /* without an adjtime file we default to UTC */
42 assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
44 fd
= mkostemp_safe(adjtime
);
46 log_info("adjtime test file: %s", adjtime
);
50 for (size_t i
= 0; i
< ELEMENTSOF(scenarios
); ++i
) {
51 log_info("scenario #%zu:, expected result %i", i
, scenarios
[i
].expected_result
);
52 log_info("%s", scenarios
[i
].contents
);
55 assert_se(write_string_stream(f
, scenarios
[i
].contents
, WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
56 assert_se(clock_is_localtime(adjtime
) == scenarios
[i
].expected_result
);
62 /* Test with the real /etc/adjtime */
63 static void test_clock_is_localtime_system(void) {
65 r
= clock_is_localtime(NULL
);
67 if (access("/etc/adjtime", F_OK
) == 0) {
68 log_info("/etc/adjtime exists, clock_is_localtime() == %i", r
);
69 /* if /etc/adjtime exists we expect some answer, no error or
71 assert_se(IN_SET(r
, 0, 1));
73 /* default is UTC if there is no /etc/adjtime */
77 int main(int argc
, char *argv
[]) {
78 test_clock_is_localtime();
79 test_clock_is_localtime_system();