| OLD | NEW |
| 1 // Copyright (c) 2011 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/string_number_conversions.h" | 7 #include "base/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" |
| 11 #if defined(OS_WIN) | 11 #if defined(OS_WIN) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 (int 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 struct sockaddr_storage addr; | 81 SockaddrStorage storage; |
| 82 size_t addr_len = sizeof(addr); | 82 EXPECT_TRUE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
| 83 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); | |
| 84 EXPECT_TRUE(ip_endpoint.ToSockAddr(sockaddr, &addr_len)); | |
| 85 | 83 |
| 86 // Basic verification. | 84 // Basic verification. |
| 87 size_t expected_size = tests[index].ipv6 ? | 85 socklen_t expected_size = tests[index].ipv6 ? |
| 88 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | 86 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
| 89 EXPECT_EQ(expected_size, addr_len); | 87 EXPECT_EQ(expected_size, storage.addr_len); |
| 90 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(sockaddr, addr_len)); | 88 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(storage.addr, |
| 89 storage.addr_len)); |
| 91 | 90 |
| 92 // And convert back to an IPEndPoint. | 91 // And convert back to an IPEndPoint. |
| 93 IPEndPoint ip_endpoint2; | 92 IPEndPoint ip_endpoint2; |
| 94 EXPECT_TRUE(ip_endpoint2.FromSockAddr(sockaddr, addr_len)); | 93 EXPECT_TRUE(ip_endpoint2.FromSockAddr(storage.addr, storage.addr_len)); |
| 95 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); | 94 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); |
| 96 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); | 95 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); |
| 97 } | 96 } |
| 98 } | 97 } |
| 99 | 98 |
| 100 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { | 99 TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { |
| 101 for (int index = 0; index < test_count; ++index) { | 100 for (int index = 0; index < test_count; ++index) { |
| 102 IPEndPoint ip_endpoint(tests[index].ip_address, index); | 101 IPEndPoint ip_endpoint(tests[index].ip_address, index); |
| 103 | 102 |
| 104 struct sockaddr_storage addr; | 103 SockaddrStorage storage; |
| 105 size_t addr_len = index; // size is too small! | 104 storage.addr_len = index; // size is too small! |
| 106 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); | 105 EXPECT_FALSE(ip_endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
| 107 EXPECT_FALSE(ip_endpoint.ToSockAddr(sockaddr, &addr_len)); | |
| 108 } | 106 } |
| 109 } | 107 } |
| 110 | 108 |
| 111 TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) { | 109 TEST_F(IPEndPointTest, FromSockAddrBufTooSmall) { |
| 112 struct sockaddr_in addr; | 110 struct sockaddr_in addr; |
| 113 memset(&addr, 0, sizeof(addr)); | 111 memset(&addr, 0, sizeof(addr)); |
| 114 addr.sin_family = AF_INET; | 112 addr.sin_family = AF_INET; |
| 115 IPEndPoint ip_endpoint; | 113 IPEndPoint ip_endpoint; |
| 116 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); | 114 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); |
| 117 EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1)); | 115 EXPECT_FALSE(ip_endpoint.FromSockAddr(sockaddr, sizeof(addr) - 1)); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 continue; | 168 continue; |
| 171 } | 169 } |
| 172 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), | 170 EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), |
| 173 result); | 171 result); |
| 174 } | 172 } |
| 175 } | 173 } |
| 176 | 174 |
| 177 } // namespace | 175 } // namespace |
| 178 | 176 |
| 179 } // namespace net | 177 } // namespace net |
| OLD | NEW |