reSIProcate/stack  9694
testEmbedded.cxx
Go to the documentation of this file.
00001 #include "rutil/DataStream.hxx"
00002 
00003 #include "resip/stack/SipMessage.hxx"
00004 #include "resip/stack/Uri.hxx"
00005 #include "resip/stack/Embedded.hxx"
00006 #include "resip/stack/ParserCategories.hxx"
00007 #include "rutil/Logger.hxx"
00008 
00009 using namespace resip;
00010 
00011 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00012 #define RESIP_COOKIE "-524287-"
00013 
00014 using namespace std;
00015 using namespace resip;
00016 
00017 int
00018 main(int argc, char** argv)
00019 {
00020    Log::initialize(Log::Cout, argc > 1 ? Log::toLevel(argv[1]) :  Log::Info, argv[0]);
00021 
00022    {
00023       Data foo("abcdefghi1232454435");
00024       assert(Embedded::encode(foo) == foo);
00025    }
00026 
00027    {
00028       // ^ is not valid in this part of a URI; see RFC 3261.
00029       Data foo("abcdefghi1232454435^ * ");
00030       cerr << Embedded::encode(foo) << endl;
00031       assert(Embedded::encode(foo) == "abcdefghi1232454435%5E%20*%20");
00032    }
00033 
00034    {
00035       Data foo("abcdefghi1232454435^&*&");
00036       unsigned int c;
00037       char* res = Embedded::decode(foo, c);
00038 
00039       cerr << Data(res, c) << endl;
00040       assert(foo == Data(res, c));
00041       delete [] res;
00042    }
00043 
00044    {
00045       Data foo("!@#$%^&*() ?:=17qwerty");
00046       Data bar = Embedded::encode(foo);
00047 
00048       cerr << bar << endl;
00049 
00050       unsigned int c;
00051       char* res = Embedded::decode(bar, c);
00052       cerr << Data(res, c) << endl;
00053       assert(foo == Data(res, c));
00054 
00055       delete [] res;
00056    }
00057 
00058    {
00059       Data foo("!@#$%^&*() ?:=17qwerty");
00060       Data bar(Embedded::encode(Embedded::encode(foo)));
00061 
00062       cerr << bar << endl;
00063 
00064       unsigned int c;
00065       char* res1 = Embedded::decode(bar, c);
00066       Data rab(res1, c);
00067       char* res2 = Embedded::decode(rab, c);
00068 
00069       assert(foo == Data(res2, c));
00070 
00071       delete [] res1;
00072       delete [] res2;
00073    }
00074 
00075    {
00076       cerr << "Produce embedded, single header" << endl;
00077       Uri foo;
00078       foo.user() = "speedy";
00079       foo.host() = "cathaynetworks.com";
00080       foo.embedded().header(h_CSeq).method() = ACK;
00081       foo.embedded().header(h_CSeq).sequence() = 4178;
00082 
00083       Data buf;
00084       {
00085          DataStream str(buf);
00086          foo.encode(str);
00087       }
00088       cerr << endl << buf << endl;
00089       assert(buf == "sip:speedy@cathaynetworks.com?CSeq=4178%20ACK");
00090    }
00091 
00092    {
00093       cerr << "Produce embedded, multiple headers" << endl;
00094       Uri foo;
00095       foo.user() = "speedy";
00096       foo.host() = "cathaynetworks.com";
00097       foo.embedded().header(h_CSeq).method() = ACK;
00098       foo.embedded().header(h_CSeq).sequence() = 4178;
00099 
00100       Via via;
00101       BranchParameter branch = via.param(p_branch);
00102 
00103       branch.reset("fobbieBletch");
00104       via.transport() = "TLS";
00105       via.sentHost() = "cathay.com";
00106       via.sentPort() = 5066;
00107       via.param(p_branch) = branch;
00108       foo.embedded().header(h_Vias).push_back(via);
00109 
00110       branch.reset("bletchieFoo");
00111       via.transport() = "TCP";
00112       via.sentHost() = "ixolib.com";
00113       via.sentPort() = 5067;
00114       via.param(p_branch) = branch;
00115       foo.embedded().header(h_Vias).push_back(via);
00116 
00117       NameAddr route;
00118       route.uri() = Uri("sip:flibble@gronk.example.com");
00119       foo.embedded().header(h_Routes).push_back(route);
00120 
00121       Data buf;
00122       {
00123          DataStream str(buf);
00124          foo.encode(str);
00125       }
00126 
00127       cerr << buf << endl;
00128 
00129       assert(buf == "sip:speedy@cathaynetworks.com?Via=SIP%2F2.0%2FTLS%20cathay.com:5066%3Bbranch%3Dz9hG4bK" RESIP_COOKIE "1---fobbieBletch%3Brport&Via=SIP%2F2.0%2FTCP%20ixolib.com:5067%3Bbranch%3Dz9hG4bK" RESIP_COOKIE "1---bletchieFoo%3Brport&Route=%3Csip:flibble%40gronk.example.com%3E&CSeq=4178%20ACK");
00130    }
00131 
00132    {
00133       cerr << "Parse <Uri> with embedded" << endl;
00134       
00135       Data nad("bob<sips:bob@foo.com?CSeq=314159%20ACK>;tag=wd834f");
00136       NameAddr na(nad); 
00137 
00138       assert(na.uri().hasEmbedded());
00139       assert(na.uri().embedded().exists(h_CSeq));
00140       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00141       assert(na.uri().embedded().header(h_CSeq).sequence() == 314159);
00142    }
00143 
00144    {
00145       cerr << "Parse Uri with embedded" << endl;
00146       
00147       Data nad("sips:bob@foo.com;ttl=134?CSeq=314159%20ACK");
00148       NameAddr na(nad); 
00149 
00150       assert(na.uri().hasEmbedded());
00151       assert(na.uri().embedded().exists(h_CSeq));
00152       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00153       assert(na.uri().embedded().header(h_CSeq).sequence() == 314159);
00154       assert(na.uri().param(p_ttl) == 134);
00155    }
00156 
00157    {
00158       cerr << "Parse Uri with embedded followed by NameAddr parameter" << endl;
00159       
00160       Data nad("sips:bob@foo.com;ttl=134?CSeq=314159%20ACK;tag=17");
00161       NameAddr na(nad);
00162 
00163       assert(na.uri().hasEmbedded());
00164       assert(na.uri().embedded().exists(h_CSeq));
00165       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00166       assert(na.uri().embedded().header(h_CSeq).sequence() == 314159);
00167       assert(na.uri().param(p_ttl) == 134);
00168       assert(na.param(p_tag) == "17");
00169    }
00170 
00171    {
00172       cerr << "Parse Uri with multiple headers" << endl;
00173       
00174       Data nad("sip:speedy@cathaynetworks.com?Via=SIP/2.0/TLS%20cathay.com:5066%3Bbranch%3Dz9hG4bKfobbieBletch-1&Via=SIP/2.0/TCP%20ixolib.com:5067%3Bbranch%3Dz9hG4bKbletchieFoo-1&CSeq=4178%20ACK");
00175       NameAddr na(nad);
00176 
00177       assert(na.uri().hasEmbedded());
00178       assert(na.uri().embedded().exists(h_Vias));
00179       assert(na.uri().embedded().exists(h_CSeq));
00180       
00181       assert(na.uri().embedded().header(h_Vias).size() == 2);
00182       assert(na.uri().embedded().header(h_Vias).front().transport() == "TLS");
00183       assert((++(na.uri().embedded().header(h_Vias).begin()))->transport() == "TCP");
00184       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00185       assert(na.uri().embedded().header(h_CSeq).sequence() == 4178);
00186    }
00187 
00188    {
00189       cerr << "Parse Request-Line with embedded" << endl;
00190       
00191       Data nad("INVITE sips:bob@foo.com?CSeq=314159%20ACK SIP/2.0");
00192       HeaderFieldValue hfv(nad.data(), nad.size());
00193       RequestLine na(hfv); 
00194 
00195       assert(na.uri().hasEmbedded());
00196       assert(na.uri().embedded().exists(h_CSeq));
00197       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00198       assert(na.uri().embedded().header(h_CSeq).sequence() == 314159);
00199    }
00200 
00201    {
00202       cerr << "Parse Request-Line with embedded (and another param)" << endl;
00203       
00204       Data nad("INVITE sips:bob@foo.com;ttl=134?CSeq=314159%20ACK SIP/2.0");
00205       HeaderFieldValue hfv(nad.data(), nad.size());
00206       RequestLine na(hfv); 
00207 
00208       assert(na.uri().hasEmbedded());
00209       assert(na.uri().embedded().exists(h_CSeq));
00210       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00211       assert(na.uri().embedded().header(h_CSeq).sequence() == 314159);
00212       assert(na.uri().param(p_ttl) == 134);
00213    }
00214 
00215    {
00216       cerr << "Parse Request-Line with multiple headers" << endl;
00217       
00218       Data nad("INVITE sip:speedy@cathaynetworks.com?Via=SIP/2.0/TLS%20cathay.com:5066%3Bbranch%3Dz9hG4bKfobbieBletch-1&Via=SIP/2.0/TCP%20ixolib.com:5067%3Bbranch%3Dz9hG4bKbletchieFoo-1&CSeq=4178%20ACK SIP/2.0");
00219       HeaderFieldValue hfv(nad.data(), nad.size());
00220       RequestLine na(hfv); 
00221 
00222       assert(na.uri().hasEmbedded());
00223       assert(na.uri().embedded().exists(h_Vias));
00224       assert(na.uri().embedded().exists(h_CSeq));
00225       
00226       assert(na.uri().embedded().header(h_Vias).size() == 2);
00227       assert(na.uri().embedded().header(h_Vias).front().transport() == "TLS");
00228       assert((++(na.uri().embedded().header(h_Vias).begin()))->transport() == "TCP");
00229       assert(na.uri().embedded().header(h_CSeq).method() == ACK);
00230       assert(na.uri().embedded().header(h_CSeq).sequence() == 4178);
00231    }
00232 
00233    cerr << endl << "Tests OK" << endl;
00234    return 0;
00235 }
00236 /* ====================================================================
00237  * The Vovida Software License, Version 1.0 
00238  * 
00239  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00240  * 
00241  * Redistribution and use in source and binary forms, with or without
00242  * modification, are permitted provided that the following conditions
00243  * are met:
00244  * 
00245  * 1. Redistributions of source code must retain the above copyright
00246  *    notice, this list of conditions and the following disclaimer.
00247  * 
00248  * 2. Redistributions in binary form must reproduce the above copyright
00249  *    notice, this list of conditions and the following disclaimer in
00250  *    the documentation and/or other materials provided with the
00251  *    distribution.
00252  * 
00253  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00254  *    and "Vovida Open Communication Application Library (VOCAL)" must
00255  *    not be used to endorse or promote products derived from this
00256  *    software without prior written permission. For written
00257  *    permission, please contact vocal@vovida.org.
00258  *
00259  * 4. Products derived from this software may not be called "VOCAL", nor
00260  *    may "VOCAL" appear in their name, without prior written
00261  *    permission of Vovida Networks, Inc.
00262  * 
00263  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00264  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00265  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00266  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00267  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00268  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00269  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00270  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00271  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00272  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00273  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00274  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00275  * DAMAGE.
00276  * 
00277  * ====================================================================
00278  * 
00279  * This software consists of voluntary contributions made by Vovida
00280  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00281  * Inc.  For more information on Vovida Networks, Inc., please see
00282  * <http://www.vovida.org/>.
00283  *
00284  */