2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2016-02-26 11:25:22 +01:00
|
|
|
/***
|
2018-06-12 17:15:23 +02:00
|
|
|
Copyright © 2016 Canonical Ltd.
|
2016-02-26 11:25:22 +01:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2016-02-29 14:03:32 +01:00
|
|
|
#include "clock-util.h"
|
|
|
|
#include "fd-util.h"
|
2016-02-26 11:25:22 +01:00
|
|
|
#include "fileio.h"
|
2018-10-23 10:50:09 +02:00
|
|
|
#include "fs-util.h"
|
2016-02-26 11:25:22 +01:00
|
|
|
#include "log.h"
|
2016-02-29 14:03:32 +01:00
|
|
|
#include "macro.h"
|
2018-11-30 21:05:27 +01:00
|
|
|
#include "tmpfile-util.h"
|
2016-02-26 11:25:22 +01:00
|
|
|
|
|
|
|
static void test_clock_is_localtime(void) {
|
2018-10-23 10:50:38 +02:00
|
|
|
_cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX";
|
2016-02-29 17:40:08 +01:00
|
|
|
_cleanup_fclose_ FILE* f = NULL;
|
2016-02-26 11:25:22 +01:00
|
|
|
|
2016-02-29 14:03:32 +01:00
|
|
|
static const struct scenario {
|
2016-02-26 11:25:22 +01:00
|
|
|
const char* contents;
|
|
|
|
int expected_result;
|
|
|
|
} scenarios[] = {
|
|
|
|
/* adjtime configures UTC */
|
|
|
|
{"0.0 0 0\n0\nUTC\n", 0},
|
|
|
|
/* adjtime configures local time */
|
|
|
|
{"0.0 0 0\n0\nLOCAL\n", 1},
|
|
|
|
/* no final EOL */
|
|
|
|
{"0.0 0 0\n0\nUTC", 0},
|
|
|
|
{"0.0 0 0\n0\nLOCAL", 1},
|
2016-02-26 12:33:41 +01:00
|
|
|
/* empty value -> defaults to UTC */
|
|
|
|
{"0.0 0 0\n0\n", 0},
|
2016-02-26 11:25:22 +01:00
|
|
|
/* unknown value -> defaults to UTC */
|
|
|
|
{"0.0 0 0\n0\nFOO\n", 0},
|
2016-02-26 12:33:41 +01:00
|
|
|
/* no third line */
|
|
|
|
{"0.0 0 0", 0},
|
|
|
|
{"0.0 0 0\n", 0},
|
|
|
|
{"0.0 0 0\n0", 0},
|
2016-02-26 11:25:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* without an adjtime file we default to UTC */
|
|
|
|
assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
|
|
|
|
|
2018-10-23 10:50:09 +02:00
|
|
|
assert_se(fmkostemp_safe(adjtime, "w", &f) == 0);
|
2016-02-26 11:25:22 +01:00
|
|
|
log_info("adjtime test file: %s", adjtime);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) {
|
|
|
|
log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result);
|
|
|
|
log_info("%s", scenarios[i].contents);
|
|
|
|
rewind(f);
|
Fix clang-11 issues
Tested with clang 11.0.0-++20200715043845+0e377e253c1-1~exp1 on Debian sid
../src/network/test-networkd-conf.c:104:56: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
test_config_parse_duid_rawdata_one(BYTES_0_128 + 2, 0, &(DUID){0, 128, BYTES_1_128});
~~~~~~~~~~~~^~~
../src/network/test-networkd-conf.c:104:56: note: use array indexing to silence this warning
test_config_parse_duid_rawdata_one(BYTES_0_128 + 2, 0, &(DUID){0, 128, BYTES_1_128});
^
& [ ]
1 warning generated.
../src/test/test-clock.c:52:17: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
ftruncate(fileno(f), 0);
^~~~~~~~~ ~~~~~~~~~~~~
1 warning generated.
(gdb) run
Starting program: systemd/build/test-alloc-util
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
x1: 0x7fffffffd2d0
x2: 0x7fffffffdac0
y: 0x7fffffffd2cc
z: 0x7fffffffd2c0
cleanup2(0x7fffffffd2cc)
cleanup3(0x7fffffffd2c0)
cleanup1(0x7fffffffdac0)
cleanup1(0x7fffffffd2d0)
*** buffer overflow detected ***: terminated
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt full
set = {__val = {0, 18446744073709551615, 0, 0, 0, 0, 140737348658240, 140737348659520, 0, 0, 0, 0, 0, 0, 0, 0}}
pid = <optimized out>
tid = <optimized out>
ret = <optimized out>
save_stage = 1
act = {__sigaction_handler = {sa_handler = 0x22ff0, sa_sigaction = 0x22ff0}, sa_mask = {__val = {140737349888603, 4217127, 4217127, 4217127, 4217127, 4217383, 0, 0, 0, 0, 143329, 143344, 140737351162752, 8959, 18446744073709551328, 4289072}},
sa_flags = -138460788, sa_restorer = 0xffffffffffffffff}
sigs = {__val = {32, 0 <repeats 15 times>}}
ap = {{gp_offset = 24, fp_offset = 0, overflow_arg_area = 0x7fffffffd280, reg_save_area = 0x7fffffffd210}}
fd = <optimized out>
list = <optimized out>
nlist = <optimized out>
cp = <optimized out>
No locals.
No locals.
No locals.
No locals.
No locals.
l = 0
No locals.
p1 = 0x405500 "\223Nd\n\351\301mA\214\262A\247\306b\276\317\327\353\346k\035\024\273{\276&!kλ\233\217\t\207\276\327\347\351\355\307R\276\063{\235w=\237E\357\277KL\245\374\245\066M\201+\333\064\272\332g>1<@"
p2 = <optimized out>
i = <optimized out>
No locals.
2020-07-25 18:23:11 +02:00
|
|
|
assert_se(ftruncate(fileno(f), 0) == 0);
|
2017-09-22 20:55:34 +02:00
|
|
|
assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
|
2016-02-26 11:25:22 +01:00
|
|
|
assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test with the real /etc/adjtime */
|
|
|
|
static void test_clock_is_localtime_system(void) {
|
|
|
|
int r;
|
|
|
|
r = clock_is_localtime(NULL);
|
|
|
|
|
2020-04-23 12:12:23 +02:00
|
|
|
if (access("/etc/adjtime", R_OK) == 0) {
|
|
|
|
log_info("/etc/adjtime is readable, clock_is_localtime() == %i", r);
|
2016-02-26 12:33:41 +01:00
|
|
|
/* if /etc/adjtime exists we expect some answer, no error or
|
|
|
|
* crash */
|
2017-10-04 16:01:32 +02:00
|
|
|
assert_se(IN_SET(r, 0, 1));
|
2016-02-26 11:25:22 +01:00
|
|
|
} else
|
|
|
|
/* default is UTC if there is no /etc/adjtime */
|
2020-04-23 12:12:23 +02:00
|
|
|
assert_se(r == 0 || ERRNO_IS_PRIVILEGE(r));
|
2016-02-26 11:25:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
test_clock_is_localtime();
|
|
|
|
test_clock_is_localtime_system();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|