blob: f93a08f61177c40e1714c3affc58f1968940cb83 [file] [log] [blame]
[email protected]7054e78f2012-05-07 21:44:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]76ff86a2011-03-04 03:21:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/ip_endpoint.h"
6
[email protected]1a2123c62011-03-12 04:42:397#include "base/string_number_conversions.h"
[email protected]76ff86a2011-03-04 03:21:318#include "net/base/net_util.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#include "testing/platform_test.h"
11#if defined(OS_WIN)
12#include <winsock2.h>
13#elif defined(OS_POSIX)
14#include <netinet/in.h>
15#endif
16
17namespace net {
18
19namespace {
20
21struct TestData {
22 std::string host;
[email protected]1a2123c62011-03-12 04:42:3923 std::string host_normalized;
[email protected]76ff86a2011-03-04 03:21:3124 bool ipv6;
25 IPAddressNumber ip_address;
26} tests[] = {
[email protected]1a2123c62011-03-12 04:42:3927 { "127.0.00.1", "127.0.0.1", false},
28 { "192.168.1.1", "192.168.1.1", false },
[email protected]17e06f12011-04-05 23:10:1029 { "::1", "[::1]", true },
30 { "2001:db8:0::42", "[2001:db8::42]", true },
[email protected]76ff86a2011-03-04 03:21:3131};
32int test_count = ARRAYSIZE_UNSAFE(tests);
33
34class IPEndPointTest : public PlatformTest {
35 public:
36 virtual void SetUp() {
37 // This is where we populate the TestData.
38 for (int index = 0; index < test_count; ++index) {
39 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host,
40 &tests[index].ip_address));
41 }
42 }
43};
44
45TEST_F(IPEndPointTest, Constructor) {
46 IPEndPoint endpoint;
47 EXPECT_EQ(0, endpoint.port());
48
49 for (int index = 0; index < test_count; ++index) {
50 IPEndPoint endpoint(tests[index].ip_address, 80);
51 EXPECT_EQ(80, endpoint.port());
52 EXPECT_EQ(tests[index].ip_address, endpoint.address());
53 }
54}
55
56TEST_F(IPEndPointTest, Assignment) {
57 for (int index = 0; index < test_count; ++index) {
58 IPEndPoint src(tests[index].ip_address, index);
59 IPEndPoint dest = src;
60
61 EXPECT_EQ(src.port(), dest.port());
62 EXPECT_EQ(src.address(), dest.address());
63 }
64}
65
66TEST_F(IPEndPointTest, Copy) {
67 for (int index = 0; index < test_count; ++index) {
68 IPEndPoint src(tests[index].ip_address, index);
69 IPEndPoint dest(src);
70
71 EXPECT_EQ(src.port(), dest.port());
72 EXPECT_EQ(src.address(), dest.address());
73 }
74}
75
[email protected]43d4a0262011-03-09 19:26:0476TEST_F(IPEndPointTest, ToFromSockAddr) {
[email protected]76ff86a2011-03-04 03:21:3177 for (int index = 0; index < test_count; ++index) {
78 IPEndPoint ip_endpoint(tests[index].ip_address, index);
79
80 // Convert to a sockaddr.
[email protected]7054e78f2012-05-07 21:44:5681 SockaddrStorage storage;
82 EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:3183
84 // Basic verification.
[email protected]7054e78f2012-05-07 21:44:5685 socklen_t expected_size = tests[index].ipv6 ?
[email protected]76ff86a2011-03-04 03:21:3186 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
[email protected]7054e78f2012-05-07 21:44:5687 EXPECT_EQ(expected_size, storage.addr_len);
88 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(storage.addr,
89 storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:3190
91 // And convert back to an IPEndPoint.
92 IPEndPoint ip_endpoint2;
[email protected]7054e78f2012-05-07 21:44:5693 EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:3194 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port());
95 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address());
96 }
97}
98
[email protected]43d4a0262011-03-09 19:26:0499TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) {
[email protected]76ff86a2011-03-04 03:21:31100 for (int index = 0; index < test_count; ++index) {
101 IPEndPoint ip_endpoint(tests[index].ip_address, index);
102
[email protected]7054e78f2012-05-07 21:44:56103 SockaddrStorage storage;
104 storage.addr_len = index; // size is too small!
105 EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len));
[email protected]76ff86a2011-03-04 03:21:31106 }
107}
108
[email protected]1a872df2011-08-20 07:38:11109TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) {
110 struct sockaddr_in addr;
111 memset(&addr, 0, sizeof(addr));
112 addr.sin_family = AF_INET;
113 IPEndPoint ip_endpoint;
114 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr);
115 EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1));
116}
117
[email protected]76ff86a2011-03-04 03:21:31118TEST_F(IPEndPointTest, Equality) {
119 for (int index = 0; index < test_count; ++index) {
120 IPEndPoint src(tests[index].ip_address, index);
121 IPEndPoint dest(src);
122 EXPECT_TRUE(src == dest);
123 }
124}
125
126TEST_F(IPEndPointTest, LessThan) {
127 // Vary by port.
128 IPEndPoint ip_endpoint1(tests[0].ip_address, 100);
129 IPEndPoint ip_endpoint2(tests[0].ip_address, 1000);
130 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
[email protected]17e06f12011-04-05 23:10:10131 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31132
133 // IPv4 vs IPv6
[email protected]17e06f12011-04-05 23:10:10134 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
[email protected]76ff86a2011-03-04 03:21:31135 ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80);
[email protected]17e06f12011-04-05 23:10:10136 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
137 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31138
139 // IPv4 vs IPv4
[email protected]17e06f12011-04-05 23:10:10140 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81);
[email protected]76ff86a2011-03-04 03:21:31141 ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80);
142 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
[email protected]17e06f12011-04-05 23:10:10143 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31144
145 // IPv6 vs IPv6
[email protected]17e06f12011-04-05 23:10:10146 ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81);
[email protected]76ff86a2011-03-04 03:21:31147 ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80);
148 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2);
[email protected]17e06f12011-04-05 23:10:10149 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
150
151 // Compare equivalent endpoints.
152 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80);
153 ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80);
154 EXPECT_FALSE(ip_endpoint1 < ip_endpoint2);
155 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1);
[email protected]76ff86a2011-03-04 03:21:31156}
157
[email protected]17e06f12011-04-05 23:10:10158TEST_F(IPEndPointTest, ToString) {
[email protected]1a2123c62011-03-12 04:42:39159 IPEndPoint endpoint;
160 EXPECT_EQ(0, endpoint.port());
161
162 for (int index = 0; index < test_count; ++index) {
163 int port = 100 + index;
164 IPEndPoint endpoint(tests[index].ip_address, port);
[email protected]17e06f12011-04-05 23:10:10165 const std::string result = endpoint.ToString();
166 if (tests[index].ipv6 && result.empty()) {
167 // NetAddressToStringWithPort may fail on systems without IPv6.
168 continue;
169 }
[email protected]1a2123c62011-03-12 04:42:39170 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port),
[email protected]17e06f12011-04-05 23:10:10171 result);
[email protected]1a2123c62011-03-12 04:42:39172 }
173}
174
[email protected]76ff86a2011-03-04 03:21:31175} // namespace
176
177} // namespace net