| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "net/base/net_util.h" | 8 #include "net/base/net_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 std::string host; | 22 std::string host; |
| 23 std::string host_normalized; | 23 std::string host_normalized; |
| 24 bool ipv6; | 24 bool ipv6; |
| 25 IPAddressNumber ip_address; | 25 IPAddressNumber ip_address; |
| 26 } tests[] = { | 26 } tests[] = { |
| 27 { "127.0.00.1", "127.0.0.1", false}, | 27 { "127.0.00.1", "127.0.0.1", false}, |
| 28 { "192.168.1.1", "192.168.1.1", false }, | 28 { "192.168.1.1", "192.168.1.1", false }, |
| 29 { "::1", "[::1]", true }, | 29 { "::1", "[::1]", true }, |
| 30 { "2001:db8:0::42", "[2001:db8::42]", true }, | 30 { "2001:db8:0::42", "[2001:db8::42]", true }, |
| 31 }; | 31 }; |
| 32 int test_count = arraysize(tests); | 32 uint16 test_count = static_cast<uint16>(arraysize(tests)); |
| 33 | 33 |
| 34 class IPEndPointTest : public PlatformTest { | 34 class IPEndPointTest : public PlatformTest { |
| 35 public: | 35 public: |
| 36 void SetUp() override { | 36 void SetUp() override { |
| 37 // This is where we populate the TestData. | 37 // This is where we populate the TestData. |
| 38 for (int index = 0; index < test_count; ++index) { | 38 for (int index = 0; index < test_count; ++index) { |
| 39 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, | 39 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, |
| 40 &tests[index].ip_address)); | 40 &tests[index].ip_address)); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 TEST_F(IPEndPointTest, Constructor) { | 45 TEST_F(IPEndPointTest, Constructor) { |
| 46 IPEndPoint endpoint; | 46 IPEndPoint endpoint; |
| 47 EXPECT_EQ(0, endpoint.port()); | 47 EXPECT_EQ(0, endpoint.port()); |
| 48 | 48 |
| 49 for (int index = 0; index < test_count; ++index) { | 49 for (uint16 index = 0; index < test_count; ++index) { |
| 50 IPEndPoint endpoint(tests[index].ip_address, 80); | 50 IPEndPoint endpoint(tests[index].ip_address, 80); |
| 51 EXPECT_EQ(80, endpoint.port()); | 51 EXPECT_EQ(80, endpoint.port()); |
| 52 EXPECT_EQ(tests[index].ip_address, endpoint.address()); | 52 EXPECT_EQ(tests[index].ip_address, endpoint.address()); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 | 55 |
| 56 TEST_F(IPEndPointTest, Assignment) { | 56 TEST_F(IPEndPointTest, Assignment) { |
| 57 for (int index = 0; index < test_count; ++index) { | 57 for (uint16 index = 0; index < test_count; ++index) { |
| 58 IPEndPoint src(tests[index].ip_address, index); | 58 IPEndPoint src(tests[index].ip_address, index); |
| 59 IPEndPoint dest = src; | 59 IPEndPoint dest = src; |
| 60 | 60 |
| 61 EXPECT_EQ(src.port(), dest.port()); | 61 EXPECT_EQ(src.port(), dest.port()); |
| 62 EXPECT_EQ(src.address(), dest.address()); | 62 EXPECT_EQ(src.address(), dest.address()); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 TEST_F(IPEndPointTest, Copy) { | 66 TEST_F(IPEndPointTest, Copy) { |
| 67 for (int index = 0; index < test_count; ++index) { | 67 for (uint16 index = 0; index < test_count; ++index) { |
| 68 IPEndPoint src(tests[index].ip_address, index); | 68 IPEndPoint src(tests[index].ip_address, index); |
| 69 IPEndPoint dest(src); | 69 IPEndPoint dest(src); |
| 70 | 70 |
| 71 EXPECT_EQ(src.port(), dest.port()); | 71 EXPECT_EQ(src.port(), dest.port()); |
| 72 EXPECT_EQ(src.address(), dest.address()); | 72 EXPECT_EQ(src.address(), dest.address()); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 TEST_F(IPEndPointTest, ToFromSockAddr) { | 76 TEST_F(IPEndPointTest, ToFromSockAddr) { |
| 77 for (int index = 0; index < test_count; ++index) { | 77 for (uint16 index = 0; index < test_count; ++index) { |
| 78 IPEndPoint ip_endpoint(tests[index].ip_address, index); | 78 IPEndPoint ip_endpoint(tests[index].ip_address, index); |
| 79 | 79 |
| 80 // Convert to a sockaddr. | 80 // Convert to a sockaddr. |
| 81 SockaddrStorage storage; | 81 SockaddrStorage storage; |
| 82 EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); | 82 EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
| 83 | 83 |
| 84 // Basic verification. | 84 // Basic verification. |
| 85 socklen_t expected_size = tests[index].ipv6 ? | 85 socklen_t expected_size = tests[index].ipv6 ? |
| 86 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | 86 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
| 87 EXPECT_EQ(expected_size, storage.addr_len); | 87 EXPECT_EQ(expected_size, storage.addr_len); |
| 88 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(storage.addr, | 88 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(storage.addr, |
| 89 storage.addr_len)); | 89 storage.addr_len)); |
| 90 | 90 |
| 91 // And convert back to an IPEndPoint. | 91 // And convert back to an IPEndPoint. |
| 92 IPEndPoint ip_endpoint2; | 92 IPEndPoint ip_endpoint2; |
| 93 EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len)); | 93 EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len)); |
| 94 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); | 94 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); |
| 95 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); | 95 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { | 99 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { |
| 100 for (int index = 0; index < test_count; ++index) { | 100 for (uint16 index = 0; index < test_count; ++index) { |
| 101 IPEndPoint ip_endpoint(tests[index].ip_address, index); | 101 IPEndPoint ip_endpoint(tests[index].ip_address, index); |
| 102 | 102 |
| 103 SockaddrStorage storage; | 103 SockaddrStorage storage; |
| 104 storage.addr_len = index; // size is too small! | 104 storage.addr_len = index; // size is too small! |
| 105 EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); | 105 EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) { | 109 TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) { |
| 110 struct sockaddr_in addr; | 110 struct sockaddr_in addr; |
| 111 memset(&addr, 0, sizeof(addr)); | 111 memset(&addr, 0, sizeof(addr)); |
| 112 addr.sin_family = AF_INET; | 112 addr.sin_family = AF_INET; |
| 113 IPEndPoint ip_endpoint; | 113 IPEndPoint ip_endpoint; |
| 114 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); | 114 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); |
| 115 EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1)); | 115 EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1)); |
| 116 } | 116 } |
| 117 | 117 |
| 118 TEST_F(IPEndPointTest, Equality) { | 118 TEST_F(IPEndPointTest, Equality) { |
| 119 for (int index = 0; index < test_count; ++index) { | 119 for (uint16 index = 0; index < test_count; ++index) { |
| 120 IPEndPoint src(tests[index].ip_address, index); | 120 IPEndPoint src(tests[index].ip_address, index); |
| 121 IPEndPoint dest(src); | 121 IPEndPoint dest(src); |
| 122 EXPECT_TRUE(src == dest); | 122 EXPECT_TRUE(src == dest); |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 | 125 |
| 126 TEST_F(IPEndPointTest, LessThan) { | 126 TEST_F(IPEndPointTest, LessThan) { |
| 127 // Vary by port. | 127 // Vary by port. |
| 128 IPEndPoint ip_endpoint1(tests[0].ip_address, 100); | 128 IPEndPoint ip_endpoint1(tests[0].ip_address, 100); |
| 129 IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); | 129 IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 152 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); | 152 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); |
| 153 ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80); | 153 ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80); |
| 154 EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); | 154 EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); |
| 155 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); | 155 EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); |
| 156 } | 156 } |
| 157 | 157 |
| 158 TEST_F(IPEndPointTest, ToString) { | 158 TEST_F(IPEndPointTest, ToString) { |
| 159 IPEndPoint endpoint; | 159 IPEndPoint endpoint; |
| 160 EXPECT_EQ(0, endpoint.port()); | 160 EXPECT_EQ(0, endpoint.port()); |
| 161 | 161 |
| 162 for (int index = 0; index < test_count; ++index) { | 162 for (uint16 index = 0; index < test_count; ++index) { |
| 163 int port = 100 + index; | 163 uint16 port = 100 + index; |
| 164 IPEndPoint endpoint(tests[index].ip_address, port); | 164 IPEndPoint endpoint(tests[index].ip_address, port); |
| 165 const std::string result = endpoint.ToString(); | 165 const std::string result = endpoint.ToString(); |
| 166 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), | 166 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), |
| 167 result); | 167 result); |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 } // namespace | 171 } // namespace |
| 172 | 172 |
| 173 } // namespace net | 173 } // namespace net |
| OLD | NEW |