Chromium Code Reviews| 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 "device/hid/hid_device_filter.h" | |
| 12 #include "device/test/test_device_client.h" | |
| 13 #include "device/u2f/u2f_hid_device.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace device { | |
| 17 | |
| 18 class U2fDeviceEnumerate { | |
| 19 public: | |
| 20 U2fDeviceEnumerate() | |
| 21 : closure_(), | |
| 22 callback_(base::Bind(&U2fDeviceEnumerate::ReceivedCallback, | |
| 23 base::Unretained(this))), | |
| 24 run_loop_() {} | |
| 25 ~U2fDeviceEnumerate() {} | |
| 26 | |
| 27 void ReceivedCallback( | |
| 28 const std::vector<scoped_refptr<HidDeviceInfo>>& devices) { | |
| 29 std::list<std::unique_ptr<U2fDevice>> u2f_devices; | |
| 30 filter_.SetUsagePage(0xf1d0); | |
| 31 for (auto device_info : devices) { | |
| 32 if (filter_.Matches(device_info)) { | |
| 33 u2f_devices.push_front(base::MakeUnique<U2fHidDevice>(device_info)); | |
| 34 } | |
|
Reilly Grant (use Gerrit)
2017/03/01 22:48:33
No braces around single-line if.
Casey Piper
2017/03/04 02:06:28
Done.
| |
| 35 } | |
| 36 devices_ = std::move(u2f_devices); | |
| 37 closure_.Run(); | |
| 38 } | |
| 39 | |
| 40 std::list<std::unique_ptr<U2fDevice>>& WaitForCallback() { | |
| 41 closure_ = run_loop_.QuitClosure(); | |
| 42 run_loop_.Run(); | |
| 43 return devices_; | |
| 44 } | |
| 45 | |
| 46 const HidService::GetDevicesCallback& callback() { return callback_; } | |
| 47 | |
| 48 private: | |
| 49 HidDeviceFilter filter_; | |
| 50 std::list<std::unique_ptr<U2fDevice>> devices_; | |
| 51 base::Closure closure_; | |
| 52 HidService::GetDevicesCallback callback_; | |
| 53 base::RunLoop run_loop_; | |
| 54 }; | |
| 55 | |
| 56 class TestVersionCallback { | |
| 57 public: | |
| 58 TestVersionCallback() | |
| 59 : closure_(), | |
| 60 callback_(base::Bind(&TestVersionCallback::ReceivedCallback, | |
| 61 base::Unretained(this))), | |
| 62 run_loop_() {} | |
| 63 ~TestVersionCallback() {} | |
| 64 | |
| 65 void ReceivedCallback(bool success, U2fDevice::ProtocolVersion version) { | |
| 66 version_ = version; | |
| 67 closure_.Run(); | |
| 68 } | |
| 69 | |
| 70 U2fDevice::ProtocolVersion WaitForCallback() { | |
| 71 closure_ = run_loop_.QuitClosure(); | |
| 72 run_loop_.Run(); | |
| 73 return version_; | |
| 74 } | |
| 75 | |
| 76 const U2fDevice::VersionCallback& callback() { return callback_; } | |
| 77 | |
| 78 private: | |
| 79 U2fDevice::ProtocolVersion version_; | |
| 80 base::Closure closure_; | |
| 81 U2fDevice::VersionCallback callback_; | |
| 82 base::RunLoop run_loop_; | |
| 83 }; | |
| 84 | |
| 85 class U2fHidDeviceTest : public testing::Test { | |
| 86 public: | |
| 87 void SetUp() override { | |
| 88 if (!U2fHidDevice::IsTestEnabled()) | |
| 89 return; | |
| 90 message_loop_.reset(new base::MessageLoopForUI()); | |
| 91 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | |
| 92 device_client_.reset( | |
| 93 new device::TestDeviceClient(io_thread_->task_runner())); | |
| 94 } | |
| 95 | |
| 96 protected: | |
| 97 std::unique_ptr<base::MessageLoopForUI> message_loop_; | |
| 98 std::unique_ptr<base::TestIOThread> io_thread_; | |
| 99 std::unique_ptr<device::TestDeviceClient> device_client_; | |
| 100 }; | |
| 101 | |
| 102 TEST_F(U2fHidDeviceTest, TestEnumerateHidDevice) { | |
| 103 if (!U2fHidDevice::IsTestEnabled()) | |
| 104 return; | |
| 105 | |
| 106 U2fDeviceEnumerate callback; | |
| 107 HidService* hid_service = DeviceClient::Get()->GetHidService(); | |
| 108 hid_service->GetDevices(callback.callback()); | |
| 109 std::list<std::unique_ptr<U2fDevice>>& u2f_devices = | |
| 110 callback.WaitForCallback(); | |
| 111 | |
| 112 for (auto it = u2f_devices.cbegin(); it != u2f_devices.end(); ++it) { | |
|
Reilly Grant (use Gerrit)
2017/03/01 22:48:33
for (auto& device : u2f_devices)
Casey Piper
2017/03/04 02:06:28
Done.
| |
| 113 TestVersionCallback vc; | |
| 114 (*it)->Version(vc.callback()); | |
| 115 U2fDevice::ProtocolVersion version = vc.WaitForCallback(); | |
| 116 ASSERT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2); | |
|
Reilly Grant (use Gerrit)
2017/03/01 22:48:33
EXPECT_EQ since this if this fails the test won't
| |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 } // namespace device | |
| OLD | NEW |