Chromium Code Reviews| 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 { | |
|
Reilly Grant (use Gerrit)
2017/04/21 20:10:02
Same comments here as for U2fRequestTest and TestR
Casey Piper
2017/04/21 22:55:16
Done.
| |
| 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 std::unique_ptr<U2fRequest> request = | |
| 72 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
| 73 std::vector<uint8_t>(32), cb.callback()); | |
| 74 request->AddDeviceForTesting(std::move(device)); | |
|
Reilly Grant (use Gerrit)
2017/04/21 20:10:02
Same comment about Start() here.
Casey Piper
2017/04/21 22:55:16
Done.
| |
| 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 std::unique_ptr<U2fRequest> request = | |
| 98 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
| 99 std::vector<uint8_t>(32), cb.callback()); | |
| 100 request->AddDeviceForTesting(std::move(device)); | |
| 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 std::unique_ptr<U2fRequest> request = | |
| 131 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
| 132 std::vector<uint8_t>(32), cb.callback()); | |
| 133 request->AddDeviceForTesting(std::move(device)); | |
| 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 std::unique_ptr<U2fRequest> request = | |
| 163 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
| 164 std::vector<uint8_t>(32), cb.callback()); | |
| 165 request->AddDeviceForTesting(std::move(device0)); | |
| 166 request->AddDeviceForTesting(std::move(device1)); | |
| 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 std::unique_ptr<U2fRequest> request = | |
| 199 U2fSign::TrySign(handles, std::vector<uint8_t>(32), | |
| 200 std::vector<uint8_t>(32), cb.callback()); | |
| 201 request->AddDeviceForTesting(std::move(device0)); | |
| 202 request->AddDeviceForTesting(std::move(device1)); | |
| 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 | |
| OLD | NEW |