OpenDNSSEC-enforcer 1.3.0
|
00001 /* 00002 * $Id: test_string_util2.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_util2.c 00031 * 00032 * Description: 00033 * This module holds the unit tests for the functions in the string_util2.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 "ksm/string_util2.h" 00049 #include "test_routines.h" 00050 00051 00052 00053 00054 /* 00055 * TestStrAppend - Test StrAppend 00056 * 00057 * Description: 00058 * Tests the dynamic string append function. 00059 -*/ 00060 00061 static void TestStrAppend() 00062 { 00063 char* result; 00064 00065 /* Check that the code doesn't fall over if passed a null target */ 00066 00067 StrAppend(NULL, NULL); 00068 StrAppend(NULL, "something"); 00069 00070 /* Check the result of trying to append a null string */ 00071 00072 result = NULL; 00073 StrAppend(&result, NULL); 00074 CU_ASSERT_PTR_NULL(result); 00075 00076 /* Now appending to a null string */ 00077 00078 StrAppend(&result, "xyzzy"); 00079 CU_ASSERT_STRING_EQUAL(result, "xyzzy"); 00080 00081 /* Now append to a fixed string */ 00082 00083 result = StrStrdup("xyzzy"); 00084 StrAppend(&result, NULL); 00085 CU_ASSERT_STRING_EQUAL(result, "xyzzy"); 00086 StrAppend(&result, ""); 00087 CU_ASSERT_STRING_EQUAL(result, "xyzzy"); 00088 StrAppend(&result, "plugh"); 00089 CU_ASSERT_STRING_EQUAL(result, "xyzzyplugh"); 00090 00091 /* ... and check that we can append to an empty string */ 00092 00093 StrFree(result); 00094 result = StrStrdup(""); 00095 StrAppend(&result, "xyzzy"); 00096 CU_ASSERT_STRING_EQUAL(result, "xyzzy"); 00097 StrFree(result); 00098 } 00099 00100 00101 00102 /*+ 00103 * TestArglistAddFree - Test Arglist Add and Free 00104 * 00105 * Description: 00106 * Test the addition of elements to the argument list. 00107 -*/ 00108 00109 static void TestStrArglistAddFree() 00110 { 00111 char** argv = NULL; 00112 00113 /* Add the first string */ 00114 00115 StrArglistAdd(&argv, "alpha"); 00116 CU_ASSERT_PTR_NOT_NULL(argv); 00117 CU_ASSERT_PTR_NOT_NULL(argv[0]); 00118 CU_ASSERT_PTR_NULL(argv[1]); 00119 CU_ASSERT_STRING_EQUAL(argv[0], "alpha"); 00120 00121 /* ...a second */ 00122 00123 StrArglistAdd(&argv, "beta"); 00124 CU_ASSERT_PTR_NOT_NULL(argv); 00125 CU_ASSERT_PTR_NOT_NULL(argv[0]); 00126 CU_ASSERT_PTR_NOT_NULL(argv[1]); 00127 CU_ASSERT_PTR_NULL(argv[2]); 00128 CU_ASSERT_STRING_EQUAL(argv[0], "alpha"); 00129 CU_ASSERT_STRING_EQUAL(argv[1], "beta"); 00130 00131 /* ... and a third */ 00132 00133 StrArglistAdd(&argv, "gamma"); 00134 CU_ASSERT_PTR_NOT_NULL(argv); 00135 CU_ASSERT_PTR_NOT_NULL(argv[0]); 00136 CU_ASSERT_PTR_NOT_NULL(argv[1]); 00137 CU_ASSERT_PTR_NOT_NULL(argv[2]); 00138 CU_ASSERT_PTR_NULL(argv[3]); 00139 CU_ASSERT_STRING_EQUAL(argv[0], "alpha"); 00140 CU_ASSERT_STRING_EQUAL(argv[1], "beta"); 00141 CU_ASSERT_STRING_EQUAL(argv[2], "gamma"); 00142 00143 /* Free up the data */ 00144 00145 StrArglistFree(&argv); 00146 CU_ASSERT_PTR_NULL(argv); 00147 00148 return; 00149 } 00150 00151 00152 /*+ 00153 * TestStrArglistCreate - Check Argument List Creation 00154 */ 00155 00156 static void TestStrArglistCreate() 00157 { 00158 char** argv; 00159 00160 /* Check with the corner cases - null and empty strings first */ 00161 00162 argv = NULL; 00163 argv = StrArglistCreate(NULL); 00164 CU_ASSERT_PTR_NOT_NULL(argv); 00165 CU_ASSERT_PTR_NULL(argv[0]); 00166 StrArglistFree(&argv); 00167 CU_ASSERT_PTR_NULL(argv); 00168 00169 argv = StrArglistCreate(" "); 00170 CU_ASSERT_PTR_NOT_NULL(argv); 00171 CU_ASSERT_PTR_NULL(argv[0]); 00172 StrArglistFree(&argv); 00173 CU_ASSERT_PTR_NULL(argv); 00174 00175 argv = StrArglistCreate(" \n\t "); 00176 CU_ASSERT_PTR_NOT_NULL(argv); 00177 CU_ASSERT_PTR_NULL(argv[0]); 00178 StrArglistFree(&argv); 00179 CU_ASSERT_PTR_NULL(argv); 00180 00181 /* Now check with command lines */ 00182 00183 argv= StrArglistCreate(" list zone -f -c co.uk "); 00184 CU_ASSERT_PTR_NOT_NULL(argv); 00185 CU_ASSERT_PTR_NOT_NULL(argv[0]); 00186 CU_ASSERT_STRING_EQUAL(argv[0], "list"); 00187 CU_ASSERT_PTR_NOT_NULL(argv[1]); 00188 CU_ASSERT_STRING_EQUAL(argv[1], "zone"); 00189 CU_ASSERT_PTR_NOT_NULL(argv[2]); 00190 CU_ASSERT_STRING_EQUAL(argv[2], "-f"); 00191 CU_ASSERT_PTR_NOT_NULL(argv[3]); 00192 CU_ASSERT_STRING_EQUAL(argv[3], "-c"); 00193 CU_ASSERT_PTR_NOT_NULL(argv[4]); 00194 CU_ASSERT_STRING_EQUAL(argv[4], "co.uk"); 00195 CU_ASSERT_PTR_NULL(argv[5]); 00196 StrArglistFree(&argv); 00197 CU_ASSERT_PTR_NULL(argv); 00198 00199 argv= StrArglistCreate("add signature -z co.uk\t-d alpha.dat\tfred"); 00200 CU_ASSERT_PTR_NOT_NULL(argv); 00201 CU_ASSERT_PTR_NOT_NULL(argv[0]); 00202 CU_ASSERT_STRING_EQUAL(argv[0], "add"); 00203 CU_ASSERT_PTR_NOT_NULL(argv[1]); 00204 CU_ASSERT_STRING_EQUAL(argv[1], "signature"); 00205 CU_ASSERT_PTR_NOT_NULL(argv[2]); 00206 CU_ASSERT_STRING_EQUAL(argv[2], "-z"); 00207 CU_ASSERT_PTR_NOT_NULL(argv[3]); 00208 CU_ASSERT_STRING_EQUAL(argv[3], "co.uk"); 00209 CU_ASSERT_PTR_NOT_NULL(argv[4]); 00210 CU_ASSERT_STRING_EQUAL(argv[4], "-d"); 00211 CU_ASSERT_PTR_NOT_NULL(argv[5]); 00212 CU_ASSERT_STRING_EQUAL(argv[5], "alpha.dat"); 00213 CU_ASSERT_PTR_NOT_NULL(argv[6]); 00214 CU_ASSERT_STRING_EQUAL(argv[6], "fred"); 00215 CU_ASSERT_PTR_NULL(argv[7]); 00216 StrArglistFree(&argv); 00217 CU_ASSERT_PTR_NULL(argv); 00218 } 00219 00220 00221 /*+ 00222 * TestStrKeywordSearch - Test Keyword Search Function 00223 -*/ 00224 00225 static void TestStrKeywordSearch() 00226 { 00227 STR_KEYWORD_ELEMENT keywords[] = { 00228 {"alpha", 5}, 00229 {"alpine", 10}, 00230 {"beta", 15}, 00231 {"gamma", 20} 00232 }; 00233 int status; /* Status return */ 00234 int value; /* Return value */ 00235 00236 status = StrKeywordSearch("alpha", keywords, &value); 00237 CU_ASSERT_EQUAL(status, 0); 00238 CU_ASSERT_EQUAL(value, 5); 00239 00240 status = StrKeywordSearch("beta", keywords, &value); 00241 CU_ASSERT_EQUAL(status, 0); 00242 CU_ASSERT_EQUAL(value, 15); 00243 00244 status = StrKeywordSearch("alp", keywords, &value); 00245 CU_ASSERT_EQUAL(status, -2); 00246 00247 status = StrKeywordSearch("xyz", keywords, &value); 00248 CU_ASSERT_EQUAL(status, -1); 00249 00250 status = StrKeywordSearch("", keywords, &value); 00251 CU_ASSERT_EQUAL(status, -2); 00252 00253 status = StrKeywordSearch(NULL, keywords, &value); 00254 CU_ASSERT_EQUAL(status, -1); 00255 00256 return; 00257 } 00258 00259 /*+ 00260 * TestStrStrtol - Test String to Long Conversion 00261 -*/ 00262 00263 static void TestStrStrtol() 00264 { 00265 int status; 00266 long value; 00267 00268 status = StrStrtol("23", &value); 00269 CU_ASSERT_EQUAL(status, 0); 00270 CU_ASSERT_EQUAL(value, 23L); 00271 00272 status = StrStrtol(" 34 ", &value); 00273 CU_ASSERT_EQUAL(status, 0); 00274 CU_ASSERT_EQUAL(value, 34L); 00275 00276 status = StrStrtol("56\t", &value); 00277 CU_ASSERT_EQUAL(status, 0); 00278 CU_ASSERT_EQUAL(value, 56L); 00279 00280 status = StrStrtol("\t-67\t", &value); 00281 CU_ASSERT_EQUAL(status, 0); 00282 CU_ASSERT_EQUAL(value, -67L); 00283 00284 status = StrStrtol(" 7 8 ", &value); 00285 CU_ASSERT_NOT_EQUAL(status, 0); 00286 00287 status = StrStrtol(" 7a ", &value); 00288 CU_ASSERT_NOT_EQUAL(status, 0); 00289 00290 status = StrStrtol(" ", &value); 00291 CU_ASSERT_NOT_EQUAL(status, 0); 00292 00293 status = StrStrtol(NULL, &value); 00294 CU_ASSERT_NOT_EQUAL(status, 0); 00295 00296 return; 00297 } 00298 00299 00300 00301 /*+ 00302 * TestStrStrtoul - Test String to Unsigned Long Conversion 00303 -*/ 00304 00305 static void TestStrStrtoul() 00306 { 00307 int status; 00308 unsigned long value; 00309 union { /* For testing the reading of signed values */ 00310 long slong; 00311 unsigned long ulong; 00312 } combined; 00313 00314 status = StrStrtoul("23", &value); 00315 CU_ASSERT_EQUAL(status, 0); 00316 CU_ASSERT_EQUAL(value, 23L); 00317 00318 status = StrStrtoul(" 34 ", &value); 00319 CU_ASSERT_EQUAL(status, 0); 00320 CU_ASSERT_EQUAL(value, 34L); 00321 00322 status = StrStrtoul("56\t", &value); 00323 CU_ASSERT_EQUAL(status, 0); 00324 CU_ASSERT_EQUAL(value, 56L); 00325 00326 status = StrStrtoul("\t-1\t", &value); 00327 CU_ASSERT_EQUAL(status, 0); 00328 combined.ulong = value; 00329 CU_ASSERT_EQUAL(combined.slong, -1); 00330 00331 status = StrStrtoul("\t-277983\t", &value); 00332 CU_ASSERT_EQUAL(status, 0); 00333 combined.ulong = value; 00334 CU_ASSERT_EQUAL(combined.slong, -277983); 00335 00336 status = StrStrtoul(" 7 8 ", &value); 00337 CU_ASSERT_NOT_EQUAL(status, 0); 00338 00339 status = StrStrtoul(" 7a ", &value); 00340 CU_ASSERT_NOT_EQUAL(status, 0); 00341 00342 status = StrStrtoul(" ", &value); 00343 CU_ASSERT_NOT_EQUAL(status, 0); 00344 00345 status = StrStrtoul(NULL, &value); 00346 CU_ASSERT_NOT_EQUAL(status, 0); 00347 00348 return; 00349 } 00350 00351 /*+ 00352 * TestStrStrtoi - Test String to Integer Conversion 00353 -*/ 00354 00355 static void TestStrStrtoi() 00356 { 00357 int status; 00358 int value; 00359 00360 status = StrStrtoi("23", &value); 00361 CU_ASSERT_EQUAL(status, 0); 00362 CU_ASSERT_EQUAL(value, 23); 00363 00364 status = StrStrtoi(" 34 ", &value); 00365 CU_ASSERT_EQUAL(status, 0); 00366 CU_ASSERT_EQUAL(value, 34); 00367 00368 status = StrStrtoi("56\t", &value); 00369 CU_ASSERT_EQUAL(status, 0); 00370 CU_ASSERT_EQUAL(value, 56); 00371 00372 status = StrStrtoi("\t-67\t", &value); 00373 CU_ASSERT_EQUAL(status, 0); 00374 CU_ASSERT_EQUAL(value, -67); 00375 00376 status = StrStrtoi(" 7 8 ", &value); 00377 CU_ASSERT_NOT_EQUAL(status, 0); 00378 00379 status = StrStrtoi(" 7a ", &value); 00380 CU_ASSERT_NOT_EQUAL(status, 0); 00381 00382 status = StrStrtoi(" ", &value); 00383 CU_ASSERT_NOT_EQUAL(status, 0); 00384 00385 status = StrStrtoi(NULL, &value); 00386 CU_ASSERT_NOT_EQUAL(status, 0); 00387 00388 return; 00389 } 00390 00391 /*+ 00392 * TestStrIsSigits - Test StrIsDigits 00393 -*/ 00394 00395 static void TestStrIsDigits() 00396 { 00397 CU_ASSERT_NOT_EQUAL(StrIsDigits("1234567"), 0); 00398 CU_ASSERT_NOT_EQUAL(StrIsDigits("17"), 0); 00399 00400 CU_ASSERT_EQUAL(StrIsDigits(" 17"), 0); 00401 CU_ASSERT_EQUAL(StrIsDigits("1 7"), 0); 00402 CU_ASSERT_EQUAL(StrIsDigits("17 "), 0); 00403 CU_ASSERT_EQUAL(StrIsDigits("A"), 0); 00404 CU_ASSERT_EQUAL(StrIsDigits("\t"), 0); 00405 CU_ASSERT_EQUAL(StrIsDigits(""), 0); 00406 CU_ASSERT_EQUAL(StrIsDigits(NULL), 0); 00407 00408 return; 00409 } 00410 00411 /*+ 00412 * TestStr2 - Create Test Suite 00413 * 00414 * Description: 00415 * Adds the string test suite to the CUnit test registry 00416 * and adds all the tests to it. 00417 * 00418 * Arguments: 00419 * None. 00420 * 00421 * Returns: 00422 * int 00423 * Return status. 0 => Success. 00424 -*/ 00425 00426 int TestStr2(void); /* Declaration */ 00427 int TestStr2(void) 00428 { 00429 struct test_testdef tests[] = { 00430 {"StrAppend", TestStrAppend}, 00431 {"StrArglistAddFree", TestStrArglistAddFree}, 00432 {"StrArglistCreate", TestStrArglistCreate}, 00433 {"StrKeywordSearch", TestStrKeywordSearch}, 00434 {"StrStrtol", TestStrStrtol}, 00435 {"StrStrtoul", TestStrStrtoul}, 00436 {"StrStrtoi", TestStrStrtoi}, 00437 {"StrIsDigits", TestStrIsDigits}, 00438 {NULL, NULL} 00439 }; 00440 00441 return TcuCreateSuite("String Utility2", NULL, NULL, tests); 00442 }