OpenDNSSEC-enforcer  1.3.15
test_string_util2.c
Go to the documentation of this file.
1 /*
2  * $Id: test_string_util2.c 3811 2010-08-26 15:05:19Z jakob $
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_string_util2.c
31  *
32  * Description:
33  * This module holds the unit tests for the functions in the string_util2.c
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 
44 #include "CUnit/Basic.h"
45 
46 #include "ksm/memory.h"
47 #include "ksm/string_util.h"
48 #include "ksm/string_util2.h"
49 #include "test_routines.h"
50 
51 
52 
53 
54 /*
55  * TestStrAppend - Test StrAppend
56  *
57  * Description:
58  * Tests the dynamic string append function.
59 -*/
60 
61 static void TestStrAppend()
62 {
63  char* result;
64 
65  /* Check that the code doesn't fall over if passed a null target */
66 
67  StrAppend(NULL, NULL);
68  StrAppend(NULL, "something");
69 
70  /* Check the result of trying to append a null string */
71 
72  result = NULL;
73  StrAppend(&result, NULL);
74  CU_ASSERT_PTR_NULL(result);
75 
76  /* Now appending to a null string */
77 
78  StrAppend(&result, "xyzzy");
79  CU_ASSERT_STRING_EQUAL(result, "xyzzy");
80 
81  /* Now append to a fixed string */
82 
83  result = StrStrdup("xyzzy");
84  StrAppend(&result, NULL);
85  CU_ASSERT_STRING_EQUAL(result, "xyzzy");
86  StrAppend(&result, "");
87  CU_ASSERT_STRING_EQUAL(result, "xyzzy");
88  StrAppend(&result, "plugh");
89  CU_ASSERT_STRING_EQUAL(result, "xyzzyplugh");
90 
91  /* ... and check that we can append to an empty string */
92 
93  StrFree(result);
94  result = StrStrdup("");
95  StrAppend(&result, "xyzzy");
96  CU_ASSERT_STRING_EQUAL(result, "xyzzy");
97  StrFree(result);
98 }
99 
100 
101 
102 /*+
103  * TestArglistAddFree - Test Arglist Add and Free
104  *
105  * Description:
106  * Test the addition of elements to the argument list.
107 -*/
108 
109 static void TestStrArglistAddFree()
110 {
111  char** argv = NULL;
112 
113  /* Add the first string */
114 
115  StrArglistAdd(&argv, "alpha");
116  CU_ASSERT_PTR_NOT_NULL(argv);
117  CU_ASSERT_PTR_NOT_NULL(argv[0]);
118  CU_ASSERT_PTR_NULL(argv[1]);
119  CU_ASSERT_STRING_EQUAL(argv[0], "alpha");
120 
121  /* ...a second */
122 
123  StrArglistAdd(&argv, "beta");
124  CU_ASSERT_PTR_NOT_NULL(argv);
125  CU_ASSERT_PTR_NOT_NULL(argv[0]);
126  CU_ASSERT_PTR_NOT_NULL(argv[1]);
127  CU_ASSERT_PTR_NULL(argv[2]);
128  CU_ASSERT_STRING_EQUAL(argv[0], "alpha");
129  CU_ASSERT_STRING_EQUAL(argv[1], "beta");
130 
131  /* ... and a third */
132 
133  StrArglistAdd(&argv, "gamma");
134  CU_ASSERT_PTR_NOT_NULL(argv);
135  CU_ASSERT_PTR_NOT_NULL(argv[0]);
136  CU_ASSERT_PTR_NOT_NULL(argv[1]);
137  CU_ASSERT_PTR_NOT_NULL(argv[2]);
138  CU_ASSERT_PTR_NULL(argv[3]);
139  CU_ASSERT_STRING_EQUAL(argv[0], "alpha");
140  CU_ASSERT_STRING_EQUAL(argv[1], "beta");
141  CU_ASSERT_STRING_EQUAL(argv[2], "gamma");
142 
143  /* Free up the data */
144 
145  StrArglistFree(&argv);
146  CU_ASSERT_PTR_NULL(argv);
147 
148  return;
149 }
150 
151 
152 /*+
153  * TestStrArglistCreate - Check Argument List Creation
154  */
155 
156 static void TestStrArglistCreate()
157 {
158  char** argv;
159 
160  /* Check with the corner cases - null and empty strings first */
161 
162  argv = NULL;
163  argv = StrArglistCreate(NULL);
164  CU_ASSERT_PTR_NOT_NULL(argv);
165  CU_ASSERT_PTR_NULL(argv[0]);
166  StrArglistFree(&argv);
167  CU_ASSERT_PTR_NULL(argv);
168 
169  argv = StrArglistCreate(" ");
170  CU_ASSERT_PTR_NOT_NULL(argv);
171  CU_ASSERT_PTR_NULL(argv[0]);
172  StrArglistFree(&argv);
173  CU_ASSERT_PTR_NULL(argv);
174 
175  argv = StrArglistCreate(" \n\t ");
176  CU_ASSERT_PTR_NOT_NULL(argv);
177  CU_ASSERT_PTR_NULL(argv[0]);
178  StrArglistFree(&argv);
179  CU_ASSERT_PTR_NULL(argv);
180 
181  /* Now check with command lines */
182 
183  argv= StrArglistCreate(" list zone -f -c co.uk ");
184  CU_ASSERT_PTR_NOT_NULL(argv);
185  CU_ASSERT_PTR_NOT_NULL(argv[0]);
186  CU_ASSERT_STRING_EQUAL(argv[0], "list");
187  CU_ASSERT_PTR_NOT_NULL(argv[1]);
188  CU_ASSERT_STRING_EQUAL(argv[1], "zone");
189  CU_ASSERT_PTR_NOT_NULL(argv[2]);
190  CU_ASSERT_STRING_EQUAL(argv[2], "-f");
191  CU_ASSERT_PTR_NOT_NULL(argv[3]);
192  CU_ASSERT_STRING_EQUAL(argv[3], "-c");
193  CU_ASSERT_PTR_NOT_NULL(argv[4]);
194  CU_ASSERT_STRING_EQUAL(argv[4], "co.uk");
195  CU_ASSERT_PTR_NULL(argv[5]);
196  StrArglistFree(&argv);
197  CU_ASSERT_PTR_NULL(argv);
198 
199  argv= StrArglistCreate("add signature -z co.uk\t-d alpha.dat\tfred");
200  CU_ASSERT_PTR_NOT_NULL(argv);
201  CU_ASSERT_PTR_NOT_NULL(argv[0]);
202  CU_ASSERT_STRING_EQUAL(argv[0], "add");
203  CU_ASSERT_PTR_NOT_NULL(argv[1]);
204  CU_ASSERT_STRING_EQUAL(argv[1], "signature");
205  CU_ASSERT_PTR_NOT_NULL(argv[2]);
206  CU_ASSERT_STRING_EQUAL(argv[2], "-z");
207  CU_ASSERT_PTR_NOT_NULL(argv[3]);
208  CU_ASSERT_STRING_EQUAL(argv[3], "co.uk");
209  CU_ASSERT_PTR_NOT_NULL(argv[4]);
210  CU_ASSERT_STRING_EQUAL(argv[4], "-d");
211  CU_ASSERT_PTR_NOT_NULL(argv[5]);
212  CU_ASSERT_STRING_EQUAL(argv[5], "alpha.dat");
213  CU_ASSERT_PTR_NOT_NULL(argv[6]);
214  CU_ASSERT_STRING_EQUAL(argv[6], "fred");
215  CU_ASSERT_PTR_NULL(argv[7]);
216  StrArglistFree(&argv);
217  CU_ASSERT_PTR_NULL(argv);
218 }
219 
220 
221 /*+
222  * TestStrKeywordSearch - Test Keyword Search Function
223 -*/
224 
225 static void TestStrKeywordSearch()
226 {
227  STR_KEYWORD_ELEMENT keywords[] = {
228  {"alpha", 5},
229  {"alpine", 10},
230  {"beta", 15},
231  {"gamma", 20}
232  };
233  int status; /* Status return */
234  int value; /* Return value */
235 
236  status = StrKeywordSearch("alpha", keywords, &value);
237  CU_ASSERT_EQUAL(status, 0);
238  CU_ASSERT_EQUAL(value, 5);
239 
240  status = StrKeywordSearch("beta", keywords, &value);
241  CU_ASSERT_EQUAL(status, 0);
242  CU_ASSERT_EQUAL(value, 15);
243 
244  status = StrKeywordSearch("alp", keywords, &value);
245  CU_ASSERT_EQUAL(status, -2);
246 
247  status = StrKeywordSearch("xyz", keywords, &value);
248  CU_ASSERT_EQUAL(status, -1);
249 
250  status = StrKeywordSearch("", keywords, &value);
251  CU_ASSERT_EQUAL(status, -2);
252 
253  status = StrKeywordSearch(NULL, keywords, &value);
254  CU_ASSERT_EQUAL(status, -1);
255 
256  return;
257 }
258 
259 /*+
260  * TestStrStrtol - Test String to Long Conversion
261 -*/
262 
263 static void TestStrStrtol()
264 {
265  int status;
266  long value;
267 
268  status = StrStrtol("23", &value);
269  CU_ASSERT_EQUAL(status, 0);
270  CU_ASSERT_EQUAL(value, 23L);
271 
272  status = StrStrtol(" 34 ", &value);
273  CU_ASSERT_EQUAL(status, 0);
274  CU_ASSERT_EQUAL(value, 34L);
275 
276  status = StrStrtol("56\t", &value);
277  CU_ASSERT_EQUAL(status, 0);
278  CU_ASSERT_EQUAL(value, 56L);
279 
280  status = StrStrtol("\t-67\t", &value);
281  CU_ASSERT_EQUAL(status, 0);
282  CU_ASSERT_EQUAL(value, -67L);
283 
284  status = StrStrtol(" 7 8 ", &value);
285  CU_ASSERT_NOT_EQUAL(status, 0);
286 
287  status = StrStrtol(" 7a ", &value);
288  CU_ASSERT_NOT_EQUAL(status, 0);
289 
290  status = StrStrtol(" ", &value);
291  CU_ASSERT_NOT_EQUAL(status, 0);
292 
293  status = StrStrtol(NULL, &value);
294  CU_ASSERT_NOT_EQUAL(status, 0);
295 
296  return;
297 }
298 
299 
300 
301 /*+
302  * TestStrStrtoul - Test String to Unsigned Long Conversion
303 -*/
304 
305 static void TestStrStrtoul()
306 {
307  int status;
308  unsigned long value;
309  union { /* For testing the reading of signed values */
310  long slong;
311  unsigned long ulong;
312  } combined;
313 
314  status = StrStrtoul("23", &value);
315  CU_ASSERT_EQUAL(status, 0);
316  CU_ASSERT_EQUAL(value, 23L);
317 
318  status = StrStrtoul(" 34 ", &value);
319  CU_ASSERT_EQUAL(status, 0);
320  CU_ASSERT_EQUAL(value, 34L);
321 
322  status = StrStrtoul("56\t", &value);
323  CU_ASSERT_EQUAL(status, 0);
324  CU_ASSERT_EQUAL(value, 56L);
325 
326  status = StrStrtoul("\t-1\t", &value);
327  CU_ASSERT_EQUAL(status, 0);
328  combined.ulong = value;
329  CU_ASSERT_EQUAL(combined.slong, -1);
330 
331  status = StrStrtoul("\t-277983\t", &value);
332  CU_ASSERT_EQUAL(status, 0);
333  combined.ulong = value;
334  CU_ASSERT_EQUAL(combined.slong, -277983);
335 
336  status = StrStrtoul(" 7 8 ", &value);
337  CU_ASSERT_NOT_EQUAL(status, 0);
338 
339  status = StrStrtoul(" 7a ", &value);
340  CU_ASSERT_NOT_EQUAL(status, 0);
341 
342  status = StrStrtoul(" ", &value);
343  CU_ASSERT_NOT_EQUAL(status, 0);
344 
345  status = StrStrtoul(NULL, &value);
346  CU_ASSERT_NOT_EQUAL(status, 0);
347 
348  return;
349 }
350 
351 /*+
352  * TestStrStrtoi - Test String to Integer Conversion
353 -*/
354 
355 static void TestStrStrtoi()
356 {
357  int status;
358  int value;
359 
360  status = StrStrtoi("23", &value);
361  CU_ASSERT_EQUAL(status, 0);
362  CU_ASSERT_EQUAL(value, 23);
363 
364  status = StrStrtoi(" 34 ", &value);
365  CU_ASSERT_EQUAL(status, 0);
366  CU_ASSERT_EQUAL(value, 34);
367 
368  status = StrStrtoi("56\t", &value);
369  CU_ASSERT_EQUAL(status, 0);
370  CU_ASSERT_EQUAL(value, 56);
371 
372  status = StrStrtoi("\t-67\t", &value);
373  CU_ASSERT_EQUAL(status, 0);
374  CU_ASSERT_EQUAL(value, -67);
375 
376  status = StrStrtoi(" 7 8 ", &value);
377  CU_ASSERT_NOT_EQUAL(status, 0);
378 
379  status = StrStrtoi(" 7a ", &value);
380  CU_ASSERT_NOT_EQUAL(status, 0);
381 
382  status = StrStrtoi(" ", &value);
383  CU_ASSERT_NOT_EQUAL(status, 0);
384 
385  status = StrStrtoi(NULL, &value);
386  CU_ASSERT_NOT_EQUAL(status, 0);
387 
388  return;
389 }
390 
391 /*+
392  * TestStrIsSigits - Test StrIsDigits
393 -*/
394 
395 static void TestStrIsDigits()
396 {
397  CU_ASSERT_NOT_EQUAL(StrIsDigits("1234567"), 0);
398  CU_ASSERT_NOT_EQUAL(StrIsDigits("17"), 0);
399 
400  CU_ASSERT_EQUAL(StrIsDigits(" 17"), 0);
401  CU_ASSERT_EQUAL(StrIsDigits("1 7"), 0);
402  CU_ASSERT_EQUAL(StrIsDigits("17 "), 0);
403  CU_ASSERT_EQUAL(StrIsDigits("A"), 0);
404  CU_ASSERT_EQUAL(StrIsDigits("\t"), 0);
405  CU_ASSERT_EQUAL(StrIsDigits(""), 0);
406  CU_ASSERT_EQUAL(StrIsDigits(NULL), 0);
407 
408  return;
409 }
410 
411 /*+
412  * TestStr2 - Create Test Suite
413  *
414  * Description:
415  * Adds the string test suite to the CUnit test registry
416  * and adds all the tests to it.
417  *
418  * Arguments:
419  * None.
420  *
421  * Returns:
422  * int
423  * Return status. 0 => Success.
424 -*/
425 
426 int TestStr2(void); /* Declaration */
427 int TestStr2(void)
428 {
429  struct test_testdef tests[] = {
430  {"StrAppend", TestStrAppend},
431  {"StrArglistAddFree", TestStrArglistAddFree},
432  {"StrArglistCreate", TestStrArglistCreate},
433  {"StrKeywordSearch", TestStrKeywordSearch},
434  {"StrStrtol", TestStrStrtol},
435  {"StrStrtoul", TestStrStrtoul},
436  {"StrStrtoi", TestStrStrtoi},
437  {"StrIsDigits", TestStrIsDigits},
438  {NULL, NULL}
439  };
440 
441  return TcuCreateSuite("String Utility2", NULL, NULL, tests);
442 }