Service workers: Allow HTTPS pages arrived at via HTTP redirect to use SW
[chromium-blink-merge.git] / chromeos / network / host_resolver_impl_chromeos_unittest.cc
blob5ba0b0cdb024af94acce9786835ff37f4ae7afe0
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
3 // found in the LICENSE file.
5 #include "chromeos/network/host_resolver_impl_chromeos.h"
7 #include "base/location.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_device_client.h"
15 #include "chromeos/dbus/shill_ipconfig_client.h"
16 #include "chromeos/dbus/shill_service_client.h"
17 #include "chromeos/network/device_state.h"
18 #include "chromeos/network/network_state.h"
19 #include "chromeos/network/network_state_handler.h"
20 #include "dbus/object_path.h"
21 #include "net/base/net_errors.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
25 namespace {
27 const char kTestIPv4Address[] = "1.2.3.4";
28 const char kTestIPv6Address[] = "1:2:3:4:5:6:7:8";
30 void DoNothingWithCallStatus(chromeos::DBusMethodCallStatus call_status) {}
31 void ErrorCallbackFunction(const std::string& error_name,
32 const std::string& error_message) {
33 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
35 void ResolveCompletionCallback(int result) {}
37 } // namespace
39 class HostResolverImplChromeOSTest : public testing::Test {
40 public:
41 HostResolverImplChromeOSTest() {}
43 ~HostResolverImplChromeOSTest() override {}
45 void SetUp() override {
46 chromeos::DBusThreadManager::Initialize();
48 network_state_handler_.reset(
49 chromeos::NetworkStateHandler::InitializeForTest());
50 base::RunLoop().RunUntilIdle();
52 const chromeos::NetworkState* default_network =
53 network_state_handler_->DefaultNetwork();
54 ASSERT_TRUE(default_network);
55 const chromeos::DeviceState* default_device =
56 network_state_handler_->GetDeviceState(default_network->device_path());
57 ASSERT_TRUE(default_device);
58 SetDefaultIPConfigs(default_device->path());
60 // Create the host resolver from the IO message loop.
61 io_message_loop_.PostTask(
62 FROM_HERE,
63 base::Bind(&HostResolverImplChromeOSTest::InitializeHostResolver,
64 base::Unretained(this)));
65 io_message_loop_.RunUntilIdle();
67 // Run the main message loop to create the network observer and initialize
68 // the ip address values.
69 base::RunLoop().RunUntilIdle();
72 void TearDown() override {
73 network_state_handler_.reset();
74 chromeos::DBusThreadManager::Shutdown();
77 protected:
78 // Run from main (UI) message loop, calls Resolve on IO message loop.
79 int CallResolve(net::HostResolver::RequestInfo& info) {
80 io_message_loop_.task_runner()->PostTask(
81 FROM_HERE, base::Bind(&HostResolverImplChromeOSTest::Resolve,
82 base::Unretained(this), info));
83 io_message_loop_.RunUntilIdle();
84 return result_;
87 net::AddressList addresses_;
88 int result_;
90 private:
91 // Run from IO message loop.
92 void InitializeHostResolver() {
93 net::HostResolver::Options options;
94 host_resolver_ =
95 chromeos::HostResolverImplChromeOS::CreateHostResolverForTest(
96 base::ThreadTaskRunnerHandle::Get(), network_state_handler_.get());
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_);
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");
114 SetIPConfig(kTestIPv4ConfigPath, shill::kTypeIPv4, kTestIPv4Address);
115 SetIPConfig(kTestIPv6ConfigPath, shill::kTypeIPv6, kTestIPv6Address);
116 base::RunLoop().RunUntilIdle();
118 base::ListValue ip_configs;
119 ip_configs.AppendString(kTestIPv4ConfigPath);
120 ip_configs.AppendString(kTestIPv6ConfigPath);
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();
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));
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_;
151 DISALLOW_COPY_AND_ASSIGN(HostResolverImplChromeOSTest);
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 info.set_is_my_ip_address(true);
159 EXPECT_EQ(net::OK, CallResolve(info));
160 ASSERT_EQ(1u, addresses_.size());
161 std::string expected = base::StringPrintf("%s:%d", kTestIPv4Address, 0);
162 EXPECT_EQ(expected, addresses_[0].ToString());
164 info.set_address_family(net::ADDRESS_FAMILY_IPV6);
165 EXPECT_EQ(net::OK, CallResolve(info));
166 ASSERT_EQ(2u, addresses_.size());
167 expected = base::StringPrintf("[%s]:%d", kTestIPv6Address, 0);
168 EXPECT_EQ(expected, addresses_[0].ToString());
169 expected = base::StringPrintf("%s:%d", kTestIPv4Address, 0);
170 EXPECT_EQ(expected, addresses_[1].ToString());