OpenDNSSEC-enforcer 1.3.0
|
00001 /* 00002 * $Id: test_string_util.c 5320 2011-07-12 10:42:26Z jakob $ 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_string_util.c 00031 * 00032 * Description: 00033 * This modules holds the unit tests for the functions in string_util.c. 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 00044 #include "CUnit/Basic.h" 00045 00046 #include "ksm/memory.h" 00047 #include "ksm/string_util.h" 00048 #include "test_routines.h" 00049 00050 00051 00052 00053 /* 00054 * TestCompare - Compare Strings 00055 * TestCompare - Compare Strings N characters 00056 * 00057 * Description: 00058 * Compares two strings. Unlike strcmp, this can cope with NULL strings, 00059 * and considers both equal if both are NULL. 00060 * 00061 * Although the CU_ASSERT_TRUE of the result could be done here, it 00062 * is actually done in the caller so that if a test fails, CUnit will 00063 * give some indication of the failing test. 00064 * 00065 * Arguments: 00066 * const char* actual (input) 00067 * The string being compared. 00068 * 00069 * const char* expected (input) 00070 * The expected value of the string. 00071 * 00072 * size_t count (TestCompareN only) 00073 * Length of sterings to compare. 00074 * 00075 * Returns: 00076 * int 00077 * 1 Strings were identical 00078 * 0 Strings were different 00079 */ 00080 00081 static int TestCompareN(const char* actual, const char* expected, size_t count) 00082 { 00083 int ok; /* Success status */ 00084 00085 if ((! actual) && (! expected)) { 00086 ok = 1; 00087 } 00088 else if (actual && (! expected)) { 00089 ok = 0; 00090 } 00091 else if ((! actual) && expected) { 00092 ok = 0; 00093 } 00094 else { 00095 ok = (memcmp(actual, expected, count) == 0); 00096 } 00097 00098 return ok; 00099 } 00100 00101 static int TestCompare(const char* actual, const char* expected) 00102 { 00103 int ok; /* Success status */ 00104 00105 if ((! actual) && (! expected)) { 00106 ok = 1; 00107 } 00108 else if (actual && (! expected)) { 00109 ok = 0; 00110 } 00111 else if ((! actual) && expected) { 00112 ok = 0; 00113 } 00114 else { 00115 ok = (strcmp(actual, expected) == 0); 00116 } 00117 00118 return ok; 00119 } 00120 00121 00122 00123 /* 00124 * TestStrXxx - Test Routines 00125 * 00126 * Description: 00127 * A set of routines, each testing one particular string utility routine. 00128 * Each utility routine is tested by two routines: 00129 * 00130 * TestStrXxx Tests utillity routine for one string 00131 * TestStrXxxExecute Calls TestStrXxx for a variety of strings. 00132 * 00133 * Arguments: 00134 * Varies. 00135 */ 00136 00137 00138 /* StrStrdup */ 00139 00140 static void TestStrStrdupExecute(const char* test) 00141 { 00142 char* testdup = StrStrdup(test); 00143 CU_ASSERT_TRUE(TestCompare(testdup, test == NULL ? "" : test)); 00144 StrFree(testdup); 00145 00146 return; 00147 } 00148 00149 static void TestStrStrdup(void) 00150 { 00151 TestStrStrdupExecute(NULL); 00152 TestStrStrdupExecute(""); 00153 TestStrStrdupExecute(" "); 00154 TestStrStrdupExecute("a test string"); 00155 00156 return; 00157 } 00158 00159 /* StrStrncpy */ 00160 00161 static void TestStrStrncpyExecute(const char* test, const char* expected, 00162 size_t destlen) 00163 { 00164 char* dest = MemMalloc(destlen); /* Create target area */ 00165 StrStrncpy(dest, test, destlen); /* Copy data */ 00166 CU_ASSERT_TRUE(TestCompare(dest, expected));/* Compare */ 00167 MemFree(dest); /* Free up memory */ 00168 } 00169 00170 static void TestStrStrncpy(void) 00171 { 00172 char dummy[100]; 00173 static const char* TEST = "A dummy string"; /* Must be < sizeof(dummy) */ 00174 00175 TestStrStrncpyExecute("alpha", "alpha", 100); /* More than enough space */ 00176 TestStrStrncpyExecute("beta", "beta", 5); /* Enough space */ 00177 TestStrStrncpyExecute("gamma", "gamm", 5); /* 1 character too small */ 00178 TestStrStrncpyExecute("delta", "d", 2); /* Very small */ 00179 TestStrStrncpyExecute("epsilon", "", 1); /* Minimum possible */ 00180 00181 /* Finally some tests on what should be no-ops */ 00182 00183 strcpy(dummy, TEST); 00184 StrStrncpy(dummy, NULL, 100); 00185 CU_ASSERT_STRING_EQUAL(dummy, ""); 00186 00187 strcpy(dummy, TEST); 00188 StrStrncpy(dummy, "xyz", 0); 00189 CU_ASSERT_STRING_EQUAL(dummy, TEST); 00190 00191 /* 00192 * The final check tests that the routine does not generate a segmentation 00193 * fault if the destination is NULL. 00194 */ 00195 00196 StrStrncpy(NULL, "xyz", 52); 00197 00198 return; 00199 } 00200 00201 /* StrStrncat */ 00202 00203 static void TestStrStrncatExecute(const char* dst, const char* src, 00204 size_t dstlen, const char* expected) 00205 { 00206 char* newdst = NULL; 00207 00208 if (dst) { 00209 newdst = MemMalloc(dstlen); /* Create target area */ 00210 StrStrncpy(newdst, dst, dstlen); /* Copy data */ 00211 } 00212 StrStrncat(newdst, src, dstlen); 00213 CU_ASSERT_TRUE(TestCompare(newdst, expected));/* Compare */ 00214 00215 MemFree(newdst); /* Free up memory */ 00216 } 00217 00218 static void TestStrStrncat(void) 00219 { 00220 TestStrStrncatExecute("alpha", "beta", 100, "alphabeta"); 00221 TestStrStrncatExecute("alpha", "beta", 6, "alpha"); 00222 TestStrStrncatExecute("alpha", "beta", 7, "alphab"); 00223 TestStrStrncatExecute("alpha", "beta", 8, "alphabe"); 00224 TestStrStrncatExecute("alpha", "beta", 9, "alphabet"); 00225 TestStrStrncatExecute("alpha", "beta", 10, "alphabeta"); 00226 TestStrStrncatExecute("alpha", "beta", 11, "alphabeta"); 00227 00228 TestStrStrncatExecute("alpha ", "beta", 9, "alpha be"); 00229 TestStrStrncatExecute("alpha ", "beta", 10, "alpha bet"); 00230 TestStrStrncatExecute("alpha ", "beta", 11, "alpha beta"); 00231 TestStrStrncatExecute("alpha ", "beta", 12, "alpha beta"); 00232 00233 TestStrStrncatExecute("", "beta", 1, ""); 00234 TestStrStrncatExecute("", "beta", 2, "b"); 00235 TestStrStrncatExecute("", "beta", 3, "be"); 00236 TestStrStrncatExecute("", "beta", 4, "bet"); 00237 TestStrStrncatExecute("", "beta", 5, "beta"); 00238 TestStrStrncatExecute("", "beta", 6, "beta"); 00239 00240 TestStrStrncatExecute(NULL, "gamma", 6, NULL); 00241 00242 return; 00243 } 00244 00245 /* StrUncomment */ 00246 00247 static void TestStrUncommentExecute(const char* test, const char* expected) 00248 { 00249 char* testdup = test ? strdup(test) : NULL; 00250 00251 StrUncomment(testdup); 00252 CU_ASSERT_TRUE(TestCompare(testdup, expected)); 00253 00254 free(testdup); 00255 00256 return; 00257 } 00258 00259 static void TestStrUncomment(void) 00260 { 00261 TestStrUncommentExecute(NULL, NULL); 00262 TestStrUncommentExecute("", ""); 00263 TestStrUncommentExecute(" \t ", " \t "); 00264 TestStrUncommentExecute("This is a string with a #comment", 00265 "This is a string with a "); 00266 TestStrUncommentExecute("This is a string with a # ## comment", 00267 "This is a string with a "); 00268 TestStrUncommentExecute("#This is a leading comment", ""); 00269 TestStrUncommentExecute("\t\t#comment", "\t\t"); 00270 TestStrUncommentExecute("A string with no comment", 00271 "A string with no comment"); 00272 00273 return; 00274 } 00275 00276 /* StrWhitespace */ 00277 00278 static void TestStrWhitespaceExecute(const char* test, const char* expected) 00279 { 00280 char* testdup = test ? strdup(test) : NULL; 00281 00282 StrWhitespace(testdup); 00283 CU_ASSERT_TRUE(TestCompare(testdup, expected)); 00284 00285 free(testdup); 00286 00287 return; 00288 } 00289 00290 static void TestStrWhitespace(void) 00291 { 00292 TestStrWhitespaceExecute(NULL, NULL); 00293 TestStrWhitespaceExecute("", ""); 00294 TestStrWhitespaceExecute(" \t ", " "); 00295 TestStrWhitespaceExecute(" \r\n", " "); 00296 TestStrWhitespaceExecute("A\tstring\twith\tembedded\ttabs", 00297 "A string with embedded tabs"); 00298 TestStrWhitespaceExecute("no_whitespace", "no_whitespace"); 00299 TestStrWhitespaceExecute("\r\nwhitespace\t\t", " whitespace "); 00300 00301 return; 00302 } 00303 00304 /* StrTrimR */ 00305 00306 static void TestStrTrimRExecute(const char* test, const char* expected) 00307 { 00308 char* testdup = test ? strdup(test) : NULL; 00309 00310 StrTrimR(testdup); 00311 CU_ASSERT_TRUE(TestCompare(testdup, expected)); 00312 00313 free(testdup); 00314 00315 return; 00316 } 00317 00318 static void TestStrTrimR(void) 00319 { 00320 TestStrTrimRExecute(NULL, NULL); 00321 TestStrTrimRExecute("", ""); 00322 TestStrTrimRExecute("\t\tabc", "\t\tabc"); 00323 TestStrTrimRExecute("abc\t\t", "abc"); 00324 TestStrTrimRExecute(" alpha ", " alpha"); 00325 TestStrTrimRExecute(" alpha beta\n", " alpha beta"); 00326 00327 return; 00328 } 00329 00330 /* StrTrimL */ 00331 00332 static void TestStrTrimLExecute(const char* test, const char* expected) 00333 { 00334 char* testdup = test ? strdup(test) : NULL; 00335 00336 char* trimmed = StrTrimL(testdup); 00337 CU_ASSERT_TRUE(TestCompare(trimmed, expected)); 00338 00339 free(testdup); 00340 00341 return; 00342 } 00343 00344 static void TestStrTrimL(void) 00345 { 00346 TestStrTrimLExecute(NULL, NULL); 00347 TestStrTrimLExecute("", ""); 00348 TestStrTrimLExecute("\t\tabc", "abc"); 00349 TestStrTrimLExecute("abc\t\t", "abc\t\t"); 00350 TestStrTrimLExecute(" alpha ", "alpha "); 00351 TestStrTrimLExecute(" alpha beta\n", "alpha beta\n"); 00352 00353 return; 00354 } 00355 00356 /* StrTrim */ 00357 00358 static void TestStrTrimExecute(const char* test, const char* expected) 00359 { 00360 char* testdup = test ? strdup(test) : NULL; 00361 00362 char* modstr = StrTrim(testdup); 00363 CU_ASSERT_TRUE(TestCompare(modstr, expected)); 00364 00365 free(testdup); 00366 00367 return; 00368 } 00369 00370 static void TestStrTrim(void) 00371 { 00372 TestStrTrimExecute(NULL, NULL); 00373 TestStrTrimExecute("", ""); 00374 TestStrTrimExecute("\t\tabc", "abc"); 00375 TestStrTrimExecute("abc\t\t", "abc"); 00376 TestStrTrimExecute(" alpha ", "alpha"); 00377 TestStrTrimExecute(" alpha beta\n", "alpha beta"); 00378 00379 return; 00380 } 00381 00382 /* StrToLower */ 00383 00384 static void TestStrToLowerExecute(const char* test, const char* expected) 00385 { 00386 char* testdup = test ? strdup(test) : NULL; 00387 00388 size_t length = StrToLower(testdup); 00389 CU_ASSERT_TRUE(TestCompare(testdup, expected)); 00390 if (test) { 00391 CU_ASSERT_EQUAL(length, strlen(expected)); 00392 } 00393 else { 00394 CU_ASSERT_EQUAL(length, 0); 00395 } 00396 00397 free(testdup); 00398 00399 return; 00400 } 00401 00402 static void TestStrToLower(void) 00403 { 00404 TestStrToLowerExecute(NULL, NULL); 00405 TestStrToLowerExecute("abc", "abc"); 00406 TestStrToLowerExecute("ABC", "abc"); 00407 TestStrToLowerExecute("AbC", "abc"); 00408 TestStrToLowerExecute("AbC d e F", "abc d e f"); 00409 00410 return; 00411 } 00412 00413 00414 /* StrToUpper */ 00415 00416 static void TestStrToUpperExecute(const char* test, const char* expected) 00417 { 00418 char* testdup = test ? strdup(test) : NULL; 00419 00420 size_t length = StrToUpper(testdup); 00421 CU_ASSERT_TRUE(TestCompare(testdup, expected)); 00422 if (test) { 00423 CU_ASSERT_EQUAL(length, strlen(expected)); 00424 } 00425 else { 00426 CU_ASSERT_EQUAL(length, 0); 00427 } 00428 00429 free(testdup); 00430 00431 return; 00432 } 00433 00434 static void TestStrToUpper(void) 00435 { 00436 TestStrToUpperExecute(NULL, NULL); 00437 TestStrToUpperExecute("abc", "ABC"); 00438 TestStrToUpperExecute("ABC", "ABC"); 00439 TestStrToUpperExecute("AbC", "ABC"); 00440 TestStrToUpperExecute("AbC d e F", "ABC D E F"); 00441 00442 return; 00443 } 00444 00445 00446 /* StrReplaceChar */ 00447 00448 static void TestStrReplaceCharExecute(const char* test, const char* expected, 00449 char search, char replace, int expected_count) 00450 { 00451 char* testdup = test ? strdup(test) : NULL; 00452 00453 int count = StrReplaceChar(testdup, search, replace); 00454 CU_ASSERT_TRUE(TestCompare(testdup, expected)); 00455 CU_ASSERT_EQUAL(count, expected_count); 00456 00457 free(testdup); 00458 00459 return; 00460 } 00461 00462 static void TestStrReplaceChar(void) 00463 { 00464 TestStrReplaceCharExecute(NULL, NULL, 'a', 'b', 0); 00465 TestStrReplaceCharExecute("ABCDEF", "ABCDEF", 'a', 'b', 0); 00466 TestStrReplaceCharExecute(",abc", "@abc", ',', '@', 1); 00467 TestStrReplaceCharExecute("abc,", "abc@", ',', '@', 1); 00468 TestStrReplaceCharExecute(",abc,", "@abc@", ',', '@', 2); 00469 TestStrReplaceCharExecute("ab,c", "ab@c", ',', '@', 1); 00470 TestStrReplaceCharExecute("abacadae", "ebecedee", 'a', 'e', 4); 00471 00472 return; 00473 } 00474 00475 00476 /* StrReplaceCharN */ 00477 00478 static void TestStrReplaceCharNExecute(const char* test, size_t testlen, 00479 const char* expected, char search, char replace, int expected_count) 00480 { 00481 int count = 0; /* Replacement count */ 00482 char* testdup = NULL; /* String copy */ 00483 00484 if (test) { 00485 testdup = MemMalloc(testlen + 1); 00486 memcpy(testdup, test, testlen); 00487 testdup[testlen] = '\0'; 00488 } 00489 00490 count = StrReplaceCharN(testdup, testlen, search, replace); 00491 CU_ASSERT_TRUE(TestCompareN(testdup, expected, testlen)); 00492 CU_ASSERT_EQUAL(count, expected_count); 00493 if (testdup) { 00494 MemFree(testdup); 00495 } 00496 00497 return; 00498 } 00499 00500 static void TestStrReplaceCharN(void) 00501 { 00502 TestStrReplaceCharNExecute(NULL, 5, NULL, 'a', 'b', 0); 00503 TestStrReplaceCharNExecute("ABCDEF", 6, "ABCDEF", 'a', 'b', 0); 00504 TestStrReplaceCharNExecute("ABCDEF", 6, "BBCDEF", 'A', 'B', 1); 00505 TestStrReplaceCharNExecute("ABC\0EF", 6, "ABCCEF", '\0', 'C', 1); 00506 TestStrReplaceCharNExecute("ABC\0EF\0", 7, "ABCCEFC", '\0', 'C', 2); 00507 TestStrReplaceCharNExecute("\0", 1, " ", '\0', ' ', 1); 00508 00509 return; 00510 } 00511 00512 00513 /* StrTrimmedLength */ 00514 00515 static void TestStrTrimmedLengthExecute(const char* test, size_t expected) 00516 { 00517 CU_ASSERT_EQUAL(StrTrimmedLength(test), expected); 00518 00519 return; 00520 } 00521 00522 static void TestStrTrimmedLength(void) 00523 { 00524 TestStrTrimmedLengthExecute(NULL, 0); 00525 TestStrTrimmedLengthExecute("", 0); 00526 TestStrTrimmedLengthExecute(" ", 0); 00527 TestStrTrimmedLengthExecute("\n\n\r\t", 0); 00528 TestStrTrimmedLengthExecute("abc", 3); 00529 TestStrTrimmedLengthExecute(" abc", 3); 00530 TestStrTrimmedLengthExecute("defg \n", 4); 00531 TestStrTrimmedLengthExecute("\t\tabcdef\t ", 6); 00532 TestStrTrimmedLengthExecute(" abcdefg ", 7); 00533 TestStrTrimmedLengthExecute(" a b c d e f ", 11); 00534 TestStrTrimmedLengthExecute(" a\r\tb", 4); 00535 TestStrTrimmedLengthExecute(" xy zzy ", 6); 00536 00537 return; 00538 } 00539 00540 00541 /* 00542 * TestStr - Create Test Suite 00543 * 00544 * Description: 00545 * Adds the string test suite to the CUnit test registry 00546 * and adds all the tests to it. 00547 * 00548 * Arguments: 00549 * None. 00550 * 00551 * Returns: 00552 * int 00553 * Return status. 0 => Success. 00554 */ 00555 00556 int TestStr(void); /* Declaration */ 00557 int TestStr(void) 00558 { 00559 struct test_testdef tests[] = { 00560 {"StrReplaceCharN", TestStrReplaceCharN}, 00561 {"StrReplaceChar", TestStrReplaceChar}, 00562 {"StrStrdup", TestStrStrdup}, 00563 {"StrStrncpy", TestStrStrncpy}, 00564 {"StrStrncat", TestStrStrncat}, 00565 {"StrToLower", TestStrToLower}, 00566 {"StrToUpper", TestStrToUpper}, 00567 {"StrTrimL", TestStrTrimL}, 00568 {"StrTrimR", TestStrTrimR}, 00569 {"StrTrim", TestStrTrim}, 00570 {"StrTrimmedLength",TestStrTrimmedLength}, 00571 {"StrUncomment", TestStrUncomment}, 00572 {"StrWhitespace", TestStrWhitespace}, 00573 {NULL, NULL} 00574 }; 00575 00576 return TcuCreateSuite("String Utility", NULL, NULL, tests); 00577 }