OpenDNSSEC-enforcer
1.3.4
|
00001 /* 00002 * $Id: test_datetime.c 3893 2010-09-03 14:10:16Z sion $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /*+ 00030 * Filename: test_datetime.c - Test Date and Time 00031 * 00032 * Description: 00033 * This is a short test module to check the functions in the date/time 00034 * module. 00035 * 00036 * The test program makes use of the CUnit framework, as described in 00037 * http://cunit.sourceforge.net 00038 -*/ 00039 00040 #include <stdlib.h> 00041 #include <stdio.h> 00042 #include <string.h> 00043 #include <time.h> 00044 00045 #include "CUnit/Basic.h" 00046 00047 #include "ksm/datetime.h" 00048 #include "ksm/string_util.h" 00049 #include "test_routines.h" 00050 00051 00052 00053 00054 /*+ 00055 * CmpDtTm - Test Date/Time Structure 00056 * 00057 * Description: 00058 * Checks the contents of a date/time structure. 00059 * 00060 * Arguments: 00061 * struct tm* test 00062 * Structure to test. 00063 * 00064 * int year, month, day, hour, minute, second 00065 * Expected values of these fields. if a value is -1, the field is 00066 * not checked. 00067 -*/ 00068 00069 static void CmpDtTm(struct tm* datetime, int year, int month, int day, int hour, 00070 int minute, int second) 00071 { 00072 if (year != -1) CU_ASSERT_EQUAL(year, 1900 + datetime->tm_year); 00073 if (month != -1) CU_ASSERT_EQUAL(month, datetime->tm_mon + 1); 00074 if (day != -1) CU_ASSERT_EQUAL(day, datetime->tm_mday); 00075 if (hour != -1) CU_ASSERT_EQUAL(hour, datetime->tm_hour); 00076 if (minute != -1) CU_ASSERT_EQUAL(minute, datetime->tm_min); 00077 if (second != -1) CU_ASSERT_EQUAL(second, datetime->tm_sec); 00078 00079 return; 00080 } 00081 00082 00083 00084 /*+ 00085 * TestDtNumeric - Test Numeric Date/Time 00086 * 00087 * Description: 00088 * Test date/time where specified as numeric. 00089 -*/ 00090 00091 static void TestDtNumeric(void) 00092 { 00093 int status; /* Status return */ 00094 struct tm datetime; /* Date/time structure returned */ 00095 00096 /* Valid date/time values */ 00097 00098 status = DtNumeric("20080102030405", &datetime); 00099 CU_ASSERT_EQUAL(status, 0); 00100 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00101 00102 status = DtNumeric("200801020304", &datetime); 00103 CU_ASSERT_EQUAL(status, 0); 00104 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00105 00106 status = DtNumeric("2008010203", &datetime); 00107 CU_ASSERT_EQUAL(status, 0); 00108 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00109 00110 status = DtNumeric("20080102", &datetime); 00111 CU_ASSERT_EQUAL(status, 0); 00112 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00113 00114 /* Some invalid dates */ 00115 00116 status = DtNumeric("2008", &datetime); /* Too short */ 00117 CU_ASSERT_NOT_EQUAL(status, 0); 00118 00119 status = DtNumeric("2008010203040506", &datetime); /* Too long */ 00120 CU_ASSERT_NOT_EQUAL(status, 0); 00121 00122 status = DtNumeric("200801020", &datetime); /* Odd no. of chars */ 00123 CU_ASSERT_NOT_EQUAL(status, 0); 00124 00125 return; 00126 } 00127 00128 00129 /*+ 00130 * TestDtAppendTime - Test Time Appending 00131 * 00132 * Description: 00133 * Tests whether tiem can be successfully appended to the date. 00134 -*/ 00135 00136 static void TestDtAppendTime(void) 00137 { 00138 char fulldt[64]; 00139 int status; 00140 00141 strcpy(fulldt, "A"); 00142 status = DtAppendTime(fulldt, NULL); 00143 CU_ASSERT_EQUAL(status, 0); 00144 CU_ASSERT_STRING_EQUAL(fulldt, "A 00:00:00"); 00145 00146 strcpy(fulldt, "A"); 00147 status = DtAppendTime(fulldt, ""); 00148 CU_ASSERT_EQUAL(status, 0); 00149 CU_ASSERT_STRING_EQUAL(fulldt, "A 00:00:00"); 00150 00151 strcpy(fulldt, "A"); 00152 status = DtAppendTime(fulldt, " 12"); 00153 CU_ASSERT_EQUAL(status, 0); 00154 CU_ASSERT_STRING_EQUAL(fulldt, "A 12:00:00"); 00155 00156 strcpy(fulldt, "A"); 00157 status = DtAppendTime(fulldt, ":12"); 00158 CU_ASSERT_EQUAL(status, 0); 00159 CU_ASSERT_STRING_EQUAL(fulldt, "A:12:00:00"); 00160 00161 strcpy(fulldt, "A"); 00162 status = DtAppendTime(fulldt, ":12:34"); 00163 CU_ASSERT_EQUAL(status, 0); 00164 CU_ASSERT_STRING_EQUAL(fulldt, "A:12:34:00"); 00165 00166 strcpy(fulldt, "A"); 00167 status = DtAppendTime(fulldt, ":12:34:56"); 00168 CU_ASSERT_EQUAL(status, 0); 00169 CU_ASSERT_STRING_EQUAL(fulldt, "A:12:34:56"); 00170 00171 strcpy(fulldt, "A"); 00172 status = DtAppendTime(fulldt, "*12:34:56"); /* Invalid separator */ 00173 CU_ASSERT_NOT_EQUAL(status, 0); 00174 00175 strcpy(fulldt, "A"); 00176 status = DtAppendTime(fulldt, ":1234:56"); /* Wrong length */ 00177 CU_ASSERT_NOT_EQUAL(status, 0); 00178 00179 return; 00180 } 00181 00182 00183 00184 /*+ 00185 * TestDtGeneral - Test General Date/Time 00186 * 00187 * Description: 00188 * Test date/time where specified as numeric. 00189 -*/ 00190 00191 static void TestDtGeneral(void) 00192 { 00193 int status; /* Status return */ 00194 struct tm datetime; /* Date/time structure returned */ 00195 00196 /* Valid date/time values */ 00197 00198 status = DtGeneral("2-Jan-2008 03:04:05", &datetime); 00199 CU_ASSERT_EQUAL(status, 0); 00200 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00201 00202 status = DtGeneral("02-Jan-2008 03:04:05", &datetime); 00203 CU_ASSERT_EQUAL(status, 0); 00204 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00205 00206 status = DtGeneral("02-Jan-2008:03:04:05", &datetime); 00207 CU_ASSERT_EQUAL(status, 0); 00208 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00209 00210 status = DtGeneral("02-Jan-2008:03:04", &datetime); 00211 CU_ASSERT_EQUAL(status, 0); 00212 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00213 00214 status = DtGeneral("02-Jan-2008:03", &datetime); 00215 CU_ASSERT_EQUAL(status, 0); 00216 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00217 00218 status = DtGeneral("02-Jan-2008", &datetime); 00219 CU_ASSERT_EQUAL(status, 0); 00220 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00221 00222 /* More valid date/time values */ 00223 00224 status = DtGeneral("2-01-2008 03:04:05", &datetime); 00225 CU_ASSERT_EQUAL(status, 0); 00226 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00227 00228 status = DtGeneral("02-01-2008 03:04:05", &datetime); 00229 CU_ASSERT_EQUAL(status, 0); 00230 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00231 00232 status = DtGeneral("02-01-2008:03:04:05", &datetime); 00233 CU_ASSERT_EQUAL(status, 0); 00234 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00235 00236 status = DtGeneral("02-01-2008:03:04", &datetime); 00237 CU_ASSERT_EQUAL(status, 0); 00238 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00239 00240 status = DtGeneral("02-01-2008:03", &datetime); 00241 CU_ASSERT_EQUAL(status, 0); 00242 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00243 00244 status = DtGeneral("02-01-2008", &datetime); 00245 CU_ASSERT_EQUAL(status, 0); 00246 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00247 00248 /* More valid date/time values, year first */ 00249 00250 status = DtGeneral("2008-Jan-02 03:04:05", &datetime); 00251 CU_ASSERT_EQUAL(status, 0); 00252 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00253 00254 status = DtGeneral("2008-Jan-02:03:04:05", &datetime); 00255 CU_ASSERT_EQUAL(status, 0); 00256 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00257 00258 status = DtGeneral("2008-Jan-02:03:04", &datetime); 00259 CU_ASSERT_EQUAL(status, 0); 00260 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00261 00262 status = DtGeneral("2008-Jan-02:03", &datetime); 00263 CU_ASSERT_EQUAL(status, 0); 00264 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00265 00266 status = DtGeneral("2008-Jan-02", &datetime); 00267 CU_ASSERT_EQUAL(status, 0); 00268 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00269 00270 /* More valid date/time values, year first */ 00271 00272 status = DtGeneral("2008-01-02 03:04:05", &datetime); 00273 CU_ASSERT_EQUAL(status, 0); 00274 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00275 00276 status = DtGeneral("2008-01-02:03:04:05", &datetime); 00277 CU_ASSERT_EQUAL(status, 0); 00278 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00279 00280 status = DtGeneral("2008-01-02:03:04", &datetime); 00281 CU_ASSERT_EQUAL(status, 0); 00282 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00283 00284 status = DtGeneral("2008-01-02:03", &datetime); 00285 CU_ASSERT_EQUAL(status, 0); 00286 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00287 00288 status = DtGeneral("2008-01-02", &datetime); 00289 CU_ASSERT_EQUAL(status, 0); 00290 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00291 00292 /* Some zero dates */ 00293 00294 status = DtGeneral("00-00-0000:00:00:00", &datetime); 00295 CU_ASSERT_NOT_EQUAL(status, 0); 00296 00297 status = DtGeneral("0000-00-00", &datetime); 00298 CU_ASSERT_NOT_EQUAL(status, 0); 00299 00300 status = DtGeneral("00000000", &datetime); 00301 CU_ASSERT_NOT_EQUAL(status, 0); 00302 00303 /* Some invalid dates */ 00304 00305 status = DtGeneral("13-Jan", &datetime); /* Too short */ 00306 CU_ASSERT_NOT_EQUAL(status, 0); 00307 00308 status = DtGeneral("02-Xxx-2008", &datetime); /* Month invalid */ 00309 CU_ASSERT_NOT_EQUAL(status, 0); 00310 00311 status = DtGeneral("02-Feb-2008:", &datetime); /* Trailing : */ 00312 CU_ASSERT_NOT_EQUAL(status, 0); 00313 00314 status = DtGeneral("02-Jan-2008:03-04-05", &datetime); 00315 CU_ASSERT_NOT_EQUAL(status, 0); /* Wrong separator */ 00316 00317 return; 00318 } 00319 00320 00321 00322 /*+ 00323 * TestDtGeneralStringTest - Test General String 00324 * 00325 * Description: 00326 * Individual test for TestDtGeneralString. 00327 -*/ 00328 00329 static void GeneralStringTest(const char* what, const char* expected) 00330 { 00331 char* actual; 00332 00333 actual = DtGeneralString(what); 00334 if (expected == NULL) { 00335 CU_ASSERT_PTR_NULL(actual); 00336 } 00337 else { 00338 CU_ASSERT_PTR_NOT_NULL(actual); 00339 CU_ASSERT_STRING_EQUAL(actual, expected); 00340 StrFree(actual); 00341 } 00342 00343 return; 00344 } 00345 00346 /*+ 00347 * TestDtGeneral - Test General Date/Time 00348 * 00349 * Description: 00350 * Test date/time where specified as numeric. 00351 -*/ 00352 00353 static void TestDtGeneralString(void) 00354 { 00355 /* Valid date/time values */ 00356 00357 GeneralStringTest("2-Jan-2008 03:04:05", "2008-01-02 03:04:05"); 00358 GeneralStringTest("02-Jan-2008 03:04:05", "2008-01-02 03:04:05"); 00359 GeneralStringTest("02-Jan-2008:03:04:05", "2008-01-02 03:04:05"); 00360 GeneralStringTest("02-Jan-2008:03:04", "2008-01-02 03:04:00"); 00361 GeneralStringTest("02-Jan-2008:03", "2008-01-02 03:00:00"); 00362 GeneralStringTest("02-Jan-2008", "2008-01-02 00:00:00"); 00363 00364 /* More valid date/time values */ 00365 00366 GeneralStringTest("2-01-2008 03:04:05", "2008-01-02 03:04:05"); 00367 GeneralStringTest("02-01-2008 03:04:05", "2008-01-02 03:04:05"); 00368 GeneralStringTest("02-01-2008:03:04:05", "2008-01-02 03:04:05"); 00369 GeneralStringTest("02-01-2008:03:04", "2008-01-02 03:04:00"); 00370 GeneralStringTest("02-01-2008:03", "2008-01-02 03:00:00"); 00371 GeneralStringTest("02-01-2008", "2008-01-02 00:00:00"); 00372 00373 /* More valid date/time values, year first */ 00374 00375 GeneralStringTest("2008-Jan-02 03:04:05", "2008-01-02 03:04:05"); 00376 GeneralStringTest("2008-Jan-02:03:04:05", "2008-01-02 03:04:05"); 00377 GeneralStringTest("2008-Jan-02:03:04", "2008-01-02 03:04:00"); 00378 GeneralStringTest("2008-Jan-02:03", "2008-01-02 03:00:00"); 00379 GeneralStringTest("2008-Jan-02", "2008-01-02 00:00:00"); 00380 00381 /* More valid date/time values, year first */ 00382 00383 GeneralStringTest("2008-01-02 03:04:05", "2008-01-02 03:04:05"); 00384 GeneralStringTest("2008-01-02:03:04:05", "2008-01-02 03:04:05"); 00385 GeneralStringTest("2008-01-02:03:04", "2008-01-02 03:04:00"); 00386 GeneralStringTest("2008-01-02:03", "2008-01-02 03:00:00"); 00387 GeneralStringTest("2008-01-02", "2008-01-02 00:00:00"); 00388 00389 /* Some zero dates */ 00390 00391 GeneralStringTest("00-00-0000:00:00:00", NULL); 00392 GeneralStringTest("0000-00-00", NULL); 00393 GeneralStringTest("00000000", NULL); 00394 00395 /* Some invalid dates */ 00396 00397 GeneralStringTest("13-Jan", NULL); /* Too short */ 00398 GeneralStringTest("02-Xxx-2008", NULL); /* Month invalid */ 00399 GeneralStringTest("02-Feb-2008:", NULL); /* Trailing : */ 00400 GeneralStringTest("02-Jan-2008:03-04-05", NULL); /* Wrong separator */ 00401 00402 return; 00403 } 00404 00405 00406 00407 /*+ 00408 * TestDtParseDateTime - Test Parsing Date/Time 00409 * 00410 * Description: 00411 * Test date/time where specified as general. This is just a concatenation 00412 * of the two previous tests using the general function. 00413 -*/ 00414 00415 static void TestDtParseDateTime(void) 00416 { 00417 int status; /* Status return */ 00418 struct tm datetime; /* Date/time structure returned */ 00419 00420 /* Valid date/time values. A few have leading.trailing spaces */ 00421 00422 status = DtParseDateTime(" 20080102030405 ", &datetime); 00423 CU_ASSERT_EQUAL(status, 0); 00424 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00425 00426 status = DtParseDateTime("200801020304 ", &datetime); 00427 CU_ASSERT_EQUAL(status, 0); 00428 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00429 00430 status = DtParseDateTime("2008010203", &datetime); 00431 CU_ASSERT_EQUAL(status, 0); 00432 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00433 00434 status = DtParseDateTime(" 20080102", &datetime); 00435 CU_ASSERT_EQUAL(status, 0); 00436 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00437 00438 status = DtParseDateTime("2-Jan-2008 03:04:05", &datetime); 00439 CU_ASSERT_EQUAL(status, 0); 00440 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00441 00442 status = DtParseDateTime(" 02-JAN-2008 03:04:05 ", &datetime); 00443 CU_ASSERT_EQUAL(status, 0); 00444 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00445 00446 status = DtParseDateTime("02-jan-2008:03:04:05", &datetime); 00447 CU_ASSERT_EQUAL(status, 0); 00448 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 5); 00449 00450 status = DtParseDateTime("02-Jan-2008:03:04", &datetime); 00451 CU_ASSERT_EQUAL(status, 0); 00452 CmpDtTm(&datetime, 2008, 1, 2, 3, 4, 0); 00453 00454 status = DtParseDateTime("02-Jan-2008:03", &datetime); 00455 CU_ASSERT_EQUAL(status, 0); 00456 CmpDtTm(&datetime, 2008, 1, 2, 3, 0, 0); 00457 00458 status = DtParseDateTime("02-Jan-2008", &datetime); 00459 CU_ASSERT_EQUAL(status, 0); 00460 CmpDtTm(&datetime, 2008, 1, 2, 0, 0, 0); 00461 00462 /* Some invalid dates */ 00463 00464 status = DtParseDateTime(NULL, &datetime); /* Null string */ 00465 CU_ASSERT_NOT_EQUAL(status, 0); 00466 00467 status = DtParseDateTime("", &datetime); /* Too short */ 00468 CU_ASSERT_NOT_EQUAL(status, 0); 00469 00470 status = DtParseDateTime(" ", &datetime); /* Too short */ 00471 CU_ASSERT_NOT_EQUAL(status, 0); 00472 00473 status = DtParseDateTime("2008", &datetime); /* Too short */ 00474 CU_ASSERT_NOT_EQUAL(status, 0); 00475 00476 status = DtParseDateTime("2008010203040506", &datetime); /* Too long */ 00477 CU_ASSERT_NOT_EQUAL(status, 0); 00478 00479 status = DtParseDateTime("200801020", &datetime); /* Odd no. of chars */ 00480 CU_ASSERT_NOT_EQUAL(status, 0); 00481 00482 status = DtParseDateTime("13-Jan", &datetime); /* Too short */ 00483 CU_ASSERT_NOT_EQUAL(status, 0); 00484 00485 status = DtParseDateTime("02-Xxx-2008", &datetime); /* Month invalid */ 00486 CU_ASSERT_NOT_EQUAL(status, 0); 00487 00488 status = DtParseDateTime("02-Feb-2008:", &datetime); /* Trailing : */ 00489 CU_ASSERT_NOT_EQUAL(status, 0); 00490 00491 status = DtParseDateTime("02-Jan-2008:03-04-05", &datetime); 00492 CU_ASSERT_NOT_EQUAL(status, 0); /* Wrong separator */ 00493 00494 return; 00495 } 00496 00497 00498 00499 /* 00500 * TestDtNow - Check DtNow Function 00501 * 00502 * Description: 00503 * Tests the "Now" function by getting the time twice and executing the 00504 * "Now" function between the two times. Where the fields of the 00505 * two times are the same, compare the result from the "Now" with that. 00506 -*/ 00507 00508 static void TestDtNow(void) 00509 { 00510 struct tm time1; 00511 struct tm time2; 00512 struct tm test; 00513 time_t curtime; 00514 int status; 00515 00516 (void) time(&curtime); 00517 (void) localtime_r(&curtime, &time1); 00518 status = DtNow(&test); 00519 (void) time(&curtime); 00520 (void) localtime_r(&curtime, &time2); 00521 00522 CU_ASSERT_EQUAL(status, 0); 00523 CmpDtTm(&test, 00524 time1.tm_year == time2.tm_year ? time1.tm_year + 1900 : -1, 00525 time1.tm_mon == time2.tm_mon ? time1.tm_mon + 1 : -1, 00526 time1.tm_mday == time2.tm_mday ? time1.tm_mday : -1, 00527 time1.tm_hour == time2.tm_hour ? time1.tm_hour : -1, 00528 time1.tm_min == time2.tm_min ? time1.tm_min : -1, 00529 time1.tm_sec == time2.tm_sec ? time1.tm_sec : -1 00530 ); 00531 00532 (void) time(&curtime); 00533 (void) localtime_r(&curtime, &time1); 00534 status = DtParseDateTime(" NOW ", &test); 00535 (void) time(&curtime); 00536 (void) localtime_r(&curtime, &time2); 00537 00538 CU_ASSERT_EQUAL(status, 0); 00539 CmpDtTm(&test, 00540 time1.tm_year == time2.tm_year ? time1.tm_year + 1900 : -1, 00541 time1.tm_mon == time2.tm_mon ? time1.tm_mon + 1 : -1, 00542 time1.tm_mday == time2.tm_mday ? time1.tm_mday : -1, 00543 time1.tm_hour == time2.tm_hour ? time1.tm_hour : -1, 00544 time1.tm_min == time2.tm_min ? time1.tm_min : -1, 00545 time1.tm_sec == time2.tm_sec ? time1.tm_sec : -1 00546 ); 00547 } 00548 00549 00550 00551 00552 00553 /*+ 00554 * CheckValidIntervalSeconds - Perform Test on Valid String 00555 * 00556 * Description: 00557 * Performs the tests on DtIntervalSecond son the strings that are supposed 00558 * to be valid. 00559 * 00560 * Arguments: 00561 * const char* string 00562 * String to test. 00563 * 00564 * long einterval 00565 * Expected interval. 00566 -*/ 00567 00568 static void CheckValidIntervalSeconds(const char* string, int einterval) 00569 { 00570 int interval; 00571 int status; 00572 00573 status = DtIntervalSeconds(string, &interval); 00574 CU_ASSERT_EQUAL(status, 0); 00575 CU_ASSERT_EQUAL(interval, einterval); 00576 00577 return; 00578 } 00579 00580 /*+ 00581 * TestDtIntervalSeconds - Test DtIntervalSeconds 00582 -*/ 00583 00584 static void TestDtIntervalSeconds(void) 00585 { 00586 int interval; 00587 int status; 00588 00589 /* Valid values */ 00590 00591 CheckValidIntervalSeconds("1", 1L); 00592 CheckValidIntervalSeconds("234", 234L); 00593 CheckValidIntervalSeconds("1223s", 1223L); 00594 CheckValidIntervalSeconds("1m", 60L); 00595 CheckValidIntervalSeconds("15m", 900L); 00596 CheckValidIntervalSeconds("2h", 7200L); 00597 CheckValidIntervalSeconds("24h", 86400L); 00598 CheckValidIntervalSeconds("1d", 86400L); 00599 CheckValidIntervalSeconds("7d", 604800L); 00600 CheckValidIntervalSeconds("1w", 604800L); 00601 CheckValidIntervalSeconds("52w", 31449600L); 00602 00603 /* Invalid ones */ 00604 00605 status = DtIntervalSeconds(NULL, NULL); 00606 CU_ASSERT_EQUAL(status, 4); 00607 status = DtIntervalSeconds("1d", NULL); 00608 CU_ASSERT_EQUAL(status, 4); 00609 status = DtIntervalSeconds(NULL, &interval); 00610 CU_ASSERT_EQUAL(status, 4); 00611 status = DtIntervalSeconds("", &interval); 00612 CU_ASSERT_EQUAL(status, 4); 00613 00614 status = DtIntervalSeconds("1234567890123456789012345678901234567890", 00615 &interval); 00616 CU_ASSERT_EQUAL(status, 3); 00617 status = DtIntervalSeconds("1234567890123456789012345678901", 00618 &interval); 00619 CU_ASSERT_EQUAL(status, 2); /* Overflow */ 00620 00621 status = DtIntervalSeconds("1ww", &interval); 00622 CU_ASSERT_EQUAL(status, 2); 00623 status = DtIntervalSeconds("2 2w", &interval); 00624 CU_ASSERT_EQUAL(status, 2); 00625 00626 status = DtIntervalSeconds("2a", &interval); 00627 CU_ASSERT_EQUAL(status, 1); 00628 00629 return; 00630 } 00631 00632 00633 /*+ 00634 * TestDtSecondsInterval 00635 -*/ 00636 00637 static void TestDtSecondsInterval(void) 00638 { 00639 char buffer[32]; 00640 00641 DtSecondsInterval(1209601, buffer, sizeof(buffer)); 00642 CU_ASSERT_STRING_EQUAL(buffer, "1209601s"); 00643 00644 DtSecondsInterval(1209600, buffer, sizeof(buffer)); 00645 CU_ASSERT_STRING_EQUAL(buffer, "2w"); 00646 00647 DtSecondsInterval(1209599, buffer, sizeof(buffer)); 00648 CU_ASSERT_STRING_EQUAL(buffer, "1209599s"); 00649 00650 DtSecondsInterval(259201, buffer, sizeof(buffer)); 00651 CU_ASSERT_STRING_EQUAL(buffer, "259201s"); 00652 00653 DtSecondsInterval(259200, buffer, sizeof(buffer)); 00654 CU_ASSERT_STRING_EQUAL(buffer, "3d"); 00655 00656 DtSecondsInterval(259199, buffer, sizeof(buffer)); 00657 CU_ASSERT_STRING_EQUAL(buffer, "259199s"); 00658 00659 DtSecondsInterval(14401, buffer, sizeof(buffer)); 00660 CU_ASSERT_STRING_EQUAL(buffer, "14401s"); 00661 00662 DtSecondsInterval(14400, buffer, sizeof(buffer)); 00663 CU_ASSERT_STRING_EQUAL(buffer, "4h"); 00664 00665 DtSecondsInterval(14399, buffer, sizeof(buffer)); 00666 CU_ASSERT_STRING_EQUAL(buffer, "14399s"); 00667 00668 DtSecondsInterval(301, buffer, sizeof(buffer)); 00669 CU_ASSERT_STRING_EQUAL(buffer, "301s"); 00670 00671 DtSecondsInterval(300, buffer, sizeof(buffer)); 00672 CU_ASSERT_STRING_EQUAL(buffer, "5m"); 00673 00674 DtSecondsInterval(299, buffer, sizeof(buffer)); 00675 CU_ASSERT_STRING_EQUAL(buffer, "299s"); 00676 00677 DtSecondsInterval(0, buffer, sizeof(buffer)); 00678 CU_ASSERT_STRING_EQUAL(buffer, "0s"); 00679 00680 return; 00681 } 00682 00683 00684 00685 /* 00686 * CheckDtDateDiff - Check Date Difference Code 00687 * TestDtDateDiff - Check Date Difference Code 00688 * 00689 * Arguments to CheckDtDateDiff are: 00690 * 00691 * const char* date1, const char* date2 00692 * Dates to test 00693 * 00694 * int status 00695 * Expected status return. 00696 * 00697 * int result 00698 * Expected result, only valid if status is zero. 00699 */ 00700 00701 static void CheckDtDateDiff(const char* date1, const char* date2, int status, 00702 int result) 00703 { 00704 int act_status = 0; /* Actual status */ 00705 int act_result = 0; /* Actual result */ 00706 00707 act_status = DtDateDiff(date1, date2, &act_result); 00708 CU_ASSERT_EQUAL(status, act_status); 00709 if (status == 0) { 00710 CU_ASSERT_EQUAL(result, act_result); 00711 } 00712 00713 return; 00714 } 00715 00716 static void TestDtDateDiff(void) 00717 { 00718 /* Valid dates on same day */ 00719 00720 CheckDtDateDiff("2001-01-01 00:00:02", "2001-01-01 00:00:01", 0, 1); 00721 CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-01 00:00:02", 0, -1); 00722 00723 CheckDtDateDiff("2001-01-01 00:01:02", "2001-01-01 00:00:01", 0, 61); 00724 CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-01 00:01:02", 0, -61); 00725 00726 CheckDtDateDiff("2001-01-01 02:01:02", "2001-01-01 00:00:01", 0, 7261); 00727 CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-01 02:01:02", 0, -7261); 00728 00729 CheckDtDateDiff("2001-01-02 02:01:02", "2001-01-01 00:00:01", 0, 93661); 00730 CheckDtDateDiff("2001-01-01 00:00:01", "2001-01-02 02:01:02", 0, -93661); 00731 00732 /* Invalid dates */ 00733 00734 CheckDtDateDiff(NULL, NULL, 3, 0); 00735 CheckDtDateDiff("2001-01-01 23:12:22", NULL, 3, 0); 00736 CheckDtDateDiff("2001-01-01 23:12:22", "", 3, 0); 00737 CheckDtDateDiff(NULL, "2001-01-01 23:12:22", 3, 0); 00738 CheckDtDateDiff("", "2001-01-01 23:12:22", 3, 0); 00739 CheckDtDateDiff("2001-01-01 23:12:22", "fred", 2, 0); 00740 CheckDtDateDiff("fred", "2001-01-01 23:12:22", 1, 0); 00741 } 00742 00743 /*+ 00744 * CheckValidXMLIntervalSeconds - Perform Test on Valid String 00745 * 00746 * Description: 00747 * Performs the tests on DtXMLIntervalSecond son the strings that are supposed 00748 * to be valid. 00749 * 00750 * Arguments: 00751 * const char* string 00752 * String to test. 00753 * 00754 * long einterval 00755 * Expected interval. 00756 -*/ 00757 00758 static void CheckValidXMLIntervalSeconds(const char* string, int einterval, int estatus) 00759 { 00760 int interval; 00761 int status; 00762 00763 status = DtXMLIntervalSeconds(string, &interval); 00764 CU_ASSERT_EQUAL(status, estatus); 00765 CU_ASSERT_EQUAL(interval, einterval); 00766 00767 return; 00768 } 00769 00770 /*+ 00771 * TestDtXMLIntervalSeconds - Test DtXMLIntervalSeconds 00772 -*/ 00773 00774 static void TestDtXMLIntervalSeconds(void) 00775 { 00776 int interval; 00777 int status; 00778 00779 /* Valid values, return status = 0 */ 00780 00781 /* CheckValidXMLIntervalSeconds("P1", 1L, 0);*/ 00782 status = DtXMLIntervalSeconds("P1", &interval); 00783 CU_ASSERT_EQUAL(status, 0); 00784 CU_ASSERT_EQUAL(interval, 1); 00785 00786 /* CheckValidXMLIntervalSeconds("P234", 234L, 0);*/ 00787 status = DtXMLIntervalSeconds("P234", &interval); 00788 CU_ASSERT_EQUAL(status, 0); 00789 CU_ASSERT_EQUAL(interval, 234); 00790 00791 CheckValidXMLIntervalSeconds("P1223S", 1223L, 0); 00792 CheckValidXMLIntervalSeconds("PT1M", 60L, 0); 00793 CheckValidXMLIntervalSeconds("PT15M", 900L, 0); 00794 CheckValidXMLIntervalSeconds("P2H", 7200L, 0); 00795 CheckValidXMLIntervalSeconds("PT2H", 7200L, 0); 00796 CheckValidXMLIntervalSeconds("P24H", 86400L, 0); 00797 CheckValidXMLIntervalSeconds("PT24H", 86400L, 0); 00798 CheckValidXMLIntervalSeconds("P1D", 86400L, 0); 00799 CheckValidXMLIntervalSeconds("P7D", 604800L, 0); 00800 CheckValidXMLIntervalSeconds("P1W", 604800L, 0); 00801 CheckValidXMLIntervalSeconds("P52W", 31449600L, 0); 00802 /* CheckValidXMLIntervalSeconds("-PT1M", -60L, 0);*/ 00803 status = DtXMLIntervalSeconds("-PT1M", &interval); 00804 CU_ASSERT_EQUAL(status, 0); 00805 CU_ASSERT_EQUAL(interval, -60); 00806 00807 CheckValidXMLIntervalSeconds("PT1223S", 1223L, 0); 00808 00809 /* Some mixed-format stuff */ 00810 CheckValidXMLIntervalSeconds("P1W1DT2H2M7S", 698527L, 0); 00811 CheckValidXMLIntervalSeconds("P1Y1W1DT2H2M7S", 32234527L, -1); 00812 CheckValidXMLIntervalSeconds("P1Y2M1W1DT2H2M7S", 37591327L, -1); 00813 00814 /* Valid but return -1 */ 00815 CheckValidXMLIntervalSeconds("P1M", 2678400L, -1); 00816 CheckValidXMLIntervalSeconds("P15M", 40176000L, -1); 00817 00818 /* CheckValidXMLIntervalSeconds("P1Y", 31536000L, -1); */ 00819 status = DtXMLIntervalSeconds("P1Y", &interval); 00820 CU_ASSERT_EQUAL(status, -1); 00821 CU_ASSERT_EQUAL(interval, 31536000); 00822 00823 00824 /* Invalid ones */ 00825 00826 status = DtXMLIntervalSeconds(NULL, NULL); 00827 CU_ASSERT_EQUAL(status, 4); 00828 status = DtXMLIntervalSeconds("1d", NULL); 00829 CU_ASSERT_EQUAL(status, 4); 00830 status = DtXMLIntervalSeconds(NULL, &interval); 00831 CU_ASSERT_EQUAL(status, 4); 00832 status = DtXMLIntervalSeconds("", &interval); 00833 CU_ASSERT_EQUAL(status, 4); 00834 00835 status = DtXMLIntervalSeconds("1234567890123456789012345678901234567890", 00836 &interval); 00837 CU_ASSERT_EQUAL(status, 3); 00838 status = DtXMLIntervalSeconds("1234567890123456789012345678901", 00839 &interval); 00840 CU_ASSERT_EQUAL(status, 3); /* Overflow */ 00841 00842 /* This test may work, depends on the bit-ness of the machine 00843 status = DtXMLIntervalSeconds("168Y", &interval); 00844 CU_ASSERT_EQUAL(status, 3); */ 00845 00846 status = DtXMLIntervalSeconds("1WW", &interval); 00847 CU_ASSERT_EQUAL(status, 2); 00848 status = DtXMLIntervalSeconds("2 2W", &interval); 00849 CU_ASSERT_EQUAL(status, 2); 00850 00851 status = DtXMLIntervalSeconds("2a", &interval); 00852 CU_ASSERT_EQUAL(status, 2); 00853 00854 return; 00855 } 00856 00857 /* 00858 * TestDt - Create Test Suite 00859 * 00860 * Description: 00861 * Adds the test suite to the CUnit test registry and adds all the tests 00862 * to it. 00863 * 00864 * Arguments: 00865 * None. 00866 * 00867 * Returns: 00868 * int 00869 * Return status. 0 => Success. 00870 */ 00871 00872 int TestDt(void); /* Declaration */ 00873 int TestDt(void) 00874 { 00875 struct test_testdef tests[] = { 00876 {"DtNumeric", TestDtNumeric}, 00877 {"DtAppendTime", TestDtAppendTime}, 00878 {"DtGeneral", TestDtGeneral}, 00879 {"DtGeneralString", TestDtGeneralString}, 00880 {"DtParseDateTime", TestDtParseDateTime}, 00881 {"DtNow", TestDtNow}, 00882 {"DtIntervalSeconds", TestDtIntervalSeconds}, 00883 {"DtSecondsInterval", TestDtSecondsInterval}, 00884 {"DtDateDiff", TestDtDateDiff}, 00885 {"DtXMLIntervalSeconds",TestDtXMLIntervalSeconds}, 00886 {NULL, NULL} 00887 }; 00888 00889 return TcuCreateSuite("Date/Time", NULL, NULL, tests); 00890 }