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/memory/ptr_util.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/test/test_io_thread.h" | |
10 #include "device/base/mock_device_client.h" | |
11 #include "device/hid/mock_hid_service.h" | |
12 #include "device/test/test_device_client.h" | |
13 #include "device/test/usb_test_gadget.h" | |
14 #include "device/u2f/u2f_hid_device.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "u2f_request.h" | |
17 | |
18 namespace device { | |
19 namespace { | |
20 #if defined(OS_MACOSX) | |
21 const uint64_t kTestDeviceId0 = 42; | |
22 const uint64_t kTestDeviceId1 = 43; | |
23 const uint64_t kTestDeviceId2 = 44; | |
24 #else | |
25 const char* kTestDeviceId0 = "device0"; | |
26 const char* kTestDeviceId1 = "device1"; | |
27 const char* kTestDeviceId2 = "device2"; | |
28 #endif | |
29 | |
30 class FakeU2fRequest : public U2fRequest { | |
31 public: | |
32 FakeU2fRequest(const ResponseCallback& cb) : U2fRequest(cb) {} | |
33 ~FakeU2fRequest() override {} | |
34 | |
35 void TryDevice() override { cb_.Run(0, std::vector<uint8_t>()); } | |
36 }; | |
37 } // namespace | |
38 | |
39 class TestResponseCallback { | |
40 public: | |
41 TestResponseCallback() | |
42 : closure_(), | |
43 callback_(base::Bind(&TestResponseCallback::ReceivedCallback, | |
44 base::Unretained(this))), | |
45 run_loop_() {} | |
Reilly Grant (use Gerrit)
2017/04/21 20:10:01
closure_ and run_loop_ don't need to be explicitly
Casey Piper
2017/04/21 22:55:16
Done.
| |
46 ~TestResponseCallback() {} | |
47 | |
48 void ReceivedCallback(uint8_t status, std::vector<uint8_t> data) { | |
49 closure_.Run(); | |
50 } | |
51 | |
52 void WaitForCallback() { | |
53 closure_ = run_loop_.QuitClosure(); | |
54 run_loop_.Run(); | |
55 } | |
56 | |
57 U2fRequest::ResponseCallback& callback() { return callback_; } | |
58 | |
59 private: | |
60 base::Closure closure_; | |
61 U2fRequest::ResponseCallback callback_; | |
62 base::RunLoop run_loop_; | |
63 }; | |
64 | |
65 class U2fRequestTest : public testing::Test { | |
66 public: | |
67 void SetUp() override { | |
68 message_loop_.reset(new base::MessageLoopForUI()); | |
69 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | |
70 device_client_.reset(new MockDeviceClient()); | |
71 } | |
72 | |
73 protected: | |
74 std::unique_ptr<base::MessageLoopForUI> message_loop_; | |
75 std::unique_ptr<base::TestIOThread> io_thread_; | |
76 std::unique_ptr<device::MockDeviceClient> device_client_; | |
77 }; | |
Reilly Grant (use Gerrit)
2017/04/21 20:10:02
Since there are no weird setup/teardown ordering i
Casey Piper
2017/04/21 22:55:16
Done.
| |
78 | |
79 TEST_F(U2fRequestTest, TestAddRemoveDevice) { | |
80 MockHidService* hid_service = device_client_->hid_service(); | |
81 HidCollectionInfo c_info; | |
82 hid_service->FirstEnumerationComplete(); | |
83 | |
84 TestResponseCallback cb; | |
85 FakeU2fRequest request(cb.callback()); | |
86 request.U2fRequest::Enumerate(); | |
87 EXPECT_EQ(static_cast<size_t>(0), request.devices_.size()); | |
88 | |
89 // Add one U2F device | |
90 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); | |
91 scoped_refptr<HidDeviceInfo> u2f_device = make_scoped_refptr( | |
92 new HidDeviceInfo(kTestDeviceId0, 0, 0, "Test Fido Device", "123FIDO", | |
93 kHIDBusTypeUSB, c_info, 64, 64, 0)); | |
94 hid_service->AddDevice(u2f_device); | |
95 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size()); | |
96 | |
97 // Add one non-U2F device. Verify that it is not added to our device list. | |
98 scoped_refptr<HidDeviceInfo> other_device = make_scoped_refptr( | |
99 new HidDeviceInfo(kTestDeviceId2, 0, 0, "Other Device", "OtherDevice", | |
100 kHIDBusTypeUSB, std::vector<uint8_t>())); | |
101 hid_service->AddDevice(other_device); | |
102 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size()); | |
103 | |
104 // Remove the non-U2F device and verify that device list was unchanged. | |
105 hid_service->RemoveDevice(kTestDeviceId2); | |
106 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size()); | |
107 | |
108 // Remove the U2F device and verify that device list is empty. | |
109 hid_service->RemoveDevice(kTestDeviceId0); | |
110 EXPECT_EQ(static_cast<size_t>(0), request.devices_.size()); | |
111 } | |
112 | |
113 TEST_F(U2fRequestTest, TestIterateDevice) { | |
114 TestResponseCallback cb; | |
115 FakeU2fRequest request(cb.callback()); | |
116 HidCollectionInfo c_info; | |
117 // Add one U2F device and one non-U2f device | |
118 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); | |
119 scoped_refptr<HidDeviceInfo> device0 = make_scoped_refptr( | |
120 new HidDeviceInfo(kTestDeviceId0, 0, 0, "Test Fido Device", "123FIDO", | |
121 kHIDBusTypeUSB, c_info, 64, 64, 0)); | |
122 request.devices_.push_back(base::MakeUnique<U2fHidDevice>(device0)); | |
123 scoped_refptr<HidDeviceInfo> device1 = make_scoped_refptr( | |
124 new HidDeviceInfo(kTestDeviceId1, 0, 0, "Test Fido Device", "123FIDO", | |
125 kHIDBusTypeUSB, c_info, 64, 64, 0)); | |
126 request.devices_.push_back(base::MakeUnique<U2fHidDevice>(device1)); | |
127 | |
128 // Move first device to current | |
129 request.U2fRequest::IterateDevice(); | |
Reilly Grant (use Gerrit)
2017/04/21 20:10:02
You don't need list the class explicitly here and
Casey Piper
2017/04/21 22:55:16
Done.
| |
130 ASSERT_NE(nullptr, request.current_device_); | |
131 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size()); | |
132 | |
133 // Move second device to current, first to attempted | |
134 request.U2fRequest::IterateDevice(); | |
135 ASSERT_NE(nullptr, request.current_device_); | |
136 EXPECT_EQ(static_cast<size_t>(1), request.attempted_devices_.size()); | |
137 | |
138 // Move second device from current to attempted, move attempted to devices as | |
139 // all devices have been attempted | |
140 request.U2fRequest::IterateDevice(); | |
141 ASSERT_EQ(nullptr, request.current_device_); | |
142 EXPECT_EQ(static_cast<size_t>(2), request.devices_.size()); | |
143 EXPECT_EQ(static_cast<size_t>(0), request.attempted_devices_.size()); | |
144 } | |
145 | |
146 TEST_F(U2fRequestTest, TestBasicMachine) { | |
147 MockHidService* hid_service = device_client_->hid_service(); | |
148 hid_service->FirstEnumerationComplete(); | |
149 TestResponseCallback cb; | |
150 FakeU2fRequest request(cb.callback()); | |
151 request.U2fRequest::Start(); | |
152 // Add one U2F device | |
153 HidCollectionInfo c_info; | |
154 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0)); | |
155 scoped_refptr<HidDeviceInfo> u2f_device = make_scoped_refptr( | |
156 new HidDeviceInfo(kTestDeviceId0, 0, 0, "Test Fido Device", "123FIDO", | |
157 kHIDBusTypeUSB, c_info, 64, 64, 0)); | |
158 hid_service->AddDevice(u2f_device); | |
159 cb.WaitForCallback(); | |
160 EXPECT_EQ(U2fRequest::State::BUSY, request.state_); | |
161 } | |
162 | |
163 } // namespace device | |
OLD | NEW |