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