LCOV - code coverage report
Current view: top level - test - test-time-util.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 383 384 99.7 %
Date: 2019-08-23 13:36:53 Functions: 20 20 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 274 540 50.7 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include "random-util.h"
       4                 :            : #include "serialize.h"
       5                 :            : #include "string-util.h"
       6                 :            : #include "strv.h"
       7                 :            : #include "tests.h"
       8                 :            : #include "time-util.h"
       9                 :            : 
      10                 :          4 : static void test_parse_sec(void) {
      11                 :            :         usec_t u;
      12                 :            : 
      13         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      14                 :            : 
      15         [ -  + ]:          4 :         assert_se(parse_sec("5s", &u) >= 0);
      16         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
      17         [ -  + ]:          4 :         assert_se(parse_sec("5s500ms", &u) >= 0);
      18         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
      19         [ -  + ]:          4 :         assert_se(parse_sec(" 5s 500ms  ", &u) >= 0);
      20         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
      21         [ -  + ]:          4 :         assert_se(parse_sec(" 5.5s  ", &u) >= 0);
      22         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
      23         [ -  + ]:          4 :         assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
      24         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
      25         [ -  + ]:          4 :         assert_se(parse_sec(" .22s ", &u) >= 0);
      26         [ -  + ]:          4 :         assert_se(u == 220 * USEC_PER_MSEC);
      27         [ -  + ]:          4 :         assert_se(parse_sec(" .50y ", &u) >= 0);
      28         [ -  + ]:          4 :         assert_se(u == USEC_PER_YEAR / 2);
      29         [ -  + ]:          4 :         assert_se(parse_sec("2.5", &u) >= 0);
      30         [ -  + ]:          4 :         assert_se(u == 2500 * USEC_PER_MSEC);
      31         [ -  + ]:          4 :         assert_se(parse_sec(".7", &u) >= 0);
      32         [ -  + ]:          4 :         assert_se(u == 700 * USEC_PER_MSEC);
      33         [ -  + ]:          4 :         assert_se(parse_sec("23us", &u) >= 0);
      34         [ -  + ]:          4 :         assert_se(u == 23);
      35         [ -  + ]:          4 :         assert_se(parse_sec("23µs", &u) >= 0);
      36         [ -  + ]:          4 :         assert_se(u == 23);
      37         [ -  + ]:          4 :         assert_se(parse_sec("infinity", &u) >= 0);
      38         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
      39         [ -  + ]:          4 :         assert_se(parse_sec(" infinity ", &u) >= 0);
      40         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
      41         [ -  + ]:          4 :         assert_se(parse_sec("+3.1s", &u) >= 0);
      42         [ -  + ]:          4 :         assert_se(u == 3100 * USEC_PER_MSEC);
      43         [ -  + ]:          4 :         assert_se(parse_sec("3.1s.2", &u) >= 0);
      44         [ -  + ]:          4 :         assert_se(u == 3300 * USEC_PER_MSEC);
      45         [ -  + ]:          4 :         assert_se(parse_sec("3.1 .2", &u) >= 0);
      46         [ -  + ]:          4 :         assert_se(u == 3300 * USEC_PER_MSEC);
      47         [ -  + ]:          4 :         assert_se(parse_sec("3.1 sec .2 sec", &u) >= 0);
      48         [ -  + ]:          4 :         assert_se(u == 3300 * USEC_PER_MSEC);
      49         [ -  + ]:          4 :         assert_se(parse_sec("3.1 sec 1.2 sec", &u) >= 0);
      50         [ -  + ]:          4 :         assert_se(u == 4300 * USEC_PER_MSEC);
      51                 :            : 
      52         [ -  + ]:          4 :         assert_se(parse_sec(" xyz ", &u) < 0);
      53         [ -  + ]:          4 :         assert_se(parse_sec("", &u) < 0);
      54         [ -  + ]:          4 :         assert_se(parse_sec(" . ", &u) < 0);
      55         [ -  + ]:          4 :         assert_se(parse_sec(" 5. ", &u) < 0);
      56         [ -  + ]:          4 :         assert_se(parse_sec(".s ", &u) < 0);
      57         [ -  + ]:          4 :         assert_se(parse_sec("-5s ", &u) < 0);
      58         [ -  + ]:          4 :         assert_se(parse_sec("-0.3s ", &u) < 0);
      59         [ -  + ]:          4 :         assert_se(parse_sec("-0.0s ", &u) < 0);
      60         [ -  + ]:          4 :         assert_se(parse_sec("-0.-0s ", &u) < 0);
      61         [ -  + ]:          4 :         assert_se(parse_sec("0.-0s ", &u) < 0);
      62         [ -  + ]:          4 :         assert_se(parse_sec("3.-0s ", &u) < 0);
      63         [ -  + ]:          4 :         assert_se(parse_sec(" infinity .7", &u) < 0);
      64         [ -  + ]:          4 :         assert_se(parse_sec(".3 infinity", &u) < 0);
      65         [ -  + ]:          4 :         assert_se(parse_sec("3.+1s", &u) < 0);
      66         [ -  + ]:          4 :         assert_se(parse_sec("3. 1s", &u) < 0);
      67         [ -  + ]:          4 :         assert_se(parse_sec("3.s", &u) < 0);
      68         [ -  + ]:          4 :         assert_se(parse_sec("12.34.56", &u) < 0);
      69         [ -  + ]:          4 :         assert_se(parse_sec("12..34", &u) < 0);
      70         [ -  + ]:          4 :         assert_se(parse_sec("..1234", &u) < 0);
      71         [ -  + ]:          4 :         assert_se(parse_sec("1234..", &u) < 0);
      72                 :          4 : }
      73                 :            : 
      74                 :          4 : static void test_parse_sec_fix_0(void) {
      75                 :            :         usec_t u;
      76                 :            : 
      77         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      78                 :            : 
      79         [ -  + ]:          4 :         assert_se(parse_sec_fix_0("5s", &u) >= 0);
      80         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
      81         [ -  + ]:          4 :         assert_se(parse_sec_fix_0("0s", &u) >= 0);
      82         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
      83         [ -  + ]:          4 :         assert_se(parse_sec_fix_0("0", &u) >= 0);
      84         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
      85         [ -  + ]:          4 :         assert_se(parse_sec_fix_0(" 0", &u) >= 0);
      86         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
      87                 :          4 : }
      88                 :            : 
      89                 :          4 : static void test_parse_sec_def_infinity(void) {
      90                 :            :         usec_t u;
      91                 :            : 
      92         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      93                 :            : 
      94         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity("5s", &u) >= 0);
      95         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
      96         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity("", &u) >= 0);
      97         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
      98         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity("     ", &u) >= 0);
      99         [ -  + ]:          4 :         assert_se(u == USEC_INFINITY);
     100         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity("0s", &u) >= 0);
     101         [ -  + ]:          4 :         assert_se(u == 0);
     102         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity("0", &u) >= 0);
     103         [ -  + ]:          4 :         assert_se(u == 0);
     104         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
     105         [ -  + ]:          4 :         assert_se(u == 0);
     106         [ -  + ]:          4 :         assert_se(parse_sec_def_infinity("-5s", &u) < 0);
     107                 :          4 : }
     108                 :            : 
     109                 :          4 : static void test_parse_time(void) {
     110                 :            :         usec_t u;
     111                 :            : 
     112         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     113                 :            : 
     114         [ -  + ]:          4 :         assert_se(parse_time("5", &u, 1) >= 0);
     115         [ -  + ]:          4 :         assert_se(u == 5);
     116                 :            : 
     117         [ -  + ]:          4 :         assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
     118         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_MSEC);
     119                 :            : 
     120         [ -  + ]:          4 :         assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
     121         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
     122                 :            : 
     123         [ -  + ]:          4 :         assert_se(parse_time("5s", &u, 1) >= 0);
     124         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
     125                 :            : 
     126         [ -  + ]:          4 :         assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
     127         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
     128                 :            : 
     129         [ -  + ]:          4 :         assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
     130         [ -  + ]:          4 :         assert_se(u == 5 * USEC_PER_SEC);
     131                 :            : 
     132         [ -  + ]:          4 :         assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE);
     133         [ -  + ]:          4 :         assert_se(parse_time("1.1111111111111y", &u, 1) >= 0);
     134                 :          4 : }
     135                 :            : 
     136                 :          4 : static void test_parse_nsec(void) {
     137                 :            :         nsec_t u;
     138                 :            : 
     139         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     140                 :            : 
     141         [ -  + ]:          4 :         assert_se(parse_nsec("5s", &u) >= 0);
     142         [ -  + ]:          4 :         assert_se(u == 5 * NSEC_PER_SEC);
     143         [ -  + ]:          4 :         assert_se(parse_nsec("5s500ms", &u) >= 0);
     144         [ -  + ]:          4 :         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
     145         [ -  + ]:          4 :         assert_se(parse_nsec(" 5s 500ms  ", &u) >= 0);
     146         [ -  + ]:          4 :         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
     147         [ -  + ]:          4 :         assert_se(parse_nsec(" 5.5s  ", &u) >= 0);
     148         [ -  + ]:          4 :         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
     149         [ -  + ]:          4 :         assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
     150         [ -  + ]:          4 :         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
     151         [ -  + ]:          4 :         assert_se(parse_nsec(" .22s ", &u) >= 0);
     152         [ -  + ]:          4 :         assert_se(u == 220 * NSEC_PER_MSEC);
     153         [ -  + ]:          4 :         assert_se(parse_nsec(" .50y ", &u) >= 0);
     154         [ -  + ]:          4 :         assert_se(u == NSEC_PER_YEAR / 2);
     155         [ -  + ]:          4 :         assert_se(parse_nsec("2.5", &u) >= 0);
     156         [ -  + ]:          4 :         assert_se(u == 2);
     157         [ -  + ]:          4 :         assert_se(parse_nsec(".7", &u) >= 0);
     158         [ -  + ]:          4 :         assert_se(u == 0);
     159         [ -  + ]:          4 :         assert_se(parse_nsec("infinity", &u) >= 0);
     160         [ -  + ]:          4 :         assert_se(u == NSEC_INFINITY);
     161         [ -  + ]:          4 :         assert_se(parse_nsec(" infinity ", &u) >= 0);
     162         [ -  + ]:          4 :         assert_se(u == NSEC_INFINITY);
     163         [ -  + ]:          4 :         assert_se(parse_nsec("+3.1s", &u) >= 0);
     164         [ -  + ]:          4 :         assert_se(u == 3100 * NSEC_PER_MSEC);
     165         [ -  + ]:          4 :         assert_se(parse_nsec("3.1s.2", &u) >= 0);
     166         [ -  + ]:          4 :         assert_se(u == 3100 * NSEC_PER_MSEC);
     167         [ -  + ]:          4 :         assert_se(parse_nsec("3.1 .2s", &u) >= 0);
     168         [ -  + ]:          4 :         assert_se(u == 200 * NSEC_PER_MSEC + 3);
     169         [ -  + ]:          4 :         assert_se(parse_nsec("3.1 sec .2 sec", &u) >= 0);
     170         [ -  + ]:          4 :         assert_se(u == 3300 * NSEC_PER_MSEC);
     171         [ -  + ]:          4 :         assert_se(parse_nsec("3.1 sec 1.2 sec", &u) >= 0);
     172         [ -  + ]:          4 :         assert_se(u == 4300 * NSEC_PER_MSEC);
     173                 :            : 
     174         [ -  + ]:          4 :         assert_se(parse_nsec(" xyz ", &u) < 0);
     175         [ -  + ]:          4 :         assert_se(parse_nsec("", &u) < 0);
     176         [ -  + ]:          4 :         assert_se(parse_nsec(" . ", &u) < 0);
     177         [ -  + ]:          4 :         assert_se(parse_nsec(" 5. ", &u) < 0);
     178         [ -  + ]:          4 :         assert_se(parse_nsec(".s ", &u) < 0);
     179         [ -  + ]:          4 :         assert_se(parse_nsec(" infinity .7", &u) < 0);
     180         [ -  + ]:          4 :         assert_se(parse_nsec(".3 infinity", &u) < 0);
     181         [ -  + ]:          4 :         assert_se(parse_nsec("-5s ", &u) < 0);
     182         [ -  + ]:          4 :         assert_se(parse_nsec("-0.3s ", &u) < 0);
     183         [ -  + ]:          4 :         assert_se(parse_nsec("-0.0s ", &u) < 0);
     184         [ -  + ]:          4 :         assert_se(parse_nsec("-0.-0s ", &u) < 0);
     185         [ -  + ]:          4 :         assert_se(parse_nsec("0.-0s ", &u) < 0);
     186         [ -  + ]:          4 :         assert_se(parse_nsec("3.-0s ", &u) < 0);
     187         [ -  + ]:          4 :         assert_se(parse_nsec(" infinity .7", &u) < 0);
     188         [ -  + ]:          4 :         assert_se(parse_nsec(".3 infinity", &u) < 0);
     189         [ -  + ]:          4 :         assert_se(parse_nsec("3.+1s", &u) < 0);
     190         [ -  + ]:          4 :         assert_se(parse_nsec("3. 1s", &u) < 0);
     191         [ -  + ]:          4 :         assert_se(parse_nsec("3.s", &u) < 0);
     192         [ -  + ]:          4 :         assert_se(parse_nsec("12.34.56", &u) < 0);
     193         [ -  + ]:          4 :         assert_se(parse_nsec("12..34", &u) < 0);
     194         [ -  + ]:          4 :         assert_se(parse_nsec("..1234", &u) < 0);
     195         [ -  + ]:          4 :         assert_se(parse_nsec("1234..", &u) < 0);
     196         [ -  + ]:          4 :         assert_se(parse_nsec("1111111111111y", &u) == -ERANGE);
     197         [ -  + ]:          4 :         assert_se(parse_nsec("1.111111111111y", &u) >= 0);
     198                 :          4 : }
     199                 :            : 
     200                 :        264 : static void test_format_timespan_one(usec_t x, usec_t accuracy) {
     201                 :            :         char l[FORMAT_TIMESPAN_MAX];
     202                 :            :         const char *t;
     203                 :            :         usec_t y;
     204                 :            : 
     205         [ +  - ]:        264 :         log_info(USEC_FMT"     (at accuracy "USEC_FMT")", x, accuracy);
     206                 :            : 
     207         [ -  + ]:        264 :         assert_se(t = format_timespan(l, sizeof l, x, accuracy));
     208         [ +  - ]:        264 :         log_info(" = <%s>", t);
     209                 :            : 
     210         [ -  + ]:        264 :         assert_se(parse_sec(t, &y) >= 0);
     211         [ +  - ]:        264 :         log_info(" = "USEC_FMT, y);
     212                 :            : 
     213         [ -  + ]:        264 :         if (accuracy <= 0)
     214                 :          0 :                 accuracy = 1;
     215                 :            : 
     216         [ -  + ]:        264 :         assert_se(x / accuracy == y / accuracy);
     217                 :        264 : }
     218                 :            : 
     219                 :         12 : static void test_format_timespan(usec_t accuracy) {
     220         [ +  - ]:         12 :         log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy);
     221                 :            : 
     222                 :         12 :         test_format_timespan_one(0, accuracy);
     223                 :         12 :         test_format_timespan_one(1, accuracy);
     224                 :         12 :         test_format_timespan_one(1*USEC_PER_SEC, accuracy);
     225                 :         12 :         test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
     226                 :         12 :         test_format_timespan_one(1234567, accuracy);
     227                 :         12 :         test_format_timespan_one(12, accuracy);
     228                 :         12 :         test_format_timespan_one(123, accuracy);
     229                 :         12 :         test_format_timespan_one(1234, accuracy);
     230                 :         12 :         test_format_timespan_one(12345, accuracy);
     231                 :         12 :         test_format_timespan_one(123456, accuracy);
     232                 :         12 :         test_format_timespan_one(1234567, accuracy);
     233                 :         12 :         test_format_timespan_one(12345678, accuracy);
     234                 :         12 :         test_format_timespan_one(1200000, accuracy);
     235                 :         12 :         test_format_timespan_one(1230000, accuracy);
     236                 :         12 :         test_format_timespan_one(1234000, accuracy);
     237                 :         12 :         test_format_timespan_one(1234500, accuracy);
     238                 :         12 :         test_format_timespan_one(1234560, accuracy);
     239                 :         12 :         test_format_timespan_one(1234567, accuracy);
     240                 :         12 :         test_format_timespan_one(986087, accuracy);
     241                 :         12 :         test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
     242                 :         12 :         test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
     243                 :         12 :         test_format_timespan_one(USEC_INFINITY, accuracy);
     244                 :         12 : }
     245                 :            : 
     246                 :          4 : static void test_timezone_is_valid(void) {
     247         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     248                 :            : 
     249         [ -  + ]:          4 :         assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR));
     250         [ -  + ]:          4 :         assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR));
     251         [ -  + ]:          4 :         assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR));
     252                 :          4 : }
     253                 :            : 
     254                 :          4 : static void test_get_timezones(void) {
     255                 :          4 :         _cleanup_strv_free_ char **zones = NULL;
     256                 :            :         int r;
     257                 :            :         char **zone;
     258                 :            : 
     259         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     260                 :            : 
     261                 :          4 :         r = get_timezones(&zones);
     262         [ -  + ]:          4 :         assert_se(r == 0);
     263                 :            : 
     264   [ +  -  +  + ]:       1400 :         STRV_FOREACH(zone, zones) {
     265         [ +  - ]:       1396 :                 log_info("zone: %s", *zone);
     266         [ -  + ]:       1396 :                 assert_se(timezone_is_valid(*zone, LOG_ERR));
     267                 :            :         }
     268                 :          4 : }
     269                 :            : 
     270                 :          4 : static void test_usec_add(void) {
     271         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     272                 :            : 
     273         [ -  + ]:          4 :         assert_se(usec_add(0, 0) == 0);
     274         [ -  + ]:          4 :         assert_se(usec_add(1, 4) == 5);
     275         [ -  + ]:          4 :         assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
     276         [ -  + ]:          4 :         assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY);
     277         [ -  + ]:          4 :         assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3);
     278         [ -  + ]:          4 :         assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY);
     279         [ -  + ]:          4 :         assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY);
     280         [ -  + ]:          4 :         assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY);
     281                 :          4 : }
     282                 :            : 
     283                 :          4 : static void test_usec_sub_unsigned(void) {
     284         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     285                 :            : 
     286         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(0, 0) == 0);
     287         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(0, 2) == 0);
     288         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0);
     289         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(1, 0) == 1);
     290         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(1, 1) == 0);
     291         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(1, 2) == 0);
     292         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(1, 3) == 0);
     293         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0);
     294         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1);
     295         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2);
     296         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3);
     297         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1);
     298         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0);
     299         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0);
     300         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY);
     301         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY);
     302         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY);
     303         [ -  + ]:          4 :         assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY);
     304                 :          4 : }
     305                 :            : 
     306                 :          4 : static void test_usec_sub_signed(void) {
     307         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     308                 :            : 
     309         [ -  + ]:          4 :         assert_se(usec_sub_signed(0, 0) == 0);
     310         [ -  + ]:          4 :         assert_se(usec_sub_signed(4, 1) == 3);
     311         [ -  + ]:          4 :         assert_se(usec_sub_signed(4, 4) == 0);
     312         [ -  + ]:          4 :         assert_se(usec_sub_signed(4, 5) == 0);
     313         [ -  + ]:          4 :         assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
     314         [ -  + ]:          4 :         assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY);
     315         [ -  + ]:          4 :         assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY);
     316         [ -  + ]:          4 :         assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY);
     317                 :          4 : }
     318                 :            : 
     319                 :          4 : static void test_format_timestamp(void) {
     320                 :            :         unsigned i;
     321                 :            : 
     322         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     323                 :            : 
     324         [ +  + ]:        404 :         for (i = 0; i < 100; i++) {
     325                 :        400 :                 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
     326                 :            :                 usec_t x, y;
     327                 :            : 
     328                 :        400 :                 random_bytes(&x, sizeof(x));
     329                 :        400 :                 x = x % (2147483600 * USEC_PER_SEC) + 1;
     330                 :            : 
     331         [ -  + ]:        400 :                 assert_se(format_timestamp(buf, sizeof(buf), x));
     332         [ +  - ]:        400 :                 log_info("%s", buf);
     333         [ -  + ]:        400 :                 assert_se(parse_timestamp(buf, &y) >= 0);
     334         [ -  + ]:        400 :                 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
     335                 :            : 
     336         [ -  + ]:        400 :                 assert_se(format_timestamp_utc(buf, sizeof(buf), x));
     337         [ +  - ]:        400 :                 log_info("%s", buf);
     338         [ -  + ]:        400 :                 assert_se(parse_timestamp(buf, &y) >= 0);
     339         [ -  + ]:        400 :                 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
     340                 :            : 
     341         [ -  + ]:        400 :                 assert_se(format_timestamp_us(buf, sizeof(buf), x));
     342         [ +  - ]:        400 :                 log_info("%s", buf);
     343         [ -  + ]:        400 :                 assert_se(parse_timestamp(buf, &y) >= 0);
     344         [ -  + ]:        400 :                 assert_se(x == y);
     345                 :            : 
     346         [ -  + ]:        400 :                 assert_se(format_timestamp_us_utc(buf, sizeof(buf), x));
     347         [ +  - ]:        400 :                 log_info("%s", buf);
     348         [ -  + ]:        400 :                 assert_se(parse_timestamp(buf, &y) >= 0);
     349         [ -  + ]:        400 :                 assert_se(x == y);
     350                 :            : 
     351         [ -  + ]:        400 :                 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
     352         [ +  - ]:        400 :                 log_info("%s", buf);
     353         [ -  + ]:        400 :                 assert_se(parse_timestamp(buf, &y) >= 0);
     354                 :            : 
     355                 :            :                 /* The two calls above will run with a slightly different local time. Make sure we are in the same
     356                 :            :                  * range however, but give enough leeway that this is unlikely to explode. And of course,
     357                 :            :                  * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
     358                 :            :                  * month, cover for that too. */
     359   [ +  +  -  + ]:        400 :                 assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
     360                 :            :         }
     361                 :          4 : }
     362                 :            : 
     363                 :         24 : static void test_format_timestamp_utc_one(usec_t val, const char *result) {
     364                 :            :         char buf[FORMAT_TIMESTAMP_MAX];
     365                 :            :         const char *t;
     366                 :            : 
     367                 :         24 :         t = format_timestamp_utc(buf, sizeof(buf), val);
     368         [ -  + ]:         24 :         assert_se(streq_ptr(t, result));
     369                 :         24 : }
     370                 :            : 
     371                 :          4 : static void test_format_timestamp_utc(void) {
     372         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     373                 :            : 
     374                 :          4 :         test_format_timestamp_utc_one(0, NULL);
     375                 :          4 :         test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC");
     376                 :          4 :         test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC");
     377                 :            : 
     378                 :            : #if SIZEOF_TIME_T == 8
     379                 :          4 :         test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC");
     380                 :          4 :         test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
     381                 :            : #elif SIZEOF_TIME_T == 4
     382                 :            :         test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC");
     383                 :            :         test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
     384                 :            : #endif
     385                 :            : 
     386                 :          4 :         test_format_timestamp_utc_one(USEC_INFINITY, NULL);
     387                 :          4 : }
     388                 :            : 
     389                 :          4 : static void test_deserialize_dual_timestamp(void) {
     390                 :            :         int r;
     391                 :            :         dual_timestamp t;
     392                 :            : 
     393         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     394                 :            : 
     395                 :          4 :         r = deserialize_dual_timestamp("1234 5678", &t);
     396         [ -  + ]:          4 :         assert_se(r == 0);
     397         [ -  + ]:          4 :         assert_se(t.realtime == 1234);
     398         [ -  + ]:          4 :         assert_se(t.monotonic == 5678);
     399                 :            : 
     400                 :          4 :         r = deserialize_dual_timestamp("1234x 5678", &t);
     401         [ -  + ]:          4 :         assert_se(r == -EINVAL);
     402                 :            : 
     403                 :          4 :         r = deserialize_dual_timestamp("1234 5678y", &t);
     404         [ -  + ]:          4 :         assert_se(r == -EINVAL);
     405                 :            : 
     406                 :          4 :         r = deserialize_dual_timestamp("-1234 5678", &t);
     407         [ -  + ]:          4 :         assert_se(r == -EINVAL);
     408                 :            : 
     409                 :          4 :         r = deserialize_dual_timestamp("1234 -5678", &t);
     410         [ -  + ]:          4 :         assert_se(r == -EINVAL);
     411                 :            : 
     412                 :            :         /* Check that output wasn't modified. */
     413         [ -  + ]:          4 :         assert_se(t.realtime == 1234);
     414         [ -  + ]:          4 :         assert_se(t.monotonic == 5678);
     415                 :            : 
     416                 :          4 :         r = deserialize_dual_timestamp("+123 567", &t);
     417         [ -  + ]:          4 :         assert_se(r == 0);
     418         [ -  + ]:          4 :         assert_se(t.realtime == 123);
     419         [ -  + ]:          4 :         assert_se(t.monotonic == 567);
     420                 :            : 
     421                 :            :         /* Check that we get "infinity" on overflow. */
     422                 :          4 :         r = deserialize_dual_timestamp("18446744073709551617 0", &t);
     423         [ -  + ]:          4 :         assert_se(r == 0);
     424         [ -  + ]:          4 :         assert_se(t.realtime == USEC_INFINITY);
     425         [ -  + ]:          4 :         assert_se(t.monotonic == 0);
     426                 :          4 : }
     427                 :            : 
     428                 :         32 : static void assert_similar(usec_t a, usec_t b) {
     429                 :            :         usec_t d;
     430                 :            : 
     431         [ +  + ]:         32 :         if (a > b)
     432                 :         10 :                 d = a - b;
     433                 :            :         else
     434                 :         22 :                 d = b - a;
     435                 :            : 
     436         [ -  + ]:         32 :         assert(d < 10*USEC_PER_SEC);
     437                 :         32 : }
     438                 :            : 
     439                 :          4 : static void test_usec_shift_clock(void) {
     440                 :            :         usec_t rt, mn, bt;
     441                 :            : 
     442         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     443                 :            : 
     444                 :          4 :         rt = now(CLOCK_REALTIME);
     445                 :          4 :         mn = now(CLOCK_MONOTONIC);
     446                 :          4 :         bt = now(clock_boottime_or_monotonic());
     447                 :            : 
     448         [ -  + ]:          4 :         assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY);
     449                 :            : 
     450                 :          4 :         assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR);
     451                 :          4 :         assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt + 2*USEC_PER_HOUR);
     452         [ -  + ]:          4 :         assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR);
     453                 :            : 
     454                 :          4 :         assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR);
     455                 :          4 :         assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, clock_boottime_or_monotonic()), bt + 5*USEC_PER_HOUR);
     456         [ -  + ]:          4 :         assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR);
     457                 :            : 
     458                 :          4 :         assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR);
     459                 :          4 :         assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR);
     460         [ -  + ]:          4 :         assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt + 9*USEC_PER_HOUR);
     461                 :            : 
     462         [ +  - ]:          4 :         if (mn > USEC_PER_MINUTE) {
     463                 :          4 :                 assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC);
     464                 :          4 :                 assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt - 50 * USEC_PER_SEC);
     465                 :            :         }
     466                 :          4 : }
     467                 :            : 
     468                 :          4 : static void test_in_utc_timezone(void) {
     469         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     470                 :            : 
     471         [ -  + ]:          4 :         assert_se(setenv("TZ", ":UTC", 1) >= 0);
     472         [ -  + ]:          4 :         assert_se(in_utc_timezone());
     473         [ -  + ]:          4 :         assert_se(streq(tzname[0], "UTC"));
     474         [ -  + ]:          4 :         assert_se(streq(tzname[1], "UTC"));
     475         [ -  + ]:          4 :         assert_se(timezone == 0);
     476         [ -  + ]:          4 :         assert_se(daylight == 0);
     477                 :            : 
     478         [ -  + ]:          4 :         assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0);
     479         [ -  + ]:          4 :         assert_se(!in_utc_timezone());
     480         [ -  + ]:          4 :         assert_se(streq(tzname[0], "CET"));
     481         [ -  + ]:          4 :         assert_se(streq(tzname[1], "CEST"));
     482                 :            : 
     483         [ -  + ]:          4 :         assert_se(unsetenv("TZ") >= 0);
     484                 :          4 : }
     485                 :            : 
     486                 :          4 : int main(int argc, char *argv[]) {
     487                 :          4 :         test_setup_logging(LOG_INFO);
     488                 :            : 
     489         [ +  - ]:          4 :         log_info("realtime=" USEC_FMT "\n"
     490                 :            :                  "monotonic=" USEC_FMT "\n"
     491                 :            :                  "boottime=" USEC_FMT "\n",
     492                 :            :                  now(CLOCK_REALTIME),
     493                 :            :                  now(CLOCK_MONOTONIC),
     494                 :            :                  now(clock_boottime_or_monotonic()));
     495                 :            : 
     496                 :          4 :         test_parse_sec();
     497                 :          4 :         test_parse_sec_fix_0();
     498                 :          4 :         test_parse_sec_def_infinity();
     499                 :          4 :         test_parse_time();
     500                 :          4 :         test_parse_nsec();
     501                 :          4 :         test_format_timespan(1);
     502                 :          4 :         test_format_timespan(USEC_PER_MSEC);
     503                 :          4 :         test_format_timespan(USEC_PER_SEC);
     504                 :          4 :         test_timezone_is_valid();
     505                 :          4 :         test_get_timezones();
     506                 :          4 :         test_usec_add();
     507                 :          4 :         test_usec_sub_signed();
     508                 :          4 :         test_usec_sub_unsigned();
     509                 :          4 :         test_format_timestamp();
     510                 :          4 :         test_format_timestamp_utc();
     511                 :          4 :         test_deserialize_dual_timestamp();
     512                 :          4 :         test_usec_shift_clock();
     513                 :          4 :         test_in_utc_timezone();
     514                 :            : 
     515                 :            :         /* Ensure time_t is signed */
     516                 :            :         assert_cc((time_t) -1 < (time_t) 1);
     517                 :            : 
     518                 :            :         /* Ensure TIME_T_MAX works correctly */
     519                 :          4 :         uintmax_t x = TIME_T_MAX;
     520                 :          4 :         x++;
     521         [ -  + ]:          4 :         assert((time_t) x < 0);
     522                 :            : 
     523                 :          4 :         return 0;
     524                 :            : }

Generated by: LCOV version 1.14