LCOV - code coverage report
Current view: top level - common - t-gettime.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 64 56.2 %
Date: 2015-11-05 17:10:59 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* t-gettime.c - Module test for gettime.c
       2             :  *      Copyright (C) 2007, 2011 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       6             :  * GnuPG is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * GnuPG is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : 
      24             : #include "util.h"
      25             : 
      26             : #define pass()  do { ; } while(0)
      27             : #define fail(a)  do { fprintf (stderr, "%s:%d: test %d failed\n",\
      28             :                                __FILE__,__LINE__, (a));          \
      29             :                      errcount++;                                 \
      30             :                    } while(0)
      31             : 
      32             : static int verbose;
      33             : static int errcount;
      34             : #define INVALID ((time_t)(-1))
      35             : 
      36             : 
      37             : static void
      38           1 : test_isotime2epoch (void)
      39             : {
      40           1 :   struct { const char *string; time_t expected; } array [] = {
      41             :     { "19700101T000001",  1 },
      42             :     { "19700101T235959",  86399 },
      43             :     { "19980815T143712",  903191832 },
      44             :     { "19700101T000000",  0 },
      45             :     { "19691231T235959",  INVALID },
      46             :     { "19000101T000000",  INVALID },
      47             :     { "",                 INVALID },
      48             :     { "19000101T00000",   INVALID },
      49             :     { "20010101t123456",  INVALID },
      50             :     { "20010101T123456",  978352496 },
      51             :     { "20070629T160000",  1183132800 },
      52             :     { "20070629T160000:",  1183132800 },
      53             :     { "20070629T160000,",  1183132800 },
      54             :     { "20070629T160000 ",  1183132800 },
      55             :     { "20070629T160000\n", 1183132800 },
      56             :     { "20070629T160000.",  INVALID },
      57             :     { NULL, 0 }
      58             :   };
      59             :   int idx;
      60             :   time_t val;
      61             :   gnupg_isotime_t tbuf;
      62             : 
      63          17 :   for (idx=0; array[idx].string; idx++)
      64             :     {
      65          16 :       val = isotime2epoch (array[idx].string);
      66          16 :       if (val != array[idx].expected )
      67             :         {
      68           0 :           fail (idx);
      69           0 :           if (verbose)
      70           0 :             fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
      71             :                      array[idx].string, (long)array[idx].expected,
      72             :                      (long)val);
      73             :         }
      74          16 :       if (array[idx].expected != INVALID)
      75             :         {
      76          10 :           epoch2isotime (tbuf, val);
      77          10 :           if (strlen (tbuf) != 15)
      78             :             {
      79           0 :               if (verbose)
      80           0 :                 fprintf (stderr, "string '%s', time-t %ld, revert: '%s'\n",
      81             :                          array[idx].string, (long)val, tbuf);
      82           0 :               fail (idx);
      83             :             }
      84          10 :           if (strncmp (array[idx].string, tbuf, 15))
      85           0 :             fail (idx);
      86             :         }
      87             :     }
      88           1 : }
      89             : 
      90             : 
      91             : 
      92             : static void
      93           1 : test_string2isotime (void)
      94             : {
      95             :   struct {
      96             :     const char *string;
      97             :     size_t result;
      98             :     const char *expected;
      99           1 :   } array [] = {
     100             :     { "19700101T000001",      15, "19700101T000001" },
     101             :     { "19700101T235959",      15, "19700101T235959" },
     102             :     { "19980815T143712",      15, "19980815T143712" },
     103             :     { "19700101T000000",      15, "19700101T000000" },
     104             :     { "19691231T235959",      15, "19691231T235959" },
     105             :     { "19000101T000000",      15, "19000101T000000" },
     106             :     { "",                      0, ""                },
     107             :     { "19000101T00000",        0, ""                },
     108             :     { "20010101t123456",       0, ""                },
     109             :     { "20010101T123456",      15, "20010101T123456" },
     110             :     { "20070629T160000",      15, "20070629T160000" },
     111             :     { "20070629T160000:",     15, "20070629T160000" },
     112             :     { "20070629T160000,",     15, "20070629T160000" },
     113             :     { "20070629T160000 ",     15, "20070629T160000" },
     114             :     { "20070629T160000\n",    15,"20070629T160000"  },
     115             :     { "20070629T160000.",      0, ""                },
     116             :     { "1066-03-20",           10, "10660320T000000" },
     117             :     { "1066-03-20,",          10, "10660320T000000" },
     118             :     { "1066-03-20:",           0, ""                },
     119             :     { "1066-03-20 00",        13, "10660320T000000" },
     120             :     { "1066-03-20 01",        13, "10660320T010000" },
     121             :     { "1066-03-20 23",        13, "10660320T230000" },
     122             :     { "1066-03-20 24",         0, ""                },
     123             :     { "1066-03-20 00:",        0, ""                },
     124             :     { "1066-03-20 00:3",       0, ""                },
     125             :     { "1066-03-20 00:31",     16, "10660320T003100" },
     126             :     { "1066-03-20 00:31:47",  19, "10660320T003147" },
     127             :     { "1066-03-20 00:31:47 ", 19, "10660320T003147" },
     128             :     { "1066-03-20 00:31:47,", 19, "10660320T003147" },
     129             :     { "1066-03-20 00:31:47:",  0, ""                },
     130             :     { "1-03-20 00:31:47:",     0, ""                },
     131             :     { "10-03-20 00:31:47:",    0, ""                },
     132             :     { "106-03-20 00:31:47:",   0, ""                },
     133             :     { "1066-23-20 00:31:47:",  0, ""                },
     134             :     { "1066-00-20 00:31:47:",  0, ""                },
     135             :     { "1066-0-20 00:31:47:",   0, ""                },
     136             :     { "1066-01-2 00:31:47:",   0, ""                },
     137             :     { "1066-01-2  00:31:47:",  0, ""                },
     138             :     { "1066-01-32 00:31:47:",  0, ""                },
     139             :     { "1066-01-00 00:31:47:",  0, ""                },
     140             :     { "1066-03-20  00:31:47:",11, "10660320T000000" },
     141             :     { "1066-03-2000:31:47:",   0, ""                },
     142             :     { "10666-03-20 00:31:47:", 0, ""                },
     143             :     { NULL, 0 }
     144             :   };
     145             :   int idx;
     146             :   size_t result;
     147             :   gnupg_isotime_t tbuf;
     148             : 
     149          44 :   for (idx=0; array[idx].string; idx++)
     150             :     {
     151          43 :       result = string2isotime (tbuf, array[idx].string);
     152          43 :       if (result != array[idx].result)
     153             :         {
     154           0 :           fail (idx);
     155           0 :           if (verbose)
     156           0 :             fprintf (stderr, "string '%s' expected: %d, got: %d\n",
     157           0 :                      array[idx].string, (int)array[idx].result, (int)result);
     158             :         }
     159          43 :       else if (result && strlen (tbuf) != 15)
     160             :         {
     161           0 :           fail (idx);
     162           0 :           if (verbose)
     163           0 :             fprintf (stderr, "string '%s' invalid isotime returned\n",
     164             :                      array[idx].string);
     165             :         }
     166          43 :       else if (result && strcmp (array[idx].expected, tbuf))
     167             :         {
     168           0 :           fail (idx);
     169           0 :           if (verbose)
     170           0 :             fprintf (stderr, "string '%s' bad isotime '%s' returned\n",
     171             :                      array[idx].string, tbuf);
     172             :         }
     173             :     }
     174           1 : }
     175             : 
     176             : 
     177             : static void
     178           1 : test_isodate_human_to_tm (void)
     179             : {
     180             :   struct {
     181             :     const char *string;
     182             :     int okay;
     183             :     int year, mon, mday;
     184           1 :   } array [] = {
     185             :     { "1970-01-01",      1, 1970,  1,  1 },
     186             :     { "1970-02-01",      1, 1970,  2,  1 },
     187             :     { "1970-12-31",      1, 1970, 12, 31 },
     188             :     { "1971-01-01",      1, 1971,  1,  1 },
     189             :     { "1998-08-15",      1, 1998,  8, 15 },
     190             :     { "2015-04-10",      1, 2015,  4, 10 },
     191             :     { "2015-04-10 11:30",1, 2015,  4, 10 },
     192             :     { "1969-12-31",      0,    0,  0,  0 },
     193             :     { "1900-01-01",      0,    0,  0,  0 },
     194             :     { "",                0,    0,  0,  0 },
     195             :     { "1970-12-32",      0,    0,  0,  0 },
     196             :     { "1970-13-01",      0,    0,  0,  0 },
     197             :     { "1970-01-00",      0,    0,  0,  0 },
     198             :     { "1970-00-01",      0,    0,  0,  0 },
     199             :     { "1970-00-01",      0,    0,  0,  0 },
     200             :     { "1970",            0,    0,  0,  0 },
     201             :     { "1970-01",         0,    0,  0,  0 },
     202             :     { "1970-01-1",       0,    0,  0,  0 },
     203             :     { "1970-1--01",      0,    0,  0,  0 },
     204             :     { "1970-01-01,",     1, 1970,  1,  1 },
     205             :     { "1970-01-01 ",     1, 1970,  1,  1 },
     206             :     { "1970-01-01\t",    1, 1970,  1,  1 },
     207             :     { "1970-01-01;",     0,    0,  0,  0 },
     208             :     { "1970-01-01:",     0,    0,  0,  0 },
     209             :     { "1970_01-01",      0,    0,  0,  0 },
     210             :     { "1970-01_01",      0,    0,  0,  0 },
     211             :     { NULL, 0 }
     212             :   };
     213             :   int idx;
     214             :   int okay;
     215             :   struct tm tmbuf;
     216             : 
     217          27 :   for (idx=0; array[idx].string; idx++)
     218             :     {
     219          26 :       okay = !isodate_human_to_tm (array[idx].string, &tmbuf);
     220          26 :       if (okay != array[idx].okay)
     221             :         {
     222           0 :           fail (idx);
     223           0 :           if (verbose)
     224           0 :             fprintf (stderr, "string '%s' expected: %d, got: %d\n",
     225             :                      array[idx].string, (int)array[idx].okay, okay);
     226             :         }
     227          26 :       else if (!okay)
     228             :         ;
     229          10 :       else if (tmbuf.tm_year + 1900 != array[idx].year
     230          10 :                || tmbuf.tm_mon +1   != array[idx].mon
     231          10 :                || tmbuf.tm_mday     != array[idx].mday)
     232             :         {
     233           0 :           fail (idx);
     234           0 :           if (verbose)
     235           0 :             fprintf (stderr, "string '%s' returned %04d-%02d-%02d\n",
     236             :                      array[idx].string,
     237           0 :                      tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday);
     238             :         }
     239          10 :       else if (tmbuf.tm_sec || tmbuf.tm_min || tmbuf.tm_hour
     240          10 :                || tmbuf.tm_isdst != -1)
     241             :         {
     242           0 :           fail (idx);
     243           0 :           if (verbose)
     244           0 :             fprintf (stderr, "string '%s' returned bad time part\n",
     245             :                      array[idx].string);
     246             :         }
     247             :     }
     248           1 : }
     249             : 
     250             : 
     251             : int
     252           1 : main (int argc, char **argv)
     253             : {
     254           1 :   if (argc > 1 && !strcmp (argv[1], "--verbose"))
     255           0 :     verbose = 1;
     256             : 
     257           1 :   test_isotime2epoch ();
     258           1 :   test_string2isotime ();
     259           1 :   test_isodate_human_to_tm ();
     260             : 
     261           1 :   return !!errcount;
     262             : }

Generated by: LCOV version 1.11