reSIProcate/rutil  9694
testParseBuffer.cxx
Go to the documentation of this file.
00001 #include "rutil/ParseBuffer.hxx"
00002 #include <string.h>
00003 #include <assert.h>
00004 #include "rutil/Logger.hxx"
00005 
00006 using namespace resip;
00007 
00008 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00009 
00010 int
00011 main(int argc, char** argv)
00012 {
00013    Log::initialize(Log::Cout, argc > 1 ? Log::toLevel(argv[1]) :  Log::Info, argv[0]);
00014    
00015    {
00016      const char buf[] = "/home/jason/test";
00017      ParseBuffer pb(buf);
00018      pb.skipToEnd();
00019      pb.skipBackToChar('/');
00020      assert(!pb.bof());
00021      Data result(pb.position());
00022      assert(result == "test");
00023    }
00024 
00025    {
00026      const char buf[] = "test";
00027      ParseBuffer pb(buf);
00028      pb.skipToEnd();
00029      pb.skipBackToChar('/');
00030      assert(pb.bof());
00031      Data result(pb.position());
00032      assert(result == "test");
00033    }
00034 
00035    {
00036      const char buf[] = "/test";
00037      ParseBuffer pb(buf);
00038      pb.skipToEnd();
00039      pb.skipBackToChar('/');
00040      Data result(pb.position());
00041      assert(result == "test");
00042    }
00043 
00044    {
00045       const char buf[] = "!^.*$!sip:user@example.com!";
00046       ParseBuffer pb(buf, strlen(buf));
00047 
00048       const char delim = buf[0];
00049       const char* start = pb.skipChar(delim);
00050       std::cerr << "start=" << start << std::endl;
00051       pb.skipToChar(delim);
00052 
00053       Data e1;
00054       pb.data(e1, start);
00055       std::cerr << "e1=" << e1 << std::endl;
00056       assert(e1 == "^.*$");
00057        
00058       start = pb.skipChar(delim);
00059       Data e2;
00060       pb.skipToChar(delim);
00061       pb.data(e2, start);
00062       std::cerr << "e2=" << e2 << std::endl;
00063       assert(e2 == "sip:user@example.com");
00064       
00065       start = pb.skipChar(delim);
00066       Data e3;
00067       pb.data(e3, start);
00068       std::cerr << "e3=" << e3 << std::endl;
00069       assert(e3.empty());
00070    }
00071    
00072    {
00073       const char buf[] = "Ducky%20%26";
00074       ParseBuffer pb(buf, strlen(buf));
00075 
00076       const char* start = pb.skipWhitespace();
00077       pb.skipToEnd();
00078       
00079       Data target;
00080       pb.dataUnescaped(target, start);
00081       
00082       assert(target == "Ducky &");
00083    }
00084    
00085    {
00086       const char buf[] = "  \r\t\r\n\t  !";
00087       ParseBuffer pb(buf, strlen(buf));
00088       
00089       pb.skipWhitespace();
00090       assert(*pb.position() == '!');
00091    }
00092 
00093    {
00094       const char buf[] = "asdfa1234123edfdf213ref@!\t  \r\t\r\n\t ";
00095       ParseBuffer pb(buf, strlen(buf));
00096       
00097       pb.skipNonWhitespace();
00098       assert(*pb.position() == '\t');
00099    }
00100 
00101    {
00102       std::cerr << "!! Test position" << std::endl;
00103       char buf[] = "Here is a buffer with some stuff.";
00104       ParseBuffer pb(buf, strlen(buf));
00105       
00106       pb.skipToChars("buff");
00107       *pb.position();
00108 
00109       pb.skipToEnd();
00110 
00111       do
00112       {
00113          try
00114          {
00115             *pb.position();
00116             assert(false);
00117          }
00118          catch (BaseException& e)
00119          {
00120             break;
00121          }
00122       } while (false);
00123    }
00124 
00125    {
00126       std::cerr << "!! Test fail one line" << std::endl;
00127       
00128       char buf[] = "Here is a \t buffer with some stuff.";
00129       ParseBuffer pb(buf, strlen(buf));
00130 
00131       do
00132       {
00133          try
00134          {
00135             pb.skipChars("Here isn't a");
00136          }
00137          catch (ParseException& e)
00138          {
00139              //std::cerr<< e << std::endl;
00140              break;
00141          }
00142          assert(0);
00143       } while (false);
00144    }
00145 
00146    {
00147       std::cerr << "!! Test fail multiline" << std::endl;
00148       
00149       const Data test("Test input");
00150       char buf[] = "Here is a \r\n buffer with \r\nsome stuff.";
00151       ParseBuffer pb(buf, strlen(buf), test);
00152 
00153       do
00154       {
00155          try
00156          {
00157             pb.skipToChars("buff");
00158             pb.skipChars("buff");
00159             pb.skipChar('g');
00160          }
00161          catch (ParseException& e)
00162          {
00163              //using namespace std;
00164              //cerr << e << endl;
00165              //cerr << '\'' << e.getMessage() << '\'' << endl;
00166              //cerr << '\'' << test << '\'' << endl;
00167             break;
00168          }
00169          assert(0);
00170       } while (false);
00171    }
00172 
00173    {
00174       char buf[] = "Content-Languages: English, \r\n French  , \r\n\t LISP   \r \n \n\r \r\n\r\n";
00175       ParseBuffer pb(buf, strlen(buf));
00176       pb.skipToTermCRLF();
00177       pb.skipChars("\r\n");
00178       pb.skipChars("\r\n");
00179       pb.assertEof();
00180    }
00181 
00182    {
00183       char buf[] = "Content-Languages: English, \r\n French  , \r\n\t LISP   \r \n \n\r \r\n\r\n";
00184       ParseBuffer pb(buf, strlen(buf));
00185       pb.skipToChars("French");
00186       pb.skipN(strlen("French"));
00187       pb.skipWhitespace();
00188       pb.skipChar(',');
00189       pb.skipLWS();
00190       std::cerr << pb.position();
00191       pb.skipChars("LISP");
00192    }
00193 
00194    {
00195       char buf[] = "123456789";
00196       ParseBuffer pb(buf, strlen(buf));
00197       pb.skipN(9);
00198       assert(pb.eof());
00199    }
00200 
00201    {
00202       char buf[] = "123456789";
00203       ParseBuffer pb(buf, strlen(buf));
00204       try
00205       {
00206          char foo = *pb.skipN(9);
00207          (void)foo;
00208          assert(0);
00209       }
00210       catch (ParseException& e)
00211       {}
00212    }
00213 
00214    {
00215       char buf[] = "Here is a \t buffer with some stuff.";
00216       ParseBuffer pb(buf, strlen(buf));
00217       pb.skipToChars("some");
00218       std::cerr << pb.position() << std::endl;
00219       pb.skipChars("some stu");
00220    }
00221 
00222    {
00223       char buf[] = "Here is asom \t buffer with some stuff.";
00224       ParseBuffer pb(buf, strlen(buf));
00225       pb.skipToChars("some");
00226       pb.skipChars("some stuf");
00227    }
00228 
00229    {
00230       char buf[] = "Here is asom \t buffer with som stuff.";
00231       ParseBuffer pb(buf, strlen(buf));
00232       pb.skipToChars("some");
00233       pb.assertEof();
00234    }
00235 
00236    {
00237       char buf[] = "Here is a \t buffer with some stuff.";
00238       ParseBuffer pb(buf, strlen(buf));
00239       pb.skipToChars(Data("some"));
00240       pb.skipChars("some stuf");
00241    }
00242 
00243    {
00244       char buf[] = "Here is asom \t buffer with some stuff.";
00245       ParseBuffer pb(buf, strlen(buf));
00246       pb.skipToChars(Data("some"));
00247       pb.skipChars("some stuf");
00248    }
00249 
00250    {
00251       char buf[] = "Here is asom \t buffer with som stuff.";
00252       ParseBuffer pb(buf, strlen(buf));
00253       pb.skipToChars(Data("some"));
00254       pb.assertEof();
00255    }
00256 
00257    {
00258       char buf[] = "Here is a \t buffer with some stuff.";
00259       ParseBuffer pb(buf, strlen(buf));
00260 
00261       pb.skipChars("Here is a");
00262    }
00263 
00264    {
00265       char buf[] = "Here is a \t buffer with some stuff.";
00266       ParseBuffer pb(buf, strlen(buf));
00267 
00268       do
00269       {
00270          try
00271          {
00272             pb.skipChars("Here isn't a");
00273          }
00274          catch (ParseException& e)
00275          {
00276             break;
00277          }
00278          assert(0);
00279       } while (false);
00280    }
00281 
00282    {
00283       char buf[] = "Here is a buf.";
00284       ParseBuffer pb(buf, strlen(buf));
00285 
00286       do
00287       {
00288          try
00289          {
00290             pb.skipChars("Here is a ");
00291             pb.skipChars("buffer");
00292          }
00293          catch (ParseException& e)
00294          {
00295             break;
00296          }
00297          assert(0);
00298       } while (false);
00299    }
00300    
00301    {
00302       const char* buf = "Here is a \t buffer with some stuff.";
00303 
00304       ParseBuffer pb(buf, strlen(buf));
00305    
00306       assert(!pb.eof());
00307       assert(pb.position() == buf);
00308 
00309       pb.skipWhitespace();
00310       assert(pb.position() == buf);
00311 
00312       pb.skipNonWhitespace();
00313       assert(*pb.position() == ' ');
00314    }
00315 
00316    {
00317       char buf[] = "    \t buffer with some stuff.";
00318       ParseBuffer pb(buf, strlen(buf));   
00319 
00320       pb.reset(pb.end());
00321       pb.skipBackToChar('s');
00322       pb.skipBackChar();
00323       pb.skipBackToChar('s');
00324       pb.skipBackChar('s');
00325 
00326       assert(Data(pb.position(), 4) == "some");
00327    }
00328 
00329    {
00330       char buf[] = "buffer with some stuff.";
00331       ParseBuffer pb(buf, strlen(buf));   
00332 
00333       pb.reset(pb.end());
00334       pb.skipBackToChar('q');
00335       assert(pb.bof());
00336       pb.skipChar('b');
00337    }
00338    
00339    {
00340       char buf[] = "    \t buffer with some stuff.";
00341       ParseBuffer pb(buf, strlen(buf));   
00342 
00343       pb.skipWhitespace();
00344       assert(*pb.position() == 'b');
00345       
00346       pb.skipToChar('s');
00347       assert(*pb.position() == 's');
00348       pb.skipNonWhitespace();
00349       pb.skipWhitespace();
00350       pb.skipNonWhitespace();
00351       assert(pb.eof());
00352    }
00353 
00354    {
00355       char buf[] = "jhsj:!hskd;|.";
00356       ParseBuffer pb(buf, strlen(buf));   
00357 
00358       pb.skipToOneOf(":@");
00359       assert(*pb.position() == ':');
00360    }
00361 
00362    {
00363       char buf[] = "user@host:port";
00364       ParseBuffer pb(buf, strlen(buf));   
00365 
00366       pb.skipToOneOf(":@");
00367       assert(*pb.position() == '@');
00368    }
00369 
00370    {
00371       char buf[] = "jhsjfhskd;|.";
00372       ParseBuffer pb(buf, strlen(buf));   
00373 
00374       pb.skipToOneOf(".|;");
00375       assert(*pb.position() == ';');
00376    }
00377 
00378    {
00379       char buf[] = "\"  \\\"Q \t buffer with some stuff.\"Z";
00380       ParseBuffer pb(buf, strlen(buf));   
00381       pb.skipWhitespace();
00382       pb.skipToChar('"');
00383       pb.skipChar();
00384       pb.skipToEndQuote();
00385       assert(*pb.position() == '"');
00386       pb.skipChar();
00387       assert(*pb.position() == 'Z');
00388    }
00389    
00390    {
00391       char buf[] = "17 ";
00392       ParseBuffer pb(buf, strlen(buf));   
00393       assert(pb.integer() == 17);
00394    }
00395    
00396    {
00397       char buf[] = "-17";
00398       ParseBuffer pb(buf, strlen(buf));   
00399       assert(pb.integer() == -17);
00400    }
00401 
00402    {
00403       char buf[] = "999999999999999999999999999 ";
00404       ParseBuffer pb(buf, strlen(buf));
00405       try
00406       {
00407          pb.integer();
00408          assert(0);
00409       }
00410       catch(ParseException& e)
00411       {}
00412    }
00413    
00414    {
00415       char buf[] = "-999999999999999999999999999 ";
00416       ParseBuffer pb(buf, strlen(buf));
00417       try
00418       {
00419          pb.integer();
00420          assert(0);
00421       }
00422       catch(ParseException& e)
00423       {}
00424    }
00425 
00426 #ifndef WIN32
00427    {
00428       char buf[] = "2890844526";
00429       ParseBuffer pb(buf, strlen(buf));   
00430       assert(pb.uInt64() == 2890844526UL);
00431    }
00432 #endif
00433 
00434 #ifndef RESIP_FIXED_POINT
00435    {
00436       char buf[] = "17.71";
00437       ParseBuffer pb(buf, strlen(buf));   
00438       float val = pb.floatVal();
00439       assert(val > 17.70 && val < 17.72);
00440    }
00441 #endif
00442 
00443    {
00444       char buf[] = "token another token";
00445       ParseBuffer pb(buf, strlen(buf));   
00446       const char *start = pb.position();
00447       pb.skipToChar(' ');
00448       pb.skipChar(' ');
00449       Data t;
00450       // make t share memry with buf
00451       pb.data(t, start);
00452       assert(t.data() == buf);
00453       // assign copies
00454       Data t1 = t;
00455       assert(t1.data() != buf);
00456       // assign copies
00457       t = t1;
00458       assert(t.data() != buf);
00459 
00460       start = pb.position();
00461       pb.skipToChar(' ');
00462       pb.skipChar(' ');
00463       Data t2;
00464       pb.data(t2, start);
00465       // should survive scope exit
00466    }
00467 
00468    std::cerr << "All OK" << std::endl;
00469    return 0;
00470 }
00471 
00472 /* ====================================================================
00473  * The Vovida Software License, Version 1.0 
00474  * 
00475  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00476  * 
00477  * Redistribution and use in source and binary forms, with or without
00478  * modification, are permitted provided that the following conditions
00479  * are met:
00480  * 
00481  * 1. Redistributions of source code must retain the above copyright
00482  *    notice, this list of conditions and the following disclaimer.
00483  * 
00484  * 2. Redistributions in binary form must reproduce the above copyright
00485  *    notice, this list of conditions and the following disclaimer in
00486  *    the documentation and/or other materials provided with the
00487  *    distribution.
00488  * 
00489  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00490  *    and "Vovida Open Communication Application Library (VOCAL)" must
00491  *    not be used to endorse or promote products derived from this
00492  *    software without prior written permission. For written
00493  *    permission, please contact vocal@vovida.org.
00494  *
00495  * 4. Products derived from this software may not be called "VOCAL", nor
00496  *    may "VOCAL" appear in their name, without prior written
00497  *    permission of Vovida Networks, Inc.
00498  * 
00499  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00500  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00501  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00502  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00503  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00504  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00505  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00506  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00507  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00508  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00509  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00510  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00511  * DAMAGE.
00512  * 
00513  * ====================================================================
00514  * 
00515  * This software consists of voluntary contributions made by Vovida
00516  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00517  * Inc.  For more information on Vovida Networks, Inc., please see
00518  * <http://www.vovida.org/>.
00519  *
00520  */