| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <time.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "calendarspec.h" |
| 8 |
#include "env-util.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "tests.h" |
| 12 |
#include "time-util.h" |
| 13 |
|
| 14 |
static void _test_one(int line, const char *input, const char *output) { |
| 15 |
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
| 16 |
_cleanup_free_ char *p = NULL, *q = NULL; |
| 17 |
usec_t u; |
| 18 |
int r; |
| 19 |
|
| 20 |
r = calendar_spec_from_string(input, &c); |
| 21 |
if (r < 0) |
| 22 |
log_error_errno(r, "Failed to parse \"%s\": %m", input); |
| 23 |
ASSERT_OK(r); |
| 24 |
|
| 25 |
ASSERT_OK(calendar_spec_to_string(c, &p)); |
| 26 |
log_info("line %d: \"%s\" → \"%s\"%s%s", line, input, p, |
| 27 |
!streq(p, output) ? " expected:" : "", |
| 28 |
!streq(p, output) ? output : ""); |
| 29 |
|
| 30 |
ASSERT_STREQ(p, output); |
| 31 |
|
| 32 |
u = now(CLOCK_REALTIME); |
| 33 |
r = calendar_spec_next_usec(c, u, &u); |
| 34 |
log_info("Next: %s", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP(u)); |
| 35 |
c = calendar_spec_free(c); |
| 36 |
|
| 37 |
ASSERT_OK(calendar_spec_from_string(p, &c)); |
| 38 |
ASSERT_OK(calendar_spec_to_string(c, &q)); |
| 39 |
|
| 40 |
ASSERT_STREQ(q, p); |
| 41 |
} |
| 42 |
#define test_one(input, output) _test_one(__LINE__, input, output) |
| 43 |
|
| 44 |
static void _test_next(int line, const char *input, const char *new_tz, usec_t after, usec_t expect) { |
| 45 |
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
| 46 |
usec_t u; |
| 47 |
int r; |
| 48 |
|
| 49 |
SAVE_TIMEZONE; |
| 50 |
|
| 51 |
if (!isempty(new_tz) && !strchr(new_tz, ',')) |
| 52 |
new_tz = strjoina(":", new_tz); |
| 53 |
|
| 54 |
assert_se(set_unset_env("TZ", new_tz, true) == 0); |
| 55 |
tzset(); |
| 56 |
|
| 57 |
ASSERT_OK(calendar_spec_from_string(input, &c)); |
| 58 |
|
| 59 |
log_info("line %d: \"%s\" new_tz=%s", line, input, strnull(new_tz)); |
| 60 |
|
| 61 |
u = after; |
| 62 |
r = calendar_spec_next_usec(c, after, &u); |
| 63 |
log_info("At: %s", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US)); |
| 64 |
if (expect != USEC_INFINITY) |
| 65 |
assert_se(r >= 0 && u == expect); |
| 66 |
else |
| 67 |
assert_se(r == -ENOENT); |
| 68 |
} |
| 69 |
#define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect) |
| 70 |
|
| 71 |
TEST(timestamp) { |
| 72 |
char buf[FORMAT_TIMESTAMP_MAX]; |
| 73 |
_cleanup_free_ char *t = NULL; |
| 74 |
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
| 75 |
usec_t x, y; |
| 76 |
|
| 77 |
/* Ensure that a timestamp is also a valid calendar specification. Convert forth and back */ |
| 78 |
|
| 79 |
x = now(CLOCK_REALTIME); |
| 80 |
|
| 81 |
assert_se(format_timestamp_style(buf, sizeof buf, x, TIMESTAMP_US)); |
| 82 |
log_info("%s", buf); |
| 83 |
ASSERT_OK(calendar_spec_from_string(buf, &c)); |
| 84 |
ASSERT_OK(calendar_spec_to_string(c, &t)); |
| 85 |
log_info("%s", t); |
| 86 |
|
| 87 |
ASSERT_OK(parse_timestamp(t, &y)); |
| 88 |
assert_se(y == x); |
| 89 |
} |
| 90 |
|
| 91 |
TEST(hourly_bug_4031) { |
| 92 |
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
| 93 |
usec_t n, u, w; |
| 94 |
int r; |
| 95 |
|
| 96 |
ASSERT_OK(calendar_spec_from_string("hourly", &c)); |
| 97 |
n = now(CLOCK_REALTIME); |
| 98 |
ASSERT_OK(r = calendar_spec_next_usec(c, n, &u)); |
| 99 |
|
| 100 |
log_info("Now: %s (%"PRIu64")", FORMAT_TIMESTAMP_STYLE(n, TIMESTAMP_US), n); |
| 101 |
log_info("Next hourly: %s (%"PRIu64")", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US), u); |
| 102 |
|
| 103 |
assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0); |
| 104 |
log_info("Next hourly: %s (%"PRIu64")", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP_STYLE(w, TIMESTAMP_US), w); |
| 105 |
|
| 106 |
assert_se(n < u); |
| 107 |
assert_se(u <= n + USEC_PER_HOUR); |
| 108 |
assert_se(u < w); |
| 109 |
assert_se(w <= u + USEC_PER_HOUR); |
| 110 |
} |
| 111 |
|
| 112 |
TEST(calendar_spec_one) { |
| 113 |
test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); |
| 114 |
test_one("Sat,Thu,Mon..Wed,Sat..Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); |
| 115 |
test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00"); |
| 116 |
test_one("Wed *-1", "Wed *-*-01 00:00:00"); |
| 117 |
test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00"); |
| 118 |
test_one("Wed..Wed,Wed *-1", "Wed *-*-01 00:00:00"); |
| 119 |
test_one("Wed, 17:48", "Wed *-*-* 17:48:00"); |
| 120 |
test_one("Wednesday,", "Wed *-*-* 00:00:00"); |
| 121 |
test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); |
| 122 |
test_one("Wed..Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); |
| 123 |
test_one("*-*-7 0:0:0", "*-*-07 00:00:00"); |
| 124 |
test_one("10-15", "*-10-15 00:00:00"); |
| 125 |
test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00"); |
| 126 |
test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45"); |
| 127 |
test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00"); |
| 128 |
test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45"); |
| 129 |
test_one("03-05 08:05:40", "*-03-05 08:05:40"); |
| 130 |
test_one("08:05:40", "*-*-* 08:05:40"); |
| 131 |
test_one("05:40", "*-*-* 05:40:00"); |
| 132 |
test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40"); |
| 133 |
test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40"); |
| 134 |
test_one("2003-03-05 05:40", "2003-03-05 05:40:00"); |
| 135 |
test_one("2003-03-05", "2003-03-05 00:00:00"); |
| 136 |
test_one("03-05", "*-03-05 00:00:00"); |
| 137 |
test_one("hourly", "*-*-* *:00:00"); |
| 138 |
test_one("daily", "*-*-* 00:00:00"); |
| 139 |
test_one("monthly", "*-*-01 00:00:00"); |
| 140 |
test_one("weekly", "Mon *-*-* 00:00:00"); |
| 141 |
test_one("minutely", "*-*-* *:*:00"); |
| 142 |
test_one("quarterly", "*-01,04,07,10-01 00:00:00"); |
| 143 |
test_one("semi-annually", "*-01,07-01 00:00:00"); |
| 144 |
test_one("annually", "*-01-01 00:00:00"); |
| 145 |
test_one("*:2/3", "*-*-* *:02/3:00"); |
| 146 |
test_one("2015-10-25 01:00:00 uTc", "2015-10-25 01:00:00 UTC"); |
| 147 |
test_one("2015-10-25 01:00:00 Asia/Vladivostok", "2015-10-25 01:00:00 Asia/Vladivostok"); |
| 148 |
test_one("weekly Pacific/Auckland", "Mon *-*-* 00:00:00 Pacific/Auckland"); |
| 149 |
test_one("2016-03-27 03:17:00.4200005", "2016-03-27 03:17:00.420001"); |
| 150 |
test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000"); |
| 151 |
test_one("9..11,13:00,30", "*-*-* 09..11,13:00,30:00"); |
| 152 |
test_one("1..3-1..3 1..3:1..3", "*-01..03-01..03 01..03:01..03:00"); |
| 153 |
test_one("00:00:1.125..2.125", "*-*-* 00:00:01.125000..02.125000"); |
| 154 |
test_one("00:00:1.0..3.8", "*-*-* 00:00:01..03"); |
| 155 |
test_one("00:00:01..03", "*-*-* 00:00:01..03"); |
| 156 |
test_one("00:00:01/2,02..03", "*-*-* 00:00:01/2,02..03"); |
| 157 |
test_one("*:4,30:0..3", "*-*-* *:04,30:00..03"); |
| 158 |
test_one("*:4,30:0/1", "*-*-* *:04,30:*"); |
| 159 |
test_one("*:4,30:0/1,3,5", "*-*-* *:04,30:*"); |
| 160 |
test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC"); |
| 161 |
test_one("*-*~05,3 ", "*-*~03,05 00:00:00"); |
| 162 |
test_one("*-*~* 00:00:00", "*-*-* 00:00:00"); |
| 163 |
test_one("Monday", "Mon *-*-* 00:00:00"); |
| 164 |
test_one("Monday *-*-*", "Mon *-*-* 00:00:00"); |
| 165 |
test_one("*-*-*", "*-*-* 00:00:00"); |
| 166 |
test_one("*:*:*", "*-*-* *:*:*"); |
| 167 |
test_one("*:*", "*-*-* *:*:00"); |
| 168 |
test_one("12:*", "*-*-* 12:*:00"); |
| 169 |
test_one("*:30", "*-*-* *:30:00"); |
| 170 |
test_one("93..00-*-*", "1993..2000-*-* 00:00:00"); |
| 171 |
test_one("00..07-*-*", "2000..2007-*-* 00:00:00"); |
| 172 |
test_one("*:20..39/5", "*-*-* *:20..35/5:00"); |
| 173 |
test_one("00:00:20..40/1", "*-*-* 00:00:20..40"); |
| 174 |
test_one("*~03/1,03..05", "*-*~03/1,03..05 00:00:00"); |
| 175 |
/* UNIX timestamps are always UTC */ |
| 176 |
test_one("@1493187147", "2017-04-26 06:12:27 UTC"); |
| 177 |
test_one("@1493187147 UTC", "2017-04-26 06:12:27 UTC"); |
| 178 |
test_one("@0", "1970-01-01 00:00:00 UTC"); |
| 179 |
test_one("@0 UTC", "1970-01-01 00:00:00 UTC"); |
| 180 |
test_one("*:05..05", "*-*-* *:05:00"); |
| 181 |
test_one("*:05..10/6", "*-*-* *:05:00"); |
| 182 |
} |
| 183 |
|
| 184 |
TEST(calendar_spec_next) { |
| 185 |
test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); |
| 186 |
test_next("2016-03-27 03:17:00", "Europe/Berlin", 12345, 1459041420000000); |
| 187 |
test_next("2016-03-27 03:17:00", "Europe/Helsinki", 12345, -1); |
| 188 |
test_next("2016-03-27 03:17:00 UTC", NULL, 12345, 1459048620000000); |
| 189 |
test_next("2016-03-27 03:17:00 UTC", "", 12345, 1459048620000000); |
| 190 |
test_next("2016-03-27 03:17:00 UTC", "Europe/Berlin", 12345, 1459048620000000); |
| 191 |
test_next("2016-03-27 03:17:00 UTC", "Europe/Helsinki", 12345, 1459048620000000); |
| 192 |
test_next("2016-03-27 03:17:00.420000001 UTC", "Europe/Helsinki", 12345, 1459048620420000); |
| 193 |
test_next("2016-03-27 03:17:00.4200005 UTC", "Europe/Helsinki", 12345, 1459048620420001); |
| 194 |
test_next("2015-11-13 09:11:23.42", "Europe/Helsinki", 12345, 1447398683420000); |
| 195 |
test_next("2015-11-13 09:11:23.42/1.77", "Europe/Helsinki", 1447398683420000, 1447398685190000); |
| 196 |
test_next("2015-11-13 09:11:23.42/1.77", "Europe/Helsinki", 1447398683419999, 1447398683420000); |
| 197 |
test_next("Sun 16:00:00", "Europe/Berlin", 1456041600123456, 1456066800000000); |
| 198 |
test_next("*-04-31", "", 12345, -1); |
| 199 |
test_next("2016-02~01 UTC", "", 12345, 1456704000000000); |
| 200 |
test_next("Mon 2017-05~01..07 UTC", "", 12345, 1496016000000000); |
| 201 |
test_next("Mon 2017-05~07/1 UTC", "", 12345, 1496016000000000); |
| 202 |
test_next("*-*-01/5 04:00:00 UTC", "", 1646010000000000, 1646107200000000); |
| 203 |
test_next("*-01/7-01 04:00:00 UTC", "", 1664607600000000, 1672545600000000); |
| 204 |
test_next("2017-08-06 9,11,13,15,17:00 UTC", "", 1502029800000000, 1502031600000000); |
| 205 |
test_next("2017-08-06 9..17/2:00 UTC", "", 1502029800000000, 1502031600000000); |
| 206 |
test_next("2016-12-* 3..21/6:00 UTC", "", 1482613200000001, 1482634800000000); |
| 207 |
test_next("2017-09-24 03:30:00 Pacific/Auckland", "", 12345, 1506177000000000); |
| 208 |
/* Due to daylight saving time - 2017-09-24 02:30:00 does not exist */ |
| 209 |
test_next("2017-09-24 02:30:00 Pacific/Auckland", "", 12345, -1); |
| 210 |
test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 12345, 1491053400000000); |
| 211 |
/* Confirm that even though it's a time change here (backward) 02:30 happens only once */ |
| 212 |
test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 1491053400000000, -1); |
| 213 |
test_next("2017-04-02 03:30:00 Pacific/Auckland", "", 12345, 1491060600000000); |
| 214 |
/* Confirm that timezones in the Spec work regardless of current timezone */ |
| 215 |
test_next("2017-09-09 20:42:00 Pacific/Auckland", "", 12345, 1504946520000000); |
| 216 |
test_next("2017-09-09 20:42:00 Pacific/Auckland", "Europe/Helsinki", 12345, 1504946520000000); |
| 217 |
/* Check that we don't start looping if mktime() moves us backwards */ |
| 218 |
test_next("Sun *-*-* 01:00:00 Europe/Dublin", "", 1616412478000000, 1617494400000000); |
| 219 |
test_next("Sun *-*-* 01:00:00 Europe/Dublin", "IST", 1616412478000000, 1617494400000000); |
| 220 |
/* Europe/Dublin TZ that moves DST backwards */ |
| 221 |
test_next("hourly", "IST-1GMT-0,M10.5.0/1,M3.5.0/1", 1743292800000000, 1743296400000000); |
| 222 |
/* Check when the year changes, see issue #40260 */ |
| 223 |
test_next("*-*-1/11 23:00:00 UTC", "", 1763938800000000, 1764630000000000); |
| 224 |
} |
| 225 |
|
| 226 |
TEST(calendar_spec_from_string) { |
| 227 |
CalendarSpec *c; |
| 228 |
|
| 229 |
assert_se(calendar_spec_from_string("test", &c) == -EINVAL); |
| 230 |
assert_se(calendar_spec_from_string(" utc", &c) == -EINVAL); |
| 231 |
assert_se(calendar_spec_from_string(" ", &c) == -EINVAL); |
| 232 |
assert_se(calendar_spec_from_string("", &c) == -EINVAL); |
| 233 |
assert_se(calendar_spec_from_string("7", &c) == -EINVAL); |
| 234 |
assert_se(calendar_spec_from_string("121212:1:2", &c) == -EINVAL); |
| 235 |
assert_se(calendar_spec_from_string("2000-03-05.23 00:00:00", &c) == -EINVAL); |
| 236 |
assert_se(calendar_spec_from_string("2000-03-05 00:00.1:00", &c) == -EINVAL); |
| 237 |
assert_se(calendar_spec_from_string("00:00:00/0.00000001", &c) == -ERANGE); |
| 238 |
assert_se(calendar_spec_from_string("00:00:00.0..00.9", &c) == -EINVAL); |
| 239 |
assert_se(calendar_spec_from_string("2016~11-22", &c) == -EINVAL); |
| 240 |
assert_se(calendar_spec_from_string("*-*~5/5", &c) == -EINVAL); |
| 241 |
assert_se(calendar_spec_from_string("Monday.. 12:00", &c) == -EINVAL); |
| 242 |
assert_se(calendar_spec_from_string("Monday..", &c) == -EINVAL); |
| 243 |
assert_se(calendar_spec_from_string("-00:+00/-5", &c) == -EINVAL); |
| 244 |
assert_se(calendar_spec_from_string("00:+00/-5", &c) == -EINVAL); |
| 245 |
assert_se(calendar_spec_from_string("2016- 11- 24 12: 30: 00", &c) == -EINVAL); |
| 246 |
assert_se(calendar_spec_from_string("*~29", &c) == -EINVAL); |
| 247 |
assert_se(calendar_spec_from_string("*~16..31", &c) == -EINVAL); |
| 248 |
assert_se(calendar_spec_from_string("12..1/2-*", &c) == -EINVAL); |
| 249 |
assert_se(calendar_spec_from_string("20/4:00", &c) == -EINVAL); |
| 250 |
assert_se(calendar_spec_from_string("00:00/60", &c) == -EINVAL); |
| 251 |
assert_se(calendar_spec_from_string("00:00:2300", &c) == -ERANGE); |
| 252 |
assert_se(calendar_spec_from_string("00:00:18446744073709551615", &c) == -ERANGE); |
| 253 |
assert_se(calendar_spec_from_string("@88588582097858858", &c) == -ERANGE); |
| 254 |
assert_se(calendar_spec_from_string("*:4,30:*,5", &c) == -EINVAL); |
| 255 |
assert_se(calendar_spec_from_string("*:4,30:5,*", &c) == -EINVAL); |
| 256 |
assert_se(calendar_spec_from_string("*:4,30:*\n", &c) == -EINVAL); |
| 257 |
} |
| 258 |
|
| 259 |
static int intro(void) { |
| 260 |
/* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */ |
| 261 |
ASSERT_OK_ERRNO(unsetenv("TZ")); |
| 262 |
|
| 263 |
return EXIT_SUCCESS; |
| 264 |
} |
| 265 |
|
| 266 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |