| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/ip_endpoint.h" | 5 #include "net/base/ip_endpoint.h" |
| 6 | 6 |
| 7 #include "base/string_number_conversions.h" |
| 7 #include "net/base/net_util.h" | 8 #include "net/base/net_util.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
| 10 #if defined(OS_WIN) | 11 #if defined(OS_WIN) |
| 11 #include <winsock2.h> | 12 #include <winsock2.h> |
| 12 #elif defined(OS_POSIX) | 13 #elif defined(OS_POSIX) |
| 13 #include <netinet/in.h> | 14 #include <netinet/in.h> |
| 14 #endif | 15 #endif |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 struct TestData { | 21 struct TestData { |
| 21 std::string host; | 22 std::string host; |
| 23 std::string host_normalized; |
| 22 bool ipv6; | 24 bool ipv6; |
| 23 IPAddressNumber ip_address; | 25 IPAddressNumber ip_address; |
| 24 } tests[] = { | 26 } tests[] = { |
| 25 { "127.0.00.1", false}, | 27 { "127.0.00.1", "127.0.0.1", false}, |
| 26 { "192.168.1.1", false }, | 28 { "192.168.1.1", "192.168.1.1", false }, |
| 27 { "::1", true }, | 29 { "::1", "::1", true }, |
| 28 { "2001:db8:0::42", true }, | 30 { "2001:db8:0::42", "2001:db8::42", true }, |
| 29 }; | 31 }; |
| 30 int test_count = ARRAYSIZE_UNSAFE(tests); | 32 int test_count = ARRAYSIZE_UNSAFE(tests); |
| 31 | 33 |
| 32 class IPEndPointTest : public PlatformTest { | 34 class IPEndPointTest : public PlatformTest { |
| 33 public: | 35 public: |
| 34 virtual void SetUp() { | 36 virtual void SetUp() { |
| 35 // This is where we populate the TestData. | 37 // This is where we populate the TestData. |
| 36 for (int index = 0; index < test_count; ++index) { | 38 for (int index = 0; index < test_count; ++index) { |
| 37 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, | 39 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, |
| 38 &tests[index].ip_address)); | 40 &tests[index].ip_address)); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); | 131 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); |
| 130 ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); | 132 ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); |
| 131 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); | 133 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); |
| 132 | 134 |
| 133 // IPv6 vs IPv6 | 135 // IPv6 vs IPv6 |
| 134 ip_endpoint1 = IPEndPoint(tests[2].ip_address, 80); | 136 ip_endpoint1 = IPEndPoint(tests[2].ip_address, 80); |
| 135 ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); | 137 ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); |
| 136 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); | 138 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); |
| 137 } | 139 } |
| 138 | 140 |
| 141 TEST_F(IPEndPointTest, ToString) { |
| 142 IPEndPoint endpoint; |
| 143 EXPECT_EQ(0, endpoint.port()); |
| 144 |
| 145 for (int index = 0; index < test_count; ++index) { |
| 146 int port = 100 + index; |
| 147 IPEndPoint endpoint(tests[index].ip_address, port); |
| 148 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), |
| 149 endpoint.ToString()); |
| 150 } |
| 151 } |
| 152 |
| 139 } // namespace | 153 } // namespace |
| 140 | 154 |
| 141 } // namespace net | 155 } // namespace net |
| OLD | NEW |