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