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

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

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

Powered by Google App Engine
This is Rietveld 408576698