Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: net/base/host_resolver_unittest.cc

Issue 18775: Add more unit tests for net/base/host_resolver. (Closed)
Patch Set: add fixes, and actually use previous mapper Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/host_resolver_unittest.h ('k') | net/base/run_all_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/host_resolver.h" 5 #include "net/base/host_resolver.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <ws2tcpip.h> 8 #include <ws2tcpip.h>
9 #include <wspiapi.h> 9 #include <wspiapi.h>
10 #elif defined(OS_POSIX) 10 #elif defined(OS_POSIX)
11 #include <netdb.h> 11 #include <netdb.h>
12 #endif 12 #endif
13 13
14 #include <string>
15
16 #include "base/compiler_specific.h"
17 #include "base/message_loop.h"
18 #include "base/ref_counted.h"
14 #include "net/base/address_list.h" 19 #include "net/base/address_list.h"
20 #include "net/base/completion_callback.h"
21 #include "net/base/host_resolver_unittest.h"
22 #include "net/base/net_errors.h"
15 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
16 24
25 using net::RuleBasedHostMapper;
26 using net::ScopedHostMapper;
27 using net::WaitingHostMapper;
28
17 namespace { 29 namespace {
18 30
19 TEST(HostResolverTest, NumericAddresses) { 31 class HostResolverTest : public testing::Test {
32 public:
33 HostResolverTest()
34 : callback_called_(false),
35 ALLOW_THIS_IN_INITIALIZER_LIST(
36 callback_(this, &HostResolverTest::OnLookupFinished)) {
37 }
38
39 protected:
40 bool callback_called_;
41 int callback_result_;
42 net::CompletionCallbackImpl<HostResolverTest> callback_;
43
44 private:
45 void OnLookupFinished(int result) {
46 callback_called_ = true;
47 callback_result_ = result;
48 MessageLoop::current()->Quit();
49 }
50 };
51
52 TEST_F(HostResolverTest, SynchronousLookup) {
53 net::HostResolver host_resolver;
54 net::AddressList adrlist;
55 const int kPortnum = 80;
56
57 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper();
58 mapper->AddRule("just.testing", "192.168.1.42");
59 ScopedHostMapper scoped_mapper(mapper.get());
60
61 int err = host_resolver.Resolve("just.testing", kPortnum, &adrlist, NULL);
62 EXPECT_EQ(net::OK, err);
63
64 const struct addrinfo* ainfo = adrlist.head();
65 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
66 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
67
68 const struct sockaddr* sa = ainfo->ai_addr;
69 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
70 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
71 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr);
72 }
73
74 TEST_F(HostResolverTest, AsynchronousLookup) {
75 net::HostResolver host_resolver;
76 net::AddressList adrlist;
77 const int kPortnum = 80;
78
79 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper();
80 mapper->AddRule("just.testing", "192.168.1.42");
81 ScopedHostMapper scoped_mapper(mapper.get());
82
83 int err = host_resolver.Resolve("just.testing", kPortnum, &adrlist,
84 &callback_);
85 EXPECT_EQ(net::ERR_IO_PENDING, err);
86
87 MessageLoop::current()->Run();
88
89 ASSERT_TRUE(callback_called_);
90 ASSERT_EQ(net::OK, callback_result_);
91
92 const struct addrinfo* ainfo = adrlist.head();
93 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
94 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
95
96 const struct sockaddr* sa = ainfo->ai_addr;
97 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
98 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
99 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr);
100 }
101
102 TEST_F(HostResolverTest, CanceledAsynchronousLookup) {
103 scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper();
104 ScopedHostMapper scoped_mapper(mapper.get());
105
106 {
107 net::HostResolver host_resolver;
108 net::AddressList adrlist;
109 const int kPortnum = 80;
110
111 int err = host_resolver.Resolve("just.testing", kPortnum, &adrlist,
112 &callback_);
113 EXPECT_EQ(net::ERR_IO_PENDING, err);
114
115 // Make sure we will exit the queue even when callback is not called.
116 MessageLoop::current()->PostDelayedTask(FROM_HERE,
117 new MessageLoop::QuitTask(),
118 1000);
119 MessageLoop::current()->Run();
120 }
121
122 mapper->Signal();
123
124 EXPECT_FALSE(callback_called_);
125 }
126
127 TEST_F(HostResolverTest, NumericAddresses) {
20 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. 128 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in.
21 129
22 net::HostResolver host_resolver; 130 net::HostResolver host_resolver;
23 net::AddressList adrlist; 131 net::AddressList adrlist;
24 const int kPortnum = 5555; 132 const int kPortnum = 5555;
25 int err = host_resolver.Resolve("127.0.0.1", kPortnum, &adrlist, NULL); 133 int err = host_resolver.Resolve("127.0.0.1", kPortnum, &adrlist, NULL);
26 EXPECT_EQ(0, err); 134 EXPECT_EQ(net::OK, err);
27 135
28 const struct addrinfo* ainfo = adrlist.head(); 136 const struct addrinfo* ainfo = adrlist.head();
29 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 137 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
30 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); 138 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
31 139
32 const struct sockaddr* sa = ainfo->ai_addr; 140 const struct sockaddr* sa = ainfo->ai_addr;
33 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; 141 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
34 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); 142 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
35 EXPECT_TRUE(htonl(0x7f000001) == sa_in->sin_addr.s_addr); 143 EXPECT_TRUE(htonl(0x7f000001) == sa_in->sin_addr.s_addr);
36 } 144 }
37 145
38 } // namespace 146 } // namespace
OLDNEW
« no previous file with comments | « net/base/host_resolver_unittest.h ('k') | net/base/run_all_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698