From 212d14930e66db247e71f72cd91c480c1f7c9374 Mon Sep 17 00:00:00 2001 From: "dkegel@google.com" Date: Fri, 5 Sep 2008 19:38:54 +0000 Subject: Modify HostResolver slightly to build on posix as well as windows. Review URL: http://codereview.chromium.org/403 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1799 0039d316-1c4b-4281-b951-d872f2087c98 --- net/base/host_resolver.cc | 10 ++++++++++ net/base/host_resolver_unittest.cc | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 net/base/host_resolver_unittest.cc (limited to 'net/base') diff --git a/net/base/host_resolver.cc b/net/base/host_resolver.cc index 5e9f317..b6d8498 100644 --- a/net/base/host_resolver.cc +++ b/net/base/host_resolver.cc @@ -4,15 +4,23 @@ #include "net/base/host_resolver.h" +#if defined(OS_WIN) #include #include // Needed for Win2k compat. +#elif defined(OS_POSIX) +#include +#include +#endif #include "base/message_loop.h" #include "base/string_util.h" #include "base/worker_pool.h" #include "net/base/address_list.h" #include "net/base/net_errors.h" + +#if defined(OS_WIN) #include "net/base/winsock_init.h" +#endif namespace net { @@ -126,7 +134,9 @@ class HostResolver::Request : //----------------------------------------------------------------------------- HostResolver::HostResolver() { +#if defined(OS_WIN) EnsureWinsockInit(); +#endif } HostResolver::~HostResolver() { diff --git a/net/base/host_resolver_unittest.cc b/net/base/host_resolver_unittest.cc new file mode 100644 index 0000000..447d5c7 --- /dev/null +++ b/net/base/host_resolver_unittest.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2006-2008 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/host_resolver.h" +#include "net/base/address_list.h" +#include "testing/gtest/include/gtest/gtest.h" + +#ifdef OS_WIN +#include +#include +#endif +#ifdef OS_POSIX +#include +#endif + +namespace { + +class HostResolverTest : public testing::Test { +}; + +TEST(HostResolverTest, NumericAddresses) { + // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. + + net::HostResolver host_resolver; + net::AddressList adrlist; + const int kPortnum = 5555; + int err = host_resolver.Resolve("127.0.0.1", kPortnum, &adrlist, NULL); + EXPECT_EQ(0, err); + + const struct addrinfo* ainfo = adrlist.head(); + EXPECT_EQ(NULL, ainfo->ai_next); + EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); + + const struct sockaddr* sa = ainfo->ai_addr; + const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; + EXPECT_EQ(htons(kPortnum), sa_in->sin_port); + EXPECT_EQ(htonl(0x7f000001), sa_in->sin_addr.s_addr); +} + +} // namespace -- cgit v1.1