]> code.citadel.org Git - citadel.git/blob - libcitadel/tests/stringbuf_conversion.c
a554c82618f5858fe2285e819c66d4c8fe4911d3
[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 static void TestRevalidateStrBuf(StrBuf *Buf)
35 {
36         CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
37 }
38
39
40
41 /*
42 Some samples from the original...
43         CU_ASSERT_EQUAL(10, 10);
44         CU_ASSERT_EQUAL(0, -0);
45         CU_ASSERT_EQUAL(-12, -12);
46         CU_ASSERT_NOT_EQUAL(10, 11);
47         CU_ASSERT_NOT_EQUAL(0, -1);
48         CU_ASSERT_NOT_EQUAL(-12, -11);
49         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
50         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
51         CU_ASSERT_PTR_NULL(NULL);
52         CU_ASSERT_PTR_NULL(0x0);
53         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
54         CU_ASSERT_STRING_EQUAL(str1, str2);
55         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
56         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
57         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
58         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
59         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
60         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
61         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
62         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
63         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
64         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -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         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
68         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
69 */
70
71 static void TestRFC822Decode(void)
72 {
73         StrBuf *Target;
74         StrBuf *Source;
75         StrBuf *DefaultCharset;
76         StrBuf *FoundCharset;
77         
78         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
79         FoundCharset = NewStrBuf();
80         Source = NewStrBufPlain(HKEY("=?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?="));
81         Target = NewStrBuf();
82
83         StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
84
85
86         TestRevalidateStrBuf(Target);
87         printf("the ugly multi:>%s<\n", ChrPtr(Target));
88         FreeStrBuf(&Source);
89         FreeStrBuf(&Target);
90         FreeStrBuf(&FoundCharset);
91         FreeStrBuf(&DefaultCharset);
92
93
94         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
95         FoundCharset = NewStrBuf();
96         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"));
97         Target = NewStrBufPlain(NULL, 256);
98
99         StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
100         TestRevalidateStrBuf(Target);
101         printf("the ugly multi:>%s<\n", ChrPtr(Target));
102         FreeStrBuf(&Source);
103         FreeStrBuf(&Target);
104         FreeStrBuf(&FoundCharset);
105         FreeStrBuf(&DefaultCharset);
106
107 }
108
109
110 static void TestRFC822DecodeStdin(void)
111 {
112         int fdin = 0;// STDIN
113         const char *Err;
114         StrBuf *Target;
115         StrBuf *Source;
116         StrBuf *DefaultCharset;
117         StrBuf *FoundCharset;
118         
119         DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
120         FoundCharset = NewStrBuf();
121         Source = NewStrBuf();
122
123         while (fdin == 0) {
124
125                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
126                 Target = NewStrBuf();
127                 
128                 StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
129                 
130                 TestRevalidateStrBuf(Target);
131                 printf("the ugly multi:>%s<\n", ChrPtr(Target));
132                 FreeStrBuf(&Target);
133         }
134         FreeStrBuf(&Source);
135         FreeStrBuf(&FoundCharset);
136         FreeStrBuf(&DefaultCharset);
137 }
138
139
140 static void TestEncodeEmail(void)
141 {
142         StrBuf *Target;
143         StrBuf *Source;
144         StrBuf *UserName = NewStrBuf();
145         StrBuf *EmailAddress = NewStrBuf();
146         StrBuf *EncBuf = NewStrBuf();
147         
148         Source = NewStrBuf();
149  
150 //      Source = NewStrBufPlain(HKEY("Art Cancro <ajc@uncensored.citadel.org>, Art Cancro <ajc@uncensored.citadel.org>"));
151
152         Source = NewStrBufPlain(HKEY("\"Alexandra Weiz, Restless GmbH\" <alexandra.weiz@boblbee.de>, \"NetIN\" <editor@netin.co.il>, \" יריב ברקאי, מולטימדי\" <info@immembed.com>")); 
153         Target = StrBufSanitizeEmailRecipientVector(
154                 Source,
155                 UserName, 
156                 EmailAddress,
157                 EncBuf
158                 );              
159         
160         TestRevalidateStrBuf(Target);
161         printf("the source:>%s<\n", ChrPtr(Source));
162         printf("the target:>%s<\n", ChrPtr(Target));
163         FreeStrBuf(&Target);
164         FreeStrBuf(&UserName);
165         FreeStrBuf(&EmailAddress);
166         FreeStrBuf(&EncBuf);
167
168         FreeStrBuf(&Source);
169 }
170
171 static void TestEncodeEmailSTDIN(void)
172 {
173         int fdin = 0;// STDIN
174         const char *Err;
175         StrBuf *Target;
176         StrBuf *Source;
177         StrBuf *UserName = NewStrBuf();
178         StrBuf *EmailAddress = NewStrBuf();
179         StrBuf *EncBuf = NewStrBuf();
180         
181         Source = NewStrBuf();
182
183         while (fdin == 0) {
184
185                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
186                 printf("the source:>%s<\n", ChrPtr(Source));
187                 Target = StrBufSanitizeEmailRecipientVector(
188                         Source,
189                         UserName, 
190                         EmailAddress,
191                         EncBuf
192                         );
193                 
194                 TestRevalidateStrBuf(Target);
195                 printf("the target:>%s<\n", ChrPtr(Target));
196                 FreeStrBuf(&Target);
197         }
198         FreeStrBuf(&UserName);
199         FreeStrBuf(&EmailAddress);
200         FreeStrBuf(&EncBuf);
201
202         FreeStrBuf(&Source);
203 }
204
205
206 static void TestHTML2ASCII_line(void)
207 {
208         int fdin = 0;// STDIN
209         const char *Err;
210         StrBuf *Source;
211         char *Target;
212
213         Source = NewStrBuf();
214
215         while (fdin == 0) {
216                 
217                 StrBufTCP_read_line(Source, &fdin, 0, &Err);
218                 printf("the source:>%s<\n", ChrPtr(Source));
219                 Target = html_to_ascii(ChrPtr(Source), StrLength(Source), 80, 0);
220                 
221                 printf("the target:>%s<\n", Target);
222                 FlushStrBuf(Source);
223                 free(Target);
224         }
225
226         FreeStrBuf(&Source);
227 }
228
229
230 static void AddStrBufSimlpeTests(void)
231 {
232         CU_pSuite pGroup = NULL;
233         CU_pTest pTest = NULL;
234
235         pGroup = CU_add_suite("TestStringBufConversions", NULL, NULL);
236         if (parse_email) {
237                 if (!fromstdin) {
238                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmail);
239                 }
240                 else
241                         pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmailSTDIN);
242         }
243         else if (parse_html) {
244                         pTest = CU_add_test(pGroup, "TestParseHTMLSTDIN", TestHTML2ASCII_line);
245         }
246         else {
247                 if (!fromstdin) {
248                         pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
249                         pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
250                         pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
251                         pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
252                 }
253                 else
254                         pTest = CU_add_test(pGroup, "testRFC822DecodeSTDIN", TestRFC822DecodeStdin);
255         }
256
257 }
258
259
260 int main(int argc, char* argv[])
261 {
262         int a;
263
264         while ((a = getopt(argc, argv, "@ih")) != EOF)
265                 switch (a) {
266                 case 'h':
267                         parse_html = 1;
268                         break;
269                 case '@':
270                         parse_email = 1;
271                         break;
272                 case 'i':
273                         fromstdin = 1;
274                         
275                         break;
276                 }
277
278
279         setvbuf(stdout, NULL, _IONBF, 0);
280
281         StartLibCitadel(8);
282         CU_BOOL Run = CU_FALSE ;
283         
284         CU_set_output_filename("TestAutomated");
285         if (CU_initialize_registry()) {
286                 printf("\nInitialize of test Registry failed.");
287         }
288         
289         Run = CU_TRUE ;
290         AddStrBufSimlpeTests();
291         
292         if (CU_TRUE == Run) {
293                 //CU_console_run_tests();
294                 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
295     
296                 ///CU_automated_run_tests();
297         }
298         
299         CU_cleanup_registry();
300
301         return 0;
302 }