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_register.h" | |
15 | |
16 namespace device { | |
17 | |
18 class U2fRegisterTest : public testing::Test { | |
19 public: | |
20 U2fRegisterTest() : io_thread_(base::TestIOThread::kAutoStart) {} | |
21 | |
22 void SetUp() override { | |
23 MockHidService* hid_service = device_client_.hid_service(); | |
24 hid_service->FirstEnumerationComplete(); | |
25 } | |
26 | |
27 protected: | |
28 base::MessageLoopForUI message_loop_; | |
29 base::TestIOThread io_thread_; | |
30 device::MockDeviceClient device_client_; | |
31 }; | |
32 | |
33 class TestRegisterCallback { | |
34 public: | |
35 TestRegisterCallback() | |
36 : callback_(base::Bind(&TestRegisterCallback::ReceivedCallback, | |
37 base::Unretained(this))) {} | |
38 ~TestRegisterCallback() {} | |
39 | |
40 void ReceivedCallback(U2fReturnCode status_code, | |
41 std::vector<uint8_t> response) { | |
42 response_ = std::make_pair(status_code, response); | |
43 closure_.Run(); | |
44 } | |
45 | |
46 std::pair<U2fReturnCode, std::vector<uint8_t>>& WaitForCallback() { | |
47 closure_ = run_loop_.QuitClosure(); | |
48 run_loop_.Run(); | |
49 return response_; | |
50 } | |
51 | |
52 const U2fRequest::ResponseCallback& callback() { return callback_; } | |
53 | |
54 private: | |
55 std::pair<U2fReturnCode, std::vector<uint8_t>> response_; | |
56 base::Closure closure_; | |
57 U2fRequest::ResponseCallback callback_; | |
58 base::RunLoop run_loop_; | |
59 }; | |
60 | |
61 TEST_F(U2fRegisterTest, TestRegisterSuccess) { | |
62 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice()); | |
63 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_)) | |
64 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorRegister)); | |
65 EXPECT_CALL(*device.get(), TryWink(testing::_)) | |
66 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
67 TestRegisterCallback cb; | |
68 std::unique_ptr<U2fRequest> request = U2fRegister::TryRegistration( | |
69 std::vector<uint8_t>(32), std::vector<uint8_t>(32), cb.callback()); | |
70 request->AddDeviceForTesting(std::move(device)); | |
71 request->Start(); | |
Reilly Grant (use Gerrit)
2017/04/22 00:16:43
To more closely mimic the normal state machine tra
Casey Piper
2017/04/22 00:50:08
Done.
| |
72 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
73 cb.WaitForCallback(); | |
74 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
75 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
76 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]); | |
77 } | |
78 | |
79 TEST_F(U2fRegisterTest, TestDelayedSuccess) { | |
80 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice()); | |
81 | |
82 // Go through the state machine twice before success | |
83 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_)) | |
84 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied)) | |
85 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorRegister)); | |
86 EXPECT_CALL(*device.get(), TryWink(testing::_)) | |
87 .Times(2) | |
88 .WillRepeatedly(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
89 TestRegisterCallback cb; | |
90 | |
91 std::unique_ptr<U2fRequest> request = U2fRegister::TryRegistration( | |
92 std::vector<uint8_t>(32), std::vector<uint8_t>(32), cb.callback()); | |
93 request->AddDeviceForTesting(std::move(device)); | |
94 request->Start(); | |
95 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
96 cb.WaitForCallback(); | |
97 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
98 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
99 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]); | |
100 } | |
101 | |
102 TEST_F(U2fRegisterTest, TestMultipleDevices) { | |
103 // Second device will have a successful touch | |
104 std::unique_ptr<MockU2fDevice> device0(new MockU2fDevice()); | |
105 std::unique_ptr<MockU2fDevice> device1(new MockU2fDevice()); | |
106 | |
107 EXPECT_CALL(*device0.get(), DeviceTransactPtr(testing::_, testing::_)) | |
108 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied)); | |
109 // One wink per device | |
110 EXPECT_CALL(*device0.get(), TryWink(testing::_)) | |
111 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
112 EXPECT_CALL(*device1.get(), DeviceTransactPtr(testing::_, testing::_)) | |
113 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorRegister)); | |
114 EXPECT_CALL(*device1.get(), TryWink(testing::_)) | |
115 .WillOnce(testing::Invoke(MockU2fDevice::WinkDoNothing)); | |
116 | |
117 TestRegisterCallback cb; | |
118 std::unique_ptr<U2fRequest> request = U2fRegister::TryRegistration( | |
119 std::vector<uint8_t>(32), std::vector<uint8_t>(32), cb.callback()); | |
120 request->AddDeviceForTesting(std::move(device0)); | |
121 request->AddDeviceForTesting(std::move(device1)); | |
122 request->Start(); | |
123 std::pair<U2fReturnCode, std::vector<uint8_t>>& response = | |
124 cb.WaitForCallback(); | |
125 EXPECT_EQ(U2fReturnCode::SUCCESS, response.first); | |
126 ASSERT_LT(static_cast<size_t>(0), response.second.size()); | |
127 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]); | |
128 } | |
129 | |
130 } // namespace device | |
OLD | NEW |