]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-time-util.c
time-util: Introduce parse_sec_def_infinity
[thirdparty/systemd.git] / src / test / test-time-util.c
index ebf85fcc7cb46236729ac38afc58c66b3466fa07..633544ff2e1d7de04aa4fd9140d161ff78261265 100644 (file)
@@ -1,24 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2013 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include "random-util.h"
+#include "serialize.h"
 #include "string-util.h"
 #include "strv.h"
 #include "time-util.h"
@@ -26,6 +9,8 @@
 static void test_parse_sec(void) {
         usec_t u;
 
+        log_info("/* %s */", __func__);
+
         assert_se(parse_sec("5s", &u) >= 0);
         assert_se(u == 5 * USEC_PER_SEC);
         assert_se(parse_sec("5s500ms", &u) >= 0);
@@ -52,32 +37,79 @@ static void test_parse_sec(void) {
         assert_se(u == USEC_INFINITY);
         assert_se(parse_sec(" infinity ", &u) >= 0);
         assert_se(u == USEC_INFINITY);
+        assert_se(parse_sec("+3.1s", &u) >= 0);
+        assert_se(u == 3100 * USEC_PER_MSEC);
+        assert_se(parse_sec("3.1s.2", &u) >= 0);
+        assert_se(u == 3300 * USEC_PER_MSEC);
+        assert_se(parse_sec("3.1 .2", &u) >= 0);
+        assert_se(u == 3300 * USEC_PER_MSEC);
+        assert_se(parse_sec("3.1 sec .2 sec", &u) >= 0);
+        assert_se(u == 3300 * USEC_PER_MSEC);
+        assert_se(parse_sec("3.1 sec 1.2 sec", &u) >= 0);
+        assert_se(u == 4300 * USEC_PER_MSEC);
 
         assert_se(parse_sec(" xyz ", &u) < 0);
         assert_se(parse_sec("", &u) < 0);
         assert_se(parse_sec(" . ", &u) < 0);
         assert_se(parse_sec(" 5. ", &u) < 0);
         assert_se(parse_sec(".s ", &u) < 0);
+        assert_se(parse_sec("-5s ", &u) < 0);
+        assert_se(parse_sec("-0.3s ", &u) < 0);
+        assert_se(parse_sec("-0.0s ", &u) < 0);
+        assert_se(parse_sec("-0.-0s ", &u) < 0);
+        assert_se(parse_sec("0.-0s ", &u) < 0);
+        assert_se(parse_sec("3.-0s ", &u) < 0);
         assert_se(parse_sec(" infinity .7", &u) < 0);
         assert_se(parse_sec(".3 infinity", &u) < 0);
+        assert_se(parse_sec("3.+1s", &u) < 0);
+        assert_se(parse_sec("3. 1s", &u) < 0);
+        assert_se(parse_sec("3.s", &u) < 0);
+        assert_se(parse_sec("12.34.56", &u) < 0);
+        assert_se(parse_sec("12..34", &u) < 0);
+        assert_se(parse_sec("..1234", &u) < 0);
+        assert_se(parse_sec("1234..", &u) < 0);
 }
 
 static void test_parse_sec_fix_0(void) {
         usec_t u;
 
+        log_info("/* %s */", __func__);
+
         assert_se(parse_sec_fix_0("5s", &u) >= 0);
         assert_se(u == 5 * USEC_PER_SEC);
         assert_se(parse_sec_fix_0("0s", &u) >= 0);
-        assert_se(u == 0 * USEC_PER_SEC);
+        assert_se(u == USEC_INFINITY);
         assert_se(parse_sec_fix_0("0", &u) >= 0);
         assert_se(u == USEC_INFINITY);
         assert_se(parse_sec_fix_0(" 0", &u) >= 0);
         assert_se(u == USEC_INFINITY);
 }
 
+static void test_parse_sec_def_infinity(void) {
+        usec_t u;
+
+        log_info("/* %s */", __func__);
+
+        assert_se(parse_sec_def_infinity("5s", &u) >= 0);
+        assert_se(u == 5 * USEC_PER_SEC);
+        assert_se(parse_sec_def_infinity("", &u) >= 0);
+        assert_se(u == USEC_INFINITY);
+        assert_se(parse_sec_def_infinity("     ", &u) >= 0);
+        assert_se(u == USEC_INFINITY);
+        assert_se(parse_sec_def_infinity("0s", &u) >= 0);
+        assert_se(u == 0);
+        assert_se(parse_sec_def_infinity("0", &u) >= 0);
+        assert_se(u == 0);
+        assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
+        assert_se(u == 0);
+        assert_se(parse_sec_def_infinity("-5s", &u) < 0);
+}
+
 static void test_parse_time(void) {
         usec_t u;
 
+        log_info("/* %s */", __func__);
+
         assert_se(parse_time("5", &u, 1) >= 0);
         assert_se(u == 5);
 
@@ -95,11 +127,16 @@ static void test_parse_time(void) {
 
         assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
         assert_se(u == 5 * USEC_PER_SEC);
+
+        assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE);
+        assert_se(parse_time("1.1111111111111y", &u, 1) >= 0);
 }
 
 static void test_parse_nsec(void) {
         nsec_t u;
 
+        log_info("/* %s */", __func__);
+
         assert_se(parse_nsec("5s", &u) >= 0);
         assert_se(u == 5 * NSEC_PER_SEC);
         assert_se(parse_nsec("5s500ms", &u) >= 0);
@@ -122,6 +159,16 @@ static void test_parse_nsec(void) {
         assert_se(u == NSEC_INFINITY);
         assert_se(parse_nsec(" infinity ", &u) >= 0);
         assert_se(u == NSEC_INFINITY);
+        assert_se(parse_nsec("+3.1s", &u) >= 0);
+        assert_se(u == 3100 * NSEC_PER_MSEC);
+        assert_se(parse_nsec("3.1s.2", &u) >= 0);
+        assert_se(u == 3100 * NSEC_PER_MSEC);
+        assert_se(parse_nsec("3.1 .2s", &u) >= 0);
+        assert_se(u == 200 * NSEC_PER_MSEC + 3);
+        assert_se(parse_nsec("3.1 sec .2 sec", &u) >= 0);
+        assert_se(u == 3300 * NSEC_PER_MSEC);
+        assert_se(parse_nsec("3.1 sec 1.2 sec", &u) >= 0);
+        assert_se(u == 4300 * NSEC_PER_MSEC);
 
         assert_se(parse_nsec(" xyz ", &u) < 0);
         assert_se(parse_nsec("", &u) < 0);
@@ -130,22 +177,36 @@ static void test_parse_nsec(void) {
         assert_se(parse_nsec(".s ", &u) < 0);
         assert_se(parse_nsec(" infinity .7", &u) < 0);
         assert_se(parse_nsec(".3 infinity", &u) < 0);
+        assert_se(parse_nsec("-5s ", &u) < 0);
+        assert_se(parse_nsec("-0.3s ", &u) < 0);
+        assert_se(parse_nsec("-0.0s ", &u) < 0);
+        assert_se(parse_nsec("-0.-0s ", &u) < 0);
+        assert_se(parse_nsec("0.-0s ", &u) < 0);
+        assert_se(parse_nsec("3.-0s ", &u) < 0);
+        assert_se(parse_nsec(" infinity .7", &u) < 0);
+        assert_se(parse_nsec(".3 infinity", &u) < 0);
+        assert_se(parse_nsec("3.+1s", &u) < 0);
+        assert_se(parse_nsec("3. 1s", &u) < 0);
+        assert_se(parse_nsec("3.s", &u) < 0);
+        assert_se(parse_nsec("12.34.56", &u) < 0);
+        assert_se(parse_nsec("12..34", &u) < 0);
+        assert_se(parse_nsec("..1234", &u) < 0);
+        assert_se(parse_nsec("1234..", &u) < 0);
+        assert_se(parse_nsec("1111111111111y", &u) == -ERANGE);
+        assert_se(parse_nsec("1.111111111111y", &u) >= 0);
 }
 
 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
-        char *r;
         char l[FORMAT_TIMESPAN_MAX];
+        const char *t;
         usec_t y;
 
         log_info(USEC_FMT"     (at accuracy "USEC_FMT")", x, accuracy);
 
-        r = format_timespan(l, sizeof(l), x, accuracy);
-        assert_se(r);
-
-        log_info(" = <%s>", l);
-
-        assert_se(parse_sec(l, &y) >= 0);
+        assert_se(t = format_timespan(l, sizeof l, x, accuracy));
+        log_info(" = <%s>", t);
 
+        assert_se(parse_sec(t, &y) >= 0);
         log_info(" = "USEC_FMT, y);
 
         if (accuracy <= 0)
@@ -155,6 +216,8 @@ static void test_format_timespan_one(usec_t x, usec_t accuracy) {
 }
 
 static void test_format_timespan(usec_t accuracy) {
+        log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy);
+
         test_format_timespan_one(0, accuracy);
         test_format_timespan_one(1, accuracy);
         test_format_timespan_one(1*USEC_PER_SEC, accuracy);
@@ -169,7 +232,6 @@ static void test_format_timespan(usec_t accuracy) {
         test_format_timespan_one(12345678, accuracy);
         test_format_timespan_one(1200000, accuracy);
         test_format_timespan_one(1230000, accuracy);
-        test_format_timespan_one(1230000, accuracy);
         test_format_timespan_one(1234000, accuracy);
         test_format_timespan_one(1234500, accuracy);
         test_format_timespan_one(1234560, accuracy);
@@ -181,9 +243,11 @@ static void test_format_timespan(usec_t accuracy) {
 }
 
 static void test_timezone_is_valid(void) {
-        assert_se(timezone_is_valid("Europe/Berlin"));
-        assert_se(timezone_is_valid("Australia/Sydney"));
-        assert_se(!timezone_is_valid("Europe/Do not exist"));
+        log_info("/* %s */", __func__);
+
+        assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR));
+        assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR));
+        assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR));
 }
 
 static void test_get_timezones(void) {
@@ -191,14 +255,18 @@ static void test_get_timezones(void) {
         int r;
         char **zone;
 
+        log_info("/* %s */", __func__);
+
         r = get_timezones(&zones);
         assert_se(r == 0);
 
         STRV_FOREACH(zone, zones)
-                assert_se(timezone_is_valid(*zone));
+                assert_se(timezone_is_valid(*zone, LOG_ERR));
 }
 
 static void test_usec_add(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(usec_add(0, 0) == 0);
         assert_se(usec_add(1, 4) == 5);
         assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
@@ -210,6 +278,8 @@ static void test_usec_add(void) {
 }
 
 static void test_usec_sub_unsigned(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(usec_sub_unsigned(0, 0) == 0);
         assert_se(usec_sub_unsigned(0, 2) == 0);
         assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0);
@@ -231,12 +301,13 @@ static void test_usec_sub_unsigned(void) {
 }
 
 static void test_usec_sub_signed(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(usec_sub_signed(0, 0) == 0);
         assert_se(usec_sub_signed(4, 1) == 3);
         assert_se(usec_sub_signed(4, 4) == 0);
         assert_se(usec_sub_signed(4, 5) == 0);
         assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
-        assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
         assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY);
         assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY);
         assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY);
