summaryrefslogtreecommitdiff
path: root/subversion/tests/libsvn_subr/time-test.c
blob: 51fbe670f93e4d801326ab829c011ec7193214b0 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * time-test.c -- test the time functions
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#include <stdio.h>
#include <string.h>
#include <apr_general.h>
#include "svn_time.h"

#include "../svn_test.h"

/* All these variables should refer to the same point in time. */
static apr_time_t test_timestamp = APR_TIME_C(1021316450966679);
static const char *test_timestring =
"2002-05-13T19:00:50.966679Z";
static const char *test_old_timestring =
"Mon 13 May 2002 22:00:50.966679 (day 133, dst 1, gmt_off 010800)";


static svn_error_t *
test_time_to_cstring(apr_pool_t *pool)
{
  const char *timestring;

  timestring = svn_time_to_cstring(test_timestamp,pool);

  if (strcmp(timestring,test_timestring) != 0)
    {
      return svn_error_createf
        (SVN_ERR_TEST_FAILED, NULL,
         "svn_time_to_cstring (%" APR_TIME_T_FMT
         ") returned date string '%s' instead of '%s'",
         test_timestamp,timestring,test_timestring);
    }

  return SVN_NO_ERROR;
}


static svn_error_t *
test_time_from_cstring(apr_pool_t *pool)
{
  apr_time_t timestamp;

  SVN_ERR(svn_time_from_cstring(&timestamp, test_timestring, pool));

  if (timestamp != test_timestamp)
    {
      return svn_error_createf
        (SVN_ERR_TEST_FAILED, NULL,
         "svn_time_from_cstring (%s) returned time '%" APR_TIME_T_FMT
         "' instead of '%" APR_TIME_T_FMT "'",
         test_timestring,timestamp,test_timestamp);
    }

  return SVN_NO_ERROR;
}

/* Before editing these tests cases please see the comment in
 * test_time_from_cstring_old regarding the requirements to exercise the bug
 * that they exist to test. */
static const char *failure_old_tests[] = {
  /* Overflow Day */
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  " 3 Oct 2000 HH:MM:SS.UUU (day 277, dst 1, gmt_off -18000)",

  /* Overflow Month */
  "Tue 3 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  " 2000 HH:MM:SS.UUU (day 277, dst 1, gmt_off -18000)",

  NULL
};

static svn_error_t *
test_time_from_cstring_old(apr_pool_t *pool)
{
  apr_time_t timestamp;
  const char **ft;

  SVN_ERR(svn_time_from_cstring(&timestamp, test_old_timestring, pool));

  if (timestamp != test_timestamp)
    {
      return svn_error_createf
        (SVN_ERR_TEST_FAILED, NULL,
         "svn_time_from_cstring (%s) returned time '%" APR_TIME_T_FMT
         "' instead of '%" APR_TIME_T_FMT "'",
         test_old_timestring,timestamp,test_timestamp);
    }

    /* These tests should fail.  They've been added to cover a string overflow
     * found in our code.  However, even if they fail that may not indicate
     * that there is no problem.  The strings being tested need to be
     * sufficently long to cause a segmentation fault in order to exercise
     * this bug.  Unfortunately due to differences in compilers, architectures,
     * etc. there is no way to be sure that the bug is being exerercised on
     * all platforms. */
    for (ft = failure_old_tests; *ft; ft++)
      {
        svn_error_t *err = svn_time_from_cstring(&timestamp, *ft, pool);
        if (! err)
          return svn_error_createf
            (SVN_ERR_TEST_FAILED, NULL,
             "svn_time_from_cstring (%s) succeeded when it should have failed",
             *ft);
        svn_error_clear(err);
      }

  return SVN_NO_ERROR;
}


static svn_error_t *
test_time_invariant(apr_pool_t *pool)
{
  apr_time_t current_timestamp = apr_time_now();
  const char *timestring;
  apr_time_t timestamp;

  timestring = svn_time_to_cstring(current_timestamp, pool);
  SVN_ERR(svn_time_from_cstring(&timestamp, timestring, pool));

  if (timestamp != current_timestamp)
    {
      return svn_error_createf
        (SVN_ERR_TEST_FAILED, NULL,
         "svn_time_from_cstring ( svn_time_to_cstring (n) ) returned time '%"
         APR_TIME_T_FMT
         "' instead of '%" APR_TIME_T_FMT "'",
         timestamp,current_timestamp);
    }

  return SVN_NO_ERROR;
}


struct date_test {
  const char *str;
  apr_int32_t year;
  apr_int32_t mon;
  apr_int32_t mday;
  apr_int32_t hour;
  apr_int32_t min;
  apr_int32_t sec;
  apr_int32_t usec;
};

