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