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

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

Issue 2821263005: Add U2F request state machines (Closed)
Patch Set: Compilation changes 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
« device/u2f/u2f_sign.cc ('K') | « device/u2f/u2f_sign.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/run_loop.h"
8 #include "base/test/test_io_thread.h"
9 #include "device/base/mock_device_client.h"
10 #include "device/hid/mock_hid_service.h"
11 #include "device/test/test_device_client.h"
12 #include "mock_u2f_device.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "u2f_sign.h"
15
16 namespace device {
17 class U2fSignTest : public testing::Test {
18 public:
19 void SetUp() override {
20 message_loop_.reset(new base::MessageLoopForUI());
21 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
22 device_client_.reset(new MockDeviceClient());
23 MockHidService* hid_service = device_client_->hid_service();
24 hid_service->FirstEnumerationComplete();
25 }
26
27 protected:
28 std::unique_ptr<base::MessageLoopForUI> message_loop_;
29 std::unique_ptr<base::TestIOThread> io_thread_;
30 std::unique_ptr<device::MockDeviceClient> device_client_;
31 };
32
33 class TestSignCallback {
34 public:
35 TestSignCallback()
36 : closure_(),
37 callback_(base::Bind(&TestSignCallback::ReceivedCallback,
38 base::Unretained(this))),
39 run_loop_() {}
40 ~TestSignCallback() {}
41
42 void ReceivedCallback(uint8_t status_code, std::vector<uint8_t> response) {
43 response_ = std::make_pair(status_code, response);
44 closure_.Run();
45 }
46
47 std::pair<uint8_t, std::vector<uint8_t>>& WaitForCallback() {
48 closure_ = run_loop_.QuitClosure();
49 run_loop_.Run();
50 return response_;
51 }
52
53 const U2fRequest::ResponseCallback& callback() { return callback_; }
54
55 private:
56 std::pair<uint8_t, std::vector<uint8_t>> response_;
57 base::Closure closure_;
58 U2fRequest::ResponseCallback callback_;
59 base::RunLoop run_loop_;
60 };
61
62 TEST_F(U2fSignTest, TestSignSuccess) {
63 std::vector<uint8_t> key(32, 0xA);
64 std::vector<std::vector<uint8_t>> handles = {key};
65 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice());
66 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_))
67 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign));
68 EXPECT_CALL(*device.get(), TryWink(testing::_))
69 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing));
70 TestSignCallback cb;
71 U2fSign request(handles, std::vector<uint8_t>(32), std::vector<uint8_t>(32),
72 cb.callback());
73 request.U2fRequest::devices_.push_back(std::move(device));
74 request.Start();
75 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
76 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
77 response.first);
78 // Correct key was sent so a sign response is expected
79 ASSERT_LT(static_cast<size_t>(0), response.second.size());
80 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]);
81 }
82
83 TEST_F(U2fSignTest, TestDelayedSuccess) {
84 std::vector<uint8_t> key(32, 0xA);
85 std::vector<std::vector<uint8_t>> handles = {key};
86 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice());
87
88 // Go through the state machine twice before success
89 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_))
90 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied))
91 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign));
92 EXPECT_CALL(*device.get(), TryWink(testing::_))
93 .Times(2)
94 .WillRepeatedly(testing::Invoke(MockU2fDevice::WinkDoNothing));
95 TestSignCallback cb;
96
97 U2fSign request(handles, std::vector<uint8_t>(32), std::vector<uint8_t>(32),
98 cb.callback());
99 request.U2fRequest::devices_.push_back(std::move(device));
100 request.Start();
101 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
102 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
103 response.first);
104 // Correct key was sent so a sign response is expected
105 ASSERT_LT(static_cast<size_t>(0), response.second.size());
106 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]);
107 }
108
109 TEST_F(U2fSignTest, TestMultipleHandles) {
110 std::vector<uint8_t> key(32, 0xA);
111 std::vector<uint8_t> wrong_key0(32, 0xB);
112 std::vector<uint8_t> wrong_key1(32, 0xC);
113 std::vector<uint8_t> wrong_key2(32, 0xD);
114 // Put wrong key first to ensure that it will be tested before the correct key
115 std::vector<std::vector<uint8_t>> handles = {wrong_key0, wrong_key1,
116 wrong_key2, key};
117 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice());
118
119 // Wrong key would respond with SW_WRONG_DATA
120 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_))
121 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
122 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
123 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
124 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign));
125 // Only one wink expected per device
126 EXPECT_CALL(*device.get(), TryWink(testing::_))
127 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing));
128
129 TestSignCallback cb;
130 U2fSign request(handles, std::vector<uint8_t>(32), std::vector<uint8_t>(32),
131 cb.callback());
132 request.U2fRequest::devices_.push_back(std::move(device));
133 request.Start();
134 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
135 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
136 response.first);
137 // Correct key was sent so a sign response is expected
138 ASSERT_LT(static_cast<size_t>(0), response.second.size());
139 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]);
140 }
141
142 TEST_F(U2fSignTest, TestMultipleDevices) {
143 std::vector<uint8_t> key0(32, 0xA);
144 std::vector<uint8_t> key1(32, 0xB);
145 // Second device will have a successful touch
146 std::vector<std::vector<uint8_t>> handles = {key0, key1};
147 std::unique_ptr<MockU2fDevice> device0(new MockU2fDevice());
148 std::unique_ptr<MockU2fDevice> device1(new MockU2fDevice());
149
150 EXPECT_CALL(*device0.get(), DeviceTransactPtr(testing::_, testing::_))
151 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
152 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied));
153 // One wink per device
154 EXPECT_CALL(*device0.get(), TryWink(testing::_))
155 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing));
156 EXPECT_CALL(*device1.get(), DeviceTransactPtr(testing::_, testing::_))
157 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign));
158 EXPECT_CALL(*device1.get(), TryWink(testing::_))
159 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing));
160
161 TestSignCallback cb;
162 U2fSign request(handles, std::vector<uint8_t>(32), std::vector<uint8_t>(32),
163 cb.callback());
164 request.U2fRequest::devices_.push_back(std::move(device0));
165 request.U2fRequest::devices_.push_back(std::move(device1));
166 request.Start();
167 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
168 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
169 response.first);
170 // Correct key was sent so a sign response is expected
171 ASSERT_LT(static_cast<size_t>(0), response.second.size());
172 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]);
173 }
174
175 TEST_F(U2fSignTest, TestFakeEnroll) {
176 std::vector<uint8_t> key0(32, 0xA);
177 std::vector<uint8_t> key1(32, 0xB);
178 // Second device will be have a successful touch
179 std::vector<std::vector<uint8_t>> handles = {key0, key1};
180 std::unique_ptr<MockU2fDevice> device0(new MockU2fDevice());
181 std::unique_ptr<MockU2fDevice> device1(new MockU2fDevice());
182
183 EXPECT_CALL(*device0.get(), DeviceTransactPtr(testing::_, testing::_))
184 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
185 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied));
186 // One wink per device
187 EXPECT_CALL(*device0.get(), TryWink(testing::_))
188 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing));
189 // Both keys will be tried, when both fail, register is tried on that device
190 EXPECT_CALL(*device1.get(), DeviceTransactPtr(testing::_, testing::_))
191 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
192 .WillOnce(testing::Invoke(MockU2fDevice::WrongData))
193 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorRegister));
194 EXPECT_CALL(*device1.get(), TryWink(testing::_))
195 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing));
196
197 TestSignCallback cb;
198 U2fSign request(handles, std::vector<uint8_t>(32), std::vector<uint8_t>(32),
199 cb.callback());
200 request.U2fRequest::devices_.push_back(std::move(device0));
201 request.U2fRequest::devices_.push_back(std::move(device1));
202 request.Start();
203 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
204 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
205 response.first);
206 // Device that responded had no correct keys, so a registration response is
207 // expected
208 ASSERT_LT(static_cast<size_t>(0), response.second.size());
209 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]);
210 }
211
212 } // namespace device
OLDNEW
« device/u2f/u2f_sign.cc ('K') | « device/u2f/u2f_sign.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698