OpenDNSSEC-enforcer  1.3.9
test_datetime.c
Go to the documentation of this file.
1 /*
2  * $Id: test_datetime.c 3893 2010-09-03 14:10:16Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * Filename: test_datetime.c - Test Date and Time
31  *
32  * Description:
33  * This is a short test module to check the functions in the date/time
34  * module.
35  *
36  * The test program makes use of the CUnit framework, as described in
37  * http://cunit.sourceforge.net
38 -*/
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 
45 #include "CUnit/Basic.h"
46 
47 #include "ksm/datetime.h"
48 #include "ksm/string_util.h"
49 #include "test_routines.h"
50 
51 
52 
53 
54 /*+
55  * CmpDtTm - Test Date/Time Structure
56  *
57  * Description:
58  * Checks the contents of a date/time structure.
59  *
60  * Arguments:
61  * struct tm* test
62  * Structure to test.
63  *
64  * int year, month, day, hour, minute, second
65  * Expected values of these fields. if a value is -1, the field is
66  * not checked.
67 -*/
68 
69 static void CmpDtTm(struct tm* datetime, int year, int month, int day, int hour,
70  int minute, int second)
71 {
72  if (year != -1) CU_ASSERT_EQUAL(year, 1900 + datetime->tm_year);
73  if (month != -1) CU_ASSERT_EQUAL(month, datetime->tm_mon + 1);
74  if (day != -1) CU_ASSERT_EQUAL(day, datetime->tm_mday);
75  if (hour != -1) CU_ASSERT_EQUAL(hour, datetime->tm_hour);
76  if (minute != -1) CU_ASSERT_EQUAL(minute, datetime->tm_min);
77  if (second != -1) CU_ASSERT_EQUAL(second, datetime->tm_sec);
78 
79  return;
80 }
81 
82 
83 
84 /*+
85  * TestDtNumeric - Test Numeric Date/Time
86  *
87  * Description:
88  * Test date/time where specified as numeric.
89 -*/
90 
91 static void TestDtNumeric(void)
92 {
93  int status; /* Status return */
94  struct tm datetime; /* Date/time structure returned */
95 
96  /* Valid date/time values */
97 
98  status = DtNumeric("20080102030405", &datetime);
99  CU_ASSERT_EQUAL(status, 0);
100  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
101 
102  status = DtNumeric("200801020304", &datetime);
103  CU_ASSERT_EQUAL(status, 0);
104  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
105 
106  status = DtNumeric("2008010203", &datetime);
107  CU_ASSERT_EQUAL(status, 0);
108  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
109 
110  status = DtNumeric("20080102", &datetime);
111  CU_ASSERT_EQUAL(status, 0);
112  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
113 
114  /* Some invalid dates */
115 
116  status = DtNumeric("2008", &datetime); /* Too short */
117  CU_ASSERT_NOT_EQUAL(status, 0);
118 
119  status = DtNumeric("2008010203040506", &datetime); /* Too long */
120  CU_ASSERT_NOT_EQUAL(status, 0);
121 
122  status = DtNumeric("200801020", &datetime); /* Odd no. of chars */
123  CU_ASSERT_NOT_EQUAL(status, 0);
124 
125  return;
126 }
127 
128 
129 /*+
130  * TestDtAppendTime - Test Time Appending
131  *
132  * Description:
133  * Tests whether tiem can be successfully appended to the date.
134 -*/
135 
136 static void TestDtAppendTime(void)
137 {
138  char fulldt[64];
139  int status;
140 
141  strcpy(fulldt, "A");
142  status = DtAppendTime(fulldt, NULL);
143  CU_ASSERT_EQUAL(status, 0);
144  CU_ASSERT_STRING_EQUAL(fulldt, "A 00:00:00");
145 
146  strcpy(fulldt, "A");
147  status = DtAppendTime(fulldt, "");
148  CU_ASSERT_EQUAL(status, 0);
149  CU_ASSERT_STRING_EQUAL(fulldt, "A 00:00:00");
150 
151  strcpy(fulldt, "A");
152  status = DtAppendTime(fulldt, " 12");
153  CU_ASSERT_EQUAL(status, 0);
154  CU_ASSERT_STRING_EQUAL(fulldt, "A 12:00:00");
155 
156  strcpy(fulldt, "A");
157  status = DtAppendTime(fulldt, ":12");
158  CU_ASSERT_EQUAL(status, 0);
159  CU_ASSERT_STRING_EQUAL(fulldt, "A:12:00:00");
160 
161  strcpy(fulldt, "A");
162  status = DtAppendTime(fulldt, ":12:34");
163  CU_ASSERT_EQUAL(status, 0);
164  CU_ASSERT_STRING_EQUAL(fulldt, "A:12:34:00");
165 
166  strcpy(fulldt, "A");
167  status = DtAppendTime(fulldt, ":12:34:56");
168  CU_ASSERT_EQUAL(status, 0);
169  CU_ASSERT_STRING_EQUAL(fulldt, "A:12:34:56");
170 
171  strcpy(fulldt, "A");
172  status = DtAppendTime(fulldt, "*12:34:56"); /* Invalid separator */
173  CU_ASSERT_NOT_EQUAL(status, 0);
174 
175  strcpy(fulldt, "A");
176  status = DtAppendTime(fulldt, ":1234:56"); /* Wrong length */
177  CU_ASSERT_NOT_EQUAL(status, 0);
178 
179  return;
180 }
181 
182 
183 
184 /*+
185  * TestDtGeneral - Test General Date/Time
186  *
187  * Description:
188  * Test date/time where specified as numeric.
189 -*/
190 
191 static void TestDtGeneral(void)
192 {
193  int status; /* Status return */
194  struct tm datetime; /* Date/time structure returned */
195 
196  /* Valid date/time values */
197 
198  status = DtGeneral("2-Jan-2008 03:04:05", &datetime);
199  CU_ASSERT_EQUAL(status, 0);
200  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
201 
202  status = DtGeneral("02-Jan-2008 03:04:05", &datetime);
203  CU_ASSERT_EQUAL(status, 0);
204  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
205 
206  status = DtGeneral("02-Jan-2008:03:04:05", &datetime);
207  CU_ASSERT_EQUAL(status, 0);
208  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
209 
210  status = DtGeneral("02-Jan-2008:03:04", &datetime);
211  CU_ASSERT_EQUAL(status, 0);
212  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
213 
214  status = DtGeneral("02-Jan-2008:03", &datetime);
215  CU_ASSERT_EQUAL(status, 0);
216  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
217 
218  status = DtGeneral("02-Jan-2008", &datetime);
219  CU_ASSERT_EQUAL(status, 0);
220  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
221 
222  /* More valid date/time values */
223 
224  status = DtGeneral("2-01-2008 03:04:05", &datetime);
225  CU_ASSERT_EQUAL(status, 0);
226  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
227 
228  status = DtGeneral("02-01-2008 03:04:05", &datetime);
229  CU_ASSERT_EQUAL(status, 0);
230  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
231 
232  status = DtGeneral("02-01-2008:03:04:05", &datetime);
233  CU_ASSERT_EQUAL(status, 0);
234  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
235 
236  status = DtGeneral("02-01-2008:03:04", &datetime);
237  CU_ASSERT_EQUAL(status, 0);
238  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
239 
240  status = DtGeneral("02-01-2008:03", &datetime);
241  CU_ASSERT_EQUAL(status, 0);
242  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
243 
244  status = DtGeneral("02-01-2008", &datetime);
245  CU_ASSERT_EQUAL(status, 0);
246  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
247 
248  /* More valid date/time values, year first */
249 
250  status = DtGeneral("2008-Jan-02 03:04:05", &datetime);
251  CU_ASSERT_EQUAL(status, 0);
252  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
253 
254  status = DtGeneral("2008-Jan-02:03:04:05", &datetime);
255  CU_ASSERT_EQUAL(status, 0);
256  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
257 
258  status = DtGeneral("2008-Jan-02:03:04", &datetime);
259  CU_ASSERT_EQUAL(status, 0);
260  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
261 
262  status = DtGeneral("2008-Jan-02:03", &datetime);
263  CU_ASSERT_EQUAL(status, 0);
264  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
265 
266  status = DtGeneral("2008-Jan-02", &datetime);
267  CU_ASSERT_EQUAL(status, 0);
268  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
269 
270  /* More valid date/time values, year first */
271 
272  status = DtGeneral("2008-01-02 03:04:05", &datetime);
273  CU_ASSERT_EQUAL(status, 0);
274  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
275 
276  status = DtGeneral("2008-01-02:03:04:05", &datetime);
277  CU_ASSERT_EQUAL(status, 0);
278  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
279 
280  status = DtGeneral("2008-01-02:03:04", &datetime);
281  CU_ASSERT_EQUAL(status, 0);
282  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
283 
284  status = DtGeneral("2008-01-02:03", &datetime);
285  CU_ASSERT_EQUAL(status, 0);
286  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
287 
288  status = DtGeneral("2008-01-02", &datetime);
289  CU_ASSERT_EQUAL(status, 0);
290  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
291 
292  /* Some zero dates */
293 
294  status = DtGeneral("00-00-0000:00:00:00", &datetime);
295  CU_ASSERT_NOT_EQUAL(status, 0);
296 
297  status = DtGeneral("0000-00-00", &datetime);
298  CU_ASSERT_NOT_EQUAL(status, 0);
299 
300  status = DtGeneral("00000000", &datetime);
301  CU_ASSERT_NOT_EQUAL(status, 0);
302 
303  /* Some invalid dates */
304 
305  status = DtGeneral("13-Jan", &datetime); /* Too short */
306  CU_ASSERT_NOT_EQUAL(status, 0);
307 
308  status = DtGeneral("02-Xxx-2008", &datetime); /* Month invalid */
309  CU_ASSERT_NOT_EQUAL(status, 0);
310 
311  status = DtGeneral("02-Feb-2008:", &datetime); /* Trailing : */
312  CU_ASSERT_NOT_EQUAL(status, 0);
313 
314  status = DtGeneral("02-Jan-2008:03-04-05", &datetime);
315  CU_ASSERT_NOT_EQUAL(status, 0); /* Wrong separator */
316 
317  return;
318 }
319 
320 
321 
322 /*+
323  * TestDtGeneralStringTest - Test General String
324  *
325  * Description:
326  * Individual test for TestDtGeneralString.
327 -*/
328 
329 static void GeneralStringTest(const char* what, const char* expected)
330 {
331  char* actual;
332 
333  actual = DtGeneralString(what);
334  if (expected == NULL) {
335  CU_ASSERT_PTR_NULL(actual);
336  }
337  else {
338  CU_ASSERT_PTR_NOT_NULL(actual);
339  CU_ASSERT_STRING_EQUAL(actual, expected);
340  StrFree(actual);
341  }
342 
343  return;
344 }
345 
346 /*+
347  * TestDtGeneral - Test General Date/Time
348  *
349  * Description:
350  * Test date/time where specified as numeric.
351 -*/
352 
353 static void TestDtGeneralString(void)
354 {
355  /* Valid date/time values */
356 
357  GeneralStringTest("2-Jan-2008 03:04:05", "2008-01-02 03:04:05");
358  GeneralStringTest("02-Jan-2008 03:04:05", "2008-01-02 03:04:05");
359  GeneralStringTest("02-Jan-2008:03:04:05", "2008-01-02 03:04:05");
360  GeneralStringTest("02-Jan-2008:03:04", "2008-01-02 03:04:00");
361  GeneralStringTest("02-Jan-2008:03", "2008-01-02 03:00:00");
362  GeneralStringTest("02-Jan-2008", "2008-01-02 00:00:00");
363 
364  /* More valid date/time values */
365 
366  GeneralStringTest("2-01-2008 03:04:05", "2008-01-02 03:04:05");
367  GeneralStringTest("02-01-2008 03:04:05", "2008-01-02 03:04:05");
368  GeneralStringTest("02-01-2008:03:04:05", "2008-01-02 03:04:05");
369  GeneralStringTest("02-01-2008:03:04", "2008-01-02 03:04:00");
370  GeneralStringTest("02-01-2008:03", "2008-01-02 03:00:00");
371  GeneralStringTest("02-01-2008", "2008-01-02 00:00:00");
372 
373  /* More valid date/time values, year first */
374 
375  GeneralStringTest("2008-Jan-02 03:04:05", "2008-01-02 03:04:05");
376  GeneralStringTest("2008-Jan-02:03:04:05", "2008-01-02 03:04:05");
377  GeneralStringTest("2008-Jan-02:03:04", "2008-01-02 03:04:00");
378  GeneralStringTest("2008-Jan-02:03", "2008-01-02 03:00:00");
379  GeneralStringTest("2008-Jan-02", "2008-01-02 00:00:00");
380 
381  /* More valid date/time values, year first */
382 
383  GeneralStringTest("2008-01-02 03:04:05", "2008-01-02 03:04:05");
384  GeneralStringTest("2008-01-02:03:04:05", "2008-01-02 03:04:05");
385  GeneralStringTest("2008-01-02:03:04", "2008-01-02 03:04:00");
386  GeneralStringTest("2008-01-02:03", "2008-01-02 03:00:00");
387  GeneralStringTest("2008-01-02", "2008-01-02 00:00:00");
388 
389  /* Some zero dates */
390 
391  GeneralStringTest("00-00-0000:00:00:00", NULL);
392  GeneralStringTest("0000-00-00", NULL);
393  GeneralStringTest("00000000", NULL);
394 
395  /* Some invalid dates */
396 
397  GeneralStringTest("13-Jan", NULL); /* Too short */
398  GeneralStringTest("02-Xxx-2008", NULL); /* Month invalid */
399  GeneralStringTest("02-Feb-2008:", NULL); /* Trailing : */
400  GeneralStringTest("02-Jan-2008:03-04-05", NULL); /* Wrong separator */
401 
402  return;
403 }
404 
405 
406 
407 /*+
408  * TestDtParseDateTime - Test Parsing Date/Time
409  *
410  * Description:
411  * Test date/time where specified as general. This is just a concatenation
412  * of the two previous tests using the general function.
413 -*/
414 
415 static void TestDtParseDateTime(void)
416 {
417  int status; /* Status return */
418  struct tm datetime; /* Date/time structure returned */
419 
420  /* Valid date/time values. A few have leading.trailing spaces */
421 
422  status = DtParseDateTime(" 20080102030405 ", &datetime);
423  CU_ASSERT_EQUAL(status, 0);
424  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
425 
426  status = DtParseDateTime("200801020304 ", &datetime);
427  CU_ASSERT_EQUAL(status, 0);
428  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
429 
430  status = DtParseDateTime("2008010203", &datetime);
431  CU_ASSERT_EQUAL(status, 0);
432  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
433 
434  status = DtParseDateTime(" 20080102", &datetime);
435  CU_ASSERT_EQUAL(status, 0);
436  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
437 
438  status = DtParseDateTime("2-Jan-2008 03:04:05", &datetime);
439  CU_ASSERT_EQUAL(status, 0);
440  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
441 
442  status = DtParseDateTime(" 02-JAN-2008 03:04:05 ", &datetime);
443  CU_ASSERT_EQUAL(status, 0);
444  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
445 
446  status = DtParseDateTime("02-jan-2008:03:04:05", &datetime);
447  CU_ASSERT_EQUAL(status, 0);
448  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5);
449 
450  status = DtParseDateTime("02-Jan-2008:03:04", &datetime);
451  CU_ASSERT_EQUAL(status, 0);
452  CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0);
453 
454  status = DtParseDateTime("02-Jan-2008:03", &datetime);
455  CU_ASSERT_EQUAL(status, 0);
456  CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0);
457 
458  status = DtParseDateTime("02-Jan-2008", &datetime);
459  CU_ASSERT_EQUAL(status, 0);
460  CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0);
461 
462  /* Some invalid dates */
463 
464  status = DtParseDateTime(NULL, &datetime); /* Null string */
465  CU_ASSERT_NOT_EQUAL(status, 0);
466 
467  status = DtParseDateTime("", &datetime); /* Too short */
468  CU_ASSERT_NOT_EQUAL(status, 0);
469 
470  status = DtParseDateTime(" ", &datetime); /* Too short */
471  CU_ASSERT_NOT_EQUAL(status, 0);
472 
473  status = DtParseDateTime("2008", &datetime); /* Too short */
474  CU_ASSERT_NOT_EQUAL(status, 0);
475 
476  status = DtParseDateTime("2008010203040506", &datetime); /* Too long */
477  CU_ASSERT_NOT_EQUAL(status, 0);
478 
479  status = DtParseDateTime("200801020", &datetime); /* Odd no. of chars */
480  CU_ASSERT_NOT_EQUAL(status, 0);
481 
482  status = DtParseDateTime("13-Jan", &datetime); /* Too short */
483  CU_ASSERT_NOT_EQUAL(status, 0);
484 
485  status = DtParseDateTime("02-Xxx-2008", &datetime); /* Month invalid */
486  CU_ASSERT_NOT_EQUAL(status, 0);
487 
488  status = DtParseDateTime("02-Feb-2008:", &datetime); /* Trailing : */
489  CU_ASSERT_NOT_EQUAL(status, 0);
490 
491  status = DtParseDateTime("02-Jan-2008:03-04-05", &datetime);
492  CU_ASSERT_NOT_EQUAL(status, 0); /* Wrong separator */
493 
494  return;
495 }
496 
497 
498 
499 /*
500  * TestDtNow - Check DtNow Function
501  *
502  * Description:
503  * Tests the "Now" function by getting the time twice and executing the
504  * "Now" function between the two times. Where the fields of the
505  * two times are the same, compare the result from the "Now" with that.
506 -*/
507 
508 static void TestDtNow(void)
509 {
510  struct tm time1;
511  struct tm time2;
512  struct tm test;
513  time_t curtime;
514  int status;
515 
516  (void) time(&curtime);
517  (void) localtime_r(&curtime, &time1);
518  status = DtNow(&test);
519  (void) time(&curtime);
520  (void) localtime_r(&curtime, &time2);
521 
522  CU_ASSERT_EQUAL(status, 0);
523  CmpDtTm(&test,
524  time1.tm_year == time2.tm_year ? time1.tm_year + 1900 : -1,
525  time1.tm_mon == time2.tm_mon ? time1.tm_mon + 1 : -1,
526  time1.tm_mday == time2.tm_mday ? time1.tm_mday : -1,
527  time1.tm_hour == time2.tm_hour ? time1.tm_hour : -1,
528  time1.tm_min == time2.tm_min ? time1.tm_min : -1,
529  time1.tm_sec == time2.tm_sec ? time1.tm_sec : -1
530  );
531 
532  (void) time(&curtime);
533  (void) localtime_r(&curtime, &time1);
534  status = DtParseDateTime(" NOW ", &test);
535  (void) time(&curtime);
536  (void) localtime_r(&curtime, &time2);
537 
538  CU_ASSERT_EQUAL(status, 0);
539  CmpDtTm(&test,
540  time1.tm_year == time2.tm_year ? time1.tm_year + 1900 : -1,
541  time1.tm_mon == time2.tm_mon ? time1.tm_mon + 1 : -1,
542  time1.tm_mday == time2.tm_mday ? time1.tm_mday : -1,
543  time1.tm_hour == time2.tm_hour ? time1.tm_hour : -1,
544  time1.tm_min == time2.tm_min ? time1.tm_min : -1,
545  time1.tm_sec == time2.tm_sec ? time1.tm_sec : -1
546  );
547 }
548 
549 
550 
551 
552 
553 /*+
554  * CheckValidIntervalSeconds - Perform Test on Valid String
555  *
556  * Description:
557  * Performs the tests on DtIntervalSecond son the strings that are supposed
558  * to be valid.
559  *
560  * Arguments:
561  * const char* string
562  * String to test.
563  *
564  * long einterval
565  * Expected interval.
566 -*/
567 
568 static void CheckValidIntervalSeconds(const char* string, int einterval)
569 {
570  int interval;
571  int status;
572 
573  status = DtIntervalSeconds(string, &interval);
574  CU_ASSERT_EQUAL(status, 0);
575  CU_ASSERT_EQUAL(interval, einterval);
576 
577  return;
578 }
579 
580 /*+
581  * TestDtIntervalSeconds - Test DtIntervalSeconds
582 -*/
583 
584 static void TestDtIntervalSeconds(void)
585 {
586  int interval;
587  int status;
588 
589  /* Valid values */
590 
591  CheckValidIntervalSeconds("1", 1L);
592  CheckValidIntervalSeconds("234", 234L);
593  CheckValidIntervalSeconds("1223s", 1223L);
594  CheckValidIntervalSeconds("1m", 60L);
595  CheckValidIntervalSeconds("15m", 900L);
596  CheckValidIntervalSeconds("2h", 7200L);
597  CheckValidIntervalSeconds("24h", 86400L);
598  CheckValidIntervalSeconds("1d", 86400L);
599  CheckValidIntervalSeconds("7d", 604800L);
600  CheckValidIntervalSeconds("1w", 604800L);
601  CheckValidIntervalSeconds("52w", 31449600L);
602 
603  /* Invalid ones */
604 
605  status = DtIntervalSeconds(NULL, NULL);
606  CU_ASSERT_EQUAL(status, 4);
607  status = DtIntervalSeconds("1d", NULL);
608  CU_ASSERT_EQUAL(status, 4);
609  status = DtIntervalSeconds(NULL, &interval);
610  CU_ASSERT_EQUAL(status, 4);
611  status = DtIntervalSeconds("", &interval);
612  CU_ASSERT_EQUAL(status, 4);
613 
614  status = DtIntervalSeconds("1234567890123456789012345678901234567890",
615  &interval);
616  CU_ASSERT_EQUAL(status, 3);
617  status = DtIntervalSeconds("1234567890123456789012345678901",
618  &interval);
619  CU_ASSERT_EQUAL(status, 2); /* Overflow */
620 
621  status = DtIntervalSeconds("1ww", &interval);
622  CU_ASSERT_EQUAL(status, 2);
623  status = DtIntervalSeconds("2 2w", &interval);
624  CU_ASSERT_EQUAL(status, 2);
625 
626  status = DtIntervalSeconds("2a", &interval);
627  CU_ASSERT_EQUAL(status, 1);
628 
629  return;
630 }
631 
632 
633 /*+
634  * TestDtSecondsInterval
635 -*/
636 
637 static void TestDtSecondsInterval(void)
638 {
639  char buffer[32];
640 
641  DtSecondsInterval(1209601, buffer, sizeof(buffer));
642  CU_ASSERT_STRING_EQUAL(buffer, "1209601s");
643 
644  DtSecondsInterval(1209600, buffer, sizeof(buffer));
645  CU_ASSERT_STRING_EQUAL(buffer, "2w");
646 
647  DtSecondsInterval(1209599, buffer, sizeof(buffer));
648  CU_ASSERT_STRING_EQUAL(buffer, "1209599s");
649 
650  DtSecondsInterval(259201, buffer, sizeof(buffer));
651  CU_ASSERT_STRING_EQUAL(buffer, "259201s");
652 
653  DtSecondsInterval(259200, buffer, sizeof(buffer));
654  CU_ASSERT_STRING_EQUAL(buffer, "3d");
655 
656  DtSecondsInterval(259199, buffer, sizeof(buffer));
657  CU_ASSERT_STRING_EQUAL(buffer, "259199s");
658 
659  DtSecondsInterval(14401, buffer, sizeof(buffer));
660  CU_ASSERT_STRING_EQUAL(buffer, "14401s");
661 
662  DtSecondsInterval(14400, buffer, sizeof(buffer));
663  CU_ASSERT_STRING_EQUAL(buffer, "4h");
664 
665  DtSecondsInterval(14399, buffer, sizeof(buffer));
666  CU_ASSERT_STRING_EQUAL(buffer, "14399s");
667 
668  DtSecondsInterval(301, buffer, sizeof(buffer));
669  CU_ASSERT_STRING_EQUAL(buffer, "301s");
670 
671  DtSecondsInterval(300, buffer, sizeof(buffer));
672  CU_ASSERT_STRING_EQUAL(buffer, "5m");
673 
674  DtSecondsInterval(299, buffer, sizeof(buffer));
675  CU_ASSERT_STRING_EQUAL(buffer, "299s");
676 
677  DtSecondsInterval(0, buffer, sizeof(buffer));
678  CU_ASSERT_STRING_EQUAL(buffer, "0s");
679 
680  return;
681 }
682 
683 
684 
685 /*
686  * CheckDtDateDiff - Check Date Difference Code
687  * TestDtDateDiff - Check Date Difference Code
688  *
689  * Arguments to CheckDtDateDiff are:
690  *
691  * const char* date1, const char* date2
692  * Dates to test
693  *
694  * int status
695  * Expected status return.
696  *
697  * int result
698  * Expected result, only valid if status is zero.
699  */
700 
701 static void CheckDtDateDiff(const char* date1, const char* date2, int status,
702  int result)
703 {
704  int act_status = 0; /* Actual status */
705  int act_result = 0; /* Actual result */
706 
707  act_status = DtDateDiff(date1, date2, &act_result);
708  CU_ASSERT_EQUAL(status, act_status);
709  if (status == 0) {
710  CU_ASSERT_EQUAL(result, act_result);
711  }
712 
713  return;
714 }
715 
716 static void TestDtDateDiff(void)
717 {
718  /* Valid dates on same day */
719 
720  CheckDtDateDiff("2001-01-01 00:00:02", "2001-01-01 00:00:01", 0, 1);
721  CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-01 00:00:02", 0, -1);
722 
723  CheckDtDateDiff("2001-01-01 00:01:02", "2001-01-01 00:00:01", 0, 61);
724  CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-01 00:01:02", 0, -61);
725 
726  CheckDtDateDiff("2001-01-01 02:01:02", "2001-01-01 00:00:01", 0, 7261);
727  CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-01 02:01:02", 0, -7261);
728 
729  CheckDtDateDiff("2001-01-02 02:01:02", "2001-01-01 00:00:01", 0, 93661);
730  CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-02 02:01:02", 0, -93661);
731 
732  /* Invalid dates */
733 
734  CheckDtDateDiff(NULL, NULL, 3, 0);
735  CheckDtDateDiff("2001-01-01 23:12:22", NULL, 3, 0);
736  CheckDtDateDiff("2001-01-01 23:12:22", "", 3, 0);
737  CheckDtDateDiff(NULL, "2001-01-01 23:12:22", 3, 0);
738  CheckDtDateDiff("", "2001-01-01 23:12:22", 3, 0);
739  CheckDtDateDiff("2001-01-01 23:12:22", "fred", 2, 0);
740  CheckDtDateDiff("fred", "2001-01-01 23:12:22", 1, 0);
741 }
742 
743 /*+
744  * CheckValidXMLIntervalSeconds - Perform Test on Valid String
745  *
746  * Description:
747  * Performs the tests on DtXMLIntervalSecond son the strings that are supposed
748  * to be valid.
749  *
750  * Arguments:
751  * const char* string
752  * String to test.
753  *
754  * long einterval
755  * Expected interval.
756 -*/
757 
758 static void CheckValidXMLIntervalSeconds(const char* string, int einterval, int estatus)
759 {
760  int interval;
761  int status;
762 
763  status = DtXMLIntervalSeconds(string, &interval);
764  CU_ASSERT_EQUAL(status, estatus);
765  CU_ASSERT_EQUAL(interval, einterval);
766 
767  return;
768 }
769 
770 /*+
771  * TestDtXMLIntervalSeconds - Test DtXMLIntervalSeconds
772 -*/
773 
774 static void TestDtXMLIntervalSeconds(void)
775 {
776  int interval;
777  int status;
778 
779  /* Valid values, return status = 0 */
780 
781  /* CheckValidXMLIntervalSeconds("P1", 1L, 0);*/
782  status = DtXMLIntervalSeconds("P1", &interval);
783  CU_ASSERT_EQUAL(status, 0);
784  CU_ASSERT_EQUAL(interval, 1);
785 
786  /* CheckValidXMLIntervalSeconds("P234", 234L, 0);*/
787  status = DtXMLIntervalSeconds("P234", &interval);
788  CU_ASSERT_EQUAL(status, 0);
789  CU_ASSERT_EQUAL(interval, 234);
790 
791  CheckValidXMLIntervalSeconds("P1223S", 1223L, 0);
792  CheckValidXMLIntervalSeconds("PT1M", 60L, 0);
793  CheckValidXMLIntervalSeconds("PT15M", 900L, 0);
794  CheckValidXMLIntervalSeconds("P2H", 7200L, 0);
795  CheckValidXMLIntervalSeconds("PT2H", 7200L, 0);
796  CheckValidXMLIntervalSeconds("P24H", 86400L, 0);
797  CheckValidXMLIntervalSeconds("PT24H", 86400L, 0);
798  CheckValidXMLIntervalSeconds("P1D", 86400L, 0);
799  CheckValidXMLIntervalSeconds("P7D", 604800L, 0);
800  CheckValidXMLIntervalSeconds("P1W", 604800L, 0);
801  CheckValidXMLIntervalSeconds("P52W", 31449600L, 0);
802  /* CheckValidXMLIntervalSeconds("-PT1M", -60L, 0);*/
803  status = DtXMLIntervalSeconds("-PT1M", &interval);
804  CU_ASSERT_EQUAL(status, 0);
805  CU_ASSERT_EQUAL(interval, -60);
806 
807  CheckValidXMLIntervalSeconds("PT1223S", 1223L, 0);
808 
809  /* Some mixed-format stuff */
810  CheckValidXMLIntervalSeconds("P1W1DT2H2M7S", 698527L, 0);
811  CheckValidXMLIntervalSeconds("P1Y1W1DT2H2M7S", 32234527L, -1);
812  CheckValidXMLIntervalSeconds("P1Y2M1W1DT2H2M7S", 37591327L, -1);
813 
814  /* Valid but return -1 */
815  CheckValidXMLIntervalSeconds("P1M", 2678400L, -1);
816  CheckValidXMLIntervalSeconds("P15M", 40176000L, -1);
817 
818  /* CheckValidXMLIntervalSeconds("P1Y", 31536000L, -1); */
819  status = DtXMLIntervalSeconds("P1Y", &interval);
820  CU_ASSERT_EQUAL(status, -1);
821  CU_ASSERT_EQUAL(interval, 31536000);
822 
823 
824  /* Invalid ones */
825 
826  status = DtXMLIntervalSeconds(NULL, NULL);
827  CU_ASSERT_EQUAL(status, 4);
828  status = DtXMLIntervalSeconds("1d", NULL);
829  CU_ASSERT_EQUAL(status, 4);
830  status = DtXMLIntervalSeconds(NULL, &interval);
831  CU_ASSERT_EQUAL(status, 4);
832  status = DtXMLIntervalSeconds("", &interval);
833  CU_ASSERT_EQUAL(status, 4);
834 
835  status = DtXMLIntervalSeconds("1234567890123456789012345678901234567890",
836  &interval);
837  CU_ASSERT_EQUAL(status, 3);
838  status = DtXMLIntervalSeconds("1234567890123456789012345678901",
839  &interval);
840  CU_ASSERT_EQUAL(status, 3); /* Overflow */
841 
842  /* This test may work, depends on the bit-ness of the machine
843  status = DtXMLIntervalSeconds("168Y", &interval);
844  CU_ASSERT_EQUAL(status, 3); */
845 
846  status = DtXMLIntervalSeconds("1WW", &interval);
847  CU_ASSERT_EQUAL(status, 2);
848  status = DtXMLIntervalSeconds("2 2W", &interval);
849  CU_ASSERT_EQUAL(status, 2);
850 
851  status = DtXMLIntervalSeconds("2a", &interval);
852  CU_ASSERT_EQUAL(status, 2);
853 
854  return;
855 }
856 
857 /*
858  * TestDt - Create Test Suite
859  *
860  * Description:
861  * Adds the test suite to the CUnit test registry and adds all the tests
862  * to it.
863  *
864  * Arguments:
865  * None.
866  *
867  * Returns:
868  * int
869  * Return status. 0 => Success.
870  */
871 
872 int TestDt(void); /* Declaration */
873 int TestDt(void)
874 {
875  struct test_testdef tests[] = {
876  {"DtNumeric", TestDtNumeric},
877  {"DtAppendTime", TestDtAppendTime},
878  {"DtGeneral", TestDtGeneral},
879  {"DtGeneralString", TestDtGeneralString},
880  {"DtParseDateTime", TestDtParseDateTime},
881  {"DtNow", TestDtNow},
882  {"DtIntervalSeconds", TestDtIntervalSeconds},
883  {"DtSecondsInterval", TestDtSecondsInterval},
884  {"DtDateDiff", TestDtDateDiff},
885  {"DtXMLIntervalSeconds",TestDtXMLIntervalSeconds},
886  {NULL, NULL}
887  };
888 
889  return TcuCreateSuite("Date/Time", NULL, NULL, tests);
890 }