/* These date strings do not specify a time zone, so we expand the
   svn_parse_date result it in the local time zone and verify that
   against the desired values. */
static struct date_test localtz_tests[] = {
  /* YYYY-M[M]-D[D] */
  { "2013-01-25",                      2013,  1, 25,  0,  0,  0,      0 },
  { "2013-1-25",                       2013,  1, 25,  0,  0,  0,      0 },
  { "2013-01-2",                       2013,  1,  2,  0,  0,  0,      0 },
  /* YYYY-M[M]-D[D][Th[h]:mm[:ss[.u[u[u[u[u[u] */
  { "2015-04-26T00:01:59.652655",      2015,  4, 26,  0,  1, 59, 652655 },
  { "2034-07-20T17:03:36.11379",       2034,  7, 20, 17,  3, 36, 113790 },
  { "1975-10-29T17:23:56.3859",        1975, 10, 29, 17, 23, 56, 385900 },
  { "2024-06-08T13:06:14.897",         2024,  6,  8, 13,  6, 14, 897000 },
  { "2000-01-10T05:11:11.65",          2000,  1, 10,  5, 11, 11, 650000 },
  { "2017-01-28T07:21:13.2",           2017,  1, 28,  7, 21, 13, 200000 },
  { "2013-05-18T13:52:49",             2013,  5, 18, 13, 52, 49,      0 },
  { "2020-05-14T15:28",                2020,  5, 14, 15, 28,  0,      0 },
  { "2032-05-14T7:28",                 2032,  5, 14,  7, 28,  0,      0 },
  { "2020-5-14T15:28",                 2020,  5, 14, 15, 28,  0,      0 },
  { "2020-05-1T15:28",                 2020,  5,  1, 15, 28,  0,      0 },
  /* YYYYMMDD */
  { "20100226",                        2010,  2, 26,  0,  0,  0,      0 },
  /* YYYYMMDD[Thhmm[ss[.u[u[u[u[u[u] */
  { "19860214T050745.6",               1986,  2, 14,  5,  7, 45, 600000 },
  { "20230219T0045",                   2023,  2, 19,  0, 45,  0,      0 },
  /* YYYY-M[M]-D[D] [h]h:mm[:ss[.u[u[u[u[u[u] */
  { "1975-07-11 06:31:49.749504",      1975,  7, 11,  6, 31, 49, 749504 },
  { "2037-05-06 00:08",                2037,  5,  6,  0,  8,  0,      0 },
  { "2037-5-6 7:01",                  2037,  5,  6,  7,  1,  0,      0 },
  /* Make sure we can do leap days. */
  { "1976-02-29",                      1976, 02, 29,  0,  0,  0,      0 },
  { "2000-02-29",                      2000, 02, 29,  0,  0,  0,      0 },
  { NULL }
};

/* These date strings specify an explicit time zone, so we expand the
   svn_parse_date result in UTC and verify that against the desired
   values (which have been adjusted for the specified time zone). */
static struct date_test gmt_tests[] = {
  /* YYYY-MM-DDThh:mm[:ss[.u[u[u[u[u[u]Z */
  { "1979-05-05T15:16:04.39Z",         1979,  5,  5, 15, 16,  4, 390000 },
  { "2012-03-25T12:14Z",               2012,  3, 25, 12, 14,  0,      0 },
  /* YYYY-MM-DDThh:mm[:ss[.u[u[u[u[u[u]+OO[:oo] */
  { "1991-09-13T20:13:01.12779+02:26", 1991,  9, 13, 17, 47,  1, 127790 },
  { "1971-07-20T06:11-10",             1971,  7, 20, 16, 11,  0,      0 },
  /* YYYYMMDDThhmm[ss[.u[u[u[u[u[u]Z */
  { "20010808T105155.527Z",            2001,  8,  8, 10, 51, 55, 527000 },
  { "19781204T1322Z",                  1978, 12,  4, 13, 22,  0,      0 },
  /* YYYYMMDDThhmm[ss[.u[u[u[u[u[u]+OO[oo] */
  { "20030930T001647.8008-0230",       2003,  9, 30,  2, 46, 47, 800800 },
  { "19810526T1705+22",                1981,  5, 25, 19,  5,  0,      0 },
  /* YYYY-MM-DD hh:mm[:ss[.u[u[u[u[u[u][ +OO[oo] */
  { "2024-06-02 11:30:36 +08",         2024,  6,  2,  3, 30, 36,      0 },
  { "1994-10-07 08:08 -1457",          1994, 10, 07, 23,  5,  0,      0 },
  { NULL }
};

/* These date strings only specify a time of day, so we fill the
   current date into the desired values before comparing.  (Currently
   we do not allow a time zone with just a time of day.) */
