]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-clock.c
2 This file is part of systemd.
4 Copyright (C) 2016 Canonical Ltd.
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.
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.
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/>.
23 #include "clock-util.h"
29 static void test_clock_is_localtime(void) {
30 char adjtime
[] = "/tmp/test-adjtime.XXXXXX";
32 _cleanup_fclose_
FILE* f
= NULL
;
34 static const struct scenario
{
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},
43 {"0.0 0 0\n0\nUTC", 0},
44 {"0.0 0 0\n0\nLOCAL", 1},
45 /* empty value -> defaults to UTC */
47 /* unknown value -> defaults to UTC */
48 {"0.0 0 0\n0\nFOO\n", 0},
55 /* without an adjtime file we default to UTC */
56 assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
58 fd
= mkostemp_safe(adjtime
);
60 log_info("adjtime test file: %s", adjtime
);
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
);
69 assert_se(write_string_stream(f
, scenarios
[i
].contents
, false) == 0);
70 assert_se(clock_is_localtime(adjtime
) == scenarios
[i
].expected_result
);
76 /* Test with the real /etc/adjtime */
77 static void test_clock_is_localtime_system(void) {
79 r
= clock_is_localtime(NULL
);
81 if (access("/etc/adjtime", F_OK
) == 0) {
82 log_info("/etc/adjtime exists, clock_is_localtime() == %i", r
);
83 /* if /etc/adjtime exists we expect some answer, no error or
85 assert_se(r
== 0 || r
== 1);
87 /* default is UTC if there is no /etc/adjtime */
91 int main(int argc
, char *argv
[]) {
92 test_clock_is_localtime();
93 test_clock_is_localtime_system();