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

Side by Side Diff: chromeos/network/host_resolver_impl_chromeos_unittest.cc

Issue 238433003: Provide Shill IP Address to myIpAddress() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Shut down correctly in unittest. Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 "chromeos/network/host_resolver_impl_chromeos.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/shill_device_client.h"
13 #include "chromeos/dbus/shill_ipconfig_client.h"
14 #include "chromeos/dbus/shill_service_client.h"
15 #include "chromeos/network/device_state.h"
16 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
18 #include "dbus/object_path.h"
19 #include "net/base/net_errors.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h"
22
23 namespace {
24
25 const char kTestIPv4Address[] = "1.2.3.4";
26 const char kTestIPv6Address[] = "1:2:3:4:5:6:7:8";
27
28 void DoNothingWithCallStatus(chromeos::DBusMethodCallStatus call_status) {}
29 void ErrorCallbackFunction(const std::string& error_name,
30 const std::string& error_message) {
31 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
32 }
33 void ResolveCompletionCallback(int result) {}
34
35 } // namespace
36
37 class HostResolverImplChromeOSTest : public testing::Test {
38 public:
39 HostResolverImplChromeOSTest() {}
40
41 virtual ~HostResolverImplChromeOSTest() {}
42
43 virtual void SetUp() OVERRIDE {
44 chromeos::DBusThreadManager::InitializeWithStub();
45
46 network_state_handler_.reset(
47 chromeos::NetworkStateHandler::InitializeForTest());
48 base::RunLoop().RunUntilIdle();
49
50 const chromeos::NetworkState* default_network =
51 network_state_handler_->DefaultNetwork();
52 ASSERT_TRUE(default_network);
53 const chromeos::DeviceState* default_device =
54 network_state_handler_->GetDeviceState(default_network->device_path());
55 ASSERT_TRUE(default_device);
56 SetDefaultIPConfigs(default_device->path());
57
58 // Create the host resolver from the IO message loop.
59 io_message_loop_.PostTask(
60 FROM_HERE,
61 base::Bind(&HostResolverImplChromeOSTest::InitializeHostResolver,
62 base::Unretained(this)));
63 io_message_loop_.RunUntilIdle();
64
65 // Run the main message loop to create the network observer and initialize
66 // the ip address values.
67 base::RunLoop().RunUntilIdle();
68 }
69
70 virtual void TearDown() OVERRIDE {
71 host_resolver_.reset();
72 base::RunLoop().RunUntilIdle();
73
74 network_state_handler_.reset();
75 chromeos::DBusThreadManager::Shutdown();
76 }
77
78 protected:
79 // Run from main (UI) message loop, calls Resolve on IO message loop.
80 int CallResolve(net::HostResolver::RequestInfo& info) {
81 io_message_loop_.PostTask(
82 FROM_HERE,
83 base::Bind(&HostResolverImplChromeOSTest::Resolve,
84 base::Unretained(this),
85 info));
86 io_message_loop_.RunUntilIdle();
87 return result_;
88 }
89
90 net::AddressList addresses_;
91 int result_;
92
93 private:
94 // Run from IO message loop.
95 void InitializeHostResolver() {
96 net::HostResolver::Options options;
97 host_resolver_ =
98 chromeos::HostResolverImplChromeOS::CreateHostResolverForTest(
99 base::MessageLoopProxy::current(),
100 network_state_handler_.get());
101 }
102
103 // Run from IO message loop.
104 void Resolve(net::HostResolver::RequestInfo info) {
105 result_ = host_resolver_->Resolve(
106 info,
107 net::DEFAULT_PRIORITY,
108 &addresses_,
109 base::Bind(&ResolveCompletionCallback),
110 NULL,
111 net_log_);
112 }
113
114 void SetDefaultIPConfigs(const std::string& default_device_path) {
115 const std::string kTestIPv4ConfigPath("test_ip_v4_config_path");
116 const std::string kTestIPv6ConfigPath("test_ip_v6_config_path");
117
118 SetIPConfig(kTestIPv4ConfigPath, shill::kTypeIPv4, kTestIPv4Address);
119 SetIPConfig(kTestIPv6ConfigPath, shill::kTypeIPv6, kTestIPv6Address);
120 base::RunLoop().RunUntilIdle();
121
122 base::ListValue ip_configs;
123 ip_configs.AppendString(kTestIPv4ConfigPath);
124 ip_configs.AppendString(kTestIPv6ConfigPath);
125
126 chromeos::DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty(
127 dbus::ObjectPath(default_device_path),
128 shill::kIPConfigsProperty,
129 ip_configs,
130 base::Bind(&base::DoNothing),
131 base::Bind(&ErrorCallbackFunction));
132 base::RunLoop().RunUntilIdle();
133 }
134
135 void SetIPConfig(const std::string& path,
136 const std::string& method,
137 const std::string& address) {
138 chromeos::DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
139 dbus::ObjectPath(path),
140 shill::kAddressProperty,
141 base::StringValue(address),
142 base::Bind(&DoNothingWithCallStatus));
143 chromeos::DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
144 dbus::ObjectPath(path),
145 shill::kMethodProperty,
146 base::StringValue(method),
147 base::Bind(&DoNothingWithCallStatus));
148 }
149
150 scoped_ptr<chromeos::NetworkStateHandler> network_state_handler_;
151 scoped_ptr<net::HostResolver> host_resolver_;
152 base::MessageLoop io_message_loop_;
153 net::BoundNetLog net_log_;
154
155 DISALLOW_COPY_AND_ASSIGN(HostResolverImplChromeOSTest);
156 };
157
158 TEST_F(HostResolverImplChromeOSTest, Resolve) {
159 net::HostResolver::RequestInfo info(
160 net::HostPortPair(net::GetHostName(), 80));
161 info.set_address_family(net::ADDRESS_FAMILY_IPV4);
162 EXPECT_EQ(net::OK, CallResolve(info));
163 ASSERT_EQ(1u, addresses_.size());
164 std::string expected = base::StringPrintf("%s:%d", kTestIPv4Address, 0);
165 EXPECT_EQ(expected, addresses_[0].ToString());
166
167 info.set_address_family(net::ADDRESS_FAMILY_IPV6);
168 EXPECT_EQ(net::OK, CallResolve(info));
169 ASSERT_EQ(2u, addresses_.size());
170 expected = base::StringPrintf("[%s]:%d", kTestIPv6Address, 0);
171 EXPECT_EQ(expected, addresses_[0].ToString());
172 expected = base::StringPrintf("%s:%d", kTestIPv4Address, 0);
173 EXPECT_EQ(expected, addresses_[1].ToString());
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698