Garrick Evans | 5486162 | 2019-07-19 09:05:09 +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/arc_service.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 6 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 7 | #include <net/if.h> |
| 8 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/bind.h> |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 13 | #include <base/bind_helpers.h> |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 14 | #include <dbus/mock_bus.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 18 | #include "patchpanel/address_manager.h" |
| 19 | #include "patchpanel/fake_process_runner.h" |
| 20 | #include "patchpanel/fake_shill_client.h" |
| 21 | #include "patchpanel/mock_datapath.h" |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 22 | #include "patchpanel/mock_firewall.h" |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 23 | #include "patchpanel/net_util.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 24 | |
| 25 | using testing::_; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 26 | using testing::AnyNumber; |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 27 | using testing::Eq; |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 28 | using testing::Invoke; |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 29 | using testing::Pair; |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 30 | using testing::Pointee; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 31 | using testing::Return; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 32 | using testing::ReturnRef; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 33 | using testing::StrEq; |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 34 | using testing::UnorderedElementsAre; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 35 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 36 | namespace patchpanel { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 37 | namespace { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 38 | constexpr pid_t kTestPID = -2; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 39 | constexpr uint32_t kTestCID = 2; |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 40 | constexpr uint32_t kArcHostIP = Ipv4Addr(100, 115, 92, 1); |
| 41 | constexpr uint32_t kArcGuestIP = Ipv4Addr(100, 115, 92, 2); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 42 | constexpr uint32_t kFirstEthHostIP = Ipv4Addr(100, 115, 92, 5); |
| 43 | constexpr uint32_t kFirstEthGuestIP = Ipv4Addr(100, 115, 92, 6); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 44 | constexpr uint32_t kSecondEthHostIP = Ipv4Addr(100, 115, 92, 9); |
| 45 | constexpr uint32_t kFirstWifiHostIP = Ipv4Addr(100, 115, 92, 13); |
| 46 | constexpr uint32_t kSecondWifiHostIP = Ipv4Addr(100, 115, 92, 17); |
| 47 | constexpr uint32_t kFirstCellHostIP = Ipv4Addr(100, 115, 92, 21); |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 48 | constexpr MacAddress kArcVmArc0MacAddr = {0x42, 0x37, 0x05, 0x13, 0x17, 0x01}; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 49 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 50 | class MockTrafficForwarder : public TrafficForwarder { |
| 51 | public: |
| 52 | MockTrafficForwarder() = default; |
| 53 | ~MockTrafficForwarder() = default; |
| 54 | |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 55 | MOCK_METHOD4(StartForwarding, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 56 | void(const std::string& ifname_physical, |
| 57 | const std::string& ifname_virtual, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 58 | bool ipv6, |
| 59 | bool multicast)); |
| 60 | |
| 61 | MOCK_METHOD4(StopForwarding, |
| 62 | void(const std::string& ifname_physical, |
| 63 | const std::string& ifname_virtual, |
| 64 | bool ipv6, |
| 65 | bool multicast)); |
| 66 | }; |
| 67 | |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 68 | class MockShillClient : public ShillClient { |
| 69 | public: |
| 70 | explicit MockShillClient(scoped_refptr<dbus::MockBus> bus) |
| 71 | : ShillClient(bus) {} |
| 72 | ~MockShillClient() = default; |
| 73 | |
| 74 | MOCK_METHOD2(GetDeviceProperties, |
| 75 | bool(const std::string& ifname, ShillClient::Device* device)); |
| 76 | }; |
| 77 | |
| 78 | void ExpectGetDeviceProperties(MockShillClient& shill_client, |
| 79 | const std::string& iface, |
| 80 | ShillClient::Device::Type type) { |
| 81 | EXPECT_CALL(shill_client, GetDeviceProperties(StrEq(iface), _)) |
| 82 | .WillRepeatedly( |
| 83 | Invoke([type](const std::string& _, ShillClient::Device* device) { |
| 84 | device->type = type; |
| 85 | return true; |
| 86 | })); |
| 87 | } |
| 88 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 89 | } // namespace |
| 90 | |
| 91 | class ArcServiceTest : public testing::Test { |
| 92 | public: |
Garrick Evans | 4ee5ce2 | 2020-03-18 07:05:17 +0900 | [diff] [blame] | 93 | ArcServiceTest() : testing::Test() {} |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 94 | |
| 95 | protected: |
| 96 | void SetUp() override { |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 97 | runner_ = std::make_unique<FakeProcessRunner>(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 98 | runner_->Capture(false); |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 99 | datapath_ = std::make_unique<MockDatapath>(runner_.get(), &firewall_); |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 100 | shill_client_ = std::make_unique<MockShillClient>(shill_helper_.mock_bus()); |
Garrick Evans | 4ee5ce2 | 2020-03-18 07:05:17 +0900 | [diff] [blame] | 101 | addr_mgr_ = std::make_unique<AddressManager>(); |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 102 | guest_devices_.clear(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 105 | std::unique_ptr<ArcService> NewService(GuestMessage::GuestType guest) { |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 106 | return std::make_unique<ArcService>( |
| 107 | shill_client_.get(), datapath_.get(), addr_mgr_.get(), &forwarder_, |
| 108 | guest, |
| 109 | base::BindRepeating(&ArcServiceTest::DeviceHandler, |
| 110 | base::Unretained(this))); |
| 111 | } |
| 112 | |
| 113 | void DeviceHandler(const Device& device, |
| 114 | Device::ChangeEvent event, |
| 115 | GuestMessage::GuestType guest_type) { |
| 116 | guest_devices_[device.host_ifname()] = event; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 117 | } |
| 118 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 119 | FakeShillClientHelper shill_helper_; |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 120 | std::unique_ptr<MockShillClient> shill_client_; |
Garrick Evans | 4ee5ce2 | 2020-03-18 07:05:17 +0900 | [diff] [blame] | 121 | std::unique_ptr<AddressManager> addr_mgr_; |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 122 | MockTrafficForwarder forwarder_; |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 123 | std::unique_ptr<MockDatapath> datapath_; |
| 124 | std::unique_ptr<FakeProcessRunner> runner_; |
Jason Jeremy Iman | a7273a3 | 2020-08-04 11:25:31 +0900 | [diff] [blame] | 125 | MockFirewall firewall_; |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 126 | std::map<std::string, Device::ChangeEvent> guest_devices_; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 127 | }; |
| 128 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 129 | TEST_F(ArcServiceTest, NotStarted_AddDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 130 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 131 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 132 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), _, _)).Times(0); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 133 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 134 | StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), _, _, false)) |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 135 | .Times(0); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 136 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 137 | auto svc = NewService(GuestMessage::ARC); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 138 | svc->OnDevicesChanged({"eth0"}, {}); |
| 139 | EXPECT_TRUE(svc->devices_.find("eth0") == svc->devices_.end()); |
| 140 | EXPECT_FALSE(svc->shill_devices_.find("eth0") == svc->shill_devices_.end()); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 143 | TEST_F(ArcServiceTest, NotStarted_AddRemoveDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 144 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 145 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 146 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), _, _)).Times(0); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 147 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 148 | StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), _, _, false)) |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 149 | .Times(0); |
| 150 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 151 | StopRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), _, _, false)) |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 152 | .Times(0); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 153 | EXPECT_CALL(*datapath_, RemoveBridge(StrEq("arc_eth0"))).Times(0); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 154 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 155 | auto svc = NewService(GuestMessage::ARC); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 156 | svc->OnDevicesChanged({"eth0"}, {}); |
| 157 | svc->OnDevicesChanged({}, {"eth0"}); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 158 | EXPECT_TRUE(svc->devices_.find("eth0") == svc->devices_.end()); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 159 | EXPECT_TRUE(svc->shill_devices_.find("eth0") == svc->shill_devices_.end()); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 160 | } |
| 161 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 162 | TEST_F(ArcServiceTest, VerifyAddrConfigs) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 163 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 164 | ShillClient::Device::Type::kEthernet); |
| 165 | ExpectGetDeviceProperties(*shill_client_, "eth1", |
| 166 | ShillClient::Device::Type::kEthernet); |
| 167 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 168 | ShillClient::Device::Type::kWifi); |
| 169 | ExpectGetDeviceProperties(*shill_client_, "wlan1", |
| 170 | ShillClient::Device::Type::kWifi); |
| 171 | ExpectGetDeviceProperties(*shill_client_, "wwan0", |
| 172 | ShillClient::Device::Type::kCellular); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 173 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 174 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 175 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 176 | .WillOnce(Return(true)); |
| 177 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 178 | .WillOnce(Return(true)); |
| 179 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth1"), kSecondEthHostIP, 30)) |
| 180 | .WillOnce(Return(true)); |
| 181 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_wlan0"), kFirstWifiHostIP, 30)) |
| 182 | .WillOnce(Return(true)); |
| 183 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_wlan1"), kSecondWifiHostIP, 30)) |
| 184 | .WillOnce(Return(true)); |
| 185 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_wwan0"), kFirstCellHostIP, 30)) |
| 186 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 187 | EXPECT_CALL(*datapath_, |
| 188 | ConnectVethPair(kTestPID, StrEq("arc_netns"), _, _, _, _, _, _)) |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 189 | .WillRepeatedly(Return(true)); |
| 190 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 191 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 192 | auto svc = NewService(GuestMessage::ARC); |
| 193 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 194 | svc->OnDevicesChanged({"eth0", "eth1", "wlan0", "wlan1", "wwan0"}, {}); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(ArcServiceTest, VerifyAddrOrder) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 198 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 199 | ShillClient::Device::Type::kWifi); |
| 200 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 201 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 202 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 203 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 204 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 205 | .WillOnce(Return(true)); |
| 206 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 207 | .Times(2) |
| 208 | .WillRepeatedly(Return(true)); |
| 209 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_wlan0"), kFirstWifiHostIP, 30)) |
| 210 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 211 | EXPECT_CALL(*datapath_, |
| 212 | ConnectVethPair(kTestPID, StrEq("arc_netns"), _, _, _, _, _, _)) |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 213 | .WillRepeatedly(Return(true)); |
| 214 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 215 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 216 | auto svc = NewService(GuestMessage::ARC); |
| 217 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 218 | svc->OnDevicesChanged({"wlan0"}, {}); |
| 219 | svc->OnDevicesChanged({"eth0"}, {}); |
| 220 | svc->OnDevicesChanged({}, {"eth0"}); |
| 221 | svc->OnDevicesChanged({"eth0"}, {}); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 224 | TEST_F(ArcServiceTest, StableArcVmMacAddrs) { |
| 225 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
| 226 | .WillRepeatedly(Return("vmtap")); |
| 227 | EXPECT_CALL(*datapath_, AddBridge(_, _, 30)).WillRepeatedly(Return(true)); |
| 228 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
| 229 | |
| 230 | auto svc = NewService(GuestMessage::ARC_VM); |
| 231 | svc->Start(kTestCID); |
| 232 | auto configs = svc->GetDeviceConfigs(); |
| 233 | EXPECT_EQ(configs.size(), 6); |
| 234 | auto mac_addr = kArcVmArc0MacAddr; |
| 235 | for (const auto* config : configs) { |
| 236 | EXPECT_EQ(config->mac_addr(), mac_addr); |
| 237 | mac_addr[5]++; |
| 238 | } |
| 239 | } |
| 240 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 241 | // ContainerImpl |
| 242 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 243 | TEST_F(ArcServiceTest, ContainerImpl_Start) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 244 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 245 | .WillOnce(Return(true)); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 246 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 247 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 248 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 249 | .WillOnce(Return(true)); |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 250 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vetharc0"))) |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 251 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 252 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 253 | .WillOnce(Return(true)); |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 254 | EXPECT_CALL(forwarder_, StartForwarding(_, _, _, _)).Times(0); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 255 | |
| 256 | auto svc = NewService(GuestMessage::ARC); |
| 257 | svc->Start(kTestPID); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 258 | } |
| 259 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 260 | TEST_F(ArcServiceTest, ContainerImpl_FailsToCreateInterface) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 261 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 262 | .WillOnce(Return(true)); |
Garrick Evans | 63378b3 | 2020-01-14 10:36:16 +0900 | [diff] [blame] | 263 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 264 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 265 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 266 | .WillOnce(Return(false)); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 267 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)).Times(0); |
Garrick Evans | 63378b3 | 2020-01-14 10:36:16 +0900 | [diff] [blame] | 268 | EXPECT_CALL(*datapath_, RemoveBridge(_)).Times(0); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 269 | |
| 270 | auto svc = NewService(GuestMessage::ARC); |
| 271 | svc->Start(kTestPID); |
Garrick Evans | 63378b3 | 2020-01-14 10:36:16 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 274 | TEST_F(ArcServiceTest, ContainerImpl_FailsToAddInterfaceToBridge) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 275 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 276 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 277 | EXPECT_CALL(*datapath_, |
| 278 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 279 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
| 280 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 281 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 282 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 283 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vetharc0"))) |
| 284 | .WillOnce(Return(false)); |
| 285 | |
| 286 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vetharc0"))); |
| 287 | EXPECT_CALL(*datapath_, RemoveBridge(_)).Times(0); |
| 288 | |
| 289 | auto svc = NewService(GuestMessage::ARC); |
| 290 | svc->Start(kTestPID); |
| 291 | } |
| 292 | |
| 293 | TEST_F(ArcServiceTest, ContainerImpl_OnStartDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 294 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 295 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 296 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 297 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 298 | // Expectations for arc0 setup. |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 299 | EXPECT_CALL(*datapath_, |
| 300 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 301 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
| 302 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 303 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 304 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 305 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vetharc0"))) |
| 306 | .WillOnce(Return(true)); |
| 307 | // Expectations for eth0 setup. |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 308 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 309 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetheth0"), |
| 310 | StrEq("eth0"), _, kFirstEthGuestIP, 30, false)) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 311 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 312 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 313 | .WillOnce(Return(true)); |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 314 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth0"), StrEq("vetheth0"))) |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 315 | .WillOnce(Return(true)); |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 316 | EXPECT_CALL(forwarder_, |
| 317 | StartForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 318 | EXPECT_CALL(*datapath_, |
| 319 | StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 320 | kFirstEthGuestIP, TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 321 | |
| 322 | auto svc = NewService(GuestMessage::ARC); |
| 323 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 324 | svc->OnDevicesChanged({"eth0"}, {}); |
| 325 | } |
| 326 | |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 327 | TEST_F(ArcServiceTest, ContainerImpl_ScanDevices) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 328 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 329 | ShillClient::Device::Type::kEthernet); |
| 330 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 331 | ShillClient::Device::Type::kWifi); |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 332 | EXPECT_CALL(*datapath_, NetnsAttachName(_, _)).WillRepeatedly(Return(true)); |
| 333 | EXPECT_CALL(*datapath_, ConnectVethPair(_, _, _, _, _, _, _, _)) |
| 334 | .WillRepeatedly(Return(true)); |
| 335 | EXPECT_CALL(*datapath_, AddBridge(_, _, _)).WillRepeatedly(Return(true)); |
| 336 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
| 337 | |
| 338 | auto svc = NewService(GuestMessage::ARC); |
| 339 | svc->Start(kTestPID); |
| 340 | svc->OnDevicesChanged({"eth0", "wlan0"}, {}); |
| 341 | |
| 342 | std::vector<std::string> devs; |
| 343 | svc->ScanDevices(base::BindRepeating( |
| 344 | [](std::vector<std::string>* list, const Device& device) { |
| 345 | list->push_back(device.host_ifname()); |
| 346 | }, |
| 347 | &devs)); |
| 348 | |
| 349 | EXPECT_EQ(devs.size(), 2); |
| 350 | EXPECT_THAT(devs, |
| 351 | UnorderedElementsAre(StrEq("arc_eth0"), StrEq("arc_wlan0"))); |
| 352 | } |
| 353 | |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 354 | TEST_F(ArcServiceTest, ContainerImpl_DeviceHandler) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 355 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 356 | ShillClient::Device::Type::kEthernet); |
| 357 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 358 | ShillClient::Device::Type::kWifi); |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 359 | EXPECT_CALL(*datapath_, NetnsAttachName(_, _)).WillRepeatedly(Return(true)); |
| 360 | EXPECT_CALL(*datapath_, ConnectVethPair(_, _, _, _, _, _, _, _)) |
| 361 | .WillRepeatedly(Return(true)); |
| 362 | EXPECT_CALL(*datapath_, AddBridge(_, _, _)).WillRepeatedly(Return(true)); |
| 363 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
| 364 | |
| 365 | auto svc = NewService(GuestMessage::ARC); |
| 366 | svc->Start(kTestPID); |
| 367 | |
| 368 | svc->OnDevicesChanged({"eth0", "wlan0"}, {}); |
| 369 | EXPECT_EQ(guest_devices_.size(), 2); |
| 370 | EXPECT_THAT(guest_devices_, |
| 371 | UnorderedElementsAre( |
| 372 | Pair(StrEq("arc_eth0"), Device::ChangeEvent::ADDED), |
| 373 | Pair(StrEq("arc_wlan0"), Device::ChangeEvent::ADDED))); |
| 374 | |
| 375 | svc->OnDevicesChanged({}, {"wlan0"}); |
| 376 | EXPECT_THAT(guest_devices_, |
| 377 | UnorderedElementsAre( |
| 378 | Pair(StrEq("arc_eth0"), Device::ChangeEvent::ADDED), |
| 379 | Pair(StrEq("arc_wlan0"), Device::ChangeEvent::REMOVED))); |
| 380 | |
| 381 | guest_devices_.clear(); |
| 382 | svc->OnDevicesChanged({"wlan0"}, {}); |
| 383 | EXPECT_THAT(guest_devices_, |
| 384 | UnorderedElementsAre( |
| 385 | Pair(StrEq("arc_wlan0"), Device::ChangeEvent::ADDED))); |
| 386 | } |
| 387 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 388 | TEST_F(ArcServiceTest, ContainerImpl_StartAfterDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 389 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 390 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 391 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 392 | .WillOnce(Return(true)); |
| 393 | // Expectations for arc0 setup. |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 394 | EXPECT_CALL(*datapath_, |
| 395 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 396 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
| 397 | .WillOnce(Return(true)); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 398 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 399 | .WillOnce(Return(true)); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 400 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vetharc0"))) |
| 401 | .WillOnce(Return(true)); |
| 402 | // Expectations for eth0 setup. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 403 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 404 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetheth0"), |
| 405 | StrEq("eth0"), _, kFirstEthGuestIP, 30, false)) |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 406 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 407 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 408 | .WillOnce(Return(true)); |
| 409 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth0"), StrEq("vetheth0"))) |
| 410 | .WillOnce(Return(true)); |
| 411 | EXPECT_CALL(forwarder_, |
| 412 | StartForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 413 | EXPECT_CALL(*datapath_, |
| 414 | StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 415 | kFirstEthGuestIP, TrafficSource::ARC, false)); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 416 | |
| 417 | auto svc = NewService(GuestMessage::ARC); |
| 418 | svc->OnDevicesChanged({"eth0"}, {}); |
| 419 | svc->Start(kTestPID); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 420 | } |
| 421 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 422 | TEST_F(ArcServiceTest, ContainerImpl_Stop) { |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 423 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 424 | .WillOnce(Return(true)); |
| 425 | EXPECT_CALL(*datapath_, NetnsDeleteName(StrEq("arc_netns"))) |
| 426 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 427 | // Expectations for arc0 setup. |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 428 | EXPECT_CALL(*datapath_, |
| 429 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 430 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
| 431 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 432 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 433 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 434 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vetharc0"))) |
| 435 | .WillOnce(Return(true)); |
| 436 | // Expectations for arc0 teardown. |
| 437 | EXPECT_CALL(*datapath_, |
| 438 | MaskInterfaceFlags(StrEq("arcbr0"), IFF_DEBUG, IFF_UP)) |
| 439 | .WillOnce(Return(true)); |
Garrick Evans | 2f581a0 | 2020-05-11 10:43:35 +0900 | [diff] [blame] | 440 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vetharc0"))); |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 441 | EXPECT_CALL(forwarder_, StopForwarding(_, _, _, _)).Times(0); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 442 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 443 | auto svc = NewService(GuestMessage::ARC); |
| 444 | svc->Start(kTestPID); |
| 445 | svc->Stop(kTestPID); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 448 | TEST_F(ArcServiceTest, ContainerImpl_OnStopDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 449 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 450 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 451 | EXPECT_CALL(*datapath_, NetnsAttachName(StrEq("arc_netns"), kTestPID)) |
| 452 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 453 | // Expectations for arc0 setup. |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 454 | EXPECT_CALL(*datapath_, |
| 455 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetharc0"), |
| 456 | StrEq("arc0"), _, kArcGuestIP, 30, false)) |
| 457 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 458 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 459 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 460 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vetharc0"))) |
| 461 | .WillOnce(Return(true)); |
| 462 | // Expectations for eth0 setup. |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 463 | EXPECT_CALL(*datapath_, |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 464 | ConnectVethPair(kTestPID, StrEq("arc_netns"), StrEq("vetheth0"), |
| 465 | StrEq("eth0"), _, kFirstEthGuestIP, 30, false)) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 466 | .WillOnce(Return(true)); |
Hugo Benichi | 82ed5cf | 2020-09-08 21:30:22 +0900 | [diff] [blame] | 467 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 468 | .WillOnce(Return(true)); |
| 469 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth0"), StrEq("vetheth0"))) |
| 470 | .WillOnce(Return(true)); |
| 471 | // Expectations for eth0 teardown. |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 472 | EXPECT_CALL(forwarder_, |
| 473 | StopForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 474 | EXPECT_CALL(*datapath_, StopRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
| 475 | Ipv4Addr(100, 115, 92, 6), |
| 476 | TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 477 | EXPECT_CALL(*datapath_, RemoveBridge(StrEq("arc_eth0"))); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 478 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 479 | auto svc = NewService(GuestMessage::ARC); |
| 480 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 481 | svc->OnDevicesChanged({"eth0"}, {}); |
| 482 | svc->OnDevicesChanged({}, {"eth0"}); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 483 | } |
| 484 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 485 | // VM Impl |
| 486 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 487 | TEST_F(ArcServiceTest, VmImpl_Start) { |
| 488 | // Expectations for tap devices pre-creation. |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 489 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 490 | .WillOnce(Return("vmtap0")) |
| 491 | .WillOnce(Return("vmtap1")) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 492 | .WillOnce(Return("vmtap2")) |
| 493 | .WillOnce(Return("vmtap3")) |
| 494 | .WillOnce(Return("vmtap4")) |
| 495 | .WillOnce(Return("vmtap5")); |
| 496 | // Expectations for "arc0" setup. |
| 497 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 498 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 499 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vmtap0"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 500 | .WillOnce(Return(true)); |
| 501 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 502 | auto svc = NewService(GuestMessage::ARC_VM); |
| 503 | svc->Start(kTestPID); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 504 | } |
| 505 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 506 | TEST_F(ArcServiceTest, VmImpl_StartDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 507 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 508 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 509 | // Expectations for tap devices pre-creation. |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 510 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 511 | .WillOnce(Return("vmtap0")) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 512 | .WillOnce(Return("vmtap1")) |
| 513 | .WillOnce(Return("vmtap2")) |
| 514 | .WillOnce(Return("vmtap3")) |
| 515 | .WillOnce(Return("vmtap4")) |
| 516 | .WillOnce(Return("vmtap5")); |
| 517 | // Expectations for "arc0" setup. |
| 518 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 519 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 520 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vmtap0"))) |
| 521 | .WillOnce(Return(true)); |
| 522 | // Expectations for eth0 setup. |
| 523 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 524 | .WillOnce(Return(true)); |
| 525 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth0"), StrEq("vmtap1"))) |
| 526 | .WillOnce(Return(true)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 527 | EXPECT_CALL(*datapath_, StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
| 528 | Ipv4Addr(100, 115, 92, 6), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 529 | TrafficSource::ARC, false)); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 530 | EXPECT_CALL(forwarder_, |
| 531 | StartForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
| 532 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 533 | auto svc = NewService(GuestMessage::ARC_VM); |
| 534 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 535 | svc->OnDevicesChanged({"eth0"}, {}); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 536 | } |
| 537 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 538 | TEST_F(ArcServiceTest, VmImpl_StartMultipleDevices) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 539 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 540 | ShillClient::Device::Type::kEthernet); |
| 541 | ExpectGetDeviceProperties(*shill_client_, "eth1", |
| 542 | ShillClient::Device::Type::kEthernet); |
| 543 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 544 | ShillClient::Device::Type::kWifi); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 545 | // Expectations for tap devices pre-creation. |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 546 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 547 | .WillOnce(Return("vmtap0")) |
| 548 | .WillOnce(Return("vmtap1")) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 549 | .WillOnce(Return("vmtap2")) |
| 550 | .WillOnce(Return("vmtap3")) |
| 551 | .WillOnce(Return("vmtap4")) |
| 552 | .WillOnce(Return("vmtap5")); |
| 553 | // Expectations for "arc0" setup. |
| 554 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 555 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 556 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vmtap0"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 557 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 558 | // Expectations for eth0 setup. |
| 559 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
| 560 | .WillOnce(Return(true)); |
| 561 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth0"), StrEq("vmtap1"))) |
| 562 | .WillOnce(Return(true)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 563 | EXPECT_CALL(*datapath_, StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
| 564 | Ipv4Addr(100, 115, 92, 6), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 565 | TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 566 | EXPECT_CALL(forwarder_, |
| 567 | StartForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
| 568 | // Expectations for wlan0 setup. |
| 569 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_wlan0"), kFirstWifiHostIP, 30)) |
| 570 | .WillOnce(Return(true)); |
| 571 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_wlan0"), StrEq("vmtap3"))) |
| 572 | .WillOnce(Return(true)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 573 | EXPECT_CALL(*datapath_, StartRoutingDevice(StrEq("wlan0"), StrEq("arc_wlan0"), |
| 574 | Ipv4Addr(100, 115, 92, 14), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 575 | TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 576 | EXPECT_CALL(forwarder_, |
| 577 | StartForwarding(StrEq("wlan0"), StrEq("arc_wlan0"), _, _)); |
| 578 | // Expectations for eth1 setup. |
| 579 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth1"), kSecondEthHostIP, 30)) |
| 580 | .WillOnce(Return(true)); |
| 581 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth1"), StrEq("vmtap2"))) |
| 582 | .WillOnce(Return(true)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 583 | EXPECT_CALL(*datapath_, StartRoutingDevice(StrEq("eth1"), StrEq("arc_eth1"), |
| 584 | Ipv4Addr(100, 115, 92, 10), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 585 | TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 586 | EXPECT_CALL(forwarder_, |
| 587 | StartForwarding(StrEq("eth1"), StrEq("arc_eth1"), _, _)); |
| 588 | |
| 589 | auto svc = NewService(GuestMessage::ARC_VM); |
| 590 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 591 | svc->OnDevicesChanged({"eth0"}, {}); |
| 592 | svc->OnDevicesChanged({"wlan0"}, {}); |
| 593 | svc->OnDevicesChanged({"eth1"}, {}); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | TEST_F(ArcServiceTest, VmImpl_Stop) { |
| 597 | // Expectations for tap devices pre-creation. |
| 598 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
| 599 | .WillOnce(Return("vmtap0")) |
| 600 | .WillOnce(Return("vmtap1")) |
| 601 | .WillOnce(Return("vmtap2")) |
| 602 | .WillOnce(Return("vmtap3")) |
| 603 | .WillOnce(Return("vmtap4")) |
| 604 | .WillOnce(Return("vmtap5")); |
| 605 | // Expectations for "arc0" setup. |
| 606 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
| 607 | .WillOnce(Return(true)); |
| 608 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vmtap0"))) |
| 609 | .WillOnce(Return(true)); |
| 610 | // Expectations for "arc0" teardown. |
| 611 | EXPECT_CALL(*datapath_, |
| 612 | MaskInterfaceFlags(StrEq("arcbr0"), IFF_DEBUG, IFF_UP)) |
| 613 | .WillOnce(Return(true)); |
| 614 | // Expectations for tap devices teardown |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 615 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vmtap0"))); |
| 616 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vmtap1"))); |
| 617 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vmtap2"))); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 618 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vmtap3"))); |
| 619 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vmtap4"))); |
| 620 | EXPECT_CALL(*datapath_, RemoveInterface(StrEq("vmtap5"))); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 621 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 622 | auto svc = NewService(GuestMessage::ARC_VM); |
| 623 | svc->Start(kTestPID); |
| 624 | svc->Stop(kTestPID); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 625 | } |
| 626 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 627 | TEST_F(ArcServiceTest, VmImpl_StopDevice) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 628 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 629 | ShillClient::Device::Type::kEthernet); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 630 | // Expectations for tap devices pre-creation. |
Garrick Evans | c707112 | 2020-04-17 12:31:57 +0900 | [diff] [blame] | 631 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 632 | .WillOnce(Return("vmtap0")) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 633 | .WillOnce(Return("vmtap1")) |
| 634 | .WillOnce(Return("vmtap2")) |
| 635 | .WillOnce(Return("vmtap3")) |
| 636 | .WillOnce(Return("vmtap4")) |
| 637 | .WillOnce(Return("vmtap5")); |
| 638 | // Expectations for "arc0" setup. |
| 639 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arcbr0"), kArcHostIP, 30)) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 640 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 641 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arcbr0"), StrEq("vmtap0"))) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 642 | .WillOnce(Return(true)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 643 | // Expectations for eth0 setup. |
| 644 | EXPECT_CALL(*datapath_, AddBridge(StrEq("arc_eth0"), kFirstEthHostIP, 30)) |
| 645 | .WillOnce(Return(true)); |
| 646 | EXPECT_CALL(*datapath_, AddToBridge(StrEq("arc_eth0"), StrEq("vmtap1"))) |
| 647 | .WillOnce(Return(true)); |
Hugo Benichi | 8d622b5 | 2020-08-13 15:24:12 +0900 | [diff] [blame] | 648 | EXPECT_CALL(*datapath_, StartRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
| 649 | Ipv4Addr(100, 115, 92, 6), |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 650 | TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 651 | EXPECT_CALL(forwarder_, |
| 652 | StartForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
| 653 | // Expectations for eth0 teardown. |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 654 | EXPECT_CALL(forwarder_, |
| 655 | StopForwarding(StrEq("eth0"), StrEq("arc_eth0"), _, _)); |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 656 | EXPECT_CALL(*datapath_, StopRoutingDevice(StrEq("eth0"), StrEq("arc_eth0"), |
| 657 | Ipv4Addr(100, 115, 92, 6), |
| 658 | TrafficSource::ARC, false)); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 659 | EXPECT_CALL(*datapath_, RemoveBridge(StrEq("arc_eth0"))); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 660 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 661 | auto svc = NewService(GuestMessage::ARC_VM); |
| 662 | svc->Start(kTestPID); |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame] | 663 | svc->OnDevicesChanged({"eth0"}, {}); |
| 664 | svc->OnDevicesChanged({}, {"eth0"}); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 665 | } |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 666 | |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 667 | TEST_F(ArcServiceTest, VmImpl_ScanDevices) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 668 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 669 | ShillClient::Device::Type::kEthernet); |
| 670 | ExpectGetDeviceProperties(*shill_client_, "eth1", |
| 671 | ShillClient::Device::Type::kEthernet); |
| 672 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 673 | ShillClient::Device::Type::kWifi); |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 674 | // Expectations for tap devices pre-creation. |
| 675 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
| 676 | .WillOnce(Return("vmtap0")) |
| 677 | .WillOnce(Return("vmtap1")) |
| 678 | .WillOnce(Return("vmtap2")) |
| 679 | .WillOnce(Return("vmtap3")) |
| 680 | .WillOnce(Return("vmtap4")) |
| 681 | .WillOnce(Return("vmtap5")); |
| 682 | EXPECT_CALL(*datapath_, AddBridge(_, _, _)).WillRepeatedly(Return(true)); |
| 683 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
| 684 | |
| 685 | auto svc = NewService(GuestMessage::ARC_VM); |
| 686 | svc->Start(kTestPID); |
| 687 | svc->OnDevicesChanged({"eth0", "wlan0", "eth1"}, {}); |
| 688 | |
| 689 | std::vector<std::string> devs; |
| 690 | svc->ScanDevices(base::BindRepeating( |
| 691 | [](std::vector<std::string>* list, const Device& device) { |
| 692 | list->push_back(device.host_ifname()); |
| 693 | }, |
| 694 | &devs)); |
| 695 | |
| 696 | EXPECT_EQ(devs.size(), 3); |
| 697 | EXPECT_THAT(devs, UnorderedElementsAre(StrEq("arc_eth0"), StrEq("arc_wlan0"), |
| 698 | StrEq("arc_eth1"))); |
| 699 | } |
| 700 | |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 701 | TEST_F(ArcServiceTest, VmImpl_DeviceHandler) { |
Hugo Benichi | 84d96c4 | 2021-02-26 14:20:13 +0900 | [diff] [blame^] | 702 | ExpectGetDeviceProperties(*shill_client_, "eth0", |
| 703 | ShillClient::Device::Type::kEthernet); |
| 704 | ExpectGetDeviceProperties(*shill_client_, "wlan0", |
| 705 | ShillClient::Device::Type::kWifi); |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 706 | // Expectations for tap devices pre-creation. |
| 707 | EXPECT_CALL(*datapath_, AddTAP(StrEq(""), _, nullptr, StrEq("crosvm"))) |
| 708 | .WillOnce(Return("vmtap0")) |
| 709 | .WillOnce(Return("vmtap1")) |
| 710 | .WillOnce(Return("vmtap2")) |
| 711 | .WillOnce(Return("vmtap3")) |
| 712 | .WillOnce(Return("vmtap4")) |
| 713 | .WillOnce(Return("vmtap5")); |
| 714 | EXPECT_CALL(*datapath_, AddBridge(_, _, _)).WillRepeatedly(Return(true)); |
| 715 | EXPECT_CALL(*datapath_, AddToBridge(_, _)).WillRepeatedly(Return(true)); |
| 716 | |
| 717 | auto svc = NewService(GuestMessage::ARC_VM); |
| 718 | svc->Start(kTestPID); |
| 719 | |
| 720 | svc->OnDevicesChanged({"eth0", "wlan0"}, {}); |
| 721 | EXPECT_EQ(guest_devices_.size(), 2); |
| 722 | EXPECT_THAT(guest_devices_, |
| 723 | UnorderedElementsAre( |
| 724 | Pair(StrEq("arc_eth0"), Device::ChangeEvent::ADDED), |
| 725 | Pair(StrEq("arc_wlan0"), Device::ChangeEvent::ADDED))); |
| 726 | |
| 727 | svc->OnDevicesChanged({}, {"wlan0"}); |
| 728 | EXPECT_THAT(guest_devices_, |
| 729 | UnorderedElementsAre( |
| 730 | Pair(StrEq("arc_eth0"), Device::ChangeEvent::ADDED), |
| 731 | Pair(StrEq("arc_wlan0"), Device::ChangeEvent::REMOVED))); |
| 732 | |
| 733 | guest_devices_.clear(); |
| 734 | svc->OnDevicesChanged({"wlan0"}, {}); |
| 735 | EXPECT_THAT(guest_devices_, |
| 736 | UnorderedElementsAre( |
| 737 | Pair(StrEq("arc_wlan0"), Device::ChangeEvent::ADDED))); |
| 738 | } |
| 739 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 740 | } // namespace patchpanel |