1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* Copyright (c) 2002-2019 Balabit
* Copyright (c) 1998-2019 Balázs Scheidler
*
* This library 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.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include <criterion/criterion.h>
#include "timeutils/wallclocktime.h"
#include "timeutils/cache.h"
#include "timeutils/conv.h"
#include "fake-time.h"
Test(wallclocktime, test_wall_clock_time_init)
{
WallClockTime wct;
wall_clock_time_unset(&wct);
cr_assert(wall_clock_time_is_set(&wct) == FALSE);
}
Test(wallclocktime, test_strptime_parses_broken_down_time)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end;
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f", "Jan 16 2019 18:23:12.012345");
cr_assert(*end == 0);
cr_expect(wct.wct_year == 119);
cr_expect(wct.wct_mon == 0);
cr_expect(wct.wct_mday == 16);
cr_expect(wct.wct_hour == 18);
cr_expect(wct.wct_min == 23);
cr_expect(wct.wct_sec == 12);
cr_expect(wct.wct_usec == 12345);
cr_expect(wct.wct_gmtoff == -1);
}
Test(wallclocktime, test_strptime_parses_truncated_usec)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end;
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f", "Jan 16 2019 18:23:12.012X");
cr_assert(*end == 'X');
cr_expect(wct.wct_usec == 12000);
}
Test(wallclocktime, test_strptime_parses_overflowed_usec)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end;
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f", "Jan 16 2019 18:23:12.0123456X");
cr_assert(*end == 'X');
cr_expect(wct.wct_usec == 12345);
}
Test(wallclocktime, test_strptime_usec_parse_finds_character)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end;
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f", "Jan 16 2019 18:23:12.boom");
cr_assert(end == NULL);
cr_expect(wct.wct_usec == 0);
}
Test(wallclocktime, test_strptime_parses_rfc822_timezone)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end;
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %z", "Jan 16 2019 18:23:12 PST");
cr_assert(*end == 0);
cr_expect(wct.wct_year == 119);
cr_expect(wct.wct_mon == 0);
cr_expect(wct.wct_mday == 16);
cr_expect(wct.wct_hour == 18);
cr_expect(wct.wct_min == 23);
cr_expect(wct.wct_sec == 12);
cr_expect(wct.wct_usec == 0);
cr_expect(wct.wct_gmtoff == -8*3600, "Unexpected timezone offset: %ld", wct.wct_gmtoff);
}
static void
_guess_missing_year(WallClockTime *wct, gint mon)
{
wct->wct_year = -1;
wct->wct_mon = mon;
wall_clock_time_guess_missing_year(wct);
}
static gboolean
_wct_year_matches(WallClockTime *wct, gint mon, gint ofs)
{
gint faked_year = 2019;
_guess_missing_year(wct, mon);
return wct->wct_year == (faked_year - 1900) + ofs;
}
static gboolean
_guessed_year_is_next_year(WallClockTime *wct, gint mon)
{
return _wct_year_matches(wct, mon, +1);
}
static gboolean
_guessed_year_is_current_year(WallClockTime *wct, gint mon)
{
return _wct_year_matches(wct, mon, 0);
}
static gboolean
_guessed_year_is_last_year(WallClockTime *wct, gint mon)
{
return _wct_year_matches(wct, mon, -1);
}
Test(wallclocktime, guess_year_returns_last_year)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end = wall_clock_time_strptime(&wct, "%b %d %H:%M:%S", "Jan 16 18:23:12");
cr_assert(*end == 0);
cr_assert(wct.wct_year == -1);
/* Sat Jan 19 18:58:48 CET 2019 */
fake_time(1547920728);
for (gint mon = 0; mon < 11; mon++)
cr_assert(_guessed_year_is_current_year(&wct, mon));
/* december is assumed to be from the last year */
cr_assert(_guessed_year_is_last_year(&wct, 11));
}
Test(wallclocktime, guess_year_returns_next_year)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar *end = wall_clock_time_strptime(&wct, "%b %d %H:%M:%S", "Jan 16 18:23:12");
cr_assert(*end == 0);
cr_assert(wct.wct_year == -1);
/* Thu Dec 19 22:25:44 CET 2019 */
fake_time(1576790744);
/* january assumed to be from the future year */
cr_assert(_guessed_year_is_next_year(&wct, 0));
for (gint mon = 1; mon <= 11; mon++)
cr_assert(_guessed_year_is_current_year(&wct, mon));
}
static void
setup(void)
{
setenv("TZ", "CET", TRUE);
tzset();
}
static void
teardown(void)
{
}
TestSuite(wallclocktime, .init = setup, .fini = teardown);
|