LCOV - code coverage report
Current view: top level - lib/util/tests - time.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 48 53 90.6 %
Date: 2024-01-11 09:59:51 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    util time testing
       5             : 
       6             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "torture/torture.h"
      24             : #include "torture/local/proto.h"
      25             : 
      26           1 : static bool test_null_time(struct torture_context *tctx)
      27             : {
      28           1 :         torture_assert(tctx, null_time(0), "0");
      29           1 :         torture_assert(tctx, null_time(0xFFFFFFFF), "0xFFFFFFFF");
      30           1 :         torture_assert(tctx, null_time(-1), "-1");
      31           1 :         torture_assert(tctx, !null_time(42), "42");
      32           0 :         return true;
      33             : }
      34             : 
      35           1 : static bool test_null_nttime(struct torture_context *tctx)
      36             : {
      37           1 :         torture_assert(tctx, null_nttime(0), "0");
      38           1 :         torture_assert(tctx, !null_nttime(NTTIME_FREEZE), "-1");
      39           1 :         torture_assert(tctx, !null_nttime(NTTIME_THAW), "-2");
      40           1 :         torture_assert(tctx, !null_nttime(42), "42");
      41           0 :         return true;
      42             : }
      43             : 
      44             : 
      45           1 : static bool test_http_timestring(struct torture_context *tctx)
      46             : {
      47           1 :         const char *start = "Thu, 01 Jan 1970";
      48           1 :         char *result;
      49             :         /*
      50             :          * Correct test for negative UTC offset.  Without the correction, the
      51             :          * test fails when run on hosts with negative UTC offsets, as the date
      52             :          * returned is back in 1969 (pre-epoch).
      53             :          */
      54           1 :         time_t now = time(NULL);
      55           1 :         struct tm local = *localtime(&now);
      56           1 :         struct tm gmt = *gmtime(&now);
      57           1 :         time_t utc_offset = mktime(&local) - mktime(&gmt);
      58             : 
      59           1 :         result = http_timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
      60           1 :         torture_assert(tctx, !strncmp(start, result, 
      61             :                                       strlen(start)), result);
      62           1 :         torture_assert_str_equal(tctx, "never", 
      63             :                                  http_timestring(tctx, get_time_t_max()), "42");
      64           0 :         return true;
      65             : }
      66             : 
      67           1 : static bool test_timestring(struct torture_context *tctx)
      68             : {
      69           1 :         const char *start = "Thu Jan  1";
      70           1 :         char *result;
      71             :         /*
      72             :          * Correct test for negative UTC offset.  Without the correction, the
      73             :          * test fails when run on hosts with negative UTC offsets, as the date
      74             :          * returned is back in 1969 (pre-epoch).
      75             :          */
      76           1 :         time_t now = time(NULL);
      77           1 :         struct tm local = *localtime(&now);
      78           1 :         struct tm gmt = *gmtime(&now);
      79           1 :         time_t utc_offset = mktime(&local) - mktime(&gmt);
      80             : 
      81           1 :         result = timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
      82           1 :         torture_assert(tctx, !strncmp(start, result, strlen(start)), result);
      83           0 :         return true;
      84             : }
      85             : 
      86           1 : static bool test_normalize_timespec(struct torture_context *tctx)
      87             : {
      88           1 :         const struct {
      89             :                 time_t in_s; long in_ns;
      90             :                 time_t out_s; long out_ns;
      91           1 :         } data [] = {
      92             :                   { 0, 0, 0, 0 }
      93             :                 , { 1, 0, 1, 0 }
      94             :                 , { -1, 0, -1, 0 }
      95             :                 , { 0, 1000000000, 1, 0 }
      96             :                 , { 0, 2000000000, 2, 0 }
      97             :                 , { 0, 1000000001, 1, 1 }
      98             :                 , { 0, 2000000001, 2, 1 }
      99             :                 , { 0, -1000000000, -1, 0 }
     100             :                 , { 0, -2000000000, -2, 0 }
     101             :                 , { 0, -1000000001, -2, 999999999 }
     102             :                 , { 0, -2000000001, -3, 999999999 }
     103             :                 , { 0, -1, -1, 999999999 }
     104             :                 , { 1, -1, 0, 999999999 }
     105             :                 , { -1, -1, -2, 999999999 }
     106             :                 , { 0, 999999999, 0, 999999999 }
     107             :                 , { 0, 1999999999, 1, 999999999 }
     108             :                 , { 0, 2999999999, 2, 999999999 }
     109             :                 , { 0, -999999999, -1, 1 }
     110             :                 , { 0, -1999999999, -2, 1 }
     111             :                 , { 0, -2999999999, -3, 1 }
     112             :                 , { LONG_MAX, 1000000001, LONG_MAX, 999999999 } /* overflow */
     113             :                 , { LONG_MAX,  999999999, LONG_MAX, 999999999 } /* harmless */
     114             :                 , { LONG_MAX, -1, LONG_MAX-1, 999999999 } /* -1 */
     115             :                 , { LONG_MIN, -1000000001, LONG_MIN, 0 } /* overflow */
     116             :                 , { LONG_MIN, 0, LONG_MIN, 0 } /* harmless */
     117             :                 , { LONG_MIN, 1000000000, LONG_MIN+1, 0 } /* +1 */
     118             :         };
     119           1 :         int i;
     120             : 
     121          27 :         for (i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
     122          26 :                 struct timespec ts = (struct timespec)
     123          26 :                                    { .tv_sec  = data[i].in_s
     124          26 :                                    , .tv_nsec = data[i].in_ns };
     125             : 
     126          26 :                 normalize_timespec(&ts);
     127             : 
     128          26 :                 torture_assert_int_equal(tctx, ts.tv_sec, data[i].out_s,
     129             :                                          "mismatch in tv_sec");
     130          26 :                 torture_assert_int_equal(tctx, ts.tv_nsec, data[i].out_ns,
     131             :                                          "mismatch in tv_nsec");
     132             :         }
     133             : 
     134           0 :         return true;
     135             : }
     136             : 
     137        2358 : struct torture_suite *torture_local_util_time(TALLOC_CTX *mem_ctx)
     138             : {
     139        2358 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "time");
     140             : 
     141        2358 :         torture_suite_add_simple_test(suite, "null_time", test_null_time);
     142        2358 :         torture_suite_add_simple_test(suite, "null_nttime", test_null_nttime);
     143        2358 :         torture_suite_add_simple_test(suite, "http_timestring", 
     144             :                                                                   test_http_timestring);
     145        2358 :         torture_suite_add_simple_test(suite, "timestring", 
     146             :                                                                   test_timestring);
     147        2358 :         torture_suite_add_simple_test(suite, "normalize_timespec",
     148             :                                       test_normalize_timespec);
     149             : 
     150        2358 :         return suite;
     151             : }

Generated by: LCOV version 1.14