ip_endpoint_unittest.cc revision dc0f95d653279beabeb9817299e2902918ba123e
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 3// found in the LICENSE file. 4 5#include "net/base/ip_endpoint.h" 6 7#include "net/base/net_util.h" 8#include "testing/gtest/include/gtest/gtest.h" 9#include "testing/platform_test.h" 10#if defined(OS_WIN) 11#include <winsock2.h> 12#elif defined(OS_POSIX) 13#include <netinet/in.h> 14#endif 15 16namespace net { 17 18namespace { 19 20struct TestData { 21 std::string host; 22 bool ipv6; 23 IPAddressNumber ip_address; 24} tests[] = { 25 { "127.0.00.1", false}, 26 { "192.168.1.1", false }, 27 { "::1", true }, 28 { "2001:db8:0::42", true }, 29}; 30int test_count = ARRAYSIZE_UNSAFE(tests); 31 32class IPEndPointTest : public PlatformTest { 33 public: 34 virtual void SetUp() { 35 // This is where we populate the TestData. 36 for (int index = 0; index < test_count; ++index) { 37 EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, 38 &tests[index].ip_address)); 39 } 40 } 41}; 42 43TEST_F(IPEndPointTest, Constructor) { 44 IPEndPoint endpoint; 45 EXPECT_EQ(0, endpoint.port()); 46 47 for (int index = 0; index < test_count; ++index) { 48 IPEndPoint endpoint(tests[index].ip_address, 80); 49 EXPECT_EQ(80, endpoint.port()); 50 EXPECT_EQ(tests[index].ip_address, endpoint.address()); 51 } 52} 53 54TEST_F(IPEndPointTest, Assignment) { 55 for (int index = 0; index < test_count; ++index) { 56 IPEndPoint src(tests[index].ip_address, index); 57 IPEndPoint dest = src; 58 59 EXPECT_EQ(src.port(), dest.port()); 60 EXPECT_EQ(src.address(), dest.address()); 61 } 62} 63 64TEST_F(IPEndPointTest, Copy) { 65 for (int index = 0; index < test_count; ++index) { 66 IPEndPoint src(tests[index].ip_address, index); 67 IPEndPoint dest(src); 68 69 EXPECT_EQ(src.port(), dest.port()); 70 EXPECT_EQ(src.address(), dest.address()); 71 } 72} 73 74TEST_F(IPEndPointTest, ToFromSockaddr) { 75 for (int index = 0; index < test_count; ++index) { 76 IPEndPoint ip_endpoint(tests[index].ip_address, index); 77 78 // Convert to a sockaddr. 79 struct sockaddr_storage addr; 80 size_t addr_len = sizeof(addr); 81 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); 82 EXPECT_TRUE(ip_endpoint.ToSockaddr(sockaddr, &addr_len)); 83 84 // Basic verification. 85 size_t expected_size = tests[index].ipv6 ? 86 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); 87 EXPECT_EQ(expected_size, addr_len); 88 EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(sockaddr, addr_len)); 89 90 // And convert back to an IPEndPoint. 91 IPEndPoint ip_endpoint2; 92 EXPECT_TRUE(ip_endpoint2.FromSockAddr(sockaddr, addr_len)); 93 EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); 94 EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); 95 } 96} 97 98TEST_F(IPEndPointTest, ToSockaddrBufTooSmall) { 99 for (int index = 0; index < test_count; ++index) { 100 IPEndPoint ip_endpoint(tests[index].ip_address, index); 101 102 struct sockaddr_storage addr; 103 size_t addr_len = index; // size is too small! 104 struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); 105 EXPECT_FALSE(ip_endpoint.ToSockaddr(sockaddr, &addr_len)); 106 } 107} 108 109TEST_F(IPEndPointTest, Equality) { 110 for (int index = 0; index < test_count; ++index) { 111 IPEndPoint src(tests[index].ip_address, index); 112 IPEndPoint dest(src); 113 EXPECT_TRUE(src == dest); 114 } 115} 116 117TEST_F(IPEndPointTest, LessThan) { 118 // Vary by port. 119 IPEndPoint ip_endpoint1(tests[0].ip_address, 100); 120 IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); 121 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 122 123 // IPv4 vs IPv6 124 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); 125 ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80); 126 EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); 127 128 // IPv4 vs IPv4 129 ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); 130 ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); 131 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 132 133 // IPv6 vs IPv6 134 ip_endpoint1 = IPEndPoint(tests[2].ip_address, 80); 135 ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); 136 EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); 137} 138 139} // namespace 140 141} // namespace net 142