* don't use sprintf while doing urlescappend; do it in place.
[citadel.git] / libcitadel / tests / stringbuf_test.c
1
2 /*
3  *  CUnit - A Unit testing framework library for C.
4  *  Copyright (C) 2001  Anil Kumar
5  *  
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "stringbuf_test.h"
26 #include "../lib/libcitadel.h"
27
28
29 static void TestRevalidateStrBuf(StrBuf *Buf)
30 {
31         CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
32 }
33
34
35 static void TestCreateBuf(void)
36 {
37         StrBuf *Buf;
38         StrBuf *Buf2;
39         long len;
40         long i;
41
42         Buf = NewStrBuf();
43         CU_ASSERT(Buf != NULL);
44         FreeStrBuf(&Buf);
45
46         CU_ASSERT(Buf == NULL);
47         Buf = NewStrBufPlain(HKEY("ABC"));
48         TestRevalidateStrBuf(Buf);
49         CU_ASSERT(StrLength(Buf) == 3);
50         CU_ASSERT_NSTRING_EQUAL("ABC", ChrPtr(Buf), 3);
51
52         len = StrLength(Buf);
53         for (i=0; i< 500; i ++)
54         {
55                 StrBufAppendBufPlain(Buf, HKEY("ABC"), 0);
56                 len += 3;
57                 CU_ASSERT(StrLength(Buf) == len);
58         }       
59         StrBufShrinkToFit(Buf, 1);
60         FreeStrBuf(&Buf);
61         CU_ASSERT(Buf == NULL);
62         
63         Buf = NewStrBufPlain(HKEY("ABC"));
64         TestRevalidateStrBuf(Buf);
65         len = StrLength(Buf);
66         for (i=0; i< 500; i ++)
67         {
68                 StrBufAppendPrintf(Buf, "%s", "ABC");
69                 len += 3;
70                 CU_ASSERT(StrLength(Buf) == len);               
71         }
72         TestRevalidateStrBuf(Buf);
73         StrBufShrinkToFit(Buf, 1);
74         TestRevalidateStrBuf(Buf);
75
76         Buf2 = NewStrBufDup(Buf);
77         CU_ASSERT(StrLength(Buf) == StrLength(Buf2));           
78         
79         CU_ASSERT_NSTRING_EQUAL(ChrPtr(Buf2), ChrPtr(Buf), StrLength(Buf2));
80         
81         CU_ASSERT(StrBufIsNumber(Buf) == 0);
82
83         FlushStrBuf(Buf2);
84         CU_ASSERT(StrLength(Buf2) == 0);
85
86         FLUSHStrBuf(Buf);
87         CU_ASSERT(StrLength(Buf) == 0);
88
89         FreeStrBuf(&Buf);
90         FreeStrBuf(&Buf2);
91         CU_ASSERT(Buf == NULL);
92         CU_ASSERT(Buf2 == NULL);
93
94
95         Buf = NewStrBufPlain(HKEY("123456"));
96 ///     CU_ASSERT(StrBufIsNumber(Buf) == 1); Todo: this is buggy.
97         FreeStrBuf(&Buf);
98         
99 }
100
101 static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens)
102 {
103         const char *pCh = NULL;
104         StrBuf *Buf2;
105         long CountTokens = 0;
106         long HaveNextToken = 0;
107         long HaveNextTokenF = 0;
108
109         printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
110         TestRevalidateStrBuf(Buf);
111                              
112         Buf2 = NewStrBuf();
113         while (HaveNextToken = StrBufHaveNextToken(Buf, &pCh),
114                HaveNextTokenF = StrBufExtract_NextToken(Buf2, Buf, &pCh, ','),
115                (HaveNextTokenF>= 0))
116         {
117                 CountTokens++;
118                 
119                 printf("Token: >%s< >%s< %ld:%ld\n", 
120                        ChrPtr(Buf2), 
121                        ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A", 
122                        HaveNextToken, 
123                        HaveNextTokenF);
124                 TestRevalidateStrBuf(Buf2);
125
126                 CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
127                 
128                 CU_ASSERT(CountTokens <= NTokens);
129         } 
130         CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
131         FreeStrBuf(&Buf2);
132 }
133
134
135
136 static void TestNextTokenizer_EndWithEmpty(void)
137 {
138         StrBuf *Buf;
139
140         Buf = NewStrBufPlain(HKEY("abc,abc, 1, ,,"));
141         NextTokenizerIterateBuf(Buf, 7);
142         FreeStrBuf(&Buf);
143 }
144
145 static void TestNextTokenizer_StartWithEmpty(void)
146 {
147         StrBuf *Buf;
148
149         Buf = NewStrBufPlain(HKEY(",cde,abc, 1, ,,bbb"));
150         NextTokenizerIterateBuf(Buf, 8);
151         FreeStrBuf(&Buf);
152 }
153
154 static void TestNextTokenizer_Empty(void)
155 {
156         StrBuf *Buf;
157
158         Buf = NewStrBufPlain(HKEY(""));
159         NextTokenizerIterateBuf(Buf, 8);
160         FreeStrBuf(&Buf);
161 }
162
163 static void TestNextTokenizer_TwoEmpty(void)
164 {
165         StrBuf *Buf;
166
167         Buf = NewStrBufPlain(HKEY(","));
168         NextTokenizerIterateBuf(Buf, 8);
169         FreeStrBuf(&Buf);
170 }
171
172 static void TestNextTokenizer_One(void)
173 {
174         StrBuf *Buf;
175
176         Buf = NewStrBufPlain(HKEY("one"));
177         NextTokenizerIterateBuf(Buf, 8);
178         FreeStrBuf(&Buf);
179 }
180
181 static void TestNextTokenizer_Sequence(void)
182 {
183         StrBuf *Buf;
184         char *teststring = "40:24524,24662,24673,27869:27935,28393,28426,31247:31258,31731,31749,31761,31778,31782,31801:31803,31813,31904,31915,33708,33935,34619,34672,34720:34723,34766,34835,37594,38854,39235,39942,40030,40142,40520,40815,40907,41201,41578,41781,41954,42292,43110,43565,43801,43998,44180,44241,44295,44401,44561,44635,44798,44861,44946,45022,45137:45148,45166,45179,45707,47114,47141:47157,47194,47314,47349,47386,47489,47496,47534:47543,54460,54601,54637:54652";
185         Buf = NewStrBufPlain(teststring, -1);
186         NextTokenizerIterateBuf(Buf, 67);
187         FreeStrBuf(&Buf);
188 }
189
190
191
192 static void NextLineterateBuf(StrBuf *Buf, int NLines)
193 {
194         int n = 0;
195         const char *pCh = NULL;
196         StrBuf *OneLine;
197         StrBuf *ConcatenatedLines;
198         long CountTokens = 0;
199         
200         TestRevalidateStrBuf(Buf);
201                              
202         OneLine = NewStrBuf();
203         ConcatenatedLines = NewStrBuf();
204
205         printf("\n");
206
207         if (StrLength(Buf) > 0) 
208                 do 
209                 {
210                         n = StrBufSipLine(OneLine, Buf, &pCh);
211                         
212                         CountTokens++;
213                         
214                         printf("Line: >%s< >%s<\n", 
215                                ChrPtr(OneLine), 
216                                ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A");
217                         TestRevalidateStrBuf(OneLine);
218                         CU_ASSERT(CountTokens <= NLines);
219                         StrBufAppendBuf(ConcatenatedLines, OneLine, 0);
220                         
221                         if ((pCh == StrBufNOTNULL) && 
222                             (*(ChrPtr(Buf) + StrLength(Buf) - 1) != '\n'))
223                         {
224                         }
225                         else 
226                                 StrBufAppendBufPlain(ConcatenatedLines, HKEY("\n"), 0);
227                         
228                 } 
229                 while ((pCh != StrBufNOTNULL) &&
230                        (pCh != NULL));
231         
232
233         printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
234         printf("\n\nAfter: >%s<\n", ChrPtr(ConcatenatedLines));
235         CU_ASSERT_NSTRING_EQUAL(ChrPtr(ConcatenatedLines), 
236                                 ChrPtr(Buf), 
237                                 StrLength(Buf));
238
239         FreeStrBuf(&OneLine);
240         FreeStrBuf(&ConcatenatedLines);
241 }
242
243
244 static void TestNextLine_Empty(void)
245 {
246         StrBuf *Buf;
247
248         Buf = NewStrBufPlain(HKEY(""));
249         NextLineterateBuf(Buf, 0);
250         FreeStrBuf(&Buf);
251 }
252
253
254 static void TestNextLine_OneLine(void)
255 {
256         StrBuf *Buf;
257
258         Buf = NewStrBufPlain(HKEY("abc\n"));
259         NextLineterateBuf(Buf, 1);
260         FreeStrBuf(&Buf);
261 }
262
263
264 static void TestNextLine_TwoLinesMissingCR(void)
265 {
266         StrBuf *Buf;
267
268         Buf = NewStrBufPlain(HKEY("abc\ncde"));
269         NextLineterateBuf(Buf, 2);
270         FreeStrBuf(&Buf);
271 }
272
273
274 static void TestNextLine_twolines(void)
275 {
276         StrBuf *Buf;
277
278         Buf = NewStrBufPlain(HKEY("abc\ncde\n"));
279         NextLineterateBuf(Buf, 2);
280         FreeStrBuf(&Buf);
281 }
282
283 static void TestNextLine_LongLine(void)
284 {
285         StrBuf *Buf;
286
287         Buf = NewStrBufPlain(HKEY("abcde\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"));
288         NextLineterateBuf(Buf, 2);
289         FreeStrBuf(&Buf);
290 }
291
292
293 static void TestStrBufRemove_token_NotThere(void)
294 {
295         StrBuf *Test = NewStrBufPlain(HKEY(" 127.0.0.1"));
296         StrBufRemove_token(Test, 0, ',');
297         TestRevalidateStrBuf(Test);
298         FreeStrBuf(&Test);
299 }
300
301
302 static void TestStrBufUrlescAppend(void)
303 {
304         const char *expect = "%20%2B%23%26%3B%60%27%7C%2A%3F%2D%7E%3C%3E%5E%28%29%5B%5D%7B%7D%2F%24%22%5C";
305         StrBuf *In = NewStrBufPlain(HKEY( " +#&;`'|*?-~<>^()[]{}/$\"\\"));
306         StrBuf *Out = NewStrBuf();
307
308         StrBufUrlescAppend (Out, In, NULL);
309         printf ("%s<\n%s<\n%s\n", ChrPtr(In), ChrPtr(Out), expect);
310         CU_ASSERT_STRING_EQUAL(ChrPtr(Out), expect);
311         FreeStrBuf(&In);
312         FreeStrBuf(&Out);
313 }
314
315 /*
316 Some samples from the original...
317         CU_ASSERT_EQUAL(10, 10);
318         CU_ASSERT_EQUAL(0, -0);
319         CU_ASSERT_EQUAL(-12, -12);
320         CU_ASSERT_NOT_EQUAL(10, 11);
321         CU_ASSERT_NOT_EQUAL(0, -1);
322         CU_ASSERT_NOT_EQUAL(-12, -11);
323         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
324         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
325         CU_ASSERT_PTR_NULL(NULL);
326         CU_ASSERT_PTR_NULL(0x0);
327         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
328         CU_ASSERT_STRING_EQUAL(str1, str2);
329         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
330         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
331         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
332         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
333         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
334         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
335         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
336         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
337         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
338         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
339         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
340         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
341         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
342         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
343 */
344
345
346
347
348
349 static void AddStrBufSimlpeTests(void)
350 {
351         CU_pSuite pGroup = NULL;
352         CU_pTest pTest = NULL;
353
354         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
355         pTest = CU_add_test(pGroup, "testCreateBuf", TestCreateBuf);
356
357         pGroup = CU_add_suite("TestStringTokenizer", NULL, NULL);
358         pTest = CU_add_test(pGroup, "testNextTokenizer_EndWithEmpty", TestNextTokenizer_EndWithEmpty);
359         pTest = CU_add_test(pGroup, "testNextTokenizer_StartWithEmpty", TestNextTokenizer_StartWithEmpty);
360         pTest = CU_add_test(pGroup, "testNextTokenizer_StartWithEmpty", TestNextTokenizer_StartWithEmpty);
361         pTest = CU_add_test(pGroup, "testNextTokenizer_Empty", TestNextTokenizer_Empty);
362         pTest = CU_add_test(pGroup, "testNextTokenizer_TwoEmpty", TestNextTokenizer_TwoEmpty);
363         pTest = CU_add_test(pGroup, "testNextTokenizer_One", TestNextTokenizer_One);
364         pTest = CU_add_test(pGroup, "testNextTokenizer_Sequence", TestNextTokenizer_Sequence);
365
366
367         pGroup = CU_add_suite("TestStrBufSipLine", NULL, NULL);
368         pTest = CU_add_test(pGroup, "TestNextLine_Empty", TestNextLine_Empty);
369         pTest = CU_add_test(pGroup, "TestNextLine_OneLine", TestNextLine_OneLine);
370         pTest = CU_add_test(pGroup, "TestNextLine_TwoLinesMissingCR", TestNextLine_TwoLinesMissingCR);
371         pTest = CU_add_test(pGroup, "TestNextLine_twolines", TestNextLine_twolines);
372         pTest = CU_add_test(pGroup, "TestNextLine_LongLine", TestNextLine_LongLine);
373         
374         pGroup = CU_add_suite("TestStrBufRemove_token", NULL, NULL);
375         pTest = CU_add_test(pGroup, "TestStrBufRemove_token_NotThere", TestStrBufRemove_token_NotThere);
376
377         pGroup = CU_add_suite("TestStrBuf_escapers", NULL, NULL);
378         pTest = CU_add_test(pGroup, "TestStrBufUrlescAppend", TestStrBufUrlescAppend);
379 }
380
381
382 int main(int argc, char* argv[])
383 {
384         setvbuf(stdout, NULL, _IONBF, 0);
385
386         StartLibCitadel(8);
387         CU_BOOL Run = CU_FALSE ;
388         
389         CU_set_output_filename("TestAutomated");
390         if (CU_initialize_registry()) {
391                 printf("\nInitialize of test Registry failed.");
392         }
393         
394         Run = CU_TRUE ;
395         AddStrBufSimlpeTests();
396         
397         if (CU_TRUE == Run) {
398                 //CU_console_run_tests();
399     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
400     
401     ///CU_automated_run_tests();
402         }
403         
404         CU_cleanup_registry();
405
406         return 0;
407 }