]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-clock.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-clock.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6369641d
MP
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
fe4f8fd1
ZJS
24#include "clock-util.h"
25#include "fd-util.h"
6369641d
MP
26#include "fileio.h"
27#include "log.h"
fe4f8fd1 28#include "macro.h"
6369641d
MP
29
30static void test_clock_is_localtime(void) {
31 char adjtime[] = "/tmp/test-adjtime.XXXXXX";
07edd3b9
MP
32 int fd = -1;
33 _cleanup_fclose_ FILE* f = NULL;
6369641d 34
fe4f8fd1 35 static const struct scenario {
6369641d
MP
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},
35f7216f
MP
46 /* empty value -> defaults to UTC */
47 {"0.0 0 0\n0\n", 0},
6369641d
MP
48 /* unknown value -> defaults to UTC */
49 {"0.0 0 0\n0\nFOO\n", 0},
35f7216f
MP
50 /* no third line */
51 {"0.0 0 0", 0},
52 {"0.0 0 0\n", 0},
53 {"0.0 0 0\n0", 0},
6369641d
MP
54 };
55
56 /* without an adjtime file we default to UTC */
57 assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
58
646853bd 59 fd = mkostemp_safe(adjtime);
d69d951b 60 assert_se(fd >= 0);
6369641d
MP
61 log_info("adjtime test file: %s", adjtime);
62 f = fdopen(fd, "w");
d69d951b 63 assert_se(f);
6369641d
MP
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);
b1837133 70 assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
6369641d
MP
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 */
78static 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);
35f7216f
MP
84 /* if /etc/adjtime exists we expect some answer, no error or
85 * crash */
4c701096 86 assert_se(IN_SET(r, 0, 1));
6369641d
MP
87 } else
88 /* default is UTC if there is no /etc/adjtime */
d69d951b 89 assert_se(r == 0);
6369641d
MP
90}
91
92int main(int argc, char *argv[]) {
93 test_clock_is_localtime();
94 test_clock_is_localtime_system();
95
96 return 0;
97}