Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 1 | // Copyright 2021 The Chromium OS 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 "dns-proxy/proxy.h" |
| 6 | |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/stat.h> |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <utility> |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 12 | #include <vector> |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 13 | |
| 14 | #include <chromeos/patchpanel/net_util.h> |
| 15 | #include <chromeos/patchpanel/dbus/fake_client.h> |
| 16 | #include <dbus/mock_bus.h> |
| 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <shill/dbus/client/fake_client.h> |
| 20 | #include <shill/dbus-constants.h> |
| 21 | #include <shill/dbus-proxy-mocks.h> |
| 22 | |
| 23 | namespace dns_proxy { |
Jason Jeremy Iman | 1bb71c2 | 2021-01-26 21:49:55 +0900 | [diff] [blame] | 24 | namespace { |
| 25 | constexpr base::TimeDelta kRequestTimeout = base::TimeDelta::FromSeconds(10000); |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 26 | constexpr base::TimeDelta kRequestRetryDelay = |
| 27 | base::TimeDelta::FromMilliseconds(200); |
| 28 | constexpr int32_t kRequestMaxRetry = 1; |
| 29 | |
Jason Jeremy Iman | 1bb71c2 | 2021-01-26 21:49:55 +0900 | [diff] [blame] | 30 | } // namespace |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 31 | using org::chromium::flimflam::ManagerProxyInterface; |
| 32 | using org::chromium::flimflam::ManagerProxyMock; |
| 33 | using testing::_; |
Garrick Evans | 09646fe | 2021-04-27 14:38:41 +0900 | [diff] [blame] | 34 | using testing::DoAll; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 35 | using testing::IsEmpty; |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 36 | using testing::Return; |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 37 | using testing::SetArgPointee; |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 38 | using testing::StrEq; |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 39 | |
| 40 | class FakeShillClient : public shill::FakeClient { |
| 41 | public: |
| 42 | FakeShillClient(scoped_refptr<dbus::Bus> bus, |
| 43 | ManagerProxyInterface* manager_proxy) |
| 44 | : shill::FakeClient(bus), manager_proxy_(manager_proxy) {} |
| 45 | |
| 46 | std::unique_ptr<shill::Client::ManagerPropertyAccessor> ManagerProperties( |
| 47 | const base::TimeDelta& timeout) const override { |
| 48 | return std::make_unique<shill::Client::ManagerPropertyAccessor>( |
| 49 | manager_proxy_); |
| 50 | } |
| 51 | |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 52 | std::unique_ptr<shill::Client::Device> DefaultDevice( |
| 53 | bool exclude_vpn) override { |
| 54 | return std::move(default_device_); |
| 55 | } |
| 56 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 57 | bool IsInitialized() const { return init_; } |
| 58 | |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 59 | std::unique_ptr<shill::Client::Device> default_device_; |
| 60 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 61 | private: |
| 62 | ManagerProxyInterface* manager_proxy_; |
| 63 | }; |
| 64 | |
| 65 | class FakePatchpanelClient : public patchpanel::FakeClient { |
| 66 | public: |
| 67 | FakePatchpanelClient() = default; |
| 68 | ~FakePatchpanelClient() = default; |
| 69 | |
| 70 | void SetConnectNamespaceResult( |
| 71 | int fd, const patchpanel::ConnectNamespaceResponse& resp) { |
| 72 | ns_fd_ = fd; |
| 73 | ns_resp_ = resp; |
| 74 | } |
| 75 | |
| 76 | std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> |
| 77 | ConnectNamespace(pid_t pid, |
| 78 | const std::string& outbound_ifname, |
| 79 | bool forward_user_traffic, |
| 80 | bool route_on_vpn, |
| 81 | patchpanel::TrafficCounter::Source traffic_source) override { |
| 82 | ns_ifname_ = outbound_ifname; |
| 83 | ns_rvpn_ = route_on_vpn; |
| 84 | ns_ts_ = traffic_source; |
| 85 | return {base::ScopedFD(ns_fd_), ns_resp_}; |
| 86 | } |
| 87 | |
| 88 | std::string ns_ifname_; |
| 89 | bool ns_rvpn_; |
| 90 | patchpanel::TrafficCounter::Source ns_ts_; |
| 91 | int ns_fd_; |
| 92 | patchpanel::ConnectNamespaceResponse ns_resp_; |
| 93 | }; |
| 94 | |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 95 | class MockResolver : public Resolver { |
| 96 | public: |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 97 | MockResolver() |
| 98 | : Resolver(kRequestTimeout, kRequestRetryDelay, kRequestMaxRetry) {} |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 99 | ~MockResolver() = default; |
| 100 | |
Jason Jeremy Iman | 6fd9855 | 2021-01-27 04:19:07 +0900 | [diff] [blame] | 101 | MOCK_METHOD(bool, ListenUDP, (struct sockaddr*), (override)); |
| 102 | MOCK_METHOD(bool, ListenTCP, (struct sockaddr*), (override)); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 103 | MOCK_METHOD(void, |
| 104 | SetNameServers, |
| 105 | (const std::vector<std::string>&), |
| 106 | (override)); |
| 107 | MOCK_METHOD(void, |
| 108 | SetDoHProviders, |
| 109 | (const std::vector<std::string>&, bool), |
| 110 | (override)); |
| 111 | }; |
| 112 | |
| 113 | class TestProxy : public Proxy { |
| 114 | public: |
| 115 | TestProxy(const Options& opts, |
| 116 | std::unique_ptr<patchpanel::Client> patchpanel, |
| 117 | std::unique_ptr<shill::Client> shill) |
| 118 | : Proxy(opts, std::move(patchpanel), std::move(shill)) {} |
| 119 | |
| 120 | std::unique_ptr<Resolver> resolver; |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 121 | std::unique_ptr<Resolver> NewResolver(base::TimeDelta timeout, |
| 122 | base::TimeDelta retry_delay, |
| 123 | int max_num_retries) override { |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 124 | return std::move(resolver); |
| 125 | } |
| 126 | }; |
| 127 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 128 | class ProxyTest : public ::testing::Test { |
| 129 | protected: |
| 130 | ProxyTest() : mock_bus_(new dbus::MockBus{dbus::Bus::Options{}}) {} |
| 131 | ~ProxyTest() { mock_bus_->ShutdownAndBlock(); } |
| 132 | |
| 133 | std::unique_ptr<FakePatchpanelClient> PatchpanelClient() const { |
| 134 | return std::make_unique<FakePatchpanelClient>(); |
| 135 | } |
| 136 | |
| 137 | std::unique_ptr<FakeShillClient> ShillClient() const { |
| 138 | return std::make_unique<FakeShillClient>( |
| 139 | mock_bus_, reinterpret_cast<ManagerProxyInterface*>( |
| 140 | const_cast<ManagerProxyMock*>(&mock_manager_))); |
| 141 | } |
| 142 | |
| 143 | int make_fd() const { |
| 144 | std::string fn( |
| 145 | ::testing::UnitTest::GetInstance()->current_test_info()->name()); |
| 146 | fn = "/tmp/" + fn; |
| 147 | return open(fn.c_str(), O_CREAT, 0600); |
| 148 | } |
| 149 | |
| 150 | protected: |
| 151 | scoped_refptr<dbus::MockBus> mock_bus_; |
| 152 | ManagerProxyMock mock_manager_; |
| 153 | }; |
| 154 | |
| 155 | TEST_F(ProxyTest, SystemProxy_OnShutdownClearsAddressPropertyOnShill) { |
| 156 | EXPECT_CALL(mock_manager_, SetProperty(shill::kDNSProxyIPv4AddressProperty, |
| 157 | brillo::Any(std::string()), _, _)) |
| 158 | .WillOnce(Return(true)); |
| 159 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 160 | ShillClient()); |
| 161 | int unused; |
| 162 | proxy.OnShutdown(&unused); |
| 163 | } |
| 164 | |
| 165 | TEST_F(ProxyTest, NonSystemProxy_OnShutdownDoesNotCallShill) { |
| 166 | EXPECT_CALL(mock_manager_, SetProperty(_, _, _, _)).Times(0); |
| 167 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kDefault}, PatchpanelClient(), |
| 168 | ShillClient()); |
| 169 | int unused; |
| 170 | proxy.OnShutdown(&unused); |
| 171 | } |
| 172 | |
| 173 | TEST_F(ProxyTest, SystemProxy_SetShillPropertyWithNoRetriesCrashes) { |
| 174 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 175 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 176 | ShillClient()); |
| 177 | EXPECT_DEATH(proxy.SetShillProperty("10.10.10.10", true, 0), ""); |
| 178 | } |
| 179 | |
| 180 | TEST_F(ProxyTest, SystemProxy_SetShillPropertyDoesntCrashIfDieFalse) { |
| 181 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 182 | EXPECT_CALL(mock_manager_, SetProperty(_, _, _, _)).Times(0); |
| 183 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 184 | ShillClient()); |
| 185 | proxy.SetShillProperty("10.10.10.10", false, 0); |
| 186 | } |
| 187 | |
Garrick Evans | ab03c46 | 2021-02-15 20:54:20 +0900 | [diff] [blame] | 188 | TEST_F(ProxyTest, ShillInitializedWhenReady) { |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 189 | auto shill = ShillClient(); |
| 190 | auto* shill_ptr = shill.get(); |
| 191 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 192 | std::move(shill)); |
Garrick Evans | ab03c46 | 2021-02-15 20:54:20 +0900 | [diff] [blame] | 193 | proxy.OnShillReady(true); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 194 | EXPECT_TRUE(shill_ptr->IsInitialized()); |
| 195 | } |
| 196 | |
| 197 | TEST_F(ProxyTest, SystemProxy_ConnectedNamedspace) { |
| 198 | auto pp = PatchpanelClient(); |
| 199 | auto* pp_ptr = pp.get(); |
| 200 | pp->SetConnectNamespaceResult(make_fd(), |
| 201 | patchpanel::ConnectNamespaceResponse()); |
| 202 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, std::move(pp), |
| 203 | ShillClient()); |
| 204 | proxy.OnPatchpanelReady(true); |
| 205 | EXPECT_TRUE(pp_ptr->ns_ifname_.empty()); |
| 206 | EXPECT_FALSE(pp_ptr->ns_rvpn_); |
| 207 | EXPECT_EQ(pp_ptr->ns_ts_, patchpanel::TrafficCounter::SYSTEM); |
| 208 | } |
| 209 | |
| 210 | TEST_F(ProxyTest, DefaultProxy_ConnectedNamedspace) { |
| 211 | auto pp = PatchpanelClient(); |
| 212 | auto* pp_ptr = pp.get(); |
| 213 | pp->SetConnectNamespaceResult(make_fd(), |
| 214 | patchpanel::ConnectNamespaceResponse()); |
| 215 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kDefault}, std::move(pp), |
| 216 | ShillClient()); |
| 217 | proxy.OnPatchpanelReady(true); |
| 218 | EXPECT_TRUE(pp_ptr->ns_ifname_.empty()); |
| 219 | EXPECT_TRUE(pp_ptr->ns_rvpn_); |
| 220 | EXPECT_EQ(pp_ptr->ns_ts_, patchpanel::TrafficCounter::USER); |
| 221 | } |
| 222 | |
| 223 | TEST_F(ProxyTest, ArcProxy_ConnectedNamedspace) { |
| 224 | auto pp = PatchpanelClient(); |
| 225 | auto* pp_ptr = pp.get(); |
| 226 | pp->SetConnectNamespaceResult(make_fd(), |
| 227 | patchpanel::ConnectNamespaceResponse()); |
| 228 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kARC, .ifname = "eth0"}, |
| 229 | std::move(pp), ShillClient()); |
| 230 | proxy.OnPatchpanelReady(true); |
| 231 | EXPECT_EQ(pp_ptr->ns_ifname_, "eth0"); |
| 232 | EXPECT_FALSE(pp_ptr->ns_rvpn_); |
| 233 | EXPECT_EQ(pp_ptr->ns_ts_, patchpanel::TrafficCounter::ARC); |
| 234 | } |
| 235 | |
| 236 | TEST_F(ProxyTest, CrashOnConnectNamespaceFailure) { |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 237 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 238 | auto pp = PatchpanelClient(); |
| 239 | pp->SetConnectNamespaceResult(-1 /* invalid fd */, |
| 240 | patchpanel::ConnectNamespaceResponse()); |
| 241 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kARC, .ifname = "eth0"}, |
| 242 | std::move(pp), ShillClient()); |
| 243 | EXPECT_DEATH(proxy.OnPatchpanelReady(true), "namespace"); |
| 244 | } |
| 245 | |
| 246 | TEST_F(ProxyTest, CrashOnPatchpanelNotReady) { |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 247 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 248 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kARC, .ifname = "eth0"}, |
| 249 | PatchpanelClient(), ShillClient()); |
| 250 | EXPECT_DEATH(proxy.OnPatchpanelReady(false), "patchpanel"); |
| 251 | } |
| 252 | |
| 253 | TEST_F(ProxyTest, ShillResetRestoresAddressProperty) { |
| 254 | auto pp = PatchpanelClient(); |
| 255 | patchpanel::ConnectNamespaceResponse resp; |
Garrick Evans | ab03c46 | 2021-02-15 20:54:20 +0900 | [diff] [blame] | 256 | resp.set_peer_ipv4_address(patchpanel::Ipv4Addr(10, 10, 10, 10)); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 257 | pp->SetConnectNamespaceResult(make_fd(), resp); |
| 258 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, std::move(pp), |
| 259 | ShillClient()); |
| 260 | proxy.OnPatchpanelReady(true); |
| 261 | EXPECT_CALL(mock_manager_, |
| 262 | SetProperty(shill::kDNSProxyIPv4AddressProperty, |
| 263 | brillo::Any(std::string("10.10.10.10")), _, _)) |
| 264 | .WillOnce(Return(true)); |
| 265 | proxy.OnShillReset(true); |
| 266 | } |
| 267 | |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 268 | TEST_F(ProxyTest, StateClearedIfDefaultServiceDrops) { |
| 269 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 270 | ShillClient()); |
| 271 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 272 | proxy.resolver_ = std::make_unique<MockResolver>(); |
| 273 | proxy.OnDefaultDeviceChanged(nullptr /* no service */); |
| 274 | EXPECT_FALSE(proxy.device_); |
| 275 | EXPECT_FALSE(proxy.resolver_); |
| 276 | } |
| 277 | |
| 278 | TEST_F(ProxyTest, ArcProxy_IgnoredIfDefaultServiceDrops) { |
| 279 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kARC}, PatchpanelClient(), |
| 280 | ShillClient()); |
| 281 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 282 | proxy.resolver_ = std::make_unique<MockResolver>(); |
| 283 | proxy.OnDefaultDeviceChanged(nullptr /* no service */); |
| 284 | EXPECT_TRUE(proxy.device_); |
| 285 | EXPECT_TRUE(proxy.resolver_); |
| 286 | } |
| 287 | |
| 288 | TEST_F(ProxyTest, StateClearedIfDefaultServiceIsNotOnline) { |
| 289 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 290 | ShillClient()); |
| 291 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 292 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 293 | proxy.resolver_ = std::make_unique<MockResolver>(); |
| 294 | shill::Client::Device dev; |
| 295 | dev.state = shill::Client::Device::ConnectionState::kReady; |
| 296 | proxy.OnDefaultDeviceChanged(&dev); |
| 297 | EXPECT_FALSE(proxy.device_); |
| 298 | EXPECT_FALSE(proxy.resolver_); |
| 299 | } |
| 300 | |
| 301 | TEST_F(ProxyTest, NewResolverStartsListeningOnDefaultServiceComesOnline) { |
| 302 | TestProxy proxy(Proxy::Options{.type = Proxy::Type::kDefault}, |
| 303 | PatchpanelClient(), ShillClient()); |
| 304 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 305 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 306 | auto resolver = std::make_unique<MockResolver>(); |
| 307 | MockResolver* mock_resolver = resolver.get(); |
| 308 | proxy.resolver = std::move(resolver); |
| 309 | shill::Client::Device dev; |
| 310 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
Jason Jeremy Iman | 6fd9855 | 2021-01-27 04:19:07 +0900 | [diff] [blame] | 311 | EXPECT_CALL(*mock_resolver, ListenUDP(_)).WillOnce(Return(true)); |
| 312 | EXPECT_CALL(*mock_resolver, ListenTCP(_)).WillOnce(Return(true)); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 313 | brillo::VariantDictionary props; |
| 314 | EXPECT_CALL(mock_manager_, GetProperties(_, _, _)) |
| 315 | .WillOnce(DoAll(SetArgPointee<0>(props), Return(true))); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 316 | proxy.OnDefaultDeviceChanged(&dev); |
| 317 | EXPECT_TRUE(proxy.resolver_); |
| 318 | } |
| 319 | |
| 320 | TEST_F(ProxyTest, CrashOnListenFailure) { |
| 321 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 322 | TestProxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, |
| 323 | PatchpanelClient(), ShillClient()); |
| 324 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 325 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 326 | auto resolver = std::make_unique<MockResolver>(); |
| 327 | MockResolver* mock_resolver = resolver.get(); |
| 328 | proxy.resolver = std::move(resolver); |
| 329 | shill::Client::Device dev; |
| 330 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
Jason Jeremy Iman | 6fd9855 | 2021-01-27 04:19:07 +0900 | [diff] [blame] | 331 | ON_CALL(*mock_resolver, ListenUDP(_)).WillByDefault(Return(false)); |
| 332 | ON_CALL(*mock_resolver, ListenTCP(_)).WillByDefault(Return(false)); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 333 | EXPECT_DEATH(proxy.OnDefaultDeviceChanged(&dev), "relay loop"); |
| 334 | } |
| 335 | |
| 336 | TEST_F(ProxyTest, NameServersUpdatedOnDefaultServiceComesOnline) { |
| 337 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kDefault}, PatchpanelClient(), |
| 338 | ShillClient()); |
| 339 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 340 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 341 | auto resolver = std::make_unique<MockResolver>(); |
| 342 | MockResolver* mock_resolver = resolver.get(); |
| 343 | proxy.resolver_ = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 344 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 345 | shill::Client::Device dev; |
| 346 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 347 | dev.ipconfig.ipv4_dns_addresses = {"a", "b"}; |
| 348 | dev.ipconfig.ipv6_dns_addresses = {"c", "d"}; |
| 349 | // Doesn't call listen since the resolver already exists. |
Jason Jeremy Iman | 6fd9855 | 2021-01-27 04:19:07 +0900 | [diff] [blame] | 350 | EXPECT_CALL(*mock_resolver, ListenUDP(_)).Times(0); |
| 351 | EXPECT_CALL(*mock_resolver, ListenTCP(_)).Times(0); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 352 | EXPECT_CALL(*mock_resolver, |
| 353 | SetNameServers( |
| 354 | ElementsAre(StrEq("a"), StrEq("b"), StrEq("c"), StrEq("d")))); |
| 355 | proxy.OnDefaultDeviceChanged(&dev); |
| 356 | } |
| 357 | |
| 358 | TEST_F(ProxyTest, SystemProxy_ShillPropertyUpdatedOnDefaultServiceComesOnline) { |
| 359 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 360 | ShillClient()); |
| 361 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 362 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 363 | auto resolver = std::make_unique<MockResolver>(); |
| 364 | MockResolver* mock_resolver = resolver.get(); |
| 365 | proxy.resolver_ = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 366 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 367 | shill::Client::Device dev; |
| 368 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 369 | EXPECT_CALL(*mock_resolver, SetNameServers(_)); |
| 370 | EXPECT_CALL(mock_manager_, |
| 371 | SetProperty(shill::kDNSProxyIPv4AddressProperty, _, _, _)) |
| 372 | .WillOnce(Return(true)); |
| 373 | proxy.OnDefaultDeviceChanged(&dev); |
| 374 | } |
| 375 | |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 376 | TEST_F(ProxyTest, SystemProxy_IgnoresVPN) { |
| 377 | TestProxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, |
| 378 | PatchpanelClient(), ShillClient()); |
| 379 | auto resolver = std::make_unique<MockResolver>(); |
| 380 | MockResolver* mock_resolver = resolver.get(); |
| 381 | ON_CALL(*mock_resolver, ListenUDP(_)).WillByDefault(Return(true)); |
| 382 | ON_CALL(*mock_resolver, ListenTCP(_)).WillByDefault(Return(true)); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 383 | brillo::VariantDictionary props; |
| 384 | EXPECT_CALL(mock_manager_, GetProperties(_, _, _)) |
| 385 | .WillOnce(DoAll(SetArgPointee<0>(props), Return(true))); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 386 | EXPECT_CALL(mock_manager_, |
| 387 | SetProperty(shill::kDNSProxyIPv4AddressProperty, _, _, _)) |
| 388 | .WillOnce(Return(true)); |
| 389 | proxy.resolver = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 390 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 391 | shill::Client::Device dev; |
| 392 | dev.type = shill::Client::Device::Type::kWifi; |
| 393 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 394 | proxy.OnDefaultDeviceChanged(&dev); |
| 395 | EXPECT_TRUE(proxy.device_); |
| 396 | EXPECT_EQ(proxy.device_->type, shill::Client::Device::Type::kWifi); |
| 397 | dev.type = shill::Client::Device::Type::kVPN; |
| 398 | proxy.OnDefaultDeviceChanged(&dev); |
| 399 | EXPECT_TRUE(proxy.device_); |
| 400 | EXPECT_EQ(proxy.device_->type, shill::Client::Device::Type::kWifi); |
| 401 | } |
| 402 | |
| 403 | TEST_F(ProxyTest, SystemProxy_GetsPhysicalDeviceOnInitialVPN) { |
| 404 | auto shill = ShillClient(); |
| 405 | auto* shill_ptr = shill.get(); |
| 406 | TestProxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, |
| 407 | PatchpanelClient(), std::move(shill)); |
| 408 | auto resolver = std::make_unique<MockResolver>(); |
| 409 | MockResolver* mock_resolver = resolver.get(); |
| 410 | ON_CALL(*mock_resolver, ListenUDP(_)).WillByDefault(Return(true)); |
| 411 | ON_CALL(*mock_resolver, ListenTCP(_)).WillByDefault(Return(true)); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 412 | brillo::VariantDictionary props; |
| 413 | EXPECT_CALL(mock_manager_, GetProperties(_, _, _)) |
| 414 | .WillOnce(DoAll(SetArgPointee<0>(props), Return(true))); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 415 | EXPECT_CALL(mock_manager_, |
| 416 | SetProperty(shill::kDNSProxyIPv4AddressProperty, _, _, _)) |
| 417 | .WillOnce(Return(true)); |
| 418 | proxy.resolver = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 419 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 420 | shill::Client::Device vpn; |
| 421 | vpn.type = shill::Client::Device::Type::kVPN; |
| 422 | vpn.state = shill::Client::Device::ConnectionState::kOnline; |
| 423 | shill_ptr->default_device_ = std::make_unique<shill::Client::Device>(); |
| 424 | shill_ptr->default_device_->type = shill::Client::Device::Type::kWifi; |
| 425 | shill_ptr->default_device_->state = |
| 426 | shill::Client::Device::ConnectionState::kOnline; |
| 427 | proxy.OnDefaultDeviceChanged(&vpn); |
| 428 | EXPECT_TRUE(proxy.device_); |
| 429 | EXPECT_EQ(proxy.device_->type, shill::Client::Device::Type::kWifi); |
| 430 | } |
| 431 | |
| 432 | TEST_F(ProxyTest, DefaultProxy_UsesVPN) { |
| 433 | TestProxy proxy(Proxy::Options{.type = Proxy::Type::kDefault}, |
| 434 | PatchpanelClient(), ShillClient()); |
| 435 | auto resolver = std::make_unique<MockResolver>(); |
| 436 | MockResolver* mock_resolver = resolver.get(); |
| 437 | ON_CALL(*mock_resolver, ListenUDP(_)).WillByDefault(Return(true)); |
| 438 | ON_CALL(*mock_resolver, ListenTCP(_)).WillByDefault(Return(true)); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 439 | brillo::VariantDictionary props; |
| 440 | EXPECT_CALL(mock_manager_, GetProperties(_, _, _)) |
| 441 | .WillOnce(DoAll(SetArgPointee<0>(props), Return(true))); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 442 | proxy.resolver = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 443 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 444 | shill::Client::Device dev; |
| 445 | dev.type = shill::Client::Device::Type::kWifi; |
| 446 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 447 | proxy.OnDefaultDeviceChanged(&dev); |
| 448 | EXPECT_TRUE(proxy.device_); |
| 449 | EXPECT_EQ(proxy.device_->type, shill::Client::Device::Type::kWifi); |
| 450 | dev.type = shill::Client::Device::Type::kVPN; |
| 451 | proxy.OnDefaultDeviceChanged(&dev); |
| 452 | EXPECT_TRUE(proxy.device_); |
| 453 | EXPECT_EQ(proxy.device_->type, shill::Client::Device::Type::kVPN); |
| 454 | } |
| 455 | |
Garrick Evans | 73e8e5e | 2021-04-27 10:16:26 +0900 | [diff] [blame] | 456 | TEST_F(ProxyTest, ArcProxy_NameServersUpdatedOnDeviceChangeEvent) { |
| 457 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kARC, .ifname = "wlan0"}, |
| 458 | PatchpanelClient(), ShillClient()); |
| 459 | auto resolver = std::make_unique<MockResolver>(); |
| 460 | MockResolver* mock_resolver = resolver.get(); |
| 461 | proxy.resolver_ = std::move(resolver); |
| 462 | proxy.doh_config_.set_resolver(mock_resolver); |
| 463 | shill::Client::Device dev; |
| 464 | dev.ifname = "wlan0"; |
| 465 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 466 | dev.ipconfig.ipv4_dns_addresses = {"a", "b"}; |
| 467 | dev.ipconfig.ipv6_dns_addresses = {"c", "d"}; |
| 468 | // Doesn't call listen since the resolver already exists. |
| 469 | EXPECT_CALL(*mock_resolver, ListenUDP(_)).Times(0); |
| 470 | EXPECT_CALL(*mock_resolver, ListenTCP(_)).Times(0); |
| 471 | EXPECT_CALL(*mock_resolver, |
| 472 | SetNameServers( |
| 473 | ElementsAre(StrEq("a"), StrEq("b"), StrEq("c"), StrEq("d")))); |
| 474 | proxy.OnDeviceChanged(&dev); |
| 475 | |
| 476 | // Verify it only applies changes for the correct interface. |
| 477 | dev.ifname = "eth0"; |
| 478 | dev.ipconfig.ipv4_dns_addresses = {"X", "Y", "Z"}; |
| 479 | EXPECT_CALL(*mock_resolver, SetNameServers(_)).Times(0); |
| 480 | proxy.OnDeviceChanged(&dev); |
| 481 | |
| 482 | dev.ifname = "wlan0"; |
| 483 | dev.ipconfig.ipv4_dns_addresses = {"X", "Y", "Z"}; |
| 484 | dev.ipconfig.ipv6_dns_addresses.clear(); |
| 485 | EXPECT_CALL(*mock_resolver, |
| 486 | SetNameServers(ElementsAre(StrEq("X"), StrEq("Y"), StrEq("Z")))); |
| 487 | proxy.OnDeviceChanged(&dev); |
| 488 | } |
| 489 | |
| 490 | TEST_F(ProxyTest, SystemProxy_NameServersUpdatedOnDeviceChangeEvent) { |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 491 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 492 | ShillClient()); |
| 493 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 494 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 495 | auto resolver = std::make_unique<MockResolver>(); |
| 496 | MockResolver* mock_resolver = resolver.get(); |
| 497 | proxy.resolver_ = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 498 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 499 | shill::Client::Device dev; |
| 500 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 501 | dev.ipconfig.ipv4_dns_addresses = {"a", "b"}; |
| 502 | dev.ipconfig.ipv6_dns_addresses = {"c", "d"}; |
| 503 | // Doesn't call listen since the resolver already exists. |
| 504 | EXPECT_CALL(mock_manager_, |
| 505 | SetProperty(shill::kDNSProxyIPv4AddressProperty, _, _, _)) |
| 506 | .WillOnce(Return(true)); |
| 507 | EXPECT_CALL(*mock_resolver, ListenUDP(_)).Times(0); |
| 508 | EXPECT_CALL(*mock_resolver, ListenTCP(_)).Times(0); |
| 509 | EXPECT_CALL(*mock_resolver, |
| 510 | SetNameServers( |
| 511 | ElementsAre(StrEq("a"), StrEq("b"), StrEq("c"), StrEq("d")))); |
| 512 | proxy.OnDefaultDeviceChanged(&dev); |
| 513 | |
| 514 | // Now trigger an ipconfig change. |
| 515 | dev.ipconfig.ipv4_dns_addresses = {"X"}; |
| 516 | EXPECT_CALL(*mock_resolver, |
| 517 | SetNameServers(ElementsAre(StrEq("X"), StrEq("c"), StrEq("d")))); |
| 518 | proxy.OnDeviceChanged(&dev); |
| 519 | } |
| 520 | |
| 521 | TEST_F(ProxyTest, DeviceChangeEventIgnored) { |
| 522 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 523 | ShillClient()); |
| 524 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 525 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 526 | auto resolver = std::make_unique<MockResolver>(); |
| 527 | MockResolver* mock_resolver = resolver.get(); |
| 528 | proxy.resolver_ = std::move(resolver); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 529 | proxy.doh_config_.set_resolver(mock_resolver); |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 530 | shill::Client::Device dev; |
| 531 | dev.ifname = "eth0"; |
| 532 | dev.state = shill::Client::Device::ConnectionState::kOnline; |
| 533 | dev.ipconfig.ipv4_dns_addresses = {"a", "b"}; |
| 534 | dev.ipconfig.ipv6_dns_addresses = {"c", "d"}; |
| 535 | // Doesn't call listen since the resolver already exists. |
| 536 | EXPECT_CALL(mock_manager_, |
| 537 | SetProperty(shill::kDNSProxyIPv4AddressProperty, _, _, _)) |
| 538 | .WillOnce(Return(true)); |
| 539 | EXPECT_CALL(*mock_resolver, ListenUDP(_)).Times(0); |
| 540 | EXPECT_CALL(*mock_resolver, ListenTCP(_)).Times(0); |
| 541 | EXPECT_CALL(*mock_resolver, |
| 542 | SetNameServers( |
| 543 | ElementsAre(StrEq("a"), StrEq("b"), StrEq("c"), StrEq("d")))); |
| 544 | proxy.OnDefaultDeviceChanged(&dev); |
| 545 | |
| 546 | // No change to ipconfig, no call to SetNameServers |
| 547 | proxy.OnDeviceChanged(&dev); |
| 548 | |
| 549 | // Different ifname, no call to SetNameServers |
| 550 | dev.ifname = "wlan0"; |
| 551 | proxy.OnDeviceChanged(&dev); |
| 552 | } |
| 553 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 554 | TEST_F(ProxyTest, BasicDoHDisable) { |
| 555 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 556 | ShillClient()); |
| 557 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 558 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 559 | auto resolver = std::make_unique<MockResolver>(); |
| 560 | MockResolver* mock_resolver = resolver.get(); |
| 561 | proxy.resolver_ = std::move(resolver); |
| 562 | proxy.doh_config_.set_resolver(mock_resolver); |
| 563 | EXPECT_CALL(*mock_resolver, SetDoHProviders(IsEmpty(), false)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 564 | brillo::VariantDictionary props; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 565 | proxy.OnDoHProvidersChanged(props); |
| 566 | } |
| 567 | |
| 568 | TEST_F(ProxyTest, BasicDoHAlwaysOn) { |
| 569 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 570 | ShillClient()); |
| 571 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 572 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 573 | auto resolver = std::make_unique<MockResolver>(); |
| 574 | MockResolver* mock_resolver = resolver.get(); |
| 575 | proxy.resolver_ = std::move(resolver); |
| 576 | proxy.doh_config_.set_resolver(mock_resolver); |
| 577 | EXPECT_CALL( |
| 578 | *mock_resolver, |
| 579 | SetDoHProviders(ElementsAre(StrEq("https://dns.google.com")), true)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 580 | brillo::VariantDictionary props; |
| 581 | props["https://dns.google.com"] = std::string(""); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 582 | proxy.OnDoHProvidersChanged(props); |
| 583 | } |
| 584 | |
| 585 | TEST_F(ProxyTest, BasicDoHAutomatic) { |
| 586 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 587 | ShillClient()); |
| 588 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 589 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 590 | auto resolver = std::make_unique<MockResolver>(); |
| 591 | MockResolver* mock_resolver = resolver.get(); |
| 592 | proxy.resolver_ = std::move(resolver); |
| 593 | proxy.doh_config_.set_resolver(mock_resolver); |
| 594 | shill::Client::IPConfig ipconfig; |
| 595 | ipconfig.ipv4_dns_addresses = {"8.8.4.4"}; |
| 596 | proxy.UpdateNameServers(ipconfig); |
| 597 | |
| 598 | EXPECT_CALL( |
| 599 | *mock_resolver, |
| 600 | SetDoHProviders(ElementsAre(StrEq("https://dns.google.com")), false)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 601 | brillo::VariantDictionary props; |
| 602 | props["https://dns.google.com"] = std::string("8.8.8.8, 8.8.4.4"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 603 | proxy.OnDoHProvidersChanged(props); |
| 604 | } |
| 605 | |
| 606 | TEST_F(ProxyTest, NewResolverConfiguredWhenSet) { |
| 607 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 608 | ShillClient()); |
| 609 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 610 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 611 | brillo::VariantDictionary props; |
| 612 | props["https://dns.google.com"] = std::string("8.8.8.8, 8.8.4.4"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 613 | props["https://chrome.cloudflare-dns.com/dns-query"] = |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 614 | std::string("1.1.1.1,2606:4700:4700::1111"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 615 | proxy.OnDoHProvidersChanged(props); |
| 616 | shill::Client::IPConfig ipconfig; |
| 617 | ipconfig.ipv4_dns_addresses = {"1.0.0.1", "1.1.1.1"}; |
| 618 | proxy.UpdateNameServers(ipconfig); |
| 619 | |
| 620 | auto resolver = std::make_unique<MockResolver>(); |
| 621 | MockResolver* mock_resolver = resolver.get(); |
| 622 | proxy.resolver_ = std::move(resolver); |
| 623 | EXPECT_CALL(*mock_resolver, SetNameServers(UnorderedElementsAre( |
| 624 | StrEq("1.1.1.1"), StrEq("1.0.0.1")))); |
| 625 | EXPECT_CALL( |
| 626 | *mock_resolver, |
| 627 | SetDoHProviders( |
| 628 | ElementsAre(StrEq("https://chrome.cloudflare-dns.com/dns-query")), |
| 629 | false)); |
| 630 | proxy.doh_config_.set_resolver(mock_resolver); |
| 631 | } |
| 632 | |
| 633 | TEST_F(ProxyTest, DoHModeChangingFixedNameServers) { |
| 634 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 635 | ShillClient()); |
| 636 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 637 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 638 | auto resolver = std::make_unique<MockResolver>(); |
| 639 | MockResolver* mock_resolver = resolver.get(); |
| 640 | proxy.resolver_ = std::move(resolver); |
| 641 | proxy.doh_config_.set_resolver(mock_resolver); |
| 642 | |
| 643 | // Initially off. |
| 644 | EXPECT_CALL(*mock_resolver, SetDoHProviders(IsEmpty(), false)); |
| 645 | shill::Client::IPConfig ipconfig; |
| 646 | ipconfig.ipv4_dns_addresses = {"1.1.1.1", "9.9.9.9"}; |
| 647 | proxy.UpdateNameServers(ipconfig); |
| 648 | |
| 649 | // Automatic mode - matched cloudflare. |
| 650 | EXPECT_CALL( |
| 651 | *mock_resolver, |
| 652 | SetDoHProviders( |
| 653 | ElementsAre(StrEq("https://chrome.cloudflare-dns.com/dns-query")), |
| 654 | false)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 655 | brillo::VariantDictionary props; |
| 656 | props["https://dns.google.com"] = std::string("8.8.8.8, 8.8.4.4"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 657 | props["https://chrome.cloudflare-dns.com/dns-query"] = |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 658 | std::string("1.1.1.1,2606:4700:4700::1111"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 659 | proxy.OnDoHProvidersChanged(props); |
| 660 | |
| 661 | // Automatic mode - no match. |
| 662 | EXPECT_CALL(*mock_resolver, SetDoHProviders(IsEmpty(), false)); |
| 663 | ipconfig.ipv4_dns_addresses = {"10.10.10.1"}; |
| 664 | proxy.UpdateNameServers(ipconfig); |
| 665 | |
| 666 | // Automatic mode - matched google. |
| 667 | EXPECT_CALL( |
| 668 | *mock_resolver, |
| 669 | SetDoHProviders(ElementsAre(StrEq("https://dns.google.com")), false)); |
| 670 | ipconfig.ipv4_dns_addresses = {"8.8.4.4", "10.10.10.1", "8.8.8.8"}; |
| 671 | proxy.UpdateNameServers(ipconfig); |
| 672 | |
| 673 | // Explicitly turned off. |
| 674 | EXPECT_CALL(*mock_resolver, SetDoHProviders(IsEmpty(), false)); |
| 675 | props.clear(); |
| 676 | proxy.OnDoHProvidersChanged(props); |
| 677 | |
| 678 | // Still off - even switching ns back. |
| 679 | EXPECT_CALL(*mock_resolver, SetDoHProviders(IsEmpty(), false)); |
| 680 | ipconfig.ipv4_dns_addresses = {"8.8.4.4", "10.10.10.1", "8.8.8.8"}; |
| 681 | proxy.UpdateNameServers(ipconfig); |
| 682 | |
| 683 | // Always-on mode. |
| 684 | EXPECT_CALL( |
| 685 | *mock_resolver, |
| 686 | SetDoHProviders(ElementsAre(StrEq("https://doh.opendns.com/dns-query")), |
| 687 | true)); |
| 688 | props.clear(); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 689 | props["https://doh.opendns.com/dns-query"] = std::string(""); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 690 | proxy.OnDoHProvidersChanged(props); |
| 691 | |
| 692 | // Back to automatic mode, though no matching ns. |
| 693 | EXPECT_CALL(*mock_resolver, SetDoHProviders(IsEmpty(), false)); |
| 694 | props.clear(); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 695 | props["https://doh.opendns.com/dns-query"] = std::string( |
| 696 | "208.67.222.222,208.67.220.220,2620:119:35::35, 2620:119:53::53"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 697 | proxy.OnDoHProvidersChanged(props); |
| 698 | |
| 699 | // Automatic mode working on ns update. |
| 700 | EXPECT_CALL( |
| 701 | *mock_resolver, |
| 702 | SetDoHProviders(ElementsAre(StrEq("https://doh.opendns.com/dns-query")), |
| 703 | false)); |
| 704 | ipconfig.ipv4_dns_addresses = {"8.8.8.8", "2620:119:35::35"}; |
| 705 | proxy.UpdateNameServers(ipconfig); |
| 706 | } |
| 707 | |
| 708 | TEST_F(ProxyTest, MultipleDoHProvidersForAlwaysOnMode) { |
| 709 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 710 | ShillClient()); |
| 711 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 712 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 713 | auto resolver = std::make_unique<MockResolver>(); |
| 714 | MockResolver* mock_resolver = resolver.get(); |
| 715 | proxy.resolver_ = std::move(resolver); |
| 716 | proxy.doh_config_.set_resolver(mock_resolver); |
| 717 | EXPECT_CALL( |
| 718 | *mock_resolver, |
| 719 | SetDoHProviders(UnorderedElementsAre(StrEq("https://dns.google.com"), |
| 720 | StrEq("https://doh.opendns.com")), |
| 721 | true)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 722 | brillo::VariantDictionary props; |
| 723 | props["https://dns.google.com"] = std::string(""); |
| 724 | props["https://doh.opendns.com"] = std::string(""); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 725 | proxy.OnDoHProvidersChanged(props); |
| 726 | } |
| 727 | |
| 728 | TEST_F(ProxyTest, MultipleDoHProvidersForAutomaticMode) { |
| 729 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 730 | ShillClient()); |
| 731 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 732 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 733 | auto resolver = std::make_unique<MockResolver>(); |
| 734 | MockResolver* mock_resolver = resolver.get(); |
| 735 | proxy.resolver_ = std::move(resolver); |
| 736 | proxy.doh_config_.set_resolver(mock_resolver); |
| 737 | shill::Client::IPConfig ipconfig; |
| 738 | ipconfig.ipv4_dns_addresses = {"1.1.1.1", "10.10.10.10"}; |
| 739 | proxy.UpdateNameServers(ipconfig); |
| 740 | |
| 741 | EXPECT_CALL( |
| 742 | *mock_resolver, |
| 743 | SetDoHProviders( |
| 744 | ElementsAre(StrEq("https://chrome.cloudflare-dns.com/dns-query")), |
| 745 | false)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 746 | brillo::VariantDictionary props; |
| 747 | props["https://dns.google.com"] = std::string("8.8.8.8, 8.8.4.4"); |
| 748 | props["https://dns.quad9.net/dns-query"] = std::string("9.9.9.9,2620:fe::9"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 749 | props["https://chrome.cloudflare-dns.com/dns-query"] = |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 750 | std::string("1.1.1.1,2606:4700:4700::1111"); |
| 751 | props["https://doh.opendns.com/dns-query"] = std::string( |
| 752 | "208.67.222.222,208.67.220.220,2620:119:35::35, 2620:119:53::53"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 753 | proxy.OnDoHProvidersChanged(props); |
| 754 | |
| 755 | EXPECT_CALL(*mock_resolver, |
| 756 | SetDoHProviders(UnorderedElementsAre( |
| 757 | StrEq("https://dns.google.com"), |
| 758 | StrEq("https://doh.opendns.com/dns-query"), |
| 759 | StrEq("https://dns.quad9.net/dns-query")), |
| 760 | false)); |
| 761 | ipconfig.ipv4_dns_addresses = {"8.8.8.8", "10.10.10.10"}; |
| 762 | ipconfig.ipv6_dns_addresses = {"2620:fe::9", "2620:119:53::53"}; |
| 763 | proxy.UpdateNameServers(ipconfig); |
| 764 | } |
| 765 | |
| 766 | TEST_F(ProxyTest, DoHBadAlwaysOnConfigSetsAutomaticMode) { |
| 767 | Proxy proxy(Proxy::Options{.type = Proxy::Type::kSystem}, PatchpanelClient(), |
| 768 | ShillClient()); |
| 769 | proxy.device_ = std::make_unique<shill::Client::Device>(); |
| 770 | proxy.device_->state = shill::Client::Device::ConnectionState::kOnline; |
| 771 | auto resolver = std::make_unique<MockResolver>(); |
| 772 | MockResolver* mock_resolver = resolver.get(); |
| 773 | proxy.resolver_ = std::move(resolver); |
| 774 | proxy.doh_config_.set_resolver(mock_resolver); |
| 775 | shill::Client::IPConfig ipconfig; |
| 776 | ipconfig.ipv4_dns_addresses = {"1.1.1.1", "10.10.10.10"}; |
| 777 | proxy.UpdateNameServers(ipconfig); |
| 778 | |
| 779 | EXPECT_CALL( |
| 780 | *mock_resolver, |
| 781 | SetDoHProviders( |
| 782 | ElementsAre(StrEq("https://chrome.cloudflare-dns.com/dns-query")), |
| 783 | false)); |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 784 | brillo::VariantDictionary props; |
| 785 | props["https://dns.opendns.com"] = std::string(""); |
| 786 | props["https://dns.google.com"] = std::string("8.8.8.8, 8.8.4.4"); |
| 787 | props["https://dns.quad9.net/dns-query"] = std::string("9.9.9.9,2620:fe::9"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 788 | props["https://chrome.cloudflare-dns.com/dns-query"] = |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 789 | std::string("1.1.1.1,2606:4700:4700::1111"); |
| 790 | props["https://doh.opendns.com/dns-query"] = std::string( |
| 791 | "208.67.222.222,208.67.220.220,2620:119:35::35, 2620:119:53::53"); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 792 | proxy.OnDoHProvidersChanged(props); |
| 793 | |
| 794 | EXPECT_CALL(*mock_resolver, |
| 795 | SetDoHProviders(UnorderedElementsAre( |
| 796 | StrEq("https://dns.google.com"), |
| 797 | StrEq("https://doh.opendns.com/dns-query"), |
| 798 | StrEq("https://dns.quad9.net/dns-query")), |
| 799 | false)); |
| 800 | ipconfig.ipv4_dns_addresses = {"8.8.8.8", "10.10.10.10"}; |
| 801 | ipconfig.ipv6_dns_addresses = {"2620:fe::9", "2620:119:53::53"}; |
| 802 | proxy.UpdateNameServers(ipconfig); |
| 803 | } |
| 804 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 805 | } // namespace dns_proxy |