1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2016 Canonical Ltd.
9 #include "clock-util.h"
15 #include "tmpfile-util.h"
17 static void test_clock_is_localtime(void) {
18 _cleanup_(unlink_tempfilep
) char adjtime
[] = "/tmp/test-adjtime.XXXXXX";
19 _cleanup_fclose_
FILE* f
= NULL
;
21 static const struct scenario
{
25 /* adjtime configures UTC */
26 {"0.0 0 0\n0\nUTC\n", 0},
27 /* adjtime configures local time */
28 {"0.0 0 0\n0\nLOCAL\n", 1},
30 {"0.0 0 0\n0\nUTC", 0},
31 {"0.0 0 0\n0\nLOCAL", 1},
32 /* empty value -> defaults to UTC */
34 /* unknown value -> defaults to UTC */
35 {"0.0 0 0\n0\nFOO\n", 0},
42 /* without an adjtime file we default to UTC */
43 assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
45 assert_se(fmkostemp_safe(adjtime
, "w", &f
) == 0);
46 log_info("adjtime test file: %s", adjtime
);
48 for (size_t i
= 0; i
< ELEMENTSOF(scenarios
); ++i
) {
49 log_info("scenario #%zu:, expected result %i", i
, scenarios
[i
].expected_result
);
50 log_info("%s", scenarios
[i
].contents
);
52 ftruncate(fileno(f
), 0);
53 assert_se(write_string_stream(f
, scenarios
[i
].contents
, WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
54 assert_se(clock_is_localtime(adjtime
) == scenarios
[i
].expected_result
);
58 /* Test with the real /etc/adjtime */
59 static void test_clock_is_localtime_system(void) {
61 r
= clock_is_localtime(NULL
);
63 if (access("/etc/adjtime", F_OK
) == 0) {
64 log_info("/etc/adjtime exists, clock_is_localtime() == %i", r
);
65 /* if /etc/adjtime exists we expect some answer, no error or
67 assert_se(IN_SET(r
, 0, 1));
69 /* default is UTC if there is no /etc/adjtime */
73 int main(int argc
, char *argv
[]) {
74 test_clock_is_localtime();
75 test_clock_is_localtime_system();