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

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

Issue 2821263005: Add U2F request state machines (Closed)
Patch Set: Call Start before adding devices Created 3 years, 8 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
« no previous file with comments | « device/u2f/u2f_request.cc ('k') | device/u2f/u2f_return_code.h » ('j') | 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/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 {
36 cb_.Run(U2fReturnCode::SUCCESS, std::vector<uint8_t>());
37 }
38 };
39 } // namespace
40
41 class TestResponseCallback {
42 public:
43 TestResponseCallback()
44 : callback_(base::Bind(&TestResponseCallback::ReceivedCallback,
45 base::Unretained(this))) {}
46 ~TestResponseCallback() {}
47
48 void ReceivedCallback(U2fReturnCode 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 U2fRequestTest() : io_thread_(base::TestIOThread::kAutoStart) {}
68
69 protected:
70 base::MessageLoopForUI message_loop_;
71 base::TestIOThread io_thread_;
72 device::MockDeviceClient device_client_;
73 };
74
75 TEST_F(U2fRequestTest, TestAddRemoveDevice) {
76 MockHidService* hid_service = device_client_.hid_service();
77 HidCollectionInfo c_info;
78 hid_service->FirstEnumerationComplete();
79
80 TestResponseCallback cb;
81 FakeU2fRequest request(cb.callback());
82 request.Enumerate();
83 EXPECT_EQ(static_cast<size_t>(0), request.devices_.size());
84
85 // Add one U2F device
86 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0));
87 scoped_refptr<HidDeviceInfo> u2f_device = make_scoped_refptr(
88 new HidDeviceInfo(kTestDeviceId0, 0, 0, "Test Fido Device", "123FIDO",
89 kHIDBusTypeUSB, c_info, 64, 64, 0));
90 hid_service->AddDevice(u2f_device);
91 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size());
92
93 // Add one non-U2F device. Verify that it is not added to our device list.
94 scoped_refptr<HidDeviceInfo> other_device = make_scoped_refptr(
95 new HidDeviceInfo(kTestDeviceId2, 0, 0, "Other Device", "OtherDevice",
96 kHIDBusTypeUSB, std::vector<uint8_t>()));
97 hid_service->AddDevice(other_device);
98 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size());
99
100 // Remove the non-U2F device and verify that device list was unchanged.
101 hid_service->RemoveDevice(kTestDeviceId2);
102 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size());
103
104 // Remove the U2F device and verify that device list is empty.
105 hid_service->RemoveDevice(kTestDeviceId0);
106 EXPECT_EQ(static_cast<size_t>(0), request.devices_.size());
107 }
108
109 TEST_F(U2fRequestTest, TestIterateDevice) {
110 TestResponseCallback cb;
111 FakeU2fRequest request(cb.callback());
112 HidCollectionInfo c_info;
113 // Add one U2F device and one non-U2f device
114 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0));
115 scoped_refptr<HidDeviceInfo> device0 = make_scoped_refptr(
116 new HidDeviceInfo(kTestDeviceId0, 0, 0, "Test Fido Device", "123FIDO",
117 kHIDBusTypeUSB, c_info, 64, 64, 0));
118 request.devices_.push_back(base::MakeUnique<U2fHidDevice>(device0));
119 scoped_refptr<HidDeviceInfo> device1 = make_scoped_refptr(
120 new HidDeviceInfo(kTestDeviceId1, 0, 0, "Test Fido Device", "123FIDO",
121 kHIDBusTypeUSB, c_info, 64, 64, 0));
122 request.devices_.push_back(base::MakeUnique<U2fHidDevice>(device1));
123
124 // Move first device to current
125 request.IterateDevice();
126 ASSERT_NE(nullptr, request.current_device_);
127 EXPECT_EQ(static_cast<size_t>(1), request.devices_.size());
128
129 // Move second device to current, first to attempted
130 request.IterateDevice();
131 ASSERT_NE(nullptr, request.current_device_);
132 EXPECT_EQ(static_cast<size_t>(1), request.attempted_devices_.size());
133
134 // Move second device from current to attempted, move attempted to devices as
135 // all devices have been attempted
136 request.IterateDevice();
137 ASSERT_EQ(nullptr, request.current_device_);
138 EXPECT_EQ(static_cast<size_t>(2), request.devices_.size());
139 EXPECT_EQ(static_cast<size_t>(0), request.attempted_devices_.size());
140 }
141
142 TEST_F(U2fRequestTest, TestBasicMachine) {
143 MockHidService* hid_service = device_client_.hid_service();
144 hid_service->FirstEnumerationComplete();
145 TestResponseCallback cb;
146 FakeU2fRequest request(cb.callback());
147 request.Start();
148 // Add one U2F device
149 HidCollectionInfo c_info;
150 c_info.usage = HidUsageAndPage(1, static_cast<HidUsageAndPage::Page>(0xf1d0));
151 scoped_refptr<HidDeviceInfo> u2f_device = make_scoped_refptr(
152 new HidDeviceInfo(kTestDeviceId0, 0, 0, "Test Fido Device", "123FIDO",
153 kHIDBusTypeUSB, c_info, 64, 64, 0));
154 hid_service->AddDevice(u2f_device);
155 cb.WaitForCallback();
156 EXPECT_EQ(U2fRequest::State::BUSY, request.state_);
157 }
158
159 } // namespace device
OLDNEW
« no previous file with comments | « device/u2f/u2f_request.cc ('k') | device/u2f/u2f_return_code.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698