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

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

Powered by Google App Engine
This is Rietveld 408576698