- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / socketaddress_unittest.cc
1 /*
2  * libjingle
3  * Copyright 2004--2011, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifdef POSIX
29 #include <netinet/in.h>  // for sockaddr_in
30 #endif
31
32 #include "talk/base/gunit.h"
33 #include "talk/base/socketaddress.h"
34 #include "talk/base/ipaddress.h"
35
36 namespace talk_base {
37
38 const in6_addr kTestV6Addr =  { { {0x20, 0x01, 0x0d, 0xb8,
39                                    0x10, 0x20, 0x30, 0x40,
40                                    0x50, 0x60, 0x70, 0x80,
41                                    0x90, 0xA0, 0xB0, 0xC0} } };
42 const in6_addr kMappedV4Addr = { { {0x00, 0x00, 0x00, 0x00,
43                                     0x00, 0x00, 0x00, 0x00,
44                                     0x00, 0x00, 0xFF, 0xFF,
45                                     0x01, 0x02, 0x03, 0x04} } };
46 const std::string kTestV6AddrString = "2001:db8:1020:3040:5060:7080:90a0:b0c0";
47 const std::string kTestV6AddrAnonymizedString = "2001:db8:1020::";
48 const std::string kTestV6AddrFullString =
49     "[2001:db8:1020:3040:5060:7080:90a0:b0c0]:5678";
50 const std::string kTestV6AddrFullAnonymizedString = "[2001:db8:1020::]:5678";
51
52 TEST(SocketAddressTest, TestDefaultCtor) {
53   SocketAddress addr;
54   EXPECT_FALSE(addr.IsUnresolvedIP());
55   EXPECT_EQ(IPAddress(), addr.ipaddr());
56   EXPECT_EQ(0, addr.port());
57   EXPECT_EQ("", addr.hostname());
58 }
59
60 TEST(SocketAddressTest, TestIPPortCtor) {
61   SocketAddress addr(IPAddress(0x01020304), 5678);
62   EXPECT_FALSE(addr.IsUnresolvedIP());
63   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
64   EXPECT_EQ(5678, addr.port());
65   EXPECT_EQ("", addr.hostname());
66   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
67 }
68
69 TEST(SocketAddressTest, TestIPv4StringPortCtor) {
70   SocketAddress addr("1.2.3.4", 5678);
71   EXPECT_FALSE(addr.IsUnresolvedIP());
72   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
73   EXPECT_EQ(5678, addr.port());
74   EXPECT_EQ("1.2.3.4", addr.hostname());
75   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
76 }
77
78 TEST(SocketAddressTest, TestIPv6StringPortCtor) {
79   SocketAddress addr2(kTestV6AddrString, 1234);
80   IPAddress tocheck(kTestV6Addr);
81
82   EXPECT_FALSE(addr2.IsUnresolvedIP());
83   EXPECT_EQ(tocheck, addr2.ipaddr());
84   EXPECT_EQ(1234, addr2.port());
85   EXPECT_EQ(kTestV6AddrString, addr2.hostname());
86   EXPECT_EQ("[" + kTestV6AddrString + "]:1234", addr2.ToString());
87 }
88
89 TEST(SocketAddressTest, TestSpecialStringPortCtor) {
90   // inet_addr doesn't handle this address properly.
91   SocketAddress addr("255.255.255.255", 5678);
92   EXPECT_FALSE(addr.IsUnresolvedIP());
93   EXPECT_EQ(IPAddress(0xFFFFFFFFU), addr.ipaddr());
94   EXPECT_EQ(5678, addr.port());
95   EXPECT_EQ("255.255.255.255", addr.hostname());
96   EXPECT_EQ("255.255.255.255:5678", addr.ToString());
97 }
98
99 TEST(SocketAddressTest, TestHostnamePortCtor) {
100   SocketAddress addr("a.b.com", 5678);
101   EXPECT_TRUE(addr.IsUnresolvedIP());
102   EXPECT_EQ(IPAddress(), addr.ipaddr());
103   EXPECT_EQ(5678, addr.port());
104   EXPECT_EQ("a.b.com", addr.hostname());
105   EXPECT_EQ("a.b.com:5678", addr.ToString());
106 }
107
108 TEST(SocketAddressTest, TestCopyCtor) {
109   SocketAddress from("1.2.3.4", 5678);
110   SocketAddress addr(from);
111   EXPECT_FALSE(addr.IsUnresolvedIP());
112   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
113   EXPECT_EQ(5678, addr.port());
114   EXPECT_EQ("1.2.3.4", addr.hostname());
115   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
116 }
117
118 TEST(SocketAddressTest, TestAssign) {
119   SocketAddress from("1.2.3.4", 5678);
120   SocketAddress addr(IPAddress(0x88888888), 9999);
121   addr = from;
122   EXPECT_FALSE(addr.IsUnresolvedIP());
123   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
124   EXPECT_EQ(5678, addr.port());
125   EXPECT_EQ("1.2.3.4", addr.hostname());
126   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
127 }
128
129 TEST(SocketAddressTest, TestSetIPPort) {
130   SocketAddress addr(IPAddress(0x88888888), 9999);
131   addr.SetIP(IPAddress(0x01020304));
132   addr.SetPort(5678);
133   EXPECT_FALSE(addr.IsUnresolvedIP());
134   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
135   EXPECT_EQ(5678, addr.port());
136   EXPECT_EQ("", addr.hostname());
137   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
138 }
139
140 TEST(SocketAddressTest, TestSetIPFromString) {
141   SocketAddress addr(IPAddress(0x88888888), 9999);
142   addr.SetIP("1.2.3.4");
143   addr.SetPort(5678);
144   EXPECT_FALSE(addr.IsUnresolvedIP());
145   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
146   EXPECT_EQ(5678, addr.port());
147   EXPECT_EQ("1.2.3.4", addr.hostname());
148   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
149 }
150
151 TEST(SocketAddressTest, TestSetIPFromHostname) {
152   SocketAddress addr(IPAddress(0x88888888), 9999);
153   addr.SetIP("a.b.com");
154   addr.SetPort(5678);
155   EXPECT_TRUE(addr.IsUnresolvedIP());
156   EXPECT_EQ(IPAddress(), addr.ipaddr());
157   EXPECT_EQ(5678, addr.port());
158   EXPECT_EQ("a.b.com", addr.hostname());
159   EXPECT_EQ("a.b.com:5678", addr.ToString());
160   addr.SetResolvedIP(IPAddress(0x01020304));
161   EXPECT_FALSE(addr.IsUnresolvedIP());
162   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
163   EXPECT_EQ("a.b.com", addr.hostname());
164   EXPECT_EQ("a.b.com:5678", addr.ToString());
165 }
166
167 TEST(SocketAddressTest, TestFromIPv4String) {
168   SocketAddress addr;
169   EXPECT_TRUE(addr.FromString("1.2.3.4:5678"));
170   EXPECT_FALSE(addr.IsUnresolvedIP());
171   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
172   EXPECT_EQ(5678, addr.port());
173   EXPECT_EQ("1.2.3.4", addr.hostname());
174   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
175 }
176
177 TEST(SocketAddressTest, TestFromIPv6String) {
178   SocketAddress addr;
179   EXPECT_TRUE(addr.FromString(kTestV6AddrFullString));
180   EXPECT_FALSE(addr.IsUnresolvedIP());
181   EXPECT_EQ(5678, addr.port());
182   EXPECT_EQ(kTestV6AddrString, addr.hostname());
183   EXPECT_EQ(kTestV6AddrFullString, addr.ToString());
184 }
185
186 TEST(SocketAddressTest, TestFromHostname) {
187   SocketAddress addr;
188   EXPECT_TRUE(addr.FromString("a.b.com:5678"));
189   EXPECT_TRUE(addr.IsUnresolvedIP());
190   EXPECT_EQ(IPAddress(), addr.ipaddr());
191   EXPECT_EQ(5678, addr.port());
192   EXPECT_EQ("a.b.com", addr.hostname());
193   EXPECT_EQ("a.b.com:5678", addr.ToString());
194 }
195
196 TEST(SocketAddressTest, TestToFromSockAddr) {
197   SocketAddress from("1.2.3.4", 5678), addr;
198   sockaddr_in addr_in;
199   from.ToSockAddr(&addr_in);
200   EXPECT_TRUE(addr.FromSockAddr(addr_in));
201   EXPECT_FALSE(addr.IsUnresolvedIP());
202   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
203   EXPECT_EQ(5678, addr.port());
204   EXPECT_EQ("", addr.hostname());
205   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
206 }
207
208 TEST(SocketAddressTest, TestToFromSockAddrStorage) {
209   SocketAddress from("1.2.3.4", 5678), addr;
210   sockaddr_storage addr_storage;
211   from.ToSockAddrStorage(&addr_storage);
212   EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
213   EXPECT_FALSE(addr.IsUnresolvedIP());
214   EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
215   EXPECT_EQ(5678, addr.port());
216   EXPECT_EQ("", addr.hostname());
217   EXPECT_EQ("1.2.3.4:5678", addr.ToString());
218
219   addr.Clear();
220   from.ToDualStackSockAddrStorage(&addr_storage);
221   EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
222   EXPECT_FALSE(addr.IsUnresolvedIP());
223   EXPECT_EQ(IPAddress(kMappedV4Addr), addr.ipaddr());
224   EXPECT_EQ(5678, addr.port());
225   EXPECT_EQ("", addr.hostname());
226   EXPECT_EQ("[::ffff:1.2.3.4]:5678", addr.ToString());
227
228   addr.Clear();
229   memset(&addr_storage, 0, sizeof(sockaddr_storage));
230   from = SocketAddress(kTestV6AddrString, 5678);
231   from.SetScopeID(6);
232   from.ToSockAddrStorage(&addr_storage);
233   EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
234   EXPECT_FALSE(addr.IsUnresolvedIP());
235   EXPECT_EQ(IPAddress(kTestV6Addr), addr.ipaddr());
236   EXPECT_EQ(5678, addr.port());
237   EXPECT_EQ("", addr.hostname());
238   EXPECT_EQ(kTestV6AddrFullString, addr.ToString());
239   EXPECT_EQ(6, addr.scope_id());
240
241   addr.Clear();
242   from.ToDualStackSockAddrStorage(&addr_storage);
243   EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
244   EXPECT_FALSE(addr.IsUnresolvedIP());
245   EXPECT_EQ(IPAddress(kTestV6Addr), addr.ipaddr());
246   EXPECT_EQ(5678, addr.port());
247   EXPECT_EQ("", addr.hostname());
248   EXPECT_EQ(kTestV6AddrFullString, addr.ToString());
249   EXPECT_EQ(6, addr.scope_id());
250
251   addr = from;
252   addr_storage.ss_family = AF_UNSPEC;
253   EXPECT_FALSE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
254   EXPECT_EQ(from, addr);
255
256   EXPECT_FALSE(SocketAddressFromSockAddrStorage(addr_storage, NULL));
257 }
258
259 bool AreEqual(const SocketAddress& addr1,
260               const SocketAddress& addr2) {
261   return addr1 == addr2 && addr2 == addr1 &&
262       !(addr1 != addr2) && !(addr2 != addr1);
263 }
264
265 bool AreUnequal(const SocketAddress& addr1,
266                 const SocketAddress& addr2) {
267   return !(addr1 == addr2) && !(addr2 == addr1) &&
268       addr1 != addr2 && addr2 != addr1;
269 }
270
271 TEST(SocketAddressTest, TestEqualityOperators) {
272   SocketAddress addr1("1.2.3.4", 5678);
273   SocketAddress addr2("1.2.3.4", 5678);
274   EXPECT_PRED2(AreEqual, addr1, addr2);
275
276   addr2 = SocketAddress("0.0.0.1", 5678);
277   EXPECT_PRED2(AreUnequal, addr1, addr2);
278
279   addr2 = SocketAddress("1.2.3.4", 1234);
280   EXPECT_PRED2(AreUnequal, addr1, addr2);
281
282   addr2 = SocketAddress(kTestV6AddrString, 5678);
283   EXPECT_PRED2(AreUnequal, addr1, addr2);
284
285   addr1 = SocketAddress(kTestV6AddrString, 5678);
286   EXPECT_PRED2(AreEqual, addr1, addr2);
287
288   addr2 = SocketAddress(kTestV6AddrString, 1234);
289   EXPECT_PRED2(AreUnequal, addr1, addr2);
290
291   addr2 = SocketAddress("fe80::1", 5678);
292   EXPECT_PRED2(AreUnequal, addr1, addr2);
293 }
294
295 bool IsLessThan(const SocketAddress& addr1,
296                                       const SocketAddress& addr2) {
297   return addr1 < addr2 &&
298       !(addr2 < addr1) &&
299       !(addr1 == addr2);
300 }
301
302 TEST(SocketAddressTest, TestComparisonOperator) {
303   SocketAddress addr1("1.2.3.4", 5678);
304   SocketAddress addr2("1.2.3.4", 5678);
305
306   EXPECT_FALSE(addr1 < addr2);
307   EXPECT_FALSE(addr2 < addr1);
308
309   addr2 = SocketAddress("1.2.3.4", 5679);
310   EXPECT_PRED2(IsLessThan, addr1, addr2);
311
312   addr2 = SocketAddress("2.2.3.4", 49152);
313   EXPECT_PRED2(IsLessThan, addr1, addr2);
314
315   addr2 = SocketAddress(kTestV6AddrString, 5678);
316   EXPECT_PRED2(IsLessThan, addr1, addr2);
317
318   addr1 = SocketAddress("fe80::1", 5678);
319   EXPECT_PRED2(IsLessThan, addr2, addr1);
320
321   addr2 = SocketAddress("fe80::1", 5679);
322   EXPECT_PRED2(IsLessThan, addr1, addr2);
323
324   addr2 = SocketAddress("fe80::1", 5678);
325   EXPECT_FALSE(addr1 < addr2);
326   EXPECT_FALSE(addr2 < addr1);
327 }
328
329 TEST(SocketAddressTest, TestToSensitiveString) {
330   SocketAddress addr_v4("1.2.3.4", 5678);
331   EXPECT_EQ("1.2.3.4", addr_v4.HostAsURIString());
332   EXPECT_EQ("1.2.3.4:5678", addr_v4.ToString());
333   EXPECT_EQ("1.2.3.4", addr_v4.HostAsSensitiveURIString());
334   EXPECT_EQ("1.2.3.4:5678", addr_v4.ToSensitiveString());
335   IPAddress::set_strip_sensitive(true);
336   EXPECT_EQ("1.2.3.x", addr_v4.HostAsSensitiveURIString());
337   EXPECT_EQ("1.2.3.x:5678", addr_v4.ToSensitiveString());
338   IPAddress::set_strip_sensitive(false);
339
340   SocketAddress addr_v6(kTestV6AddrString, 5678);
341   EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsURIString());
342   EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToString());
343   EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsSensitiveURIString());
344   EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToSensitiveString());
345   IPAddress::set_strip_sensitive(true);
346   EXPECT_EQ("[" + kTestV6AddrAnonymizedString + "]",
347             addr_v6.HostAsSensitiveURIString());
348   EXPECT_EQ(kTestV6AddrFullAnonymizedString, addr_v6.ToSensitiveString());
349   IPAddress::set_strip_sensitive(false);
350 }
351
352 }  // namespace talk_base