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/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 U2fSignTest() : io_thread_(base::TestIOThread::kAutoStart) {} | |
20 | |
21 void SetUp() override { | |
22 MockHidService* hid_service = device_client_.hid_service(); | |
23 hid_service->FirstEnumerationComplete(); | |
24 } | |
25 | |
26 protected: | |
27 base::MessageLoopForUI message_loop_; | |
28 base::TestIOThread io_thread_; | |
29 device::MockDeviceClient device_client_; | |
30 }; | |
31 | |
32 class TestSignCallback { | |
33 public: | |
34 TestSignCallback() | |
35 : callback_(base::Bind(&TestSignCallback::ReceivedCallback, | |
36 base::Unretained(this))) {} | |
37 ~TestSignCallback() {} | |
38 | |
39 void ReceivedCallback(U2fReturnCode status_code, | |
40 std::vector<uint8_t> response) { | |
41 response_ = std::make_pair(status_code, response); | |
42 closure_.Run(); | |
43 } | |
44 | |
45 std::pair<U2fReturnCode, std::vector<uint8_t>>& WaitForCallback() { | |
46 closure_ = run_loop_.QuitClosure(); | |
47 run_loop_.Run(); | |
48 return response_; | |
49 } | |
50 | |
51 const U2fRequest::ResponseCallback& callback() { return callback_; } | |
52 | |
53 private: | |
54 std::pair<U2fReturnCode, std::vector<uint8_t>> response_; | |
55 base::Closure closure_; | |
56 U2fRequest::ResponseCallback callback_; | |
57 base::RunLoop run_loop_; | |
58 }; | |
59 | |
60 TEST_F(U2fSignTest, TestSignSuccess) { | |
61 std::vector<uint8_t> key(32, 0xA); | |
62 std::vector<std::vector<uint8_t>> handles = {key}; | |
63 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice()); | |
64 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_)) | |
65 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign)); | |
66 EXPECT_CALL(*device.get(), TryWink(testing::_)) | |
67 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
68 TestSignCallback cb; | |
69 std::unique_ptr<U2fRequest> request = | |
70 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
71 std::vector<uint8_t>(32), cb.callback()); | |
72 request->Start(); | |
73 request->AddDeviceForTesting(std::move(device)); | |
74 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
75 cb.WaitForCallback(); | |
76 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
77 // Correct key was sent so a sign response is expected | |
78 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
79 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]); | |
80 } | |
81 | |
82 TEST_F(U2fSignTest, TestDelayedSuccess) { | |
83 std::vector<uint8_t> key(32, 0xA); | |
84 std::vector<std::vector<uint8_t>> handles = {key}; | |
85 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice()); | |
86 | |
87 // Go through the state machine twice before success | |
88 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_)) | |
89 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied)) | |
90 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign)); | |
91 EXPECT_CALL(*device.get(), TryWink(testing::_)) | |
92 .Times(2) | |
93 .WillRepeatedly(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
94 TestSignCallback cb; | |
95 | |
96 std::unique_ptr<U2fRequest> request = | |
97 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
98 std::vector<uint8_t>(32), cb.callback()); | |
99 request->Start(); | |
100 request->AddDeviceForTesting(std::move(device)); | |
101 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
102 cb.WaitForCallback(); | |
103 EXPECT_EQ(U2fReturnCode::SUCCESS, 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 std::unique_ptr<U2fRequest> request = | |
131 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
132 std::vector<uint8_t>(32), cb.callback()); | |
133 request->Start(); | |
134 request->AddDeviceForTesting(std::move(device)); | |
135 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
136 cb.WaitForCallback(); | |
137 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
138 // Correct key was sent so a sign response is expected | |
139 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
140 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]); | |
141 } | |
142 | |
143 TEST_F(U2fSignTest, TestMultipleDevices) { | |
144 std::vector<uint8_t> key0(32, 0xA); | |
145 std::vector<uint8_t> key1(32, 0xB); | |
146 // Second device will have a successful touch | |
147 std::vector<std::vector<uint8_t>> handles = {key0, key1}; | |
148 std::unique_ptr<MockU2fDevice> device0(new MockU2fDevice()); | |
149 std::unique_ptr<MockU2fDevice> device1(new MockU2fDevice()); | |
150 | |
151 EXPECT_CALL(*device0.get(), DeviceTransactPtr(testing::_, testing::_)) | |
152 .WillOnce(testing::Invoke(MockU2fDevice::WrongData)) | |
153 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied)); | |
154 // One wink per device | |
155 EXPECT_CALL(*device0.get(), TryWink(testing::_)) | |
156 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
157 EXPECT_CALL(*device1.get(), DeviceTransactPtr(testing::_, testing::_)) | |
158 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorSign)); | |
159 EXPECT_CALL(*device1.get(), TryWink(testing::_)) | |
160 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
161 | |
162 TestSignCallback cb; | |
163 std::unique_ptr<U2fRequest> request = | |
164 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
165 std::vector<uint8_t>(32), cb.callback()); | |
166 request->Start(); | |
167 request->AddDeviceForTesting(std::move(device0)); | |
168 request->AddDeviceForTesting(std::move(device1)); | |
169 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
170 cb.WaitForCallback(); | |
171 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
172 // Correct key was sent so a sign response is expected | |
173 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
174 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kSign), response.second[0]); | |
175 } | |
176 | |
177 TEST_F(U2fSignTest, TestFakeEnroll) { | |
178 std::vector<uint8_t> key0(32, 0xA); | |
179 std::vector<uint8_t> key1(32, 0xB); | |
180 // Second device will be have a successful touch | |
181 std::vector<std::vector<uint8_t>> handles = {key0, key1}; | |
182 std::unique_ptr<MockU2fDevice> device0(new MockU2fDevice()); | |
183 std::unique_ptr<MockU2fDevice> device1(new MockU2fDevice()); | |
184 | |
185 EXPECT_CALL(*device0.get(), DeviceTransactPtr(testing::_, testing::_)) | |
186 .WillOnce(testing::Invoke(MockU2fDevice::WrongData)) | |
187 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied)); | |
188 // One wink per device | |
189 EXPECT_CALL(*device0.get(), TryWink(testing::_)) | |
190 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
191 // Both keys will be tried, when both fail, register is tried on that device | |
192 EXPECT_CALL(*device1.get(), DeviceTransactPtr(testing::_, testing::_)) | |
193 .WillOnce(testing::Invoke(MockU2fDevice::WrongData)) | |
194 .WillOnce(testing::Invoke(MockU2fDevice::WrongData)) | |
195 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorRegister)); | |
196 EXPECT_CALL(*device1.get(), TryWink(testing::_)) | |
197 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
198 | |
199 TestSignCallback cb; | |
200 std::unique_ptr<U2fRequest> request = | |
201 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
202 std::vector<uint8_t>(32), cb.callback()); | |
203 request->Start(); | |
204 request->AddDeviceForTesting(std::move(device0)); | |
205 request->AddDeviceForTesting(std::move(device1)); | |
206 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
207 cb.WaitForCallback(); | |
208 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
209 // Device that responded had no correct keys, so a registration response is | |
210 // expected | |
211 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
212 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]); | |
213 } | |
214 | |
215 } // namespace device | |
OLD | NEW |