1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2016 Canonical Ltd.
8 #include "clock-util.h"
13 #include "tmpfile-util.h"
15 TEST(clock_is_localtime
) {
16 _cleanup_(unlink_tempfilep
) char adjtime
[] = "/tmp/test-adjtime.XXXXXX";
17 _cleanup_fclose_
FILE* f
= NULL
;
19 static const struct scenario
{
23 /* adjtime configures UTC */
24 {"0.0 0 0\n0\nUTC\n", 0},
25 /* adjtime configures local time */
26 {"0.0 0 0\n0\nLOCAL\n", 1},
28 {"0.0 0 0\n0\nUTC", 0},
29 {"0.0 0 0\n0\nLOCAL", 1},
30 /* empty value -> defaults to UTC */
32 /* unknown value -> defaults to UTC */
33 {"0.0 0 0\n0\nFOO\n", 0},
40 /* without an adjtime file we default to UTC */
41 ASSERT_FALSE(clock_is_localtime("/nonexisting/adjtime"));
43 ASSERT_OK(fmkostemp_safe(adjtime
, "w", &f
));
44 log_info("adjtime test file: %s", adjtime
);
46 for (size_t i
= 0; i
< ELEMENTSOF(scenarios
); ++i
) {
47 log_info("scenario #%zu:, expected result %i", i
, scenarios
[i
].expected_result
);
48 log_info("%s", scenarios
[i
].contents
);
50 ASSERT_OK_ERRNO(ftruncate(fileno(f
), 0));
51 ASSERT_OK(write_string_stream(f
, scenarios
[i
].contents
, WRITE_STRING_FILE_AVOID_NEWLINE
));
52 ASSERT_TRUE(clock_is_localtime(adjtime
) == scenarios
[i
].expected_result
);
56 /* Test with the real /etc/adjtime */
57 TEST(clock_is_localtime_system
) {
59 r
= clock_is_localtime(NULL
);
61 if (access("/etc/adjtime", R_OK
) == 0) {
62 log_info("/etc/adjtime is readable, clock_is_localtime() == %i", r
);
63 /* if /etc/adjtime exists we expect some answer, no error or
65 ASSERT_TRUE(IN_SET(r
, 0, 1));
67 /* default is UTC if there is no /etc/adjtime */
68 ASSERT_TRUE(r
== 0 || ERRNO_IS_PRIVILEGE(r
));
71 DEFINE_TEST_MAIN(LOG_INFO
);