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