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