Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 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 "permission_broker/fake_libusb_wrapper.h" |
| 6 | #include "permission_broker/usb_control.h" |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <brillo/message_loops/fake_message_loop.h> |
| 14 | #include <brillo/message_loops/message_loop.h> |
| 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::Invoke; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::SetArgPointee; |
| 20 | |
| 21 | namespace permission_broker { |
| 22 | |
| 23 | class UsbControlTest : public testing::Test { |
| 24 | public: |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 25 | UsbControlTest() : loop_(nullptr) { loop_.SetAsCurrent(); } |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 26 | UsbControlTest(const UsbControlTest&) = delete; |
| 27 | UsbControlTest& operator=(const UsbControlTest&) = delete; |
| 28 | |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 29 | ~UsbControlTest() override = default; |
| 30 | |
| 31 | protected: |
| 32 | brillo::FakeMessageLoop loop_; |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 35 | TEST_F(UsbControlTest, DeviceNotAllowed) { |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 36 | auto manager = std::make_unique<FakeUsbDeviceManager>( |
| 37 | std::vector<std::unique_ptr<UsbDeviceInterface>>()); |
| 38 | UsbControl usb_control(std::move(manager)); |
| 39 | |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 40 | EXPECT_FALSE(usb_control.IsDeviceAllowed(0x0, 0x0011)); |
| 41 | EXPECT_FALSE(usb_control.IsDeviceAllowed(0x2bd9, 0x0)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 44 | TEST_F(UsbControlTest, DeviceAllowed) { |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 45 | auto manager = std::make_unique<FakeUsbDeviceManager>( |
| 46 | std::vector<std::unique_ptr<UsbDeviceInterface>>()); |
| 47 | UsbControl usb_control(std::move(manager)); |
| 48 | |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 49 | EXPECT_TRUE(usb_control.IsDeviceAllowed(0x2bd9, 0x0011)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void TestResultCallback(std::shared_ptr<bool> result_out, bool result) { |
| 53 | *result_out = result; |
| 54 | } |
| 55 | |
| 56 | TEST_F(UsbControlTest, PowerCycleSingleDeviceSucceeds) { |
| 57 | // Create the fake valid device that will be used together with the associated |
| 58 | // device parent. |
| 59 | // - Target device. |
| 60 | FakeUsbDevice::State state(false, false); |
| 61 | UsbDeviceInfo info(0x2bd9, 0x0011); |
| 62 | UsbDeviceInfo parent_info(0x1111, 0x2222, LIBUSB_CLASS_HUB); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 63 | auto valid_device = |
| 64 | std::make_unique<FakeUsbDevice>(info, parent_info, &state); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 65 | // Add the device into the vector of extracted devices and create a manager |
| 66 | // that is capable of returing such a vector. |
| 67 | std::vector<std::unique_ptr<UsbDeviceInterface>> devices; |
| 68 | devices.push_back(std::move(valid_device)); |
| 69 | auto manager = std::make_unique<FakeUsbDeviceManager>(std::move(devices)); |
| 70 | |
| 71 | // Test that usb_control is correctly able to power-cycle the device. |
| 72 | UsbControl usb_control(std::move(manager)); |
| 73 | std::shared_ptr<bool> result = std::make_shared<bool>(false); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 74 | usb_control.PowerCycleUsbPorts(base::Bind(&TestResultCallback, result), |
| 75 | 0x2bd9, 0x0011, |
| 76 | base::TimeDelta::FromMilliseconds(1)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 77 | |
| 78 | EXPECT_EQ(state.power_off_counter, 1); |
| 79 | EXPECT_EQ(state.power_on_counter, 0); |
| 80 | |
| 81 | loop_.RunOnce(true); |
| 82 | |
| 83 | EXPECT_EQ(state.power_off_counter, 1); |
| 84 | EXPECT_EQ(state.power_on_counter, 1); |
| 85 | EXPECT_TRUE(*result); |
| 86 | } |
| 87 | |
| 88 | TEST_F(UsbControlTest, PowerCycleMultipleDevicesSucceed) { |
| 89 | // Create two fake valid devices that will be used together with the |
| 90 | // associated device parent. |
| 91 | // - First target device. |
| 92 | UsbDeviceInfo info(0x2bd9, 0x0011); |
| 93 | UsbDeviceInfo parent_info(0x1111, 0x2222, LIBUSB_CLASS_HUB); |
| 94 | FakeUsbDevice::State state1(false, false); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 95 | auto valid_device1 = |
| 96 | std::make_unique<FakeUsbDevice>(info, parent_info, &state1); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 97 | // - Second target device. |
| 98 | FakeUsbDevice::State state2(false, false); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 99 | auto valid_device2 = |
| 100 | std::make_unique<FakeUsbDevice>(info, parent_info, &state2); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 101 | // Add the device into the vector of extracted devices and create a manager |
| 102 | // that is capable of returing such a vector. |
| 103 | std::vector<std::unique_ptr<UsbDeviceInterface>> devices; |
| 104 | devices.push_back(std::move(valid_device1)); |
| 105 | devices.push_back(std::move(valid_device2)); |
| 106 | auto manager = std::make_unique<FakeUsbDeviceManager>(std::move(devices)); |
| 107 | |
| 108 | // Test that usb_control is correctly able to power-cycle the device. |
| 109 | UsbControl usb_control(std::move(manager)); |
| 110 | std::shared_ptr<bool> result = std::make_shared<bool>(false); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 111 | usb_control.PowerCycleUsbPorts(base::Bind(&TestResultCallback, result), |
| 112 | 0x2bd9, 0x0011, |
| 113 | base::TimeDelta::FromMilliseconds(1)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 114 | |
| 115 | EXPECT_EQ(state1.power_off_counter, 1); |
| 116 | EXPECT_EQ(state1.power_on_counter, 0); |
| 117 | EXPECT_EQ(state2.power_off_counter, 1); |
| 118 | EXPECT_EQ(state2.power_on_counter, 0); |
| 119 | |
| 120 | loop_.RunOnce(true); |
| 121 | |
| 122 | EXPECT_EQ(state1.power_off_counter, 1); |
| 123 | EXPECT_EQ(state1.power_on_counter, 1); |
| 124 | EXPECT_EQ(state2.power_off_counter, 1); |
| 125 | EXPECT_EQ(state2.power_on_counter, 1); |
| 126 | EXPECT_TRUE(*result); |
| 127 | } |
| 128 | |
| 129 | TEST_F(UsbControlTest, DeviceNotFound) { |
| 130 | // Create a fake manager that returns an empty vector when querying the |
| 131 | // available USB devices. |
| 132 | std::vector<std::unique_ptr<UsbDeviceInterface>> devices; |
| 133 | auto manager = std::make_unique<FakeUsbDeviceManager>(std::move(devices)); |
| 134 | |
| 135 | // Test that usb_control is not able to apply the requested operation because |
| 136 | // the device is not available. |
| 137 | UsbControl usb_control(std::move(manager)); |
| 138 | std::shared_ptr<bool> result = std::make_shared<bool>(false); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 139 | usb_control.PowerCycleUsbPorts(base::Bind(&TestResultCallback, result), |
| 140 | 0x2bd9, 0x0011, |
| 141 | base::TimeDelta::FromMilliseconds(1)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 142 | |
| 143 | EXPECT_FALSE(*result); |
| 144 | } |
| 145 | |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 146 | TEST_F(UsbControlTest, PowerCycleNotAllowedDevice) { |
| 147 | // Set up a target device that was not allowed. |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 148 | FakeUsbDevice::State state(false, false); |
| 149 | UsbDeviceInfo info(0x1234, 0x5678); |
| 150 | UsbDeviceInfo parent_info; |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 151 | auto invalid_device = |
| 152 | std::make_unique<FakeUsbDevice>(info, parent_info, &state); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 153 | |
| 154 | std::vector<std::unique_ptr<UsbDeviceInterface>> devices; |
| 155 | devices.push_back(std::move(invalid_device)); |
| 156 | auto manager = std::make_unique<FakeUsbDeviceManager>(std::move(devices)); |
| 157 | |
| 158 | // Test that usb_control unable to powercycle the device that is not |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 159 | // allowed. |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 160 | UsbControl usb_control(std::move(manager)); |
| 161 | std::shared_ptr<bool> result = std::make_shared<bool>(false); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 162 | usb_control.PowerCycleUsbPorts(base::Bind(&TestResultCallback, result), |
| 163 | 0x1234, 0x5678, |
| 164 | base::TimeDelta::FromMilliseconds(1)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 165 | |
| 166 | EXPECT_EQ(state.power_off_counter, 0); |
| 167 | EXPECT_EQ(state.power_on_counter, 0); |
| 168 | EXPECT_FALSE(*result); |
| 169 | } |
| 170 | |
| 171 | TEST_F(UsbControlTest, PowerOffFails) { |
| 172 | // Create the fake valid device that will be used together with the associated |
| 173 | // device parent. |
| 174 | // - Target device. |
| 175 | FakeUsbDevice::State state(true, false); |
| 176 | UsbDeviceInfo info(0x2bd9, 0x0011); |
| 177 | UsbDeviceInfo parent_info(0x1111, 0x2222, LIBUSB_CLASS_HUB); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 178 | auto device = std::make_unique<FakeUsbDevice>(info, parent_info, &state); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 179 | |
| 180 | std::vector<std::unique_ptr<UsbDeviceInterface>> devices; |
| 181 | devices.push_back(std::move(device)); |
| 182 | auto manager = std::make_unique<FakeUsbDeviceManager>(std::move(devices)); |
| 183 | |
| 184 | // Test that usb_control will return false when the device to be powercycled |
| 185 | // failes to turn off. |
| 186 | std::shared_ptr<bool> result = std::make_shared<bool>(false); |
| 187 | UsbControl usb_control(std::move(manager)); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 188 | usb_control.PowerCycleUsbPorts(base::Bind(&TestResultCallback, result), |
| 189 | 0x2bd9, 0x0011, |
| 190 | base::TimeDelta::FromMilliseconds(1)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 191 | |
| 192 | EXPECT_EQ(state.power_off_counter, 1); |
| 193 | EXPECT_EQ(state.power_on_counter, 0); |
| 194 | |
| 195 | loop_.RunOnce(true); |
| 196 | |
| 197 | EXPECT_EQ(state.power_off_counter, 1); |
| 198 | EXPECT_EQ(state.power_on_counter, 1); |
| 199 | EXPECT_FALSE(*result); |
| 200 | } |
| 201 | |
| 202 | TEST_F(UsbControlTest, PowerOnFails) { |
| 203 | // Create the fake valid device that will be used together with the associated |
| 204 | // device parent. |
| 205 | // - Target device. |
| 206 | FakeUsbDevice::State state(false, true); |
| 207 | UsbDeviceInfo info(0x2bd9, 0x0011); |
| 208 | UsbDeviceInfo parent_info(0x1111, 0x2222, LIBUSB_CLASS_HUB); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 209 | auto device = std::make_unique<FakeUsbDevice>(info, parent_info, &state); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 210 | |
| 211 | std::vector<std::unique_ptr<UsbDeviceInterface>> devices; |
| 212 | devices.push_back(std::move(device)); |
| 213 | auto manager = std::make_unique<FakeUsbDeviceManager>(std::move(devices)); |
| 214 | |
| 215 | // Test that usb_control will return false when the device to be powercycled |
| 216 | // failes to turn on. |
| 217 | std::shared_ptr<bool> result = std::make_shared<bool>(false); |
| 218 | UsbControl usb_control(std::move(manager)); |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 219 | usb_control.PowerCycleUsbPorts(base::Bind(&TestResultCallback, result), |
| 220 | 0x2bd9, 0x0011, |
| 221 | base::TimeDelta::FromMilliseconds(1)); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 222 | |
| 223 | EXPECT_EQ(state.power_off_counter, 1); |
| 224 | EXPECT_EQ(state.power_on_counter, 0); |
| 225 | |
| 226 | loop_.RunOnce(true); |
| 227 | |
| 228 | EXPECT_EQ(state.power_off_counter, 1); |
| 229 | EXPECT_EQ(state.power_on_counter, 1); |
| 230 | EXPECT_FALSE(*result); |
| 231 | } |
| 232 | |
| 233 | } // namespace permission_broker |