/[resiprocate]/main/resip/stack/test/testTuple.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testTuple.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11153 - (show annotations) (download)
Wed Apr 23 17:25:02 2014 UTC (5 years, 7 months ago) by dpetrie
File MIME type: text/plain
File size: 14604 byte(s)
added netns scope/context to Uri and Tuple

1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <iostream>
6 #include "rutil/resipfaststreams.hxx"
7 #include "rutil/Inserter.hxx"
8 #include "resip/stack/Connection.hxx"
9 #include "resip/stack/Tuple.hxx"
10 #ifdef USE_NETNS
11 # include "rutil/NetNs.hxx"
12 #endif
13
14 using namespace resip;
15 using namespace std;
16
17 int
18 main()
19 {
20 typedef HashMap<Tuple, Connection*> AddrMap;
21 //typedef std::map<Tuple, Connection*> AddrMap;
22
23 // Test isPrivateAddress function - v4
24 {
25 Tuple testTuple("192.168.1.106", 5069, V4, TCP);
26 assert(testTuple.isPrivateAddress());
27 Tuple testTuple2("10.0.0.5", 5069, V4, TCP);
28 assert(testTuple2.isPrivateAddress());
29 Tuple testTuple3("172.16.10.5", 5069, V4, TCP);
30 assert(testTuple3.isPrivateAddress());
31 Tuple testTuple4("150.1.1.106", 5069, V4, TCP);
32 assert(!testTuple4.isPrivateAddress());
33 Tuple testTuple5("127.0.0.1", 5069, V4, TCP);
34 assert(testTuple5.isPrivateAddress());
35 }
36
37 #ifdef USE_IPV6
38 // Test isPrivateAddress function - v6
39 {
40 Tuple testTuple("fd00::1", 5069, V6, TCP);
41 assert(testTuple.isPrivateAddress());
42 Tuple testTuple1("fd9b:70f6:0a18:31cc::1", 5069, V6, TCP);
43 assert(testTuple1.isPrivateAddress());
44 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
45 assert(!testTuple2.isPrivateAddress());
46 Tuple testTuple3("::ffff:10.0.0.5", 5069, V6, TCP); // V4 mapped
47 assert(!testTuple3.isPrivateAddress());
48 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
49 assert(!testTuple4.isPrivateAddress());
50 Tuple testTuple5("::10.0.0.5", 5069, V6, TCP); // V4 compatible
51 assert(!testTuple5.isPrivateAddress());
52 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
53 assert(!testTuple6.isPrivateAddress());
54 Tuple testTuple7("2000:1::203:baff:fe30:1176", 5069, V6, TCP);
55 assert(!testTuple7.isPrivateAddress());
56 }
57 #endif
58
59 {
60 Tuple testTuple("192.168.1.106", 5069, V4, TCP);
61 Data binaryToken;
62 Tuple::writeBinaryToken(testTuple, binaryToken);
63 Data binaryTokenWithSalt;
64 Tuple::writeBinaryToken(testTuple, binaryTokenWithSalt, "salt");
65 Tuple madeTestTuple = Tuple::makeTupleFromBinaryToken(binaryToken);
66 Tuple madeTestTupleWithSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "salt");
67 Tuple madeTestTupleWithBadSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "badsalt");
68 assert(testTuple == madeTestTuple);
69 assert(testTuple.onlyUseExistingConnection == madeTestTuple.onlyUseExistingConnection);
70 assert(testTuple.mFlowKey == madeTestTuple.mFlowKey);
71 assert(testTuple == madeTestTupleWithSalt);
72 assert(testTuple.onlyUseExistingConnection == madeTestTupleWithSalt.onlyUseExistingConnection);
73 assert(testTuple.mFlowKey == madeTestTupleWithSalt.mFlowKey);
74 assert(madeTestTupleWithBadSalt == Tuple());
75 }
76
77 #ifdef USE_IPV6
78 {
79 Tuple testTuple("2000:1::203:baff:fe30:1176", 5069, V6, TCP);
80 Data binaryToken;
81 Tuple::writeBinaryToken(testTuple, binaryToken);
82 Data binaryTokenWithSalt;
83 Tuple::writeBinaryToken(testTuple, binaryTokenWithSalt, "salt");
84 Tuple madeTestTuple = Tuple::makeTupleFromBinaryToken(binaryToken);
85 Tuple madeTestTupleWithSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "salt");
86 Tuple madeTestTupleWithBadSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "badsalt");
87 assert(testTuple == madeTestTuple);
88 assert(testTuple.onlyUseExistingConnection == madeTestTuple.onlyUseExistingConnection);
89 assert(testTuple.mFlowKey == madeTestTuple.mFlowKey);
90 assert(testTuple == madeTestTupleWithSalt);
91 assert(testTuple.onlyUseExistingConnection == madeTestTupleWithSalt.onlyUseExistingConnection);
92 assert(testTuple.mFlowKey == madeTestTupleWithSalt.mFlowKey);
93 assert(madeTestTupleWithBadSalt == Tuple());
94 }
95 #endif
96
97 #ifdef USE_IPV6
98 {
99 AddrMap mMap;
100 Tuple t("2000:1::203:baff:fe30:1176", 5100, V6, TCP);
101 Tuple s = t;
102 assert(s == t);
103 assert(s.hash() == t.hash());
104 mMap[t] = 0;
105 resipCerr << mMap.count(t) << endl;
106 resipCerr << Inserter(mMap) << std::endl;
107
108 assert(mMap.count(t) == 1);
109 }
110 #endif
111
112 {
113 Tuple t1("192.168.1.2", 2060, UDP);
114 Tuple t2("192.168.1.2", 2060, UDP);
115 Tuple t3("192.168.1.3", 2060, UDP);
116 Tuple t4("192.1.2.3", 2061, UDP);
117 Tuple t5("192.168.1.2", 2060, TCP);
118 Tuple t6("192.168.1.2", 2061, UDP);
119 Tuple loopback("127.0.0.1",2062,TCP);
120
121 assert(t1.isEqualWithMask(t2, 32, false /* ignorePort? */));
122 assert(!t1.isEqualWithMask(t3, 32, false)); // address is different
123 assert(t1.isEqualWithMask(t3, 24, false));
124 assert(t1.isEqualWithMask(t4, 8, true));
125 assert(!t1.isEqualWithMask(t5, 8, true)); // transport type is different
126 assert(!t1.isEqualWithMask(t6, 8, false)); // port is different
127 assert(loopback.isLoopback());
128 assert(!t1.isLoopback());
129
130 resip::Data token1;
131 resip::Data token2;
132 resip::Data token3;
133 resip::Data token4;
134 resip::Data token5;
135 resip::Data token6;
136 resip::Data tokenloopback;
137
138 Tuple::writeBinaryToken(t1,token1);
139 Tuple::writeBinaryToken(t2,token2);
140 Tuple::writeBinaryToken(t3,token3);
141 Tuple::writeBinaryToken(t4,token4);
142 Tuple::writeBinaryToken(t5,token5);
143 Tuple::writeBinaryToken(t6,token6);
144 Tuple::writeBinaryToken(loopback,tokenloopback);
145
146 Tuple t1prime=Tuple::makeTupleFromBinaryToken(token1);
147 Tuple t2prime=Tuple::makeTupleFromBinaryToken(token2);
148 Tuple t3prime=Tuple::makeTupleFromBinaryToken(token3);
149 Tuple t4prime=Tuple::makeTupleFromBinaryToken(token4);
150 Tuple t5prime=Tuple::makeTupleFromBinaryToken(token5);
151 Tuple t6prime=Tuple::makeTupleFromBinaryToken(token6);
152 Tuple loopbackprime=Tuple::makeTupleFromBinaryToken(tokenloopback);
153
154 assert(t1==t1prime);
155 assert(t2==t2prime);
156 assert(t3==t3prime);
157 assert(t4==t4prime);
158 assert(t5==t5prime);
159 assert(t6==t6prime);
160 assert(loopback==loopbackprime);
161 assert(t1.onlyUseExistingConnection == t1prime.onlyUseExistingConnection);
162 assert(t2.onlyUseExistingConnection == t2prime.onlyUseExistingConnection);
163 assert(t3.onlyUseExistingConnection == t3prime.onlyUseExistingConnection);
164 assert(t4.onlyUseExistingConnection == t4prime.onlyUseExistingConnection);
165 assert(t5.onlyUseExistingConnection == t5prime.onlyUseExistingConnection);
166 assert(t6.onlyUseExistingConnection == t6prime.onlyUseExistingConnection);
167 assert(loopback.onlyUseExistingConnection == loopbackprime.onlyUseExistingConnection);
168 assert(t1.mFlowKey == t1prime.mFlowKey);
169 assert(t2.mFlowKey == t2prime.mFlowKey);
170 assert(t3.mFlowKey == t3prime.mFlowKey);
171 assert(t4.mFlowKey == t4prime.mFlowKey);
172 assert(t5.mFlowKey == t5prime.mFlowKey);
173 assert(t6.mFlowKey == t6prime.mFlowKey);
174 assert(loopback.mFlowKey == loopbackprime.mFlowKey);
175 }
176
177 #ifdef USE_IPV6
178 {
179 Tuple t1("2000:1::203:baff:fe30:1176", 2060, UDP);
180 Tuple t2("2000:1::203:baff:fe30:1176", 2060, UDP);
181 Tuple t3("2000:1::203:1111:2222:3333", 2060, UDP);
182 Tuple t4("2000:1::0000:1111:2222:3333", 2061, UDP);
183 Tuple t5("2000:1::204:1111:2222:3333", 2061, TCP);
184 Tuple t6("2000:1::203:baff:fe30:1177", 2060, UDP);
185 Tuple loopback("::1",2062,TCP);
186
187 assert(t1.isEqualWithMask(t2, 128, false /* ignorePort? */));
188 assert(t1.isEqualWithMask(t3, 80, false));
189 assert(t1.isEqualWithMask(t4, 64, true));
190 assert(!t1.isEqualWithMask(t5, 64, true)); // transport type is different
191 assert(t1.isEqualWithMask(t6, 120, false));
192 assert(loopback.isLoopback());
193 assert(!t1.isLoopback());
194
195 resip::Data token1;
196 resip::Data token2;
197 resip::Data token3;
198 resip::Data token4;
199 resip::Data token5;
200 resip::Data token6;
201 resip::Data tokenloopback;
202
203 Tuple::writeBinaryToken(t1,token1);
204 Tuple::writeBinaryToken(t2,token2);
205 Tuple::writeBinaryToken(t3,token3);
206 Tuple::writeBinaryToken(t4,token4);
207 Tuple::writeBinaryToken(t5,token5);
208 Tuple::writeBinaryToken(t6,token6);
209 Tuple::writeBinaryToken(loopback,tokenloopback);
210
211 Tuple t1prime=Tuple::makeTupleFromBinaryToken(token1);
212 Tuple t2prime=Tuple::makeTupleFromBinaryToken(token2);
213 Tuple t3prime=Tuple::makeTupleFromBinaryToken(token3);
214 Tuple t4prime=Tuple::makeTupleFromBinaryToken(token4);
215 Tuple t5prime=Tuple::makeTupleFromBinaryToken(token5);
216 Tuple t6prime=Tuple::makeTupleFromBinaryToken(token6);
217 Tuple loopbackprime=Tuple::makeTupleFromBinaryToken(tokenloopback);
218
219 assert(t1==t1prime);
220 assert(t2==t2prime);
221 assert(t3==t3prime);
222 assert(t4==t4prime);
223 assert(t5==t5prime);
224 assert(t6==t6prime);
225 assert(loopback==loopbackprime);
226 assert(t1.onlyUseExistingConnection == t1prime.onlyUseExistingConnection);
227 assert(t2.onlyUseExistingConnection == t2prime.onlyUseExistingConnection);
228 assert(t3.onlyUseExistingConnection == t3prime.onlyUseExistingConnection);
229 assert(t4.onlyUseExistingConnection == t4prime.onlyUseExistingConnection);
230 assert(t5.onlyUseExistingConnection == t5prime.onlyUseExistingConnection);
231 assert(t6.onlyUseExistingConnection == t6prime.onlyUseExistingConnection);
232 assert(loopback.onlyUseExistingConnection == loopbackprime.onlyUseExistingConnection);
233 assert(t1.mFlowKey == t1prime.mFlowKey);
234 assert(t2.mFlowKey == t2prime.mFlowKey);
235 assert(t3.mFlowKey == t3prime.mFlowKey);
236 assert(t4.mFlowKey == t4prime.mFlowKey);
237 assert(t5.mFlowKey == t5prime.mFlowKey);
238 assert(t6.mFlowKey == t6prime.mFlowKey);
239 assert(loopback.mFlowKey == loopbackprime.mFlowKey);
240 }
241 #endif
242
243 #ifdef USE_NETNS
244 {
245 Tuple testNetNsTuple("192.168.1.106", 5069, V4, TCP, Data::Empty, "namespace1");
246 assert(testNetNsTuple.getNetNs() == "namespace1");
247 // Check assignment copies netns
248 Tuple netNsTupleCopy = testNetNsTuple;
249 assert(netNsTupleCopy.getNetNs() == "namespace1");
250 assert(testNetNsTuple == netNsTupleCopy);
251 assert(!(testNetNsTuple < netNsTupleCopy));
252 assert(!(netNsTupleCopy < testNetNsTuple));
253 assert(!(Tuple::AnyPortCompare().operator()(testNetNsTuple, netNsTupleCopy)));
254 assert(!(Tuple::AnyPortCompare().operator()(netNsTupleCopy, testNetNsTuple)));
255
256 netNsTupleCopy.setNetNs("namespace2");
257 assert(netNsTupleCopy.getNetNs() == "namespace2");
258 assert(!(testNetNsTuple == netNsTupleCopy));
259 assert(testNetNsTuple < netNsTupleCopy);
260 assert(!(netNsTupleCopy < testNetNsTuple));
261 assert((Tuple::AnyPortCompare().operator()(testNetNsTuple, netNsTupleCopy)));
262 assert(!(Tuple::AnyPortCompare().operator()(netNsTupleCopy, testNetNsTuple)));
263
264 Tuple copyTuple2(netNsTupleCopy);
265 assert(copyTuple2.getNetNs() == "namespace2");
266
267 Data binaryToken;
268 // NetNs keeps a dictionary of netns. If we have not yet used a
269 // netns, its not in the dictionary. So we need to prime the dictionary
270 // here.
271 NetNs::setNs("namespace1");
272 NetNs::setNs("");
273 //cout << "testNetNsTuple: " << testNetNsTuple << endl;
274 Tuple::writeBinaryToken(testNetNsTuple, binaryToken);
275 Tuple reconstructed = Tuple::makeTupleFromBinaryToken(binaryToken);
276 //cout << "reconstructed: " << reconstructed<< endl;
277 assert(reconstructed.getNetNs() == "namespace1");
278 assert(reconstructed == testNetNsTuple);
279 Data binaryTokenWithSalt;
280 Tuple::writeBinaryToken(testNetNsTuple, binaryTokenWithSalt, "iLikePeperToo");
281 Tuple reconstructedWithSalt = Tuple::makeTupleFromBinaryToken(binaryTokenWithSalt, "iLikePeperToo");
282 //cout << "reconstructedWithSalt: " << reconstructedWithSalt << endl;
283 assert(reconstructedWithSalt.getNetNs() == "namespace1");
284 assert(reconstructedWithSalt == testNetNsTuple);
285
286 resipCerr << "NETNS OK and tested" << std::endl;
287 }
288 #endif
289
290 resipCerr << "ALL OK" << std::endl;
291 }
292
293 /* ====================================================================
294 * The Vovida Software License, Version 1.0
295 *
296 * Redistribution and use in source and binary forms, with or without
297 * modification, are permitted provided that the following conditions
298 * are met:
299 *
300 * 1. Redistributions of source code must retain the above copyright
301 * notice, this list of conditions and the following disclaimer.
302 *
303 * 2. Redistributions in binary form must reproduce the above copyright
304 * notice, this list of conditions and the following disclaimer in
305 * the documentation and/or other materials provided with the
306 * distribution.
307 *
308 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
309 * and "Vovida Open Communication Application Library (VOCAL)" must
310 * not be used to endorse or promote products derived from this
311 * software without prior written permission. For written
312 * permission, please contact vocal@vovida.org.
313 *
314 * 4. Products derived from this software may not be called "VOCAL", nor
315 * may "VOCAL" appear in their name, without prior written
316 * permission of Vovida Networks, Inc.
317 *
318 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
319 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
320 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
321 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
322 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
323 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
324 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
325 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
326 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
327 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
328 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
329 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
330 * DAMAGE.
331 *
332 * ====================================================================
333 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27