]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/test/test-clock.c
ukify: fix parsing uname version with '+'
[thirdparty/systemd.git] / src / test / test-clock.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/***
3 Copyright © 2016 Canonical Ltd.
4***/
5
6#include <unistd.h>
7
8#include "clock-util.h"
9#include "fd-util.h"
10#include "fileio.h"
11#include "log.h"
12#include "tests.h"
13#include "tmpfile-util.h"
14
15TEST(clock_is_localtime) {
16 _cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX";
17 _cleanup_fclose_ FILE* f = NULL;
18
19 static const struct scenario {
20 const char* contents;
21 int expected_result;
22 } scenarios[] = {
23 /* adjtime configures UTC */
24 {"0.0 0 0\n0\nUTC\n", 0},
25 /* adjtime configures local time */
26 {"0.0 0 0\n0\nLOCAL\n", 1},
27 /* no final EOL */
28 {"0.0 0 0\n0\nUTC", 0},
29 {"0.0 0 0\n0\nLOCAL", 1},
30 /* empty value -> defaults to UTC */
31 {"0.0 0 0\n0\n", 0},
32 /* unknown value -> defaults to UTC */
33 {"0.0 0 0\n0\nFOO\n", 0},
34 /* no third line */
35 {"0.0 0 0", 0},
36 {"0.0 0 0\n", 0},
37 {"0.0 0 0\n0", 0},
38 };
39
40 /* without an adjtime file we default to UTC */
41 ASSERT_FALSE(clock_is_localtime("/nonexisting/adjtime"));
42
43 ASSERT_OK(fmkostemp_safe(adjtime, "w", &f));
44 log_info("adjtime test file: %s", adjtime);
45
46 for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) {
47 log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result);
48 log_info("%s", scenarios[i].contents);
49 rewind(f);
50 ASSERT_OK_ERRNO(ftruncate(fileno(f), 0));
51 ASSERT_OK(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE));
52 ASSERT_TRUE(clock_is_localtime(adjtime) == scenarios[i].expected_result);
53 }
54}
55
56/* Test with the real /etc/adjtime */
57TEST(clock_is_localtime_system) {
58 int r;
59 r = clock_is_localtime(NULL);
60
61 if (access("/etc/adjtime", R_OK) == 0) {
62 log_info("/etc/adjtime is readable, clock_is_localtime() == %i", r);
63 /* if /etc/adjtime exists we expect some answer, no error or
64 * crash */
65 ASSERT_TRUE(IN_SET(r, 0, 1));
66 } else
67 /* default is UTC if there is no /etc/adjtime */
68 ASSERT_TRUE(r == 0 || ERRNO_IS_PRIVILEGE(r));
69}
70
71DEFINE_TEST_MAIN(LOG_INFO);