add StrBufRFC2047encodeMessage which does quoted printeable encoding for a message.
[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 static void StrBufRFC2047encodeMessageStdin(void)
325 {
326         int fdin = 0;// STDIN
327         const char *Err;
328         StrBuf *Target;
329         StrBuf *Source;
330         StrBuf *Src;
331
332         Source = NewStrBuf();
333         Src = NewStrBuf();
334
335         printf("[");
336         while (fdin == 0) {
337
338                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
339                 StrBufAppendBuf(Src, Source, 0);
340                 StrBufAppendBufPlain(Src, HKEY("\n"), 0);
341                                 
342         }
343
344         Target = StrBufRFC2047encodeMessage(Src);
345
346         printf("Target: \n%s\n", ChrPtr(Target));
347         FreeStrBuf(&Source);
348         FreeStrBuf(&Src);
349         FreeStrBuf(&Target);
350 }
351
352 static void TestHTML2ASCII_line(void)
353 {
354         int fdin = 0;// STDIN
355         const char *Err;
356         StrBuf *Source;
357         char *Target;
358
359         Source = NewStrBuf();
360
361         while (fdin == 0) {
362                 
363                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
364                 printf("the source:>%s<\n", ChrPtr(Source));
365                 Target = html_to_ascii(ChrPtr(Source), StrLength(Source), 80, 0);
366                 
367                 printf("the target:>%s<\n", Target);
368                 FlushStrBuf(Source);
369                 free(Target);
370         }
371
372         FreeStrBuf(&Source);
373 }
374
375
376 static void AddStrBufSimlpeTests(void)
377 {
378         CU_pSuite pGroup = NULL;
379         CU_pTest pTest = NULL;
380
381         pGroup = CU_add_suite("TestStringBufConversions", NULL, NULL);
382         if (parse_email) {
383                 if (!fromstdin) {
384                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmail);
385                 }
386                 else
387                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmailSTDIN);
388         }
389         else if (parse_html) {
390                         pTest = CU_add_test(pGroup, "TestParseHTMLSTDIN", TestHTML2ASCII_line);
391         }
392         else {
393                 if (!fromstdin) {
394                         pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
395                         pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
396                         pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
397                         pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
398                 }
399                 else
400                 {
401                         if (!OutputEscape)
402                                 pTest = CU_add_test(pGroup, "testRFC822DecodeSTDIN", TestRFC822DecodeStdin);
403                         else switch(OutputEscapeAs)
404                              {
405                              case 'H':
406                                      pTest = CU_add_test(pGroup, "TestHTMLEscEncodeStdin", TestHTMLEscEncodeStdin);
407                                      break;
408                              case 'X':
409                                      pTest = CU_add_test(pGroup, "TestEscEncodeStdin", TestEscEncodeStdin);
410                                      break;
411                              case 'J':
412                                      pTest = CU_add_test(pGroup, "TestECMAEscEncodeStdin", TestECMAEscEncodeStdin);
413                                      break;
414                              case 'K':
415                                      pTest = CU_add_test(pGroup, "TestHtmlEcmaEscEncodeStdin", TestHtmlEcmaEscEncodeStdin);
416                                      break;
417                              case 'U':
418                                      pTest = CU_add_test(pGroup, "TestUrlescEncodeStdin", TestUrlescEncodeStdin);
419                                      break;
420                              case 'R':
421                                      pTest = CU_add_test(pGroup, "StrBufRFC2047encodeMessageStdin", StrBufRFC2047encodeMessageStdin);
422                                      break;
423                              default:
424                                      printf("%c not supported!\n", OutputEscapeAs);
425                                      CU_ASSERT(1);
426                              }
427                 }
428         }
429
430 }
431
432
433 int main(int argc, char* argv[])
434 {
435         int a;
436
437         while ((a = getopt(argc, argv, "@iho:")) != EOF)
438                 switch (a) {
439                 case 'o':
440                         if (optarg)
441                         {
442                                 OutputEscape = 1;
443                                 OutputEscapeAs = *optarg;
444                         }
445                         break;
446                 case 'h':
447                         parse_html = 1;
448                         break;
449                 case '@':
450                         parse_email = 1;
451                         break;
452                 case 'i':
453                         fromstdin = 1;
454                         
455                         break;
456                 }
457
458
459         setvbuf(stdout, NULL, _IONBF, 0);
460
461         StartLibCitadel(8);
462         CU_BOOL Run = CU_FALSE ;
463         
464         CU_set_output_filename("TestAutomated");
465         if (CU_initialize_registry()) {
466                 printf("\nInitialize of test Registry failed.");
467         }
468         
469         Run = CU_TRUE ;
470         AddStrBufSimlpeTests();
471         
472         if (CU_TRUE == Run) {
473                 //CU_console_run_tests();
474                 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
475     
476                 ///CU_automated_run_tests();
477         }
478         
479         CU_cleanup_registry();
480
481         return 0;
482 }