static struct date_test daytime_tests[] = {
  /* hh:mm[:ss[.u[u[u[u[u[u] */
  { "00:54:15.46",                        0,  0,  0,  0, 54, 15, 460000 },
  { "18:21",                              0,  0,  0, 18, 21,  0,      0 },
  { NULL }
};

/* These date strings should not parse correctly. */
static const char *failure_tests[] = {
  "2000-00-02",           /* Invalid month */
  "2000-13-02",           /* Invalid month */
  "2000-01-32",           /* Invalid day */
  "2000-01-00",           /* Invalid day */
  "1999-02-29",           /* Invalid leap day */
  "2000-01-01 24:00:00",  /* Invalid hour */
  "2000-01-01 00:60:00",  /* Invalid minute */
  "2000-01-01 00:00:61",  /* Invalid second (60 is okay for leap seconds) */
  "2000-01-01+24:00",     /* Invalid timezone hours */
  "2000-01-01-02:60",     /* Invalid timezone minutes */
  "2000-01-01Z",          /* Date with timezone, but no time */
  "2000-01-01+01:00",
  "20000101Z",
  "20000101-0100",
  "2000-01-01T10",        /* Time with hours but no minutes */
  "20000101T10",
  "2000-01-01 10",
  NULL
};

static svn_error_t *
compare_results(struct date_test *dt,
                apr_time_exp_t *expt)
{
  if (expt->tm_year + 1900 != dt->year || expt->tm_mon + 1 != dt->mon
      || expt->tm_mday != dt->mday || expt->tm_hour != dt->hour
      || expt->tm_min != dt->min || expt->tm_sec != dt->sec
      || expt->tm_usec != dt->usec)
    return svn_error_createf
      (SVN_ERR_TEST_FAILED, NULL, "Comparison failed for '%s'", dt->str);
  return SVN_NO_ERROR;
}

static svn_error_t *
test_parse_date(apr_pool_t *pool)
{
  apr_time_t now, result;
  apr_time_exp_t nowexp, expt;
  svn_boolean_t matched;
  struct date_test *dt;
  const char **ft;

  now = apr_time_now();
  if (apr_time_exp_lt(&nowexp, now) != APR_SUCCESS)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Can't expand time");

  for (dt = localtz_tests; dt->str; dt++)
    {
      SVN_ERR(svn_parse_date(&matched, &result, dt->str, now, pool));
      if (!matched)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Match failed for '%s'", dt->str);
      if (apr_time_exp_lt(&expt, result) != APR_SUCCESS)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Expand failed for '%s'", dt->str);
      SVN_ERR(compare_results(dt, &expt));
    }

  for (dt = gmt_tests; dt->str; dt++)
    {
      SVN_ERR(svn_parse_date(&matched, &result, dt->str, now, pool));
      if (!matched)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Match failed for '%s'", dt->str);
      if (apr_time_exp_gmt(&expt, result) != APR_SUCCESS)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Expand failed for '%s'", dt->str);
      SVN_ERR(compare_results(dt, &expt));
    }

  for (dt = daytime_tests; dt->str; dt++)
    {
      SVN_ERR(svn_parse_date(&matched, &result, dt->str, now, pool));
      if (!matched)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Match failed for '%s'", dt->str);
      if (apr_time_exp_lt(&expt, result) != APR_SUCCESS)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Expand failed for '%s'", dt->str);
      dt->year = nowexp.tm_year + 1900;
      dt->mon = nowexp.tm_mon + 1;
      dt->mday = nowexp.tm_mday;
      SVN_ERR(compare_results(dt, &expt));
    }

  for (ft = failure_tests; *ft; ft++)
    {
      SVN_ERR(svn_parse_date(&matched, &result, *ft, now, pool));
      if (matched)
        return svn_error_createf
          (SVN_ERR_TEST_FAILED, NULL, "Match succeeded for '%s'", *ft);
    }

  return SVN_NO_ERROR;
}



/* The test table.  */

static int max_threads = 1;

static struct svn_test_descriptor_t test_funcs[] =
  {
    SVN_TEST_NULL,
    SVN_TEST_PASS2(test_time_to_cstring,
                   "test svn_time_to_cstring"),
    SVN_TEST_PASS2(test_time_from_cstring,
                   "test svn_time_from_cstring"),
    SVN_TEST_PASS2(test_time_from_cstring_old,
                   "test svn_time_from_cstring (old format)"),
    SVN_TEST_PASS2(test_time_invariant,
                   "test svn_time_[to/from]_cstring() invariant"),
    SVN_TEST_PASS2(test_parse_date,
                   "test svn_parse_date"),
    SVN_TEST_NULL
  };

SVN_TEST_MAIN