@@ -245,6 +316,8 @@ static void test_usec_sub_signed(void) {
 static void test_format_timestamp(void) {
         unsigned i;
 
+        log_info("/* %s */", __func__);
+
         for (i = 0; i < 100; i++) {
                 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
                 usec_t x, y;
@@ -284,62 +357,66 @@ static void test_format_timestamp(void) {
         }
 }
 
-static void test_format_timestamp_utc_one(usec_t t, const char *result) {
+static void test_format_timestamp_utc_one(usec_t val, const char *result) {
         char buf[FORMAT_TIMESTAMP_MAX];
+        const char *t;
 
-        assert_se(!format_timestamp_utc(buf, sizeof(buf), t) == !result);
-
-        if (result)
-                assert_se(streq(result, buf));
+        t = format_timestamp_utc(buf, sizeof(buf), val);
+        assert_se(streq_ptr(t, result));
 }
 
 static void test_format_timestamp_utc(void) {
+        log_info("/* %s */", __func__);
+
         test_format_timestamp_utc_one(0, NULL);
         test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC");
         test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC");
 
 #if SIZEOF_TIME_T == 8
         test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC");
+        test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
 #elif SIZEOF_TIME_T == 4
         test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC");
+        test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
 #endif
 
-        test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX+1, NULL);
         test_format_timestamp_utc_one(USEC_INFINITY, NULL);
 }
 
-static void test_dual_timestamp_deserialize(void) {
+static void test_deserialize_dual_timestamp(void) {
         int r;
         dual_timestamp t;
 
-        r = dual_timestamp_deserialize("1234 5678", &t);
+        log_info("/* %s */", __func__);
+
+        r = deserialize_dual_timestamp("1234 5678", &t);
         assert_se(r == 0);
         assert_se(t.realtime == 1234);
         assert_se(t.monotonic == 5678);
 
-        r = dual_timestamp_deserialize("1234x 5678", &t);
+        r = deserialize_dual_timestamp("1234x 5678", &t);
         assert_se(r == -EINVAL);
 
-        r = dual_timestamp_deserialize("1234 5678y", &t);
+        r = deserialize_dual_timestamp("1234 5678y", &t);
         assert_se(r == -EINVAL);
 
-        r = dual_timestamp_deserialize("-1234 5678", &t);
+        r = deserialize_dual_timestamp("-1234 5678", &t);
         assert_se(r == -EINVAL);
 
-        r = dual_timestamp_deserialize("1234 -5678", &t);
+        r = deserialize_dual_timestamp("1234 -5678", &t);
         assert_se(r == -EINVAL);
 
         /* Check that output wasn't modified. */
         assert_se(t.realtime == 1234);
         assert_se(t.monotonic == 5678);
 
-        r = dual_timestamp_deserialize("+123 567", &t);
+        r = deserialize_dual_timestamp("+123 567", &t);
         assert_se(r == 0);
         assert_se(t.realtime == 123);
         assert_se(t.monotonic == 567);
 
         /* Check that we get "infinity" on overflow. */
-        r = dual_timestamp_deserialize("18446744073709551617 0", &t);
+        r = deserialize_dual_timestamp("18446744073709551617 0", &t);
         assert_se(r == 0);
         assert_se(t.realtime == USEC_INFINITY);
         assert_se(t.monotonic == 0);
@@ -359,6 +436,8 @@ static void assert_similar(usec_t a, usec_t b) {
 static void test_usec_shift_clock(void) {
         usec_t rt, mn, bt;
 
+        log_info("/* %s */", __func__);
+
         rt = now(CLOCK_REALTIME);
         mn = now(CLOCK_MONOTONIC);
         bt = now(clock_boottime_or_monotonic());
@@ -384,6 +463,8 @@ static void test_usec_shift_clock(void) {
 }
 
 static void test_in_utc_timezone(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(setenv("TZ", ":UTC", 1) >= 0);
         assert_se(in_utc_timezone());
         assert_se(streq(tzname[0], "UTC"));
@@ -411,6 +492,7 @@ int main(int argc, char *argv[]) {
 
         test_parse_sec();
         test_parse_sec_fix_0();
+        test_parse_sec_def_infinity();
         test_parse_time();
         test_parse_nsec();
         test_format_timespan(1);
@@ -423,7 +505,7 @@ int main(int argc, char *argv[]) {
         test_usec_sub_unsigned();
         test_format_timestamp();
         test_format_timestamp_utc();
-        test_dual_timestamp_deserialize();
+        test_deserialize_dual_timestamp();
         test_usec_shift_clock();
         test_in_utc_timezone();