Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #include "patchpanel/shill_client.h" |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | #include <set> |
| 9 | #include <string> |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 10 | #include <utility> |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 17 | #include "patchpanel/fake_shill_client.h" |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 18 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 19 | namespace patchpanel { |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 20 | |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 21 | class ShillClientTest : public testing::Test { |
| 22 | protected: |
| 23 | void SetUp() override { |
Garrick Evans | ef6b20d | 2019-05-31 13:29:24 +0900 | [diff] [blame] | 24 | helper_ = std::make_unique<FakeShillClientHelper>(); |
| 25 | client_ = helper_->FakeClient(); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 26 | client_->RegisterDefaultDeviceChangedHandler(base::Bind( |
| 27 | &ShillClientTest::DefaultDeviceChangedHandler, base::Unretained(this))); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 28 | client_->RegisterDevicesChangedHandler(base::Bind( |
| 29 | &ShillClientTest::DevicesChangedHandler, base::Unretained(this))); |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 30 | client_->RegisterIPConfigsChangedHandler(base::Bind( |
| 31 | &ShillClientTest::IPConfigsChangedHandler, base::Unretained(this))); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 32 | default_ifname_.clear(); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 33 | added_.clear(); |
| 34 | removed_.clear(); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 35 | } |
| 36 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 37 | void DefaultDeviceChangedHandler(const ShillClient::Device& new_device, |
| 38 | const ShillClient::Device& prev_device) { |
| 39 | default_ifname_ = new_device.ifname; |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 40 | } |
| 41 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 42 | void DevicesChangedHandler(const std::set<std::string>& added, |
| 43 | const std::set<std::string>& removed) { |
| 44 | added_ = added; |
| 45 | removed_ = removed; |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 48 | void IPConfigsChangedHandler(const std::string& device, |
| 49 | const ShillClient::IPConfig& ipconfig) { |
| 50 | ipconfig_change_calls_.emplace_back(std::make_pair(device, ipconfig)); |
| 51 | } |
| 52 | |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 53 | protected: |
| 54 | std::string default_ifname_; |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 55 | std::set<std::string> added_; |
| 56 | std::set<std::string> removed_; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 57 | std::vector<std::pair<std::string, ShillClient::IPConfig>> |
| 58 | ipconfig_change_calls_; |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 59 | std::unique_ptr<FakeShillClient> client_; |
Garrick Evans | ef6b20d | 2019-05-31 13:29:24 +0900 | [diff] [blame] | 60 | std::unique_ptr<FakeShillClientHelper> helper_; |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | TEST_F(ShillClientTest, DevicesChangedHandlerCalledOnDevicesPropertyChange) { |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 64 | std::vector<dbus::ObjectPath> devices = {dbus::ObjectPath("/device/eth0"), |
| 65 | dbus::ObjectPath("/device/wlan0")}; |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 66 | auto value = brillo::Any(devices); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 67 | client_->SetFakeDefaultDevice("eth0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 68 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 69 | EXPECT_EQ(added_.size(), devices.size()); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 70 | EXPECT_NE(added_.find("eth0"), added_.end()); |
| 71 | EXPECT_NE(added_.find("wlan0"), added_.end()); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 72 | EXPECT_EQ(removed_.size(), 0); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 73 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 74 | // Implies the default callback was run; |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 75 | EXPECT_EQ(default_ifname_, "eth0"); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 76 | EXPECT_NE(added_.find(default_ifname_), added_.end()); |
| 77 | |
| 78 | devices.pop_back(); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 79 | devices.emplace_back(dbus::ObjectPath("/device/eth1")); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 80 | value = brillo::Any(devices); |
| 81 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
| 82 | EXPECT_EQ(added_.size(), 1); |
| 83 | EXPECT_EQ(*added_.begin(), "eth1"); |
| 84 | EXPECT_EQ(removed_.size(), 1); |
| 85 | EXPECT_EQ(*removed_.begin(), "wlan0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST_F(ShillClientTest, VerifyDevicesPrefixStripped) { |
| 89 | std::vector<dbus::ObjectPath> devices = {dbus::ObjectPath("/device/eth0")}; |
| 90 | auto value = brillo::Any(devices); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 91 | client_->SetFakeDefaultDevice("eth0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 92 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 93 | EXPECT_EQ(added_.size(), 1); |
| 94 | EXPECT_EQ(*added_.begin(), "eth0"); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 95 | // Implies the default callback was run; |
| 96 | EXPECT_EQ(default_ifname_, "eth0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 99 | TEST_F(ShillClientTest, DefaultDeviceChangedHandlerCalledOnNewDefaultDevice) { |
| 100 | client_->SetFakeDefaultDevice("eth0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 101 | client_->NotifyManagerPropertyChange(shill::kDefaultServiceProperty, |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 102 | brillo::Any() /* ignored */); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 103 | EXPECT_EQ(default_ifname_, "eth0"); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 104 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 105 | client_->SetFakeDefaultDevice("wlan0"); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 106 | client_->NotifyManagerPropertyChange(shill::kDefaultServiceProperty, |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 107 | brillo::Any() /* ignored */); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 108 | EXPECT_EQ(default_ifname_, "wlan0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 111 | TEST_F(ShillClientTest, DefaultDeviceChangedHandlerNotCalledForSameDefault) { |
| 112 | client_->SetFakeDefaultDevice("eth0"); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 113 | client_->NotifyManagerPropertyChange(shill::kDefaultServiceProperty, |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 114 | brillo::Any() /* ignored */); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 115 | EXPECT_EQ(default_ifname_, "eth0"); |
| 116 | |
| 117 | default_ifname_.clear(); |
| 118 | client_->NotifyManagerPropertyChange(shill::kDefaultServiceProperty, |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 119 | brillo::Any() /* ignored */); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 120 | // Implies the callback was not run the second time. |
| 121 | EXPECT_EQ(default_ifname_, ""); |
| 122 | } |
| 123 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 124 | TEST_F(ShillClientTest, DefaultDeviceChanges) { |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 125 | // One network device appears. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 126 | std::vector<dbus::ObjectPath> devices = {dbus::ObjectPath("/device/wlan0")}; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 127 | auto value = brillo::Any(devices); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 128 | client_->SetFakeDefaultDevice("wlan0"); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 129 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 130 | EXPECT_EQ(default_ifname_, "wlan0"); |
| 131 | |
| 132 | // A second device appears. |
| 133 | default_ifname_.clear(); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 134 | devices = {dbus::ObjectPath("/device/eth0"), |
| 135 | dbus::ObjectPath("/device/wlan0")}; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 136 | value = brillo::Any(devices); |
| 137 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 138 | EXPECT_EQ(default_ifname_, ""); |
| 139 | |
| 140 | // The second device becomes the default interface. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 141 | client_->SetFakeDefaultDevice("eth0"); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 142 | client_->NotifyManagerPropertyChange(shill::kDefaultServiceProperty, |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 143 | brillo::Any() /* ignored */); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 144 | EXPECT_EQ(default_ifname_, "eth0"); |
| 145 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 146 | // The first device disappears. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 147 | devices = {dbus::ObjectPath("/device/eth0")}; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 148 | value = brillo::Any(devices); |
| 149 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 150 | // The default device is still the same. |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 151 | EXPECT_EQ(default_ifname_, "eth0"); |
| 152 | |
| 153 | // All devices have disappeared. |
| 154 | devices = {}; |
| 155 | value = brillo::Any(devices); |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 156 | client_->SetFakeDefaultDevice(""); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 157 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 158 | EXPECT_EQ(default_ifname_, ""); |
Garrick Evans | f2e11f0 | 2018-12-25 15:25:39 +0900 | [diff] [blame] | 159 | } |
| 160 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 161 | TEST_F(ShillClientTest, ListenToDeviceChangeSignalOnNewDevices) { |
| 162 | // Adds a device. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 163 | std::vector<dbus::ObjectPath> devices = {dbus::ObjectPath("/device/wlan0")}; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 164 | auto value = brillo::Any(devices); |
| 165 | EXPECT_CALL(*helper_->mock_proxy(), |
| 166 | DoConnectToSignal(shill::kFlimflamDeviceInterface, |
| 167 | shill::kMonitorPropertyChanged, _, _)) |
| 168 | .Times(1); |
| 169 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
| 170 | |
| 171 | // Adds another device. DoConnectToSignal() called only for the new added one. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 172 | devices = {dbus::ObjectPath("/device/wlan0"), |
| 173 | dbus::ObjectPath("/device/eth0")}; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 174 | value = brillo::Any(devices); |
| 175 | EXPECT_CALL(*helper_->mock_proxy(), |
| 176 | DoConnectToSignal(shill::kFlimflamDeviceInterface, |
| 177 | shill::kMonitorPropertyChanged, _, _)) |
| 178 | .Times(1); |
| 179 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, value); |
| 180 | } |
| 181 | |
| 182 | TEST_F(ShillClientTest, TriggerOnIPConfigsChangeHandlerOnce) { |
| 183 | // Adds a device. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 184 | std::vector<dbus::ObjectPath> devices = {dbus::ObjectPath("/device/wlan0")}; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 185 | auto devices_value = brillo::Any(devices); |
| 186 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, devices_value); |
| 187 | client_->NotifyDevicePropertyChange("wlan0", shill::kIPConfigsProperty, |
| 188 | brillo::Any()); |
| 189 | ASSERT_EQ(ipconfig_change_calls_.size(), 1u); |
| 190 | EXPECT_EQ(ipconfig_change_calls_.back().first, "wlan0"); |
| 191 | |
| 192 | // Removes the device and adds it again. |
| 193 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, brillo::Any()); |
| 194 | client_->NotifyManagerPropertyChange(shill::kDevicesProperty, devices_value); |
| 195 | client_->NotifyDevicePropertyChange("wlan0", shill::kIPConfigsProperty, |
| 196 | brillo::Any()); |
| 197 | ASSERT_EQ(ipconfig_change_calls_.size(), 2u); |
| 198 | EXPECT_EQ(ipconfig_change_calls_.back().first, "wlan0"); |
| 199 | } |
| 200 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 201 | } // namespace patchpanel |