2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
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);
|
2018-10-23 10:50:09 +02:00
|
|
|
ftruncate(fileno(f), 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;
|
|
|
|
}
|