]>
Commit | Line | Data |
---|---|---|
6369641d MP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright (C) 2016 Canonical Ltd. | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <unistd.h> | |
21 | #include <fcntl.h> | |
22 | ||
fe4f8fd1 ZJS |
23 | #include "clock-util.h" |
24 | #include "fd-util.h" | |
6369641d MP |
25 | #include "fileio.h" |
26 | #include "log.h" | |
fe4f8fd1 | 27 | #include "macro.h" |
6369641d MP |
28 | |
29 | static void test_clock_is_localtime(void) { | |
30 | char adjtime[] = "/tmp/test-adjtime.XXXXXX"; | |
fe4f8fd1 | 31 | _cleanup_close_ int fd = -1; |
6369641d MP |
32 | FILE* f; |
33 | ||
fe4f8fd1 | 34 | static const struct scenario { |
6369641d MP |
35 | const char* contents; |
36 | int expected_result; | |
37 | } scenarios[] = { | |
38 | /* adjtime configures UTC */ | |
39 | {"0.0 0 0\n0\nUTC\n", 0}, | |
40 | /* adjtime configures local time */ | |
41 | {"0.0 0 0\n0\nLOCAL\n", 1}, | |
42 | /* no final EOL */ | |
43 | {"0.0 0 0\n0\nUTC", 0}, | |
44 | {"0.0 0 0\n0\nLOCAL", 1}, | |
35f7216f MP |
45 | /* empty value -> defaults to UTC */ |
46 | {"0.0 0 0\n0\n", 0}, | |
6369641d MP |
47 | /* unknown value -> defaults to UTC */ |
48 | {"0.0 0 0\n0\nFOO\n", 0}, | |
35f7216f MP |
49 | /* no third line */ |
50 | {"0.0 0 0", 0}, | |
51 | {"0.0 0 0\n", 0}, | |
52 | {"0.0 0 0\n0", 0}, | |
6369641d MP |
53 | }; |
54 | ||
55 | /* without an adjtime file we default to UTC */ | |
56 | assert_se(clock_is_localtime("/nonexisting/adjtime") == 0); | |
57 | ||
58 | fd = mkostemp_safe(adjtime, O_WRONLY|O_CLOEXEC); | |
d69d951b | 59 | assert_se(fd >= 0); |
6369641d MP |
60 | log_info("adjtime test file: %s", adjtime); |
61 | f = fdopen(fd, "w"); | |
d69d951b | 62 | assert_se(f); |
6369641d MP |
63 | |
64 | for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) { | |
65 | log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result); | |
66 | log_info("%s", scenarios[i].contents); | |
67 | rewind(f); | |
68 | ftruncate(fd, 0); | |
69 | assert_se(write_string_stream(f, scenarios[i].contents, false) == 0); | |
70 | assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result); | |
71 | } | |
72 | ||
73 | unlink(adjtime); | |
74 | } | |
75 | ||
76 | /* Test with the real /etc/adjtime */ | |
77 | static void test_clock_is_localtime_system(void) { | |
78 | int r; | |
79 | r = clock_is_localtime(NULL); | |
80 | ||
81 | if (access("/etc/adjtime", F_OK) == 0) { | |
82 | log_info("/etc/adjtime exists, clock_is_localtime() == %i", r); | |
35f7216f MP |
83 | /* if /etc/adjtime exists we expect some answer, no error or |
84 | * crash */ | |
d69d951b | 85 | assert_se(r == 0 || r == 1); |
6369641d MP |
86 | } else |
87 | /* default is UTC if there is no /etc/adjtime */ | |
d69d951b | 88 | assert_se(r == 0); |
6369641d MP |
89 | } |
90 | ||
91 | int main(int argc, char *argv[]) { | |
92 | test_clock_is_localtime(); | |
93 | test_clock_is_localtime_system(); | |
94 | ||
95 | return 0; | |
96 | } |