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

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

Issue 2821263005: Add U2F request state machines (Closed)
Patch Set: Switch to const vectors and modify tests 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 {
Reilly Grant (use Gerrit) 2017/04/21 20:10:01 Same comments here as for U2fRequestTest and TestR
Casey Piper 2017/04/21 22:55:16 Done.
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 std::unique_ptr<U2fRequest> request = U2fRegister::TryRegistration(
71 std::vector<uint8_t>(32), std::vector<uint8_t>(32), cb.callback());
72 request->AddDeviceForTesting(std::move(device));
Reilly Grant (use Gerrit) 2017/04/21 20:10:01 Even though it's not necessary to make these tests
Casey Piper 2017/04/21 22:55:16 Done.
73 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
74 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
75 response.first);
76 ASSERT_LT(static_cast<size_t>(0), response.second.size());
77 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]);
78 }
79
80 TEST_F(U2fRegisterTest, TestDelayedSuccess) {
81 std::unique_ptr<MockU2fDevice> device(new MockU2fDevice());
82
83 // Go through the state machine twice before success
84 EXPECT_CALL(*device.get(), DeviceTransactPtr(testing::_, testing::_))
85 .WillOnce(testing::Invoke(MockU2fDevice::NotSatisfied))
86 .WillOnce(testing::Invoke(MockU2fDevice::NoErrorRegister));
87 EXPECT_CALL(*device.get(), TryWink(testing::_))
88 .Times(2)
89 .WillRepeatedly(testing::Invoke(MockU2fDevice::WinkDoNothing));
90 TestRegisterCallback cb;
91
92 std::unique_ptr<U2fRequest> request = U2fRegister::TryRegistration(
93 std::vector<uint8_t>(32), std::vector<uint8_t>(32), cb.callback());
94 request->AddDeviceForTesting(std::move(device));
95 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
96 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
97 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 std::pair<uint8_t, std::vector<uint8_t>>& response = cb.WaitForCallback();
123 EXPECT_EQ(static_cast<uint8_t>(U2fDevice::ReturnCode::SUCCESS),
124 response.first);
125 ASSERT_LT(static_cast<size_t>(0), response.second.size());
126 EXPECT_EQ(static_cast<uint8_t>(MockU2fDevice::kRegister), response.second[0]);
127 }
128
129 } // namespace device
OLDNEW
« no previous file with comments | « device/u2f/u2f_register.cc ('k') | device/u2f/u2f_request.h » ('j') | device/u2f/u2f_request.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698