]>
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 | ||
6 | #include <unistd.h> | |
7 | #include <fcntl.h> | |
8 | ||
fe4f8fd1 ZJS |
9 | #include "clock-util.h" |
10 | #include "fd-util.h" | |
6369641d | 11 | #include "fileio.h" |
d8351049 | 12 | #include "fs-util.h" |
6369641d | 13 | #include "log.h" |
fe4f8fd1 | 14 | #include "macro.h" |
4f7452a8 | 15 | #include "tests.h" |
e4de7287 | 16 | #include "tmpfile-util.h" |
6369641d | 17 | |
4f7452a8 | 18 | TEST(clock_is_localtime) { |
75e7d50e | 19 | _cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX"; |
07edd3b9 | 20 | _cleanup_fclose_ FILE* f = NULL; |
6369641d | 21 | |
fe4f8fd1 | 22 | static const struct scenario { |
6369641d MP |
23 | const char* contents; |
24 | int expected_result; | |
25 | } scenarios[] = { | |
26 | /* adjtime configures UTC */ | |
27 | {"0.0 0 0\n0\nUTC\n", 0}, | |
28 | /* adjtime configures local time */ | |
29 | {"0.0 0 0\n0\nLOCAL\n", 1}, | |
30 | /* no final EOL */ | |
31 | {"0.0 0 0\n0\nUTC", 0}, | |
32 | {"0.0 0 0\n0\nLOCAL", 1}, | |
35f7216f MP |
33 | /* empty value -> defaults to UTC */ |
34 | {"0.0 0 0\n0\n", 0}, | |
6369641d MP |
35 | /* unknown value -> defaults to UTC */ |
36 | {"0.0 0 0\n0\nFOO\n", 0}, | |
35f7216f MP |
37 | /* no third line */ |
38 | {"0.0 0 0", 0}, | |
39 | {"0.0 0 0\n", 0}, | |
40 | {"0.0 0 0\n0", 0}, | |
6369641d MP |
41 | }; |
42 | ||
43 | /* without an adjtime file we default to UTC */ | |
44 | assert_se(clock_is_localtime("/nonexisting/adjtime") == 0); | |
45 | ||
d8351049 | 46 | assert_se(fmkostemp_safe(adjtime, "w", &f) == 0); |
6369641d | 47 | log_info("adjtime test file: %s", adjtime); |
6369641d MP |
48 | |
49 | for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) { | |
50 | log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result); | |
51 | log_info("%s", scenarios[i].contents); | |
52 | rewind(f); | |
c8f12abc | 53 | assert_se(ftruncate(fileno(f), 0) == 0); |
b1837133 | 54 | assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
6369641d MP |
55 | assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result); |
56 | } | |
6369641d MP |
57 | } |
58 | ||
59 | /* Test with the real /etc/adjtime */ | |
4f7452a8 | 60 | TEST(clock_is_localtime_system) { |
6369641d MP |
61 | int r; |
62 | r = clock_is_localtime(NULL); | |
63 | ||
3c14dc61 TM |
64 | if (access("/etc/adjtime", R_OK) == 0) { |
65 | log_info("/etc/adjtime is readable, clock_is_localtime() == %i", r); | |
35f7216f MP |
66 | /* if /etc/adjtime exists we expect some answer, no error or |
67 | * crash */ | |
4c701096 | 68 | assert_se(IN_SET(r, 0, 1)); |
6369641d MP |
69 | } else |
70 | /* default is UTC if there is no /etc/adjtime */ | |
3c14dc61 | 71 | assert_se(r == 0 || ERRNO_IS_PRIVILEGE(r)); |
6369641d MP |
72 | } |
73 | ||
4f7452a8 | 74 | DEFINE_TEST_MAIN(LOG_INFO); |