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

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

Issue 2743623006: Modify U2F apdu classes to use unique pointers (Closed)
Patch Set: Use more concise form of base::Passed Created 3 years, 9 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
« no previous file with comments | « device/u2f/u2f_hid_device.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <list> 5 #include <list>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_io_thread.h" 10 #include "base/test/test_io_thread.h"
(...skipping 11 matching lines...) Expand all
22 #include "u2f_packet.h" 22 #include "u2f_packet.h"
23 23
24 namespace { 24 namespace {
25 25
26 #if defined(OS_MACOSX) 26 #if defined(OS_MACOSX)
27 const uint64_t kTestDeviceId = 42; 27 const uint64_t kTestDeviceId = 42;
28 #else 28 #else
29 const char* kTestDeviceId = "device"; 29 const char* kTestDeviceId = "device";
30 #endif 30 #endif
31 31
32 void ResponseCallback(scoped_refptr<device::U2fApduResponse>* output, 32 void ResponseCallback(std::unique_ptr<device::U2fApduResponse>* output,
33 bool success, 33 bool success,
34 scoped_refptr<device::U2fApduResponse> response) { 34 std::unique_ptr<device::U2fApduResponse> response) {
35 *output = response; 35 *output = std::move(response);
36 } 36 }
37 37
38 class MockHidErrorConnection : public device::HidConnection { 38 class MockHidErrorConnection : public device::HidConnection {
39 public: 39 public:
40 explicit MockHidErrorConnection( 40 explicit MockHidErrorConnection(
41 scoped_refptr<device::HidDeviceInfo> device_info) 41 scoped_refptr<device::HidDeviceInfo> device_info)
42 : device::HidConnection(device_info) {} 42 : device::HidConnection(device_info) {}
43 43
44 void PlatformClose() override {} 44 void PlatformClose() override {}
45 45
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 134
135 class TestDeviceCallback { 135 class TestDeviceCallback {
136 public: 136 public:
137 TestDeviceCallback() 137 TestDeviceCallback()
138 : closure_(), 138 : closure_(),
139 callback_(base::Bind(&TestDeviceCallback::ReceivedCallback, 139 callback_(base::Bind(&TestDeviceCallback::ReceivedCallback,
140 base::Unretained(this))), 140 base::Unretained(this))),
141 run_loop_() {} 141 run_loop_() {}
142 ~TestDeviceCallback() {} 142 ~TestDeviceCallback() {}
143 143
144 void ReceivedCallback(bool success, scoped_refptr<U2fApduResponse> response) { 144 void ReceivedCallback(bool success,
145 response_ = response; 145 std::unique_ptr<U2fApduResponse> response) {
146 response_ = std::move(response);
146 closure_.Run(); 147 closure_.Run();
147 } 148 }
148 149
149 scoped_refptr<U2fApduResponse> WaitForCallback() { 150 std::unique_ptr<U2fApduResponse> WaitForCallback() {
150 closure_ = run_loop_.QuitClosure(); 151 closure_ = run_loop_.QuitClosure();
151 run_loop_.Run(); 152 run_loop_.Run();
152 return response_; 153 return std::move(response_);
153 } 154 }
154 155
155 const U2fDevice::DeviceCallback& callback() { return callback_; } 156 const U2fDevice::DeviceCallback& callback() { return callback_; }
156 157
157 private: 158 private:
158 scoped_refptr<U2fApduResponse> response_; 159 std::unique_ptr<U2fApduResponse> response_;
159 base::Closure closure_; 160 base::Closure closure_;
160 U2fDevice::DeviceCallback callback_; 161 U2fDevice::DeviceCallback callback_;
161 base::RunLoop run_loop_; 162 base::RunLoop run_loop_;
162 }; 163 };
163 164
164 class U2fHidDeviceTest : public testing::Test { 165 class U2fHidDeviceTest : public testing::Test {
165 public: 166 public:
166 void SetUp() override { 167 void SetUp() override {
167 message_loop_.reset(new base::MessageLoopForUI()); 168 message_loop_.reset(new base::MessageLoopForUI());
168 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); 169 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 236
236 ASSERT_EQ(static_cast<size_t>(1), u2f_devices.size()); 237 ASSERT_EQ(static_cast<size_t>(1), u2f_devices.size());
237 auto& device = u2f_devices.front(); 238 auto& device = u2f_devices.front();
238 // Put device in IDLE state 239 // Put device in IDLE state
239 TestDeviceCallback cb0; 240 TestDeviceCallback cb0;
240 device->state_ = U2fHidDevice::State::IDLE; 241 device->state_ = U2fHidDevice::State::IDLE;
241 242
242 // Manually delete connection 243 // Manually delete connection
243 device->connection_ = nullptr; 244 device->connection_ = nullptr;
244 // Add pending transactions manually and ensure they are processed 245 // Add pending transactions manually and ensure they are processed
245 scoped_refptr<U2fApduResponse> response1( 246 std::unique_ptr<U2fApduResponse> response1(
246 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 247 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
247 device->pending_transactions_.push_back( 248 device->pending_transactions_.push_back(
248 {U2fApduCommand::CreateVersion(), 249 {U2fApduCommand::CreateVersion(),
249 base::Bind(&ResponseCallback, &response1)}); 250 base::Bind(&ResponseCallback, &response1)});
250 scoped_refptr<U2fApduResponse> response2( 251 std::unique_ptr<U2fApduResponse> response2(
251 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 252 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
252 device->pending_transactions_.push_back( 253 device->pending_transactions_.push_back(
253 {U2fApduCommand::CreateVersion(), 254 {U2fApduCommand::CreateVersion(),
254 base::Bind(&ResponseCallback, &response2)}); 255 base::Bind(&ResponseCallback, &response2)});
255 scoped_refptr<U2fApduResponse> response3( 256 std::unique_ptr<U2fApduResponse> response3(
256 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 257 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
257 device->DeviceTransact(U2fApduCommand::CreateVersion(), 258 device->DeviceTransact(U2fApduCommand::CreateVersion(),
258 base::Bind(&ResponseCallback, &response3)); 259 base::Bind(&ResponseCallback, &response3));
259 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_); 260 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_);
260 EXPECT_EQ(nullptr, response1); 261 EXPECT_EQ(nullptr, response1);
261 EXPECT_EQ(nullptr, response2); 262 EXPECT_EQ(nullptr, response2);
262 EXPECT_EQ(nullptr, response3); 263 EXPECT_EQ(nullptr, response3);
263 }; 264 };
264 265
265 TEST_F(U2fHidDeviceTest, TestDeviceError) { 266 TEST_F(U2fHidDeviceTest, TestDeviceError) {
(...skipping 12 matching lines...) Expand all
278 std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices = 279 std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices =
279 callback.WaitForCallback(); 280 callback.WaitForCallback();
280 281
281 ASSERT_EQ(static_cast<size_t>(1), u2f_devices.size()); 282 ASSERT_EQ(static_cast<size_t>(1), u2f_devices.size());
282 auto& device = u2f_devices.front(); 283 auto& device = u2f_devices.front();
283 // Mock connection where writes always fail 284 // Mock connection where writes always fail
284 scoped_refptr<MockHidErrorConnection> connection( 285 scoped_refptr<MockHidErrorConnection> connection(
285 new MockHidErrorConnection(device0)); 286 new MockHidErrorConnection(device0));
286 device->connection_ = connection; 287 device->connection_ = connection;
287 device->state_ = U2fHidDevice::State::IDLE; 288 device->state_ = U2fHidDevice::State::IDLE;
288 scoped_refptr<U2fApduResponse> response0( 289 std::unique_ptr<U2fApduResponse> response0(
289 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 290 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
290 device->DeviceTransact(U2fApduCommand::CreateVersion(), 291 device->DeviceTransact(U2fApduCommand::CreateVersion(),
291 base::Bind(&ResponseCallback, &response0)); 292 base::Bind(&ResponseCallback, &response0));
292 EXPECT_EQ(nullptr, response0); 293 EXPECT_EQ(nullptr, response0);
293 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_); 294 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_);
294 295
295 // Add pending transactions manually and ensure they are processed 296 // Add pending transactions manually and ensure they are processed
296 scoped_refptr<U2fApduResponse> response1( 297 std::unique_ptr<U2fApduResponse> response1(
297 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 298 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
298 device->pending_transactions_.push_back( 299 device->pending_transactions_.push_back(
299 {U2fApduCommand::CreateVersion(), 300 {U2fApduCommand::CreateVersion(),
300 base::Bind(&ResponseCallback, &response1)}); 301 base::Bind(&ResponseCallback, &response1)});
301 scoped_refptr<U2fApduResponse> response2( 302 std::unique_ptr<U2fApduResponse> response2(
302 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 303 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
303 device->pending_transactions_.push_back( 304 device->pending_transactions_.push_back(
304 {U2fApduCommand::CreateVersion(), 305 {U2fApduCommand::CreateVersion(),
305 base::Bind(&ResponseCallback, &response2)}); 306 base::Bind(&ResponseCallback, &response2)});
306 scoped_refptr<U2fApduResponse> response3( 307 std::unique_ptr<U2fApduResponse> response3(
307 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0}))); 308 U2fApduResponse::CreateFromMessage(std::vector<uint8_t>({0x0, 0x0})));
308 device->DeviceTransact(U2fApduCommand::CreateVersion(), 309 device->DeviceTransact(U2fApduCommand::CreateVersion(),
309 base::Bind(&ResponseCallback, &response3)); 310 base::Bind(&ResponseCallback, &response3));
310 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_); 311 EXPECT_EQ(U2fHidDevice::State::DEVICE_ERROR, device->state_);
311 EXPECT_EQ(nullptr, response1); 312 EXPECT_EQ(nullptr, response1);
312 EXPECT_EQ(nullptr, response2); 313 EXPECT_EQ(nullptr, response2);
313 EXPECT_EQ(nullptr, response3); 314 EXPECT_EQ(nullptr, response3);
314 }; 315 };
315 316
316 } // namespace device 317 } // namespace device
OLDNEW
« no previous file with comments | « device/u2f/u2f_hid_device.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698