03cc1bc96e8824aa565ef0438595872a7ee718ce
[citadel.git] / libcitadel / tests / html_to_ascii_test.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 static void TestRevalidateStrBuf(StrBuf *Buf)
29 {
30         CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
31 }
32
33
34
35
36 static void TestRFC822Decode(void)
37 {
38         StrBuf *Target;
39         StrBuf *Source;
40         StrBuf *DefaultCharset;
41         StrBuf *FoundCharset;
42         
43         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
44         FoundCharset = NewStrBuf();
45         Source = NewStrBufPlain(HKEY("=?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?="));
46         Target = NewStrBuf();
47
48         StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
49
50
51         TestRevalidateStrBuf(Target);
52         printf("the ugly multi:>%s<\n", ChrPtr(Target));
53         FreeStrBuf(&Source);
54         FreeStrBuf(&Target);
55         FreeStrBuf(&FoundCharset);
56         FreeStrBuf(&DefaultCharset);
57
58
59         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
60         FoundCharset = NewStrBuf();
61         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"));
62         Target = NewStrBufPlain(NULL, 256);
63
64         StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
65         TestRevalidateStrBuf(Target);
66         printf("the ugly multi:>%s<\n", ChrPtr(Target));
67         FreeStrBuf(&Source);
68         FreeStrBuf(&Target);
69         FreeStrBuf(&FoundCharset);
70         FreeStrBuf(&DefaultCharset);
71
72 }
73
74
75 static void TestRFC822DecodeStdin(void)
76 {
77         int fdin = 0;// STDIN
78         const char *Err;
79         StrBuf *Target;
80         StrBuf *Source;
81         StrBuf *DefaultCharset;
82         StrBuf *FoundCharset;
83         
84         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
85         FoundCharset = NewStrBuf();
86         Source = NewStrBuf();
87
88         while (fdin == 0) {
89
90                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
91                 Target = NewStrBuf();
92                 
93                 StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
94                 
95                 TestRevalidateStrBuf(Target);
96                 printf("the ugly multi:>%s<\n", ChrPtr(Target));
97                 FreeStrBuf(&Target);
98         }
99         FreeStrBuf(&Source);
100         FreeStrBuf(&FoundCharset);
101         FreeStrBuf(&DefaultCharset);
102 }
103
104
105 static void TestEncodeEmail(void)
106 {
107         StrBuf *Target;
108         StrBuf *Source;
109         StrBuf *UserName = NewStrBuf();
110         StrBuf *EmailAddress = NewStrBuf();
111         StrBuf *EncBuf = NewStrBuf();
112         
113         Source = NewStrBuf();
114  
115 //      Source = NewStrBufPlain(HKEY("Art Cancro <ajc@uncensored.citadel.org>, Art Cancro <ajc@uncensored.citadel.org>"));
116
117         Source = NewStrBufPlain(HKEY("\"Alexandra Weiz, Restless GmbH\" <alexandra.weiz@boblbee.de>, \"NetIN\" <editor@netin.co.il>, \" יריב ברקאי, מולטימדי\" <info@immembed.com>")); 
118         Target = StrBufSanitizeEmailRecipientVector(
119                 Source,
120                 UserName, 
121                 EmailAddress,
122                 EncBuf
123                 );              
124         
125         TestRevalidateStrBuf(Target);
126         printf("the source:>%s<\n", ChrPtr(Source));
127         printf("the target:>%s<\n", ChrPtr(Target));
128         FreeStrBuf(&Target);
129         FreeStrBuf(&UserName);
130         FreeStrBuf(&EmailAddress);
131         FreeStrBuf(&EncBuf);
132
133         FreeStrBuf(&Source);
134 }
135
136 static void TestEncodeEmailSTDIN(void)
137 {
138         int fdin = 0;// STDIN
139         const char *Err;
140         StrBuf *Target;
141         StrBuf *Source;
142         StrBuf *UserName = NewStrBuf();
143         StrBuf *EmailAddress = NewStrBuf();
144         StrBuf *EncBuf = NewStrBuf();
145         
146         Source = NewStrBuf();
147
148         while (fdin == 0) {
149
150                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
151                 printf("the source:>%s<\n", ChrPtr(Source));
152                 Target = StrBufSanitizeEmailRecipientVector(
153                         Source,
154                         UserName, 
155                         EmailAddress,
156                         EncBuf
157                         );
158                 
159                 TestRevalidateStrBuf(Target);
160                 printf("the target:>%s<\n", ChrPtr(Target));
161                 FreeStrBuf(&Target);
162         }
163         FreeStrBuf(&UserName);
164         FreeStrBuf(&EmailAddress);
165         FreeStrBuf(&EncBuf);
166
167         FreeStrBuf(&Source);
168 }
169
170
171
172 static void AddStrBufSimlpeTests(void)
173 {
174         CU_pSuite pGroup = NULL;
175         CU_pTest pTest = NULL;
176
177         pGroup = CU_add_suite("TestStringBufConversions", NULL, NULL);
178         if (!parse_email) {
179                 if (!fromstdin) {
180                         pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
181                         pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
182                         pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
183                         pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
184                 }
185                 else
186                         pTest = CU_add_test(pGroup, "testRFC822DecodeSTDIN", TestRFC822DecodeStdin);
187         }
188         else {
189                 if (!fromstdin) {
190                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmail);
191                 }
192                 else
193                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmailSTDIN);
194         }
195
196 }
197
198
199 int main(int argc, char* argv[])
200 {
201         int a;
202
203         while ((a = getopt(argc, argv, "@i")) != EOF)
204                 switch (a) {
205                 case '@':
206                         parse_email = 1;
207                         break;
208                 case 'i':
209                         fromstdin = 1;
210                         
211                         break;
212                 }
213
214
215         setvbuf(stdout, NULL, _IONBF, 0);
216
217         StartLibCitadel(8);
218         CU_BOOL Run = CU_FALSE ;
219         
220         CU_set_output_filename("TestAutomated");
221         if (CU_initialize_registry()) {
222                 printf("\nInitialize of test Registry failed.");
223         }
224         
225         Run = CU_TRUE ;
226         AddStrBufSimlpeTests();
227         
228         if (CU_TRUE == Run) {
229                 //CU_console_run_tests();
230                 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
231     
232                 ///CU_automated_run_tests();
233         }
234         
235         CU_cleanup_registry();
236
237         return 0;
238 }