LCOV - code coverage report
Current view: top level - test - test-calendarspec.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 182 184 98.9 %
Date: 2019-08-23 13:36:53 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 64 122 52.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <string.h>
       4                 :            : 
       5                 :            : #include "alloc-util.h"
       6                 :            : #include "calendarspec.h"
       7                 :            : #include "errno-util.h"
       8                 :            : #include "string-util.h"
       9                 :            : #include "util.h"
      10                 :            : 
      11                 :        252 : static void test_one(const char *input, const char *output) {
      12                 :            :         CalendarSpec *c;
      13                 :        252 :         _cleanup_free_ char *p = NULL, *q = NULL;
      14                 :            :         usec_t u;
      15                 :            :         char buf[FORMAT_TIMESTAMP_MAX];
      16                 :            :         int r;
      17                 :            : 
      18         [ -  + ]:        252 :         assert_se(calendar_spec_from_string(input, &c) >= 0);
      19                 :            : 
      20         [ -  + ]:        252 :         assert_se(calendar_spec_to_string(c, &p) >= 0);
      21                 :        252 :         printf("\"%s\" → \"%s\"\n", input, p);
      22                 :            : 
      23         [ -  + ]:        252 :         assert_se(streq(p, output));
      24                 :            : 
      25                 :        252 :         u = now(CLOCK_REALTIME);
      26                 :        252 :         r = calendar_spec_next_usec(c, u, &u);
      27         [ +  + ]:        252 :         printf("Next: %s\n", r < 0 ? strerror_safe(r) : format_timestamp(buf, sizeof(buf), u));
      28                 :        252 :         calendar_spec_free(c);
      29                 :            : 
      30         [ -  + ]:        252 :         assert_se(calendar_spec_from_string(p, &c) >= 0);
      31         [ -  + ]:        252 :         assert_se(calendar_spec_to_string(c, &q) >= 0);
      32                 :        252 :         calendar_spec_free(c);
      33                 :            : 
      34         [ -  + ]:        252 :         assert_se(streq(q, p));
      35                 :        252 : }
      36                 :            : 
      37                 :        108 : static void test_next(const char *input, const char *new_tz, usec_t after, usec_t expect) {
      38                 :            :         CalendarSpec *c;
      39                 :            :         usec_t u;
      40                 :            :         char *old_tz;
      41                 :            :         char buf[FORMAT_TIMESTAMP_MAX];
      42                 :            :         int r;
      43                 :            : 
      44                 :        108 :         old_tz = getenv("TZ");
      45         [ -  + ]:        108 :         if (old_tz)
      46                 :          0 :                 old_tz = strdupa(old_tz);
      47                 :            : 
      48         [ +  + ]:        108 :         if (new_tz)
      49         [ -  + ]:        104 :                 assert_se(setenv("TZ", new_tz, 1) >= 0);
      50                 :            :         else
      51         [ -  + ]:          4 :                 assert_se(unsetenv("TZ") >= 0);
      52                 :        108 :         tzset();
      53                 :            : 
      54         [ -  + ]:        108 :         assert_se(calendar_spec_from_string(input, &c) >= 0);
      55                 :            : 
      56                 :        108 :         printf("\"%s\"\n", input);
      57                 :            : 
      58                 :        108 :         u = after;
      59                 :        108 :         r = calendar_spec_next_usec(c, after, &u);
      60         [ +  + ]:        108 :         printf("At: %s\n", r < 0 ? strerror_safe(r) : format_timestamp_us(buf, sizeof buf, u));
      61         [ +  + ]:        108 :         if (expect != (usec_t)-1)
      62   [ +  -  -  + ]:         92 :                 assert_se(r >= 0 && u == expect);
      63                 :            :         else
      64         [ -  + ]:         16 :                 assert(r == -ENOENT);
      65                 :            : 
      66                 :        108 :         calendar_spec_free(c);
      67                 :            : 
      68         [ -  + ]:        108 :         if (old_tz)
      69         [ #  # ]:          0 :                 assert_se(setenv("TZ", old_tz, 1) >= 0);
      70                 :            :         else
      71         [ -  + ]:        108 :                 assert_se(unsetenv("TZ") >= 0);
      72                 :        108 :         tzset();
      73                 :        108 : }
      74                 :            : 
      75                 :          4 : static void test_timestamp(void) {
      76                 :            :         char buf[FORMAT_TIMESTAMP_MAX];
      77                 :          4 :         _cleanup_free_ char *t = NULL;
      78                 :            :         CalendarSpec *c;
      79                 :            :         usec_t x, y;
      80                 :            : 
      81                 :            :         /* Ensure that a timestamp is also a valid calendar specification. Convert forth and back */
      82                 :            : 
      83                 :          4 :         x = now(CLOCK_REALTIME);
      84                 :            : 
      85         [ -  + ]:          4 :         assert_se(format_timestamp_us(buf, sizeof(buf), x));
      86                 :          4 :         printf("%s\n", buf);
      87         [ -  + ]:          4 :         assert_se(calendar_spec_from_string(buf, &c) >= 0);
      88         [ -  + ]:          4 :         assert_se(calendar_spec_to_string(c, &t) >= 0);
      89                 :          4 :         calendar_spec_free(c);
      90                 :          4 :         printf("%s\n", t);
      91                 :            : 
      92         [ -  + ]:          4 :         assert_se(parse_timestamp(t, &y) >= 0);
      93         [ -  + ]:          4 :         assert_se(y == x);
      94                 :          4 : }
      95                 :            : 
      96                 :          4 : static void test_hourly_bug_4031(void) {
      97                 :            :         CalendarSpec *c;
      98                 :            :         usec_t n, u, w;
      99                 :            :         char buf[FORMAT_TIMESTAMP_MAX], zaf[FORMAT_TIMESTAMP_MAX];
     100                 :            :         int r;
     101                 :            : 
     102         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("hourly", &c) >= 0);
     103                 :          4 :         n = now(CLOCK_REALTIME);
     104         [ -  + ]:          4 :         assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0);
     105                 :            : 
     106                 :          4 :         printf("Now: %s (%"PRIu64")\n", format_timestamp_us(buf, sizeof buf, n), n);
     107         [ -  + ]:          4 :         printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_us(buf, sizeof buf, u), u);
     108                 :            : 
     109         [ -  + ]:          4 :         assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0);
     110         [ -  + ]:          4 :         printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_us(zaf, sizeof zaf, w), w);
     111                 :            : 
     112         [ -  + ]:          4 :         assert_se(n < u);
     113         [ -  + ]:          4 :         assert_se(u <= n + USEC_PER_HOUR);
     114         [ -  + ]:          4 :         assert_se(u < w);
     115         [ -  + ]:          4 :         assert_se(w <= u + USEC_PER_HOUR);
     116                 :            : 
     117                 :          4 :         calendar_spec_free(c);
     118                 :          4 : }
     119                 :            : 
     120                 :          4 : int main(int argc, char* argv[]) {
     121                 :            :         CalendarSpec *c;
     122                 :            : 
     123                 :          4 :         test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00");
     124                 :          4 :         test_one("Sat,Thu,Mon..Wed,Sat..Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00");
     125                 :          4 :         test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00");
     126                 :          4 :         test_one("Wed *-1", "Wed *-*-01 00:00:00");
     127                 :          4 :         test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00");
     128                 :          4 :         test_one("Wed..Wed,Wed *-1", "Wed *-*-01 00:00:00");
     129                 :          4 :         test_one("Wed, 17:48", "Wed *-*-* 17:48:00");
     130                 :          4 :         test_one("Wednesday,", "Wed *-*-* 00:00:00");
     131                 :          4 :         test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03");
     132                 :          4 :         test_one("Wed..Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03");
     133                 :          4 :         test_one("*-*-7 0:0:0", "*-*-07 00:00:00");
     134                 :          4 :         test_one("10-15", "*-10-15 00:00:00");
     135                 :          4 :         test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00");
     136                 :          4 :         test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45");
     137                 :          4 :         test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00");
     138                 :          4 :         test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45");
     139                 :          4 :         test_one("03-05 08:05:40", "*-03-05 08:05:40");
     140                 :          4 :         test_one("08:05:40", "*-*-* 08:05:40");
     141                 :          4 :         test_one("05:40", "*-*-* 05:40:00");
     142                 :          4 :         test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40");
     143                 :          4 :         test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40");
     144                 :          4 :         test_one("2003-03-05 05:40", "2003-03-05 05:40:00");
     145                 :          4 :         test_one("2003-03-05", "2003-03-05 00:00:00");
     146                 :          4 :         test_one("03-05", "*-03-05 00:00:00");
     147                 :          4 :         test_one("hourly", "*-*-* *:00:00");
     148                 :          4 :         test_one("daily", "*-*-* 00:00:00");
     149                 :          4 :         test_one("monthly", "*-*-01 00:00:00");
     150                 :          4 :         test_one("weekly", "Mon *-*-* 00:00:00");
     151                 :          4 :         test_one("minutely", "*-*-* *:*:00");
     152                 :          4 :         test_one("quarterly", "*-01,04,07,10-01 00:00:00");
     153                 :          4 :         test_one("semi-annually", "*-01,07-01 00:00:00");
     154                 :          4 :         test_one("annually", "*-01-01 00:00:00");
     155                 :          4 :         test_one("*:2/3", "*-*-* *:02/3:00");
     156                 :          4 :         test_one("2015-10-25 01:00:00 uTc", "2015-10-25 01:00:00 UTC");
     157                 :          4 :         test_one("2015-10-25 01:00:00 Asia/Vladivostok", "2015-10-25 01:00:00 Asia/Vladivostok");
     158                 :          4 :         test_one("weekly Pacific/Auckland", "Mon *-*-* 00:00:00 Pacific/Auckland");
     159                 :          4 :         test_one("2016-03-27 03:17:00.4200005", "2016-03-27 03:17:00.420001");
     160                 :          4 :         test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000");
     161                 :          4 :         test_one("9..11,13:00,30", "*-*-* 09..11,13:00,30:00");
     162                 :          4 :         test_one("1..3-1..3 1..3:1..3", "*-01..03-01..03 01..03:01..03:00");
     163                 :          4 :         test_one("00:00:1.125..2.125", "*-*-* 00:00:01.125000..02.125000");
     164                 :          4 :         test_one("00:00:1.0..3.8", "*-*-* 00:00:01..03");
     165                 :          4 :         test_one("00:00:01..03", "*-*-* 00:00:01..03");
     166                 :          4 :         test_one("00:00:01/2,02..03", "*-*-* 00:00:01/2,02..03");
     167                 :          4 :         test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC");
     168                 :          4 :         test_one("*-*~05,3 ", "*-*~03,05 00:00:00");
     169                 :          4 :         test_one("*-*~* 00:00:00", "*-*-* 00:00:00");
     170                 :          4 :         test_one("Monday", "Mon *-*-* 00:00:00");
     171                 :          4 :         test_one("Monday *-*-*", "Mon *-*-* 00:00:00");
     172                 :          4 :         test_one("*-*-*", "*-*-* 00:00:00");
     173                 :          4 :         test_one("*:*:*", "*-*-* *:*:*");
     174                 :          4 :         test_one("*:*", "*-*-* *:*:00");
     175                 :          4 :         test_one("12:*", "*-*-* 12:*:00");
     176                 :          4 :         test_one("*:30", "*-*-* *:30:00");
     177                 :          4 :         test_one("93..00-*-*", "1993..2000-*-* 00:00:00");
     178                 :          4 :         test_one("00..07-*-*", "2000..2007-*-* 00:00:00");
     179                 :          4 :         test_one("*:20..39/5", "*-*-* *:20..35/5:00");
     180                 :          4 :         test_one("00:00:20..40/1", "*-*-* 00:00:20..40");
     181                 :          4 :         test_one("*~03/1,03..05", "*-*~03/1,03..05 00:00:00");
     182                 :            :         /* UNIX timestamps are always UTC */
     183                 :          4 :         test_one("@1493187147", "2017-04-26 06:12:27 UTC");
     184                 :          4 :         test_one("@1493187147 UTC", "2017-04-26 06:12:27 UTC");
     185                 :          4 :         test_one("@0", "1970-01-01 00:00:00 UTC");
     186                 :          4 :         test_one("@0 UTC", "1970-01-01 00:00:00 UTC");
     187                 :            : 
     188                 :          4 :         test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000);
     189                 :          4 :         test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000);
     190                 :          4 :         test_next("2016-03-27 03:17:00", "EET", 12345, -1);
     191                 :          4 :         test_next("2016-03-27 03:17:00 UTC", NULL, 12345, 1459048620000000);
     192                 :          4 :         test_next("2016-03-27 03:17:00 UTC", "", 12345, 1459048620000000);
     193                 :          4 :         test_next("2016-03-27 03:17:00 UTC", "CET", 12345, 1459048620000000);
     194                 :          4 :         test_next("2016-03-27 03:17:00 UTC", "EET", 12345, 1459048620000000);
     195                 :          4 :         test_next("2016-03-27 03:17:00.420000001 UTC", "EET", 12345, 1459048620420000);
     196                 :          4 :         test_next("2016-03-27 03:17:00.4200005 UTC", "EET", 12345, 1459048620420001);
     197                 :          4 :         test_next("2015-11-13 09:11:23.42", "EET", 12345, 1447398683420000);
     198                 :          4 :         test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683420000, 1447398685190000);
     199                 :          4 :         test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683419999, 1447398683420000);
     200                 :          4 :         test_next("Sun 16:00:00", "CET", 1456041600123456, 1456066800000000);
     201                 :          4 :         test_next("*-04-31", "", 12345, -1);
     202                 :          4 :         test_next("2016-02~01 UTC", "", 12345, 1456704000000000);
     203                 :          4 :         test_next("Mon 2017-05~01..07 UTC", "", 12345, 1496016000000000);
     204                 :          4 :         test_next("Mon 2017-05~07/1 UTC", "", 12345, 1496016000000000);
     205                 :          4 :         test_next("2017-08-06 9,11,13,15,17:00 UTC", "", 1502029800000000, 1502031600000000);
     206                 :          4 :         test_next("2017-08-06 9..17/2:00 UTC", "", 1502029800000000, 1502031600000000);
     207                 :          4 :         test_next("2016-12-* 3..21/6:00 UTC", "", 1482613200000001, 1482634800000000);
     208                 :          4 :         test_next("2017-09-24 03:30:00 Pacific/Auckland", "", 12345, 1506177000000000);
     209                 :            :         // Due to daylight saving time - 2017-09-24 02:30:00 does not exist
     210                 :          4 :         test_next("2017-09-24 02:30:00 Pacific/Auckland", "", 12345, -1);
     211                 :          4 :         test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 12345, 1491053400000000);
     212                 :            :         // Confirm that even though it's a time change here (backward) 02:30 happens only once
     213                 :          4 :         test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 1491053400000000, -1);
     214                 :          4 :         test_next("2017-04-02 03:30:00 Pacific/Auckland", "", 12345, 1491060600000000);
     215                 :            :         // Confirm that timezones in the Spec work regardless of current timezone
     216                 :          4 :         test_next("2017-09-09 20:42:00 Pacific/Auckland", "", 12345, 1504946520000000);
     217                 :          4 :         test_next("2017-09-09 20:42:00 Pacific/Auckland", "EET", 12345, 1504946520000000);
     218                 :            : 
     219         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("test", &c) < 0);
     220         [ -  + ]:          4 :         assert_se(calendar_spec_from_string(" utc", &c) < 0);
     221         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("    ", &c) < 0);
     222         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("", &c) < 0);
     223         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("7", &c) < 0);
     224         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("121212:1:2", &c) < 0);
     225         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("2000-03-05.23 00:00:00", &c) < 0);
     226         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("2000-03-05 00:00.1:00", &c) < 0);
     227         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("00:00:00/0.00000001", &c) < 0);
     228         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("00:00:00.0..00.9", &c) < 0);
     229         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("2016~11-22", &c) < 0);
     230         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("*-*~5/5", &c) < 0);
     231         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("Monday.. 12:00", &c) < 0);
     232         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("Monday..", &c) < 0);
     233         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("-00:+00/-5", &c) < 0);
     234         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("00:+00/-5", &c) < 0);
     235         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("2016- 11- 24 12: 30: 00", &c) < 0);
     236         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("*~29", &c) < 0);
     237         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("*~16..31", &c) < 0);
     238         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("12..1/2-*", &c) < 0);
     239         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("*:05..05", &c) < 0);
     240         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("*:05..10/6", &c) < 0);
     241         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("20/4:00", &c) < 0);
     242         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("00:00/60", &c) < 0);
     243         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("00:00:2300", &c) < 0);
     244         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("00:00:18446744073709551615", &c) < 0);
     245         [ -  + ]:          4 :         assert_se(calendar_spec_from_string("@88588582097858858", &c) == -ERANGE);
     246                 :            : 
     247                 :          4 :         test_timestamp();
     248                 :          4 :         test_hourly_bug_4031();
     249                 :            : 
     250                 :          4 :         return 0;
     251                 :            : }

Generated by: LCOV version 1.14