diff options
author | mbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-04 03:21:31 +0000 |
---|---|---|
committer | mbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-04 03:21:31 +0000 |
commit | 76ff86af64d9260bc6042b3ab80adf78cc1a1582 (patch) | |
tree | a8c81194db224fcef75c6696a9c06def27171ad8 /net/base/ip_endpoint_unittest.cc | |
parent | 211486bd44160962954c2b10874323f4ac6c838e (diff) | |
download | chromium_src-76ff86af64d9260bc6042b3ab80adf78cc1a1582.zip chromium_src-76ff86af64d9260bc6042b3ab80adf78cc1a1582.tar.gz chromium_src-76ff86af64d9260bc6042b3ab80adf78cc1a1582.tar.bz2 |
Add an IPEndPoint class as a simple class for dealing with struct sockaddr.
BUG=none
TEST=ip_endpoint_unittest
Review URL: http://codereview.chromium.org/6592096
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76868 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/ip_endpoint_unittest.cc')
-rw-r--r-- | net/base/ip_endpoint_unittest.cc | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/net/base/ip_endpoint_unittest.cc b/net/base/ip_endpoint_unittest.cc new file mode 100644 index 0000000..b9fe48b --- /dev/null +++ b/net/base/ip_endpoint_unittest.cc @@ -0,0 +1,141 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/base/ip_endpoint.h" + +#include "net/base/net_util.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" +#if defined(OS_WIN) +#include <winsock2.h> +#elif defined(OS_POSIX) +#include <netinet/in.h> +#endif + +namespace net { + +namespace { + +struct TestData { + std::string host; + bool ipv6; + IPAddressNumber ip_address; +} tests[] = { + { "127.0.00.1", false}, + { "192.168.1.1", false }, + { "::1", true }, + { "2001:db8:0::42", true }, +}; +int test_count = ARRAYSIZE_UNSAFE(tests); + +class IPEndPointTest : public PlatformTest { + public: + virtual void SetUp() { + // This is where we populate the TestData. + for (int index = 0; index < test_count; ++index) { + EXPECT_TRUE(ParseIPLiteralToNumber(tests[index].host, + &tests[index].ip_address)); + } + } +}; + +TEST_F(IPEndPointTest, Constructor) { + IPEndPoint endpoint; + EXPECT_EQ(0, endpoint.port()); + + for (int index = 0; index < test_count; ++index) { + IPEndPoint endpoint(tests[index].ip_address, 80); + EXPECT_EQ(80, endpoint.port()); + EXPECT_EQ(tests[index].ip_address, endpoint.address()); + } +} + +TEST_F(IPEndPointTest, Assignment) { + for (int index = 0; index < test_count; ++index) { + IPEndPoint src(tests[index].ip_address, index); + IPEndPoint dest = src; + + EXPECT_EQ(src.port(), dest.port()); + EXPECT_EQ(src.address(), dest.address()); + } +} + +TEST_F(IPEndPointTest, Copy) { + for (int index = 0; index < test_count; ++index) { + IPEndPoint src(tests[index].ip_address, index); + IPEndPoint dest(src); + + EXPECT_EQ(src.port(), dest.port()); + EXPECT_EQ(src.address(), dest.address()); + } +} + +TEST_F(IPEndPointTest, ToFromSockaddr) { + for (int index = 0; index < test_count; ++index) { + IPEndPoint ip_endpoint(tests[index].ip_address, index); + + // Convert to a sockaddr. + struct sockaddr_storage addr; + size_t addr_len = sizeof(addr); + struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); + EXPECT_TRUE(ip_endpoint.ToSockaddr(sockaddr, &addr_len)); + + // Basic verification. + size_t expected_size = tests[index].ipv6 ? + sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); + EXPECT_EQ(expected_size, addr_len); + EXPECT_EQ(ip_endpoint.port(), GetPortFromSockaddr(sockaddr, addr_len)); + + // And convert back to an IPEndPoint. + IPEndPoint ip_endpoint2; + EXPECT_TRUE(ip_endpoint2.FromSockAddr(sockaddr, addr_len)); + EXPECT_EQ(ip_endpoint.port(), ip_endpoint2.port()); + EXPECT_EQ(ip_endpoint.address(), ip_endpoint2.address()); + } +} + +TEST_F(IPEndPointTest, ToSockaddrBufTooSmall) { + for (int index = 0; index < test_count; ++index) { + IPEndPoint ip_endpoint(tests[index].ip_address, index); + + struct sockaddr_storage addr; + size_t addr_len = index; // size is too small! + struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); + EXPECT_FALSE(ip_endpoint.ToSockaddr(sockaddr, &addr_len)); + } +} + +TEST_F(IPEndPointTest, Equality) { + for (int index = 0; index < test_count; ++index) { + IPEndPoint src(tests[index].ip_address, index); + IPEndPoint dest(src); + EXPECT_TRUE(src == dest); + } +} + +TEST_F(IPEndPointTest, LessThan) { + // Vary by port. + IPEndPoint ip_endpoint1(tests[0].ip_address, 100); + IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); + EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); + + // IPv4 vs IPv6 + ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); + ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80); + EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); + + // IPv4 vs IPv4 + ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); + ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); + EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); + + // IPv6 vs IPv6 + ip_endpoint1 = IPEndPoint(tests[2].ip_address, 80); + ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); + EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); +} + +} // namespace + +} // namespace net |