]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
6369641d | 2 | /*** |
810adae9 | 3 | Copyright © 2016 Canonical Ltd. |
6369641d MP |
4 | ***/ |
5 | ||
1cf40697 | 6 | #include <unistd.h> |
6369641d | 7 | |
fe4f8fd1 ZJS |
8 | #include "clock-util.h" |
9 | #include "fd-util.h" | |
6369641d MP |
10 | #include "fileio.h" |
11 | #include "log.h" | |
4f7452a8 | 12 | #include "tests.h" |
e4de7287 | 13 | #include "tmpfile-util.h" |
6369641d | 14 | |
4f7452a8 | 15 | TEST(clock_is_localtime) { |
75e7d50e | 16 | _cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX"; |
07edd3b9 | 17 | _cleanup_fclose_ FILE* f = NULL; |
6369641d | 18 | |
fe4f8fd1 | 19 | static const struct scenario { |
6369641d MP |
20 | const char* contents; |
21 | int expected_result; | |
22 | } scenarios[] = { | |
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}, | |
27 | /* no final EOL */ | |
28 | {"0.0 0 0\n0\nUTC", 0}, | |
29 | {"0.0 0 0\n0\nLOCAL", 1}, | |
35f7216f MP |
30 | /* empty value -> defaults to UTC */ |
31 | {"0.0 0 0\n0\n", 0}, | |
6369641d MP |
32 | /* unknown value -> defaults to UTC */ |
33 | {"0.0 0 0\n0\nFOO\n", 0}, | |
35f7216f MP |
34 | /* no third line */ |
35 | {"0.0 0 0", 0}, | |
36 | {"0.0 0 0\n", 0}, | |
37 | {"0.0 0 0\n0", 0}, | |
6369641d MP |
38 | }; |
39 | ||
40 | /* without an adjtime file we default to UTC */ | |
364a0ba2 | 41 | ASSERT_FALSE(clock_is_localtime("/nonexisting/adjtime")); |
6369641d | 42 | |
364a0ba2 | 43 | ASSERT_OK(fmkostemp_safe(adjtime, "w", &f)); |
6369641d | 44 | log_info("adjtime test file: %s", adjtime); |
6369641d MP |
45 | |
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); | |
49 | rewind(f); | |
364a0ba2 AC |
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); | |
6369641d | 53 | } |
6369641d MP |
54 | } |
55 | ||
56 | /* Test with the real /etc/adjtime */ | |
4f7452a8 | 57 | TEST(clock_is_localtime_system) { |
6369641d MP |
58 | int r; |
59 | r = clock_is_localtime(NULL); | |
60 | ||
3c14dc61 TM |
61 | if (access("/etc/adjtime", R_OK) == 0) { |
62 | log_info("/etc/adjtime is readable, clock_is_localtime() == %i", r); | |
35f7216f MP |
63 | /* if /etc/adjtime exists we expect some answer, no error or |
64 | * crash */ | |
364a0ba2 | 65 | ASSERT_TRUE(IN_SET(r, 0, 1)); |
6369641d MP |
66 | } else |
67 | /* default is UTC if there is no /etc/adjtime */ | |
364a0ba2 | 68 | ASSERT_TRUE(r == 0 || ERRNO_IS_PRIVILEGE(r)); |
6369641d MP |
69 | } |
70 | ||
4f7452a8 | 71 | DEFINE_TEST_MAIN(LOG_INFO); |