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

Unified Diff: device/u2f/u2f_hid_device_unittest.cc

Issue 2721223002: Add support for U2fHidDevice interaction (Closed)
Patch Set: Ensure queued callbacks are called in error scenarios 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 side-by-side diff with in-line comments
Download patch
« device/u2f/u2f_hid_device.cc ('K') | « device/u2f/u2f_hid_device.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/u2f/u2f_hid_device_unittest.cc
diff --git a/device/u2f/u2f_hid_device_unittest.cc b/device/u2f/u2f_hid_device_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9955f59a1e99e4bf8bdcb245501d8f5ee9d4181d
--- /dev/null
+++ b/device/u2f/u2f_hid_device_unittest.cc
@@ -0,0 +1,269 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <list>
+
+#include "base/bind.h"
+#include "base/memory/ptr_util.h"
+#include "base/run_loop.h"
+#include "base/test/test_io_thread.h"
+#include "device/hid/hid_connection.h"
+#include "device/hid/hid_device_filter.h"
+#include "device/test/test_device_client.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "u2f_apdu_command.h"
+#include "u2f_apdu_response.h"
+#include "u2f_hid_device.h"
+
+namespace device {
+
+class U2fDeviceEnumerate {
+ public:
+ U2fDeviceEnumerate()
+ : closure_(),
+ callback_(base::Bind(&U2fDeviceEnumerate::ReceivedCallback,
+ base::Unretained(this))),
+ run_loop_() {}
+ ~U2fDeviceEnumerate() {}
+
+ void ReceivedCallback(
+ const std::vector<scoped_refptr<HidDeviceInfo>>& devices) {
+ std::list<std::unique_ptr<U2fHidDevice>> u2f_devices;
+ filter_.SetUsagePage(0xf1d0);
+ for (auto device_info : devices) {
+ if (filter_.Matches(device_info))
+ u2f_devices.push_front(base::MakeUnique<U2fHidDevice>(device_info));
+ }
+ devices_ = std::move(u2f_devices);
+ closure_.Run();
+ }
+
+ std::list<std::unique_ptr<U2fHidDevice>>& WaitForCallback() {
+ closure_ = run_loop_.QuitClosure();
+ run_loop_.Run();
+ return devices_;
+ }
+
+ const HidService::GetDevicesCallback& callback() { return callback_; }
+
+ private:
+ HidDeviceFilter filter_;
+ std::list<std::unique_ptr<U2fHidDevice>> devices_;
+ base::Closure closure_;
+ HidService::GetDevicesCallback callback_;
+ base::RunLoop run_loop_;
+};
+
+class TestVersionCallback {
+ public:
+ TestVersionCallback()
+ : closure_(),
+ callback_(base::Bind(&TestVersionCallback::ReceivedCallback,
+ base::Unretained(this))),
+ run_loop_() {}
+ ~TestVersionCallback() {}
+
+ void ReceivedCallback(bool success, U2fDevice::ProtocolVersion version) {
+ version_ = version;
+ closure_.Run();
+ }
+
+ U2fDevice::ProtocolVersion WaitForCallback() {
+ closure_ = run_loop_.QuitClosure();
+ run_loop_.Run();
+ return version_;
+ }
+
+ const U2fDevice::VersionCallback& callback() { return callback_; }
+
+ private:
+ U2fDevice::ProtocolVersion version_;
+ base::Closure closure_;
+ U2fDevice::VersionCallback callback_;
+ base::RunLoop run_loop_;
+};
+
+class TestDeviceCallback {
+ public:
+ TestDeviceCallback()
+ : closure_(),
+ callback_(base::Bind(&TestDeviceCallback::ReceivedCallback,
+ base::Unretained(this))),
+ run_loop_() {}
+ ~TestDeviceCallback() {}
+
+ void ReceivedCallback(bool success, scoped_refptr<U2fApduResponse> response) {
+ response_ = response;
+ closure_.Run();
+ }
+
+ scoped_refptr<U2fApduResponse> WaitForCallback() {
+ closure_ = run_loop_.QuitClosure();
+ run_loop_.Run();
+ return response_;
+ }
+
+ const U2fDevice::DeviceCallback& callback() { return callback_; }
+
+ private:
+ scoped_refptr<U2fApduResponse> response_;
+ base::Closure closure_;
+ U2fDevice::DeviceCallback callback_;
+ base::RunLoop run_loop_;
+};
+
+class TestFailureCallback {
Reilly Grant (use Gerrit) 2017/03/09 00:37:03 There's a simpler way to do this: void ResponseCa
Casey Piper 2017/03/10 00:32:10 Done.
+ public:
+ TestFailureCallback()
+ : callback_(base::Bind(&TestFailureCallback::ReceivedCallback,
+ base::Unretained(this))) {}
+ ~TestFailureCallback() {}
+
+ void ReceivedCallback(bool success, scoped_refptr<U2fApduResponse> response) {
+ response_ = response;
+ }
+
+ scoped_refptr<U2fApduResponse> GetCallbackResult() { return response_; }
+
+ const U2fDevice::DeviceCallback& callback() { return callback_; }
+
+ private:
+ scoped_refptr<U2fApduResponse> response_;
+ U2fDevice::DeviceCallback callback_;
+};
+
+class U2fHidDeviceTest : public testing::Test {
+ public:
+ void SetUp() override {
+ if (!U2fHidDevice::IsTestEnabled())
+ return;
+ message_loop_.reset(new base::MessageLoopForUI());
+ io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
+ device_client_.reset(
+ new device::TestDeviceClient(io_thread_->task_runner()));
+ }
+
+ protected:
+ std::unique_ptr<base::MessageLoopForUI> message_loop_;
+ std::unique_ptr<base::TestIOThread> io_thread_;
+ std::unique_ptr<device::TestDeviceClient> device_client_;
+};
+
+TEST_F(U2fHidDeviceTest, TestHidDeviceVersion) {
+ if (!U2fHidDevice::IsTestEnabled())
+ return;
+
+ U2fDeviceEnumerate callback;
+ HidService* hid_service = DeviceClient::Get()->GetHidService();
+ hid_service->GetDevices(callback.callback());
+ std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices =
+ callback.WaitForCallback();
+
+ for (auto& device : u2f_devices) {
+ TestVersionCallback vc;
+ device->Version(vc.callback());
+ U2fDevice::ProtocolVersion version = vc.WaitForCallback();
+ EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2);
+ }
+};
+
+TEST_F(U2fHidDeviceTest, TestMultipleRequests) {
+ if (!U2fHidDevice::IsTestEnabled())
+ return;
+
+ U2fDeviceEnumerate callback;
+ HidService* hid_service = DeviceClient::Get()->GetHidService();
+ hid_service->GetDevices(callback.callback());
+ std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices =
+ callback.WaitForCallback();
+
+ for (auto& device : u2f_devices) {
+ TestVersionCallback vc;
+ TestVersionCallback vc2;
+ // Call version twice to check message queueing
+ device->Version(vc.callback());
+ device->Version(vc2.callback());
+ U2fDevice::ProtocolVersion version = vc.WaitForCallback();
+ EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2);
+ version = vc2.WaitForCallback();
+ EXPECT_EQ(version, U2fDevice::ProtocolVersion::U2F_V2);
+ }
+};
+
+TEST_F(U2fHidDeviceTest, TestConnectionFailure) {
+ if (!U2fHidDevice::IsTestEnabled())
+ return;
+
+ U2fDeviceEnumerate callback;
+ HidService* hid_service = DeviceClient::Get()->GetHidService();
Reilly Grant (use Gerrit) 2017/03/09 00:37:03 Since this logic is handling failure cases can you
Casey Piper 2017/03/10 00:32:10 Switched to using mock device client and device co
+ hid_service->GetDevices(callback.callback());
+ std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices =
+ callback.WaitForCallback();
+
+ if (!u2f_devices.empty()) {
+ auto& device = u2f_devices.front();
+ // Write a successful message to put device in IDLE state
+ TestDeviceCallback cb0;
+ device->DeviceTransact(U2fApduCommand::CreateVersion(), cb0.callback());
+ scoped_refptr<U2fApduResponse> response = cb0.WaitForCallback();
+ EXPECT_NE(nullptr, response);
+ // Manually delete connection
+ device->connection_->Close();
+ device->connection_ = nullptr;
+ // Add pending transactions manually and ensure they are processed
+ TestFailureCallback cb1;
+ TestFailureCallback cb2;
+ device->pending_transactions_.push_back(
+ {U2fApduCommand::CreateVersion(), cb1.callback()});
+ device->pending_transactions_.push_back(
+ {U2fApduCommand::CreateVersion(), cb2.callback()});
+ TestFailureCallback cb3;
+ device->DeviceTransact(U2fApduCommand::CreateVersion(), cb3.callback());
+ response = cb3.GetCallbackResult();
+ EXPECT_EQ(nullptr, response);
+ response = cb1.GetCallbackResult();
+ EXPECT_EQ(nullptr, response);
+ response = cb2.GetCallbackResult();
+ EXPECT_EQ(nullptr, response);
+ }
+};
+
+TEST_F(U2fHidDeviceTest, TestDeviceError) {
+ if (!U2fHidDevice::IsTestEnabled())
+ return;
+
+ U2fDeviceEnumerate callback;
+ HidService* hid_service = DeviceClient::Get()->GetHidService();
+ hid_service->GetDevices(callback.callback());
+ std::list<std::unique_ptr<U2fHidDevice>>& u2f_devices =
+ callback.WaitForCallback();
+
+ if (!u2f_devices.empty()) {
+ auto& device = u2f_devices.front();
+ // Write a successful message to put device in IDLE state
+ TestDeviceCallback cb0;
+ device->DeviceTransact(U2fApduCommand::CreateVersion(), cb0.callback());
+ scoped_refptr<U2fApduResponse> response = cb0.WaitForCallback();
+ EXPECT_NE(nullptr, response);
+ // Put the device into an error state
+ device->state_ = U2fHidDevice::State::DEVICE_ERROR;
+ // Add pending transactions manually and ensure they are processed
+ TestFailureCallback cb1;
+ TestFailureCallback cb2;
+ device->pending_transactions_.push_back(
+ {U2fApduCommand::CreateVersion(), cb1.callback()});
+ device->pending_transactions_.push_back(
+ {U2fApduCommand::CreateVersion(), cb2.callback()});
+ TestFailureCallback cb3;
+ device->DeviceTransact(U2fApduCommand::CreateVersion(), cb3.callback());
+ response = cb3.GetCallbackResult();
+ EXPECT_EQ(nullptr, response);
+ response = cb1.GetCallbackResult();
+ EXPECT_EQ(nullptr, response);
+ response = cb2.GetCallbackResult();
+ EXPECT_EQ(nullptr, response);
+ }
+};
+
+} // namespace device
« device/u2f/u2f_hid_device.cc ('K') | « device/u2f/u2f_hid_device.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698