Back to index

libcitadel  8.12
stringbuf_conversion.c
Go to the documentation of this file.
00001 /*
00002  *  CUnit - A Unit testing framework library for C.
00003  *  Copyright (C) 2001  Anil Kumar
00004  *  
00005  *  This library is open source software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  */
00015 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <stdio.h>
00020 #include <unistd.h>
00021 
00022 #include "stringbuf_test.h"
00023 #include "../lib/libcitadel.h"
00024 
00025 
00026 int fromstdin = 0;
00027 int parse_email = 0;
00028 int parse_html = 0;
00029 int OutputEscape = 0;
00030 int OutputEscapeAs = 0;
00031 
00032 static void TestRevalidateStrBuf(StrBuf *Buf)
00033 {
00034        CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
00035 }
00036 
00037 
00038 
00039 /*
00040 Some samples from the original...
00041        CU_ASSERT_EQUAL(10, 10);
00042        CU_ASSERT_EQUAL(0, -0);
00043        CU_ASSERT_EQUAL(-12, -12);
00044        CU_ASSERT_NOT_EQUAL(10, 11);
00045        CU_ASSERT_NOT_EQUAL(0, -1);
00046        CU_ASSERT_NOT_EQUAL(-12, -11);
00047        CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
00048        CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
00049        CU_ASSERT_PTR_NULL(NULL);
00050        CU_ASSERT_PTR_NULL(0x0);
00051        CU_ASSERT_PTR_NOT_NULL((void*)0x23);
00052        CU_ASSERT_STRING_EQUAL(str1, str2);
00053        CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
00054        CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
00055        CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
00056        CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
00057        CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
00058        CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
00059        CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
00060        CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
00061        CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
00062        CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
00063        CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
00064        CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
00065        CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
00066        CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
00067 */
00068 
00069 static void TestRFC822Decode(void)
00070 {
00071        StrBuf *Target;
00072        StrBuf *Source;
00073        StrBuf *DefaultCharset;
00074        StrBuf *FoundCharset;
00075        
00076        DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
00077        FoundCharset = NewStrBuf();
00078        Source = NewStrBufPlain(HKEY("=?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?="));
00079        Target = NewStrBuf();
00080 
00081        StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
00082 
00083 
00084        TestRevalidateStrBuf(Target);
00085        printf("the ugly multi:>%s<\n", ChrPtr(Target));
00086        FreeStrBuf(&Source);
00087        FreeStrBuf(&Target);
00088        FreeStrBuf(&FoundCharset);
00089        FreeStrBuf(&DefaultCharset);
00090 
00091 
00092        DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
00093        FoundCharset = NewStrBuf();
00094        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"));
00095        Target = NewStrBufPlain(NULL, 256);
00096 
00097        StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
00098        TestRevalidateStrBuf(Target);
00099        printf("the ugly multi:>%s<\n", ChrPtr(Target));
00100        FreeStrBuf(&Source);
00101        FreeStrBuf(&Target);
00102        FreeStrBuf(&FoundCharset);
00103        FreeStrBuf(&DefaultCharset);
00104 
00105 }
00106 
00107 
00108 static void TestRFC822DecodeStdin(void)
00109 {
00110        int fdin = 0;// STDIN
00111        const char *Err;
00112        StrBuf *Target;
00113        StrBuf *Source;
00114        StrBuf *DefaultCharset;
00115        StrBuf *FoundCharset;
00116        
00117        DefaultCharset = NewStrBufPlain(HKEY("iso-8859-1"));
00118        FoundCharset = NewStrBuf();
00119        Source = NewStrBuf();
00120 
00121        while (fdin == 0) {
00122 
00123               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00124               Target = NewStrBuf();
00125               
00126               StrBuf_RFC822_to_Utf8(Target, Source, DefaultCharset, FoundCharset);
00127               
00128               TestRevalidateStrBuf(Target);
00129               printf("the ugly multi:>%s<\n", ChrPtr(Target));
00130               FreeStrBuf(&Target);
00131        }
00132        FreeStrBuf(&Source);
00133        FreeStrBuf(&FoundCharset);
00134        FreeStrBuf(&DefaultCharset);
00135 }
00136 
00137 
00138 static void TestHTMLEscEncodeStdin(void)
00139 {
00140        int fdin = 0;// STDIN
00141        const char *Err;
00142        StrBuf *Target;
00143        StrBuf *Source;
00144 
00145        Source = NewStrBuf();
00146 
00147        while (fdin == 0) {
00148 
00149               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00150               Target = NewStrBuf();
00151               
00152               StrEscAppend(Target, Source, NULL, 0, 2);
00153               
00154               TestRevalidateStrBuf(Target);
00155               printf("%s\n", ChrPtr(Target));
00156               FreeStrBuf(&Target);
00157        }
00158        FreeStrBuf(&Source);
00159 }
00160 
00161 static void TestEscEncodeStdin(void)
00162 {
00163        int fdin = 0;// STDIN
00164        const char *Err;
00165        StrBuf *Target;
00166        StrBuf *Source;
00167 
00168        Source = NewStrBuf();
00169 
00170        while (fdin == 0) {
00171 
00172               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00173               Target = NewStrBuf();
00174               
00175               StrEscAppend(Target, Source, NULL, 0, 0);
00176               
00177               TestRevalidateStrBuf(Target);
00178               printf("%s\n", ChrPtr(Target));
00179               FreeStrBuf(&Target);
00180        }
00181        FreeStrBuf(&Source);
00182 }
00183 
00184 
00185 static void TestECMAEscEncodeStdin(void)
00186 {
00187        int fdin = 0;// STDIN
00188        const char *Err;
00189        StrBuf *Target;
00190        StrBuf *Source;
00191 
00192        Source = NewStrBuf();
00193 
00194        printf("[");
00195        while (fdin == 0) {
00196 
00197               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00198               Target = NewStrBuf();
00199               
00200               StrECMAEscAppend(Target, Source, NULL);
00201               
00202               TestRevalidateStrBuf(Target);
00203               printf("\"%s\",\n", ChrPtr(Target));
00204               FreeStrBuf(&Target);
00205        }
00206        printf("]\n");
00207        FreeStrBuf(&Source);
00208 }
00209 
00210 static void TestHtmlEcmaEscEncodeStdin(void)
00211 {
00212        int fdin = 0;// STDIN
00213        const char *Err;
00214        StrBuf *Target;
00215        StrBuf *Source;
00216 
00217        Source = NewStrBuf();
00218 
00219        printf("[");
00220        while (fdin == 0) {
00221 
00222               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00223               Target = NewStrBuf();
00224               
00225               StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 2);
00226               
00227               TestRevalidateStrBuf(Target);
00228               printf("\"%s\",\n", ChrPtr(Target));
00229               FreeStrBuf(&Target);
00230        }
00231        printf("]");
00232        FreeStrBuf(&Source);
00233 }
00234 
00235 static void TestUrlescEncodeStdin(void)
00236 {
00237        int fdin = 0;// STDIN
00238        const char *Err;
00239        StrBuf *Target;
00240        StrBuf *Source;
00241 
00242        Source = NewStrBuf();
00243 
00244        while (fdin == 0) {
00245 
00246               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00247               Target = NewStrBuf();
00248               
00249               StrBufUrlescAppend(Target, Source, NULL);
00250               
00251               TestRevalidateStrBuf(Target);
00252               printf("%s\n", ChrPtr(Target));
00253               FreeStrBuf(&Target);
00254        }
00255        FreeStrBuf(&Source);
00256 }
00257 
00258 
00259 static void TestEncodeEmail(void)
00260 {
00261        StrBuf *Target;
00262        StrBuf *Source;
00263        StrBuf *UserName = NewStrBuf();
00264        StrBuf *EmailAddress = NewStrBuf();
00265        StrBuf *EncBuf = NewStrBuf();
00266        
00267        Source = NewStrBuf();
00268  
00269 //     Source = NewStrBufPlain(HKEY("Art Cancro <ajc@uncensored.citadel.org>, Art Cancro <ajc@uncensored.citadel.org>"));
00270 
00271        Source = NewStrBufPlain(HKEY("\"Alexandra Weiz, Restless GmbH\" <alexandra.weiz@boblbee.de>, \"NetIN\" <editor@netin.co.il>, \" יריב ברקאי, מולטימדי\" <info@immembed.com>"));     
00272        Target = StrBufSanitizeEmailRecipientVector(
00273               Source,
00274               UserName, 
00275               EmailAddress,
00276               EncBuf
00277               );            
00278        
00279        TestRevalidateStrBuf(Target);
00280        printf("the source:>%s<\n", ChrPtr(Source));
00281        printf("the target:>%s<\n", ChrPtr(Target));
00282        FreeStrBuf(&Target);
00283        FreeStrBuf(&UserName);
00284        FreeStrBuf(&EmailAddress);
00285        FreeStrBuf(&EncBuf);
00286 
00287        FreeStrBuf(&Source);
00288 }
00289 
00290 static void TestEncodeEmailSTDIN(void)
00291 {
00292        int fdin = 0;// STDIN
00293        const char *Err;
00294        StrBuf *Target;
00295        StrBuf *Source;
00296        StrBuf *UserName = NewStrBuf();
00297        StrBuf *EmailAddress = NewStrBuf();
00298        StrBuf *EncBuf = NewStrBuf();
00299        
00300        Source = NewStrBuf();
00301 
00302        while (fdin == 0) {
00303 
00304               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00305               printf("the source:>%s<\n", ChrPtr(Source));
00306               Target = StrBufSanitizeEmailRecipientVector(
00307                      Source,
00308                      UserName, 
00309                      EmailAddress,
00310                      EncBuf
00311                      );
00312               
00313               TestRevalidateStrBuf(Target);
00314               printf("the target:>%s<\n", ChrPtr(Target));
00315               FreeStrBuf(&Target);
00316        }
00317        FreeStrBuf(&UserName);
00318        FreeStrBuf(&EmailAddress);
00319        FreeStrBuf(&EncBuf);
00320 
00321        FreeStrBuf(&Source);
00322 }
00323 
00324 
00325 static void TestHTML2ASCII_line(void)
00326 {
00327        int fdin = 0;// STDIN
00328        const char *Err;
00329        StrBuf *Source;
00330        char *Target;
00331 
00332        Source = NewStrBuf();
00333 
00334        while (fdin == 0) {
00335               
00336               StrBufTCP_read_line(Source, &fdin, 0, &Err);
00337               printf("the source:>%s<\n", ChrPtr(Source));
00338               Target = html_to_ascii(ChrPtr(Source), StrLength(Source), 80, 0);
00339               
00340               printf("the target:>%s<\n", Target);
00341               FlushStrBuf(Source);
00342               free(Target);
00343        }
00344 
00345        FreeStrBuf(&Source);
00346 }
00347 
00348 
00349 static void AddStrBufSimlpeTests(void)
00350 {
00351        CU_pSuite pGroup = NULL;
00352        CU_pTest pTest = NULL;
00353 
00354        pGroup = CU_add_suite("TestStringBufConversions", NULL, NULL);
00355        if (parse_email) {
00356               if (!fromstdin) {
00357                      pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmail);
00358               }
00359               else
00360                      pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmailSTDIN);
00361        }
00362        else if (parse_html) {
00363                      pTest = CU_add_test(pGroup, "TestParseHTMLSTDIN", TestHTML2ASCII_line);
00364        }
00365        else {
00366               if (!fromstdin) {
00367                      pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
00368                      pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
00369                      pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
00370                      pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
00371               }
00372               else
00373               {
00374                      if (!OutputEscape)
00375                             pTest = CU_add_test(pGroup, "testRFC822DecodeSTDIN", TestRFC822DecodeStdin);
00376                      else switch(OutputEscapeAs)
00377                           {
00378                           case 'H':
00379                                  pTest = CU_add_test(pGroup, "TestHTMLEscEncodeStdin", TestHTMLEscEncodeStdin);
00380                                  break;
00381                           case 'X':
00382                                  pTest = CU_add_test(pGroup, "TestEscEncodeStdin", TestEscEncodeStdin);
00383                                  break;
00384                           case 'J':
00385                                  pTest = CU_add_test(pGroup, "TestECMAEscEncodeStdin", TestECMAEscEncodeStdin);
00386                                  break;
00387                           case 'K':
00388                                  pTest = CU_add_test(pGroup, "TestHtmlEcmaEscEncodeStdin", TestHtmlEcmaEscEncodeStdin);
00389                                  break;
00390                           case 'U':
00391                                  pTest = CU_add_test(pGroup, "TestUrlescEncodeStdin", TestUrlescEncodeStdin);
00392                                  break;
00393                           default:
00394                                  printf("%c not supported!\n", OutputEscapeAs);
00395                                  CU_ASSERT(1);
00396                           }
00397               }
00398        }
00399 
00400 }
00401 
00402 
00403 int main(int argc, char* argv[])
00404 {
00405        int a;
00406 
00407        while ((a = getopt(argc, argv, "@iho:")) != EOF)
00408               switch (a) {
00409               case 'o':
00410                      if (optarg)
00411                      {
00412                             OutputEscape = 1;
00413                             OutputEscapeAs = *optarg;
00414                      }
00415                      break;
00416               case 'h':
00417                      parse_html = 1;
00418                      break;
00419               case '@':
00420                      parse_email = 1;
00421                      break;
00422               case 'i':
00423                      fromstdin = 1;
00424                      
00425                      break;
00426               }
00427 
00428 
00429        setvbuf(stdout, NULL, _IONBF, 0);
00430 
00431        StartLibCitadel(8);
00432        CU_BOOL Run = CU_FALSE ;
00433        
00434        CU_set_output_filename("TestAutomated");
00435        if (CU_initialize_registry()) {
00436               printf("\nInitialize of test Registry failed.");
00437        }
00438        
00439        Run = CU_TRUE ;
00440        AddStrBufSimlpeTests();
00441        
00442        if (CU_TRUE == Run) {
00443               //CU_console_run_tests();
00444               printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
00445     
00447        }
00448        
00449        CU_cleanup_registry();
00450 
00451        return 0;
00452 }