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

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

Powered by Google App Engine
This is Rietveld 408576698