15634cc160755cbac330d1fee73951bde4b80262
[citadel] / libcitadel / tests / stringbuf_conversion.c
1 /*
2  *  CUnit - A Unit testing framework library for C.
3  *  Copyright (C) 2001  Anil Kumar
4  *  
5  *  This library is open source software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <unistd.h>
21
22 #include "stringbuf_test.h"
23 #include "../lib/libcitadel.h"
24
25
26 int fromstdin = 0;
27 int parse_email = 0;
28 int parse_html = 0;
29 int OutputEscape = 0;
30 int OutputEscapeAs = 0;
31
32 static void TestRevalidateStrBuf(StrBuf *Buf)
33 {
34         CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
35 }
36
37
38
39 /*
40 Some samples from the original...
41         CU_ASSERT_EQUAL(10, 10);
42         CU_ASSERT_EQUAL(0, -0);
43         CU_ASSERT_EQUAL(-12, -12);
44         CU_ASSERT_NOT_EQUAL(10, 11);
45         CU_ASSERT_NOT_EQUAL(0, -1);
46         CU_ASSERT_NOT_EQUAL(-12, -11);
47         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
48         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
49         CU_ASSERT_PTR_NULL(NULL);
50         CU_ASSERT_PTR_NULL(0x0);
51         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
52         CU_ASSERT_STRING_EQUAL(str1, str2);
53         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
54         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
55         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
56         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
57         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
58         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
59         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
60         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
61         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
62         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
63         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
64         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
65         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
66         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
67 */
68
69 static void TestRFC822Decode(void)
70 {
71         StrBuf *Target;
72         StrBuf *Source;
73         StrBuf *DefaultCharset;
74         StrBuf *FoundCharset;
75         
76         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
77         FoundCharset = NewStrBuf();
78         Source = NewStrBufPlain(HKEY("=?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?="));
79         Target = NewStrBuf();
80
81         StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
82
83
84         TestRevalidateStrBuf(Target);
85         printf("the ugly multi:>%s<\n", ChrPtr(Target));
86         FreeStrBuf(&Source);
87         FreeStrBuf(&Target);
88         FreeStrBuf(&FoundCharset);
89         FreeStrBuf(&DefaultCharset);
90
91
92         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
93         FoundCharset = NewStrBuf();
94         Source = NewStrBufPlain(HKEY("\"w.goesgens\" <w.goesgens@aoeuaoeuaoeu.org>, =?ISO-8859-15?Q?Walter_?= =?ISO-8859-15?Q?G=F6aoeus?= <aoeuaoeu@aoe.de>, =?ISO-8859-15?Q?aoeuaoeuh?= =?ISO-8859-15?Q?_G=F6aoeus?= <aoeuoeuaoeu@oeu.de>, aoeuao aoeuaoeu <aoeuaoeuaoeaoe@aoe.de"));
95         Target = NewStrBufPlain(NULL, 256);
96
97         StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
98         TestRevalidateStrBuf(Target);
99         printf("the ugly multi:>%s<\n", ChrPtr(Target));
100         FreeStrBuf(&Source);
101         FreeStrBuf(&Target);
102         FreeStrBuf(&FoundCharset);
103         FreeStrBuf(&DefaultCharset);
104
105 }
106
107
108 static void TestRFC822DecodeStdin(void)
109 {
110         int fdin = 0;// STDIN
111         const char *Err;
112         StrBuf *Target;
113         StrBuf *Source;
114         StrBuf *DefaultCharset;
115         StrBuf *FoundCharset;
116         
117         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
118         FoundCharset = NewStrBuf();
119         Source = NewStrBuf();
120
121         while (fdin == 0) {
122
123                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
124                 Target = NewStrBuf();
125                 
126                 StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
127                 
128                 TestRevalidateStrBuf(Target);
129                 printf("the ugly multi:>%s<\n", ChrPtr(Target));
130                 FreeStrBuf(&Target);
131         }
132         FreeStrBuf(&Source);
133         FreeStrBuf(&FoundCharset);
134         FreeStrBuf(&DefaultCharset);
135 }
136
137
138 static void TestHTMLEscEncodeStdin(void)
139 {
140         int fdin = 0;// STDIN
141         const char *Err;
142         StrBuf *Target;
143         StrBuf *Source;
144
145         Source = NewStrBuf();
146
147         while (fdin == 0) {
148
149                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
150                 Target = NewStrBuf();
151                 
152                 StrEscAppend(Target, Source, NULL, 0, 2);
153                 
154                 TestRevalidateStrBuf(Target);
155                 printf("%s\n", ChrPtr(Target));
156                 FreeStrBuf(&Target);
157         }
158         FreeStrBuf(&Source);
159 }
160
161 static void TestEscEncodeStdin(void)
162 {
163         int fdin = 0;// STDIN
164         const char *Err;
165         StrBuf *Target;
166         StrBuf *Source;
167
168         Source = NewStrBuf();
169
170         while (fdin == 0) {
171
172                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
173                 Target = NewStrBuf();
174                 
175                 StrEscAppend(Target, Source, NULL, 0, 0);
176                 
177                 TestRevalidateStrBuf(Target);
178                 printf("%s\n", ChrPtr(Target));
179                 FreeStrBuf(&Target);
180         }
181         FreeStrBuf(&Source);
182 }
183
184
185 static void TestECMAEscEncodeStdin(void)
186 {
187         int fdin = 0;// STDIN
188         const char *Err;
189         StrBuf *Target;
190         StrBuf *Source;
191
192         Source = NewStrBuf();
193
194         printf("[");
195         while (fdin == 0) {
196
197                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
198                 Target = NewStrBuf();
199                 
200                 StrECMAEscAppend(Target, Source, NULL);
201                 
202                 TestRevalidateStrBuf(Target);
203                 printf("\"%s\",\n", ChrPtr(Target));
204                 FreeStrBuf(&Target);
205         }
206         printf("]\n");
207         FreeStrBuf(&Source);
208 }
209
210 static void TestHtmlEcmaEscEncodeStdin(void)
211 {
212         int fdin = 0;// STDIN
213         const char *Err;
214         StrBuf *Target;
215         StrBuf *Source;
216
217         Source = NewStrBuf();
218
219         printf("[");
220         while (fdin == 0) {
221
222                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
223                 Target = NewStrBuf();
224                 
225                 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 2);
226                 
227                 TestRevalidateStrBuf(Target);
228                 printf("\"%s\",\n", ChrPtr(Target));
229                 FreeStrBuf(&Target);
230         }
231         printf("]");
232         FreeStrBuf(&Source);
233 }
234
235 static void TestUrlescEncodeStdin(void)
236 {
237         int fdin = 0;// STDIN
238         const char *Err;
239         StrBuf *Target;
240         StrBuf *Source;
241
242         Source = NewStrBuf();
243
244         while (fdin == 0) {
245
246                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
247                 Target = NewStrBuf();
248                 
249                 StrBufUrlescAppend(Target, Source, NULL);
250                 
251                 TestRevalidateStrBuf(Target);
252                 printf("%s\n", ChrPtr(Target));
253                 FreeStrBuf(&Target);
254         }
255         FreeStrBuf(&Source);
256 }
257
258
259 static void TestEncodeEmail(void)
260 {
261         StrBuf *Target;
262         StrBuf *Source;
263         StrBuf *UserName = NewStrBuf();
264         StrBuf *EmailAddress = NewStrBuf();
265         StrBuf *EncBuf = NewStrBuf();
266         
267         Source = NewStrBuf();
268  
269 //      Source = NewStrBufPlain(HKEY("Art Cancro <ajc@uncensored.citadel.org>, Art Cancro <ajc@uncensored.citadel.org>"));
270
271         Source = NewStrBufPlain(HKEY("\"Alexandra Weiz, Restless GmbH\" <alexandra.weiz@boblbee.de>, \"NetIN\" <editor@netin.co.il>, \" יריב ברקאי, מולטימדי\" <info@immembed.com>")); 
272         Target = StrBufSanitizeEmailRecipientVector(
273                 Source,
274                 UserName, 
275                 EmailAddress,
276                 EncBuf
277                 );              
278         
279         TestRevalidateStrBuf(Target);
280         printf("the source:>%s<\n", ChrPtr(Source));
281         printf("the target:>%s<\n", ChrPtr(Target));
282         FreeStrBuf(&Target);
283         FreeStrBuf(&UserName);
284         FreeStrBuf(&EmailAddress);
285         FreeStrBuf(&EncBuf);
286
287         FreeStrBuf(&Source);
288 }
289
290 static void TestEncodeEmailSTDIN(void)
291 {
292         int fdin = 0;// STDIN
293         const char *Err;
294         StrBuf *Target;
295         StrBuf *Source;
296         StrBuf *UserName = NewStrBuf();
297         StrBuf *EmailAddress = NewStrBuf();
298         StrBuf *EncBuf = NewStrBuf();
299         
300         Source = NewStrBuf();
301
302         while (fdin == 0) {
303
304                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
305                 printf("the source:>%s<\n", ChrPtr(Source));
306                 Target = StrBufSanitizeEmailRecipientVector(
307                         Source,
308                         UserName, 
309                         EmailAddress,
310                         EncBuf
311                         );
312                 
313                 TestRevalidateStrBuf(Target);
314                 printf("the target:>%s<\n", ChrPtr(Target));
315                 FreeStrBuf(&Target);
316         }
317         FreeStrBuf(&UserName);
318         FreeStrBuf(&EmailAddress);
319         FreeStrBuf(&EncBuf);
320
321         FreeStrBuf(&Source);
322 }
323
324
325 static void TestHTML2ASCII_line(void)
326 {
327         int fdin = 0;// STDIN
328         const char *Err;
329         StrBuf *Source;
330         char *Target;
331
332         Source = NewStrBuf();
333
334         while (fdin == 0) {
335                 
336                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
337                 printf("the source:>%s<\n", ChrPtr(Source));
338                 Target = html_to_ascii(ChrPtr(Source), StrLength(Source), 80, 0);
339                 
340                 printf("the target:>%s<\n", Target);
341                 FlushStrBuf(Source);
342                 free(Target);
343         }
344
345         FreeStrBuf(&Source);
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("TestStringBufConversions", NULL, NULL);
355         if (parse_email) {
356                 if (!fromstdin) {
357                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmail);
358                 }
359                 else
360                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmailSTDIN);
361         }
362         else if (parse_html) {
363                         pTest = CU_add_test(pGroup, "TestParseHTMLSTDIN", TestHTML2ASCII_line);
364         }
365         else {
366                 if (!fromstdin) {
367                         pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
368                         pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
369                         pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
370                         pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
371                 }
372                 else
373                 {
374                         if (!OutputEscape)
375                                 pTest = CU_add_test(pGroup, "testRFC822DecodeSTDIN", TestRFC822DecodeStdin);
376                         else switch(OutputEscapeAs)
377                              {
378                              case 'H':
379                                      pTest = CU_add_test(pGroup, "TestHTMLEscEncodeStdin", TestHTMLEscEncodeStdin);
380                                      break;
381                              case 'X':
382                                      pTest = CU_add_test(pGroup, "TestEscEncodeStdin", TestEscEncodeStdin);
383                                      break;
384                              case 'J':
385                                      pTest = CU_add_test(pGroup, "TestECMAEscEncodeStdin", TestECMAEscEncodeStdin);
386                                      break;
387                              case 'K':
388                                      pTest = CU_add_test(pGroup, "TestHtmlEcmaEscEncodeStdin", TestHtmlEcmaEscEncodeStdin);
389                                      break;
390                              case 'U':
391                                      pTest = CU_add_test(pGroup, "TestUrlescEncodeStdin", TestUrlescEncodeStdin);
392                                      break;
393                              default:
394                                      printf("%c not supported!\n", OutputEscapeAs);
395                                      CU_ASSERT(1);
396                              }
397                 }
398         }
399
400 }
401
402
403 int main(int argc, char* argv[])
404 {
405         int a;
406
407         while ((a = getopt(argc, argv, "@iho:")) != EOF)
408                 switch (a) {
409                 case 'o':
410                         if (optarg)
411                         {
412                                 OutputEscape = 1;
413                                 OutputEscapeAs = *optarg;
414                         }
415                         break;
416                 case 'h':
417                         parse_html = 1;
418                         break;
419                 case '@':
420                         parse_email = 1;
421                         break;
422                 case 'i':
423                         fromstdin = 1;
424                         
425                         break;
426                 }
427
428
429         setvbuf(stdout, NULL, _IONBF, 0);
430
431         StartLibCitadel(8);
432         CU_BOOL Run = CU_FALSE ;
433         
434         CU_set_output_filename("TestAutomated");
435         if (CU_initialize_registry()) {
436                 printf("\nInitialize of test Registry failed.");
437         }
438         
439         Run = CU_TRUE ;
440         AddStrBufSimlpeTests();
441         
442         if (CU_TRUE == Run) {
443                 //CU_console_run_tests();
444                 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
445     
446                 ///CU_automated_run_tests();
447         }
448         
449         CU_cleanup_registry();
450
451         return 0;
452 }