OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <list> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/test/test_io_thread.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "device/base/mock_device_client.h" |
| 13 #include "device/hid/hid_connection.h" |
| 14 #include "device/hid/hid_device_filter.h" |
| 15 #include "device/hid/mock_hid_service.h" |
| 16 #include "device/test/test_device_client.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "u2f_apdu_command.h" |
| 20 #include "u2f_apdu_response.h" |
| 21 #include "u2f_hid_device.h" |
| 22 #include "u2f_packet.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 #if defined(OS_MACOSX) |
| 27 const uint64_t kTestDeviceId = 42; |
| 28 #else |
| 29 const char* kTestDeviceId = "device"; |
| 30 #endif |
| 31 |
| 32 void ResponseCallback(scoped_refptr<device::U2fApduResponse>* output, |
| 33 bool success, |
| 34 scoped_refptr<device::U2fApduResponse> response) { |
| 35 *output = response; |
| 36 } |
| 37 |
| 38 class MockHidErrorConnection : public device::HidConnection { |
| 39 public: |
| 40 explicit MockHidErrorConnection( |
| 41 scoped_refptr<device::HidDeviceInfo> device_info) |
| 42 : device::HidConnection(device_info) {} |
| 43 |
| 44 void PlatformClose() override {} |
| 45 |
| 46 void PlatformRead(const ReadCallback& callback) override {} |
| 47 |
| 48 void PlatformWrite(scoped_refptr<net::IOBuffer> buffer, |
| 49 size_t size, |
| 50 const WriteCallback& callback) override { |
| 51 callback.Run(false); |
| 52 } |
| 53 |
| 54 void PlatformGetFeatureReport(uint8_t report_id, |
| 55 const ReadCallback& callback) override {} |
| 56 |
| 57 void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer, |
| 58 size_t size, |
| 59 const WriteCallback& callback) override {} |
| 60 |
| 61 private: |
| 62 ~MockHidErrorConnection() override {} |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 namespace device { |
| 68 |
| 69 class U2fDeviceEnumerate { |
| 70 public: |
| 71 U2fDeviceEnumerate() |
| 72 : closure_(), |
| 73 callback_(base::Bind(&U2fDeviceEnumerate::ReceivedCallback, |
| 74 base::Unretained(this))), |
| 75 run_loop_() {} |
| 76 ~U2fDeviceEnumerate() {} |
| 77 |
| 78 void ReceivedCallback( |
| 79 const std::vector<scoped_refptr<HidDeviceInfo>>& devices) { |
| 80 std::list<std::unique_ptr<U2fHidDevice>> u2f_devices; |
| 81 filter_.SetUsagePage(0xf1d0); |
| 82 for (auto device_info : devices) { |
| 83 if (filter_.Matches(device_info)) |
| 84 u2f_devices.push_front(base::MakeUnique<U2fHidDevice>(device_info)); |
| 85 } |
| 86 devices_ = std::move(u2f_devices); |
| 87 closure_.Run(); |
| 88 } |
| 89 |
| 90 std::list<std::unique_ptr<U2fHidDevice>>& WaitForCallback() { |
| 91 closure_ = run_loop_.QuitClosure(); |
| 92 run_loop_.Run(); |
| 93 return devices_; |
| 94 } |
| 95 |
| 96 const HidService::GetDevicesCallback& callback() { return callback_; } |
| 97 |
| 98 private: |
| 99 HidDeviceFilter filter_; |
| 100 std::list<std::unique_ptr<U2fHidDevice>> devices_; |
| 101 base::Closure closure_; |
| 102 HidService::GetDevicesCallback callback_; |
| 103 base::RunLoop run_loop_; |
| 104 }; |
| 105 |
| 106 class TestVersionCallback { |
| 107 public: |
| 108 TestVersionCallback() |
| 109 : closure_(), |
| 110 callback_(base::Bind(&TestVersionCallback::ReceivedCallback, |
| 111 base::Unretained(this))), |
| 112 run_loop_() {} |
| 113 ~TestVersionCallback() {} |
| 114 |
| 115 void ReceivedCallback(bool success, U2fDevice::ProtocolVersion version) { |
| 116 version_ = version; |
| 117 closure_.Run(); |
| 118 } |
| 119 |
| 120 U2fDevice::ProtocolVersion WaitForCallback() { |
| 121 closure_ = run_loop_.QuitClosure(); |
| 122 run_loop_.Run(); |
| 123 return version_; |
| 124 } |
| 125 |
| 126 const U2fDevice::VersionCallback& callback() { return callback_; } |
| 127 |
| 128 private: |
| 129 U2fDevice::ProtocolVersion version_; |
| 130 base::Closure closure_; |
| 131 U2fDevice::VersionCallback callback_; |
| 132 base::RunLoop run_loop_; |
| 133 }; |
| 134 |
| 135 class TestDeviceCallback { |
| 136 public: |
| 137 TestDeviceCallback() |
| 138 : closure_(), |
| 139 callback_(base::Bind(&TestDeviceCallback::ReceivedCallback, |
| 140 base::Unretained(this))), |
| 141 run_loop_() {} |
| 142 ~TestDeviceCallback() {} |
| 143 |
| 144 void ReceivedCallback(bool success, scoped_refptr<U2fApduResponse> response) { |
| 145 response_ = response; |
| 146 closure_.Run(); |
| 147 } |
| 148 |
| 149 scoped_refptr<U2fApduResponse> WaitForCallback() { |
| 150 closure_ = run_loop_.QuitClosure(); |
| 151 run_loop_.Run(); |
| 152 return response_; |
| 153 } |
| 154 |
| 155 const U2fDevice::DeviceCallback& callback() { return callback_; } |
| 156 |
| 157 private: |
| 158 scoped_refptr<U2fApduResponse> response_; |
| 159 base::Closure closure_; |
| 160 U2fDevice::DeviceCallback callback_; |
| 161 base::RunLoop run_loop_; |
| 162 }; |
| 163 |
| 164 class U2fHidDeviceTest : public testing::Test { |
| 165 public: |
| 166 void SetUp() override { |
| 167 message_loop_.reset(new base::MessageLoopForUI()); |
| 168 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
| 169 device_client_.reset( |
| 170 new device::TestDeviceClient(io_thread_->task_runner())); |
| 171 } |
| 172 |
| 173 protected: |
| 174 std::unique_ptr<base::MessageLoopForUI> message_loop_; |
| 175 std::unique_ptr<base::TestIOThread> io_thread_; |
| 176 std::unique_ptr<device::TestDeviceClient> device_client_; |
| 177 }; |
| 178 |
| 179 TEST_F(U2fHidDeviceTest, TestHidDeviceVersion) { |
| 180 if (!U2fHidDevice::IsTestEnabled()) |
| 181 return; |
| 182 |
| 183 U2fDeviceEnumerate callback; |
| 184 HidService* hid_service = DeviceClient::Get()->GetHidService(); |
| 185 hid_service->GetDevices(callback.callback()); |
| 186 std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices = |
| 187 callback.WaitForCallback(); |
| 188 |
| 189 for (auto& device : u2f_devices) { |
| 190 TestVersionCallback vc; |
| 191 device->Version(vc.callback()); |
| 192 U2fDevice::ProtocolVersion version = vc.WaitForCallback(); |
| 193 EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2); |
| 194 } |
| 195 }; |
| 196 |
| 197 TEST_F(U2fHidDeviceTest, TestMultipleRequests) { |
| 198 if (!U2fHidDevice::IsTestEnabled()) |
| 199 return; |
| 200 |
| 201 U2fDeviceEnumerate callback; |
| 202 HidService* hid_service = DeviceClient::Get()->GetHidService(); |
| 203 hid_service->GetDevices(callback.callback()); |
| 204 std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices = |
| 205 callback.WaitForCallback(); |
| 206 |
| 207 for (auto& device : u2f_devices) { |
| 208 TestVersionCallback vc; |
| 209 TestVersionCallback vc2; |
| 210 // Call version twice to check message queueing |
| 211 device->Version(vc.callback()); |
| 212 device->Version(vc2.callback()); |
| 213 U2fDevice::ProtocolVersion version = vc.WaitForCallback(); |
| 214 EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2); |
| 215 version = vc2.WaitForCallback(); |
| 216 EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2); |
| 217 } |
| 218 }; |
| 219 |
| 220 TEST_F(U2fHidDeviceTest, TestConnectionFailure) { |
| 221 // Setup and enumerate mock device |
| 222 auto client = base::MakeUnique<MockDeviceClient>(); |
| 223 U2fDeviceEnumerate callback; |
| 224 MockHidService* hid_service = client->hid_service(); |
| 225 HidCollectionInfo c_info; |
| 226 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); |
| 227 scoped_refptr<HidDeviceInfo> device0 = |
| 228 new HidDeviceInfo(kTestDeviceId, 0, 0, "Test Fido Device", "123FIDO", |
| 229 kHIDBusTypeUSB, c_info, 64, 64, 0); |
| 230 hid_service->AddDevice(device0); |
| 231 hid_service->FirstEnumerationComplete(); |
| 232 hid_service->GetDevices(callback.callback()); |
| 233 std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices = |
| 234 callback.WaitForCallback(); |
| 235 |
| 236 ASSERT_EQ(static_cast<size_t>(1), u2f_devices.size()); |
| 237 auto& device = u2f_devices.front(); |
| 238 // Put device in IDLE state |
| 239 TestDeviceCallback cb0; |
| 240 device->state_ = U2fHidDevice::State::IDLE; |
| 241 |
| 242 // Manually delete connection |
| 243 device->connection_ = nullptr; |
| 244 // Add pending transactions manually and ensure they are processed |
| 245 scoped_refptr<U2fApduResponse> response1( |
| 246 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 247 device->pending_transactions_.push_back( |
| 248 {U2fApduCommand::CreateVersion(), |
| 249 base::Bind(&ResponseCallback, &response1)}); |
| 250 scoped_refptr<U2fApduResponse> response2( |
| 251 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 252 device->pending_transactions_.push_back( |
| 253 {U2fApduCommand::CreateVersion(), |
| 254 base::Bind(&ResponseCallback, &response2)}); |
| 255 scoped_refptr<U2fApduResponse> response3( |
| 256 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 257 device->DeviceTransact(U2fApduCommand::CreateVersion(), |
| 258 base::Bind(&ResponseCallback, &response3)); |
| 259 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_); |
| 260 EXPECT_EQ(nullptr, response1); |
| 261 EXPECT_EQ(nullptr, response2); |
| 262 EXPECT_EQ(nullptr, response3); |
| 263 }; |
| 264 |
| 265 TEST_F(U2fHidDeviceTest, TestDeviceError) { |
| 266 // Setup and enumerate mock device |
| 267 auto client = base::MakeUnique<MockDeviceClient>(); |
| 268 U2fDeviceEnumerate callback; |
| 269 MockHidService* hid_service = client->hid_service(); |
| 270 HidCollectionInfo c_info; |
| 271 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); |
| 272 scoped_refptr<HidDeviceInfo> device0 = |
| 273 new HidDeviceInfo(kTestDeviceId, 0, 0, "Test Fido Device", "123FIDO", |
| 274 kHIDBusTypeUSB, c_info, 64, 64, 0); |
| 275 hid_service->AddDevice(device0); |
| 276 hid_service->FirstEnumerationComplete(); |
| 277 hid_service->GetDevices(callback.callback()); |
| 278 std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices = |
| 279 callback.WaitForCallback(); |
| 280 |
| 281 ASSERT_EQ(static_cast<size_t>(1), u2f_devices.size()); |
| 282 auto& device = u2f_devices.front(); |
| 283 // Mock connection where writes always fail |
| 284 scoped_refptr<MockHidErrorConnection> connection( |
| 285 new MockHidErrorConnection(device0)); |
| 286 device->connection_ = connection; |
| 287 device->state_ = U2fHidDevice::State::IDLE; |
| 288 scoped_refptr<U2fApduResponse> response0( |
| 289 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 290 device->DeviceTransact(U2fApduCommand::CreateVersion(), |
| 291 base::Bind(&ResponseCallback, &response0)); |
| 292 EXPECT_EQ(nullptr, response0); |
| 293 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_); |
| 294 |
| 295 // Add pending transactions manually and ensure they are processed |
| 296 scoped_refptr<U2fApduResponse> response1( |
| 297 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 298 device->pending_transactions_.push_back( |
| 299 {U2fApduCommand::CreateVersion(), |
| 300 base::Bind(&ResponseCallback, &response1)}); |
| 301 scoped_refptr<U2fApduResponse> response2( |
| 302 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 303 device->pending_transactions_.push_back( |
| 304 {U2fApduCommand::CreateVersion(), |
| 305 base::Bind(&ResponseCallback, &response2)}); |
| 306 scoped_refptr<U2fApduResponse> response3( |
| 307 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); |
| 308 device->DeviceTransact(U2fApduCommand::CreateVersion(), |
| 309 base::Bind(&ResponseCallback, &response3)); |
| 310 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_); |
| 311 EXPECT_EQ(nullptr, response1); |
| 312 EXPECT_EQ(nullptr, response2); |
| 313 EXPECT_EQ(nullptr, response3); |
| 314 }; |
| 315 |
| 316 } // namespace device |
OLD | NEW |