Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(578)

Side by Side Diff: device/u2f/u2f_hid_device_unittest.cc

Issue 2721223002: Add support for U2fHidDevice interaction (Closed)
Patch Set: Add comment and change logic for executing pending transactions Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« device/u2f/u2f_hid_device.cc ('K') | « device/u2f/u2f_hid_device.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
35 devices_ = std::move(u2f_devices);
36 closure_.Run();
37 }
38
39 std::list<std::unique_ptr<U2fDevice>>& WaitForCallback() {
40 closure_ = run_loop_.QuitClosure();
41 run_loop_.Run();
42 return devices_;
43 }
44
45 const HidService::GetDevicesCallback& callback() { return callback_; }
46
47 private:
48 HidDeviceFilter filter_;
49 std::list<std::unique_ptr<U2fDevice>> devices_;
50 base::Closure closure_;
51 HidService::GetDevicesCallback callback_;
52 base::RunLoop run_loop_;
53 };
54
55 class TestVersionCallback {
56 public:
57 TestVersionCallback()
58 : closure_(),
59 callback_(base::Bind(&TestVersionCallback::ReceivedCallback,
60 base::Unretained(this))),
61 run_loop_() {}
62 ~TestVersionCallback() {}
63
64 void ReceivedCallback(bool success, U2fDevice::ProtocolVersion version) {
65 version_ = version;
66 closure_.Run();
67 }
68
69 U2fDevice::ProtocolVersion WaitForCallback() {
70 closure_ = run_loop_.QuitClosure();
71 run_loop_.Run();
72 return version_;
73 }
74
75 const U2fDevice::VersionCallback& callback() { return callback_; }
76
77 private:
78 U2fDevice::ProtocolVersion version_;
79 base::Closure closure_;
80 U2fDevice::VersionCallback callback_;
81 base::RunLoop run_loop_;
82 };
83
84 class U2fHidDeviceTest : public testing::Test {
85 public:
86 void SetUp() override {
87 if (!U2fHidDevice::IsTestEnabled())
88 return;
89 message_loop_.reset(new base::MessageLoopForUI());
90 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
91 device_client_.reset(
92 new device::TestDeviceClient(io_thread_->task_runner()));
93 }
94
95 protected:
96 std::unique_ptr<base::MessageLoopForUI> message_loop_;
97 std::unique_ptr<base::TestIOThread> io_thread_;
98 std::unique_ptr<device::TestDeviceClient> device_client_;
99 };
100
101 TEST_F(U2fHidDeviceTest, TestHidDeviceVersion) {
102 if (!U2fHidDevice::IsTestEnabled())
103 return;
104
105 U2fDeviceEnumerate callback;
106 HidService* hid_service = DeviceClient::Get()->GetHidService();
107 hid_service->GetDevices(callback.callback());
108 std::list<std::unique_ptr<U2fDevice>>& u2f_devices =
109 callback.WaitForCallback();
110
111 for (auto& device : u2f_devices) {
112 TestVersionCallback vc;
113 device->Version(vc.callback());
114 U2fDevice::ProtocolVersion version = vc.WaitForCallback();
115 EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2);
116 }
117 };
118
119 TEST_F(U2fHidDeviceTest, TestMultipleRequests) {
120 if (!U2fHidDevice::IsTestEnabled())
121 return;
122
123 U2fDeviceEnumerate callback;
124 HidService* hid_service = DeviceClient::Get()->GetHidService();
125 hid_service->GetDevices(callback.callback());
126 std::list<std::unique_ptr<U2fDevice>>& u2f_devices =
127 callback.WaitForCallback();
128
129 for (auto& device : u2f_devices) {
130 TestVersionCallback vc;
131 TestVersionCallback vc2;
132 // Call version twice to check message queueing
133 device->Version(vc.callback());
134 device->Version(vc2.callback());
135 U2fDevice::ProtocolVersion version = vc.WaitForCallback();
136 EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2);
137 }
138 };
139
140 } // namespace device
OLDNEW
« device/u2f/u2f_hid_device.cc ('K') | « device/u2f/u2f_hid_device.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698