reSIProcate/stack  9694
testTuple.cxx
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <iostream>
00006 #include "rutil/resipfaststreams.hxx"
00007 #include "rutil/Inserter.hxx"
00008 #include "resip/stack/Connection.hxx"
00009 #include "resip/stack/Tuple.hxx"
00010 
00011 using namespace resip;
00012 using namespace std;
00013 
00014 int
00015 main()
00016 {
00017    typedef HashMap<Tuple, Connection*> AddrMap;
00018    //typedef std::map<Tuple, Connection*> AddrMap;
00019 
00020    // Test isPrivateAddress function - v4
00021    {
00022       Tuple testTuple("192.168.1.106", 5069, V4, TCP);
00023       assert(testTuple.isPrivateAddress());
00024       Tuple testTuple2("10.0.0.5", 5069, V4, TCP);
00025       assert(testTuple2.isPrivateAddress());
00026       Tuple testTuple3("172.16.10.5", 5069, V4, TCP);
00027       assert(testTuple3.isPrivateAddress());
00028       Tuple testTuple4("150.1.1.106", 5069, V4, TCP);
00029       assert(!testTuple4.isPrivateAddress());
00030       Tuple testTuple5("127.0.0.1", 5069, V4, TCP);
00031       assert(!testTuple5.isPrivateAddress());
00032    }
00033 
00034 #ifdef USE_IPV6
00035    // Test isPrivateAddress function - v6
00036    {
00037       Tuple testTuple("fd00::1", 5069, V6, TCP);
00038       assert(testTuple.isPrivateAddress());
00039       Tuple testTuple1("fd9b:70f6:0a18:31cc::1", 5069, V6, TCP);
00040       assert(testTuple1.isPrivateAddress());
00041       Tuple testTuple2("fe80::1", 5069, V6, TCP);          // Link Local ?slg? do we want this to return private or not?  For now we do only RFC 4193
00042       assert(!testTuple2.isPrivateAddress());
00043       Tuple testTuple3("::ffff:10.0.0.5", 5069, V6, TCP);  // V4 mapped
00044       assert(!testTuple3.isPrivateAddress());
00045       Tuple testTuple4("::ffff:150.1.1.106", 5069, V6, TCP); // ?slg? do we want this to return private or not?    For now we do only RFC 4193
00046       assert(!testTuple4.isPrivateAddress());
00047       Tuple testTuple5("::10.0.0.5", 5069, V6, TCP);       // V4 compatible
00048       assert(!testTuple5.isPrivateAddress());
00049       Tuple testTuple6("::150.1.1.106", 5069, V6, TCP);   // ?slg? do we want this to return private or not?  For now we do only RFC 4193
00050       assert(!testTuple6.isPrivateAddress());
00051       Tuple testTuple7("2000:1::203:baff:fe30:1176", 5069, V6, TCP);
00052       assert(!testTuple7.isPrivateAddress());
00053    }
00054 #endif
00055 
00056    {
00057       Tuple testTuple("192.168.1.106", 5069, V4, TCP);
00058       Data binaryToken;
00059       Tuple::writeBinaryToken(testTuple, binaryToken);
00060       Data binaryTokenWithSalt;
00061       Tuple::writeBinaryToken(testTuple, binaryTokenWithSalt, "salt");
00062       Tuple madeTestTuple = Tuple::makeTupleFromBinaryToken(binaryToken);
00063       Tuple madeTestTupleWithSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "salt");
00064       Tuple madeTestTupleWithBadSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "badsalt");
00065       assert(testTuple == madeTestTuple);
00066       assert(testTuple.onlyUseExistingConnection == madeTestTuple.onlyUseExistingConnection);
00067       assert(testTuple.mFlowKey == madeTestTuple.mFlowKey);
00068       assert(testTuple == madeTestTupleWithSalt);
00069       assert(testTuple.onlyUseExistingConnection == madeTestTupleWithSalt.onlyUseExistingConnection);
00070       assert(testTuple.mFlowKey == madeTestTupleWithSalt.mFlowKey);
00071       assert(madeTestTupleWithBadSalt == Tuple());
00072    }
00073 
00074 #ifdef USE_IPV6
00075    {
00076       Tuple testTuple("2000:1::203:baff:fe30:1176", 5069, V6, TCP);
00077       Data binaryToken;
00078       Tuple::writeBinaryToken(testTuple, binaryToken);
00079       Data binaryTokenWithSalt;
00080       Tuple::writeBinaryToken(testTuple, binaryTokenWithSalt, "salt");
00081       Tuple madeTestTuple = Tuple::makeTupleFromBinaryToken(binaryToken);
00082       Tuple madeTestTupleWithSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "salt");
00083       Tuple madeTestTupleWithBadSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "badsalt");
00084       assert(testTuple == madeTestTuple);
00085       assert(testTuple.onlyUseExistingConnection == madeTestTuple.onlyUseExistingConnection);
00086       assert(testTuple.mFlowKey == madeTestTuple.mFlowKey);
00087       assert(testTuple == madeTestTupleWithSalt);
00088       assert(testTuple.onlyUseExistingConnection == madeTestTupleWithSalt.onlyUseExistingConnection);
00089       assert(testTuple.mFlowKey == madeTestTupleWithSalt.mFlowKey);
00090       assert(madeTestTupleWithBadSalt == Tuple());
00091    }
00092 #endif
00093 
00094 #ifdef USE_IPV6
00095    {
00096       AddrMap mMap;
00097       Tuple t("2000:1::203:baff:fe30:1176", 5100, V6, TCP);
00098       Tuple s = t;
00099       assert(s == t);
00100       assert(s.hash() == t.hash());
00101       mMap[t] = 0;
00102       resipCerr << mMap.count(t) << endl;
00103       resipCerr << Inserter(mMap) << std::endl;
00104       
00105       assert(mMap.count(t) == 1);
00106    }
00107 #endif
00108 
00109    {
00110       Tuple t1("192.168.1.2", 2060, UDP);
00111       Tuple t2("192.168.1.2", 2060, UDP);
00112       Tuple t3("192.168.1.3", 2060, UDP);
00113       Tuple t4("192.1.2.3", 2061, UDP);
00114       Tuple t5("192.168.1.2", 2060, TCP);
00115       Tuple t6("192.168.1.2", 2061, UDP);
00116       Tuple loopback("127.0.0.1",2062,TCP);
00117 
00118       assert(t1.isEqualWithMask(t2, 32, false /* ignorePort? */));
00119       assert(!t1.isEqualWithMask(t3, 32, false));  // address is different
00120       assert(t1.isEqualWithMask(t3, 24, false));
00121       assert(t1.isEqualWithMask(t4, 8, true));
00122       assert(!t1.isEqualWithMask(t5, 8, true));    // transport type is different
00123       assert(!t1.isEqualWithMask(t6, 8, false));   // port is different
00124       assert(loopback.isLoopback());
00125       assert(!t1.isLoopback());
00126       
00127       resip::Data token1;
00128       resip::Data token2;
00129       resip::Data token3;
00130       resip::Data token4;
00131       resip::Data token5;
00132       resip::Data token6;
00133       resip::Data tokenloopback;
00134       
00135       Tuple::writeBinaryToken(t1,token1);
00136       Tuple::writeBinaryToken(t2,token2);
00137       Tuple::writeBinaryToken(t3,token3);
00138       Tuple::writeBinaryToken(t4,token4);
00139       Tuple::writeBinaryToken(t5,token5);
00140       Tuple::writeBinaryToken(t6,token6);
00141       Tuple::writeBinaryToken(loopback,tokenloopback);
00142             
00143       Tuple t1prime=Tuple::makeTupleFromBinaryToken(token1);
00144       Tuple t2prime=Tuple::makeTupleFromBinaryToken(token2);
00145       Tuple t3prime=Tuple::makeTupleFromBinaryToken(token3);
00146       Tuple t4prime=Tuple::makeTupleFromBinaryToken(token4);
00147       Tuple t5prime=Tuple::makeTupleFromBinaryToken(token5);
00148       Tuple t6prime=Tuple::makeTupleFromBinaryToken(token6);
00149       Tuple loopbackprime=Tuple::makeTupleFromBinaryToken(tokenloopback);
00150       
00151       assert(t1==t1prime);
00152       assert(t2==t2prime);
00153       assert(t3==t3prime);
00154       assert(t4==t4prime);
00155       assert(t5==t5prime);
00156       assert(t6==t6prime);
00157       assert(loopback==loopbackprime);
00158       assert(t1.onlyUseExistingConnection == t1prime.onlyUseExistingConnection);
00159       assert(t2.onlyUseExistingConnection == t2prime.onlyUseExistingConnection);
00160       assert(t3.onlyUseExistingConnection == t3prime.onlyUseExistingConnection);
00161       assert(t4.onlyUseExistingConnection == t4prime.onlyUseExistingConnection);
00162       assert(t5.onlyUseExistingConnection == t5prime.onlyUseExistingConnection);
00163       assert(t6.onlyUseExistingConnection == t6prime.onlyUseExistingConnection);
00164       assert(loopback.onlyUseExistingConnection == loopbackprime.onlyUseExistingConnection);
00165       assert(t1.mFlowKey == t1prime.mFlowKey);
00166       assert(t2.mFlowKey == t2prime.mFlowKey);
00167       assert(t3.mFlowKey == t3prime.mFlowKey);
00168       assert(t4.mFlowKey == t4prime.mFlowKey);
00169       assert(t5.mFlowKey == t5prime.mFlowKey);
00170       assert(t6.mFlowKey == t6prime.mFlowKey);
00171       assert(loopback.mFlowKey == loopbackprime.mFlowKey);
00172    }
00173 
00174 #ifdef USE_IPV6
00175    {
00176       Tuple t1("2000:1::203:baff:fe30:1176", 2060, UDP);
00177       Tuple t2("2000:1::203:baff:fe30:1176", 2060, UDP);
00178       Tuple t3("2000:1::203:1111:2222:3333", 2060, UDP);
00179       Tuple t4("2000:1::0000:1111:2222:3333", 2061, UDP);
00180       Tuple t5("2000:1::204:1111:2222:3333", 2061, TCP);
00181       Tuple t6("2000:1::203:baff:fe30:1177", 2060, UDP);
00182       Tuple loopback("::1",2062,TCP);
00183       
00184       assert(t1.isEqualWithMask(t2, 128, false /* ignorePort? */));
00185       assert(t1.isEqualWithMask(t3, 80, false));
00186       assert(t1.isEqualWithMask(t4, 64, true));
00187       assert(!t1.isEqualWithMask(t5, 64, true));    // transport type is different
00188       assert(t1.isEqualWithMask(t6, 120, false)); 
00189       assert(loopback.isLoopback());
00190       assert(!t1.isLoopback());
00191       
00192       resip::Data token1;
00193       resip::Data token2;
00194       resip::Data token3;
00195       resip::Data token4;
00196       resip::Data token5;
00197       resip::Data token6;
00198       resip::Data tokenloopback;
00199       
00200       Tuple::writeBinaryToken(t1,token1);
00201       Tuple::writeBinaryToken(t2,token2);
00202       Tuple::writeBinaryToken(t3,token3);
00203       Tuple::writeBinaryToken(t4,token4);
00204       Tuple::writeBinaryToken(t5,token5);
00205       Tuple::writeBinaryToken(t6,token6);
00206       Tuple::writeBinaryToken(loopback,tokenloopback);
00207             
00208       Tuple t1prime=Tuple::makeTupleFromBinaryToken(token1);
00209       Tuple t2prime=Tuple::makeTupleFromBinaryToken(token2);
00210       Tuple t3prime=Tuple::makeTupleFromBinaryToken(token3);
00211       Tuple t4prime=Tuple::makeTupleFromBinaryToken(token4);
00212       Tuple t5prime=Tuple::makeTupleFromBinaryToken(token5);
00213       Tuple t6prime=Tuple::makeTupleFromBinaryToken(token6);
00214       Tuple loopbackprime=Tuple::makeTupleFromBinaryToken(tokenloopback);
00215       
00216       assert(t1==t1prime);
00217       assert(t2==t2prime);
00218       assert(t3==t3prime);
00219       assert(t4==t4prime);
00220       assert(t5==t5prime);
00221       assert(t6==t6prime);
00222       assert(loopback==loopbackprime);
00223       assert(t1.onlyUseExistingConnection == t1prime.onlyUseExistingConnection);
00224       assert(t2.onlyUseExistingConnection == t2prime.onlyUseExistingConnection);
00225       assert(t3.onlyUseExistingConnection == t3prime.onlyUseExistingConnection);
00226       assert(t4.onlyUseExistingConnection == t4prime.onlyUseExistingConnection);
00227       assert(t5.onlyUseExistingConnection == t5prime.onlyUseExistingConnection);
00228       assert(t6.onlyUseExistingConnection == t6prime.onlyUseExistingConnection);
00229       assert(loopback.onlyUseExistingConnection == loopbackprime.onlyUseExistingConnection);
00230       assert(t1.mFlowKey == t1prime.mFlowKey);
00231       assert(t2.mFlowKey == t2prime.mFlowKey);
00232       assert(t3.mFlowKey == t3prime.mFlowKey);
00233       assert(t4.mFlowKey == t4prime.mFlowKey);
00234       assert(t5.mFlowKey == t5prime.mFlowKey);
00235       assert(t6.mFlowKey == t6prime.mFlowKey);
00236       assert(loopback.mFlowKey == loopbackprime.mFlowKey);
00237    }
00238 #endif
00239 
00240    resipCerr << "ALL OK" << std::endl;
00241 }
00242 
00243 /* ====================================================================
00244  * The Vovida Software License, Version 1.0 
00245  * 
00246  * Redistribution and use in source and binary forms, with or without
00247  * modification, are permitted provided that the following conditions
00248  * are met:
00249  * 
00250  * 1. Redistributions of source code must retain the above copyright
00251  *    notice, this list of conditions and the following disclaimer.
00252  * 
00253  * 2. Redistributions in binary form must reproduce the above copyright
00254  *    notice, this list of conditions and the following disclaimer in
00255  *    the documentation and/or other materials provided with the
00256  *    distribution.
00257  * 
00258  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00259  *    and "Vovida Open Communication Application Library (VOCAL)" must
00260  *    not be used to endorse or promote products derived from this
00261  *    software without prior written permission. For written
00262  *    permission, please contact vocal@vovida.org.
00263  *
00264  * 4. Products derived from this software may not be called "VOCAL", nor
00265  *    may "VOCAL" appear in their name, without prior written
00266  *    permission of Vovida Networks, Inc.
00267  * 
00268  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00269  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00270  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00271  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00272  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00273  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00274  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00275  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00276  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00277  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00278  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00279  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00280  * DAMAGE.
00281  * 
00282  * ====================================================================
00283  */