]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-clock.c
fileio: simplify mkostemp_safe() (#4090)
[thirdparty/systemd.git] / src / test / test-clock.c
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
23 #include "clock-util.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "log.h"
27 #include "macro.h"
28
29 static void test_clock_is_localtime(void) {
30 char adjtime[] = "/tmp/test-adjtime.XXXXXX";
31 int fd = -1;
32 _cleanup_fclose_ FILE* f = NULL;
33
34 static const struct scenario {
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},
45 /* empty value -> defaults to UTC */
46 {"0.0 0 0\n0\n", 0},
47 /* unknown value -> defaults to UTC */
48 {"0.0 0 0\n0\nFOO\n", 0},
49 /* no third line */
50 {"0.0 0 0", 0},
51 {"0.0 0 0\n", 0},
52 {"0.0 0 0\n0", 0},
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);
59 assert_se(fd >= 0);
60 log_info("adjtime test file: %s", adjtime);
61 f = fdopen(fd, "w");
62 assert_se(f);
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);
83 /* if /etc/adjtime exists we expect some answer, no error or
84 * crash */
85 assert_se(r == 0 || r == 1);
86 } else
87 /* default is UTC if there is no /etc/adjtime */
88 assert_se(r == 0);
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 }