Commit 4260e609 authored by Jun Choi's avatar Jun Choi Committed by Commit Bot

Code improvement in authenticator_impl_unittest.cc

Refactor repeated code to construct Authenticator with timer into a
function.

Bug: 798573
Change-Id: I5b3ecedb4cc56a30f5b32ed3872a709e18545cee
Reviewed-on: https://chromium-review.googlesource.com/1034096
Commit-Queue: Jun Choi <hongjunchoi@chromium.org>
Reviewed-by: default avatarBalazs Engedy <engedy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561941}
parent 9a416976
...@@ -306,6 +306,24 @@ class AuthenticatorImplTest : public content::RenderViewHostTestHarness { ...@@ -306,6 +306,24 @@ class AuthenticatorImplTest : public content::RenderViewHostTestHarness {
return authenticator; return authenticator;
} }
AuthenticatorPtr ConstructAuthenticatorWithTimer(
scoped_refptr<base::TestMockTimeTaskRunner> task_runner) {
connector_ = service_manager::Connector::Create(&request_);
fake_hid_manager_ = std::make_unique<device::FakeHidManager>();
service_manager::Connector::TestApi test_api(connector_.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(device::mojom::kServiceName),
device::mojom::HidManager::Name_,
base::Bind(&device::FakeHidManager::AddBinding,
base::Unretained(fake_hid_manager_.get())));
// Set up a timer for testing.
auto timer =
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
return ConnectToAuthenticator(connector_.get(), std::move(timer));
}
url::Origin GetTestOrigin() { url::Origin GetTestOrigin() {
const GURL test_relying_party_url(kTestOrigin1); const GURL test_relying_party_url(kTestOrigin1);
CHECK(test_relying_party_url.is_valid()); CHECK(test_relying_party_url.is_valid());
...@@ -349,6 +367,9 @@ class AuthenticatorImplTest : public content::RenderViewHostTestHarness { ...@@ -349,6 +367,9 @@ class AuthenticatorImplTest : public content::RenderViewHostTestHarness {
private: private:
std::unique_ptr<AuthenticatorImpl> authenticator_impl_; std::unique_ptr<AuthenticatorImpl> authenticator_impl_;
service_manager::mojom::ConnectorRequest request_;
std::unique_ptr<service_manager::Connector> connector_;
std::unique_ptr<device::FakeHidManager> fake_hid_manager_;
}; };
// Verify behavior for various combinations of origins and RP IDs. // Verify behavior for various combinations of origins and RP IDs.
...@@ -557,26 +578,9 @@ TEST_F(AuthenticatorImplTest, TestMakeCredentialTimeout) { ...@@ -557,26 +578,9 @@ TEST_F(AuthenticatorImplTest, TestMakeCredentialTimeout) {
GetTestPublicKeyCredentialCreationOptions(); GetTestPublicKeyCredentialCreationOptions();
TestMakeCredentialCallback callback_receiver; TestMakeCredentialCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
service_manager::Connector::TestApi test_api(connector.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(device::mojom::kServiceName),
device::mojom::HidManager::Name_,
base::Bind(&device::FakeHidManager::AddBinding,
base::Unretained(fake_hid_manager.get())));
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
authenticator->MakeCredential(std::move(options), authenticator->MakeCredential(std::move(options),
callback_receiver.callback()); callback_receiver.callback());
...@@ -661,26 +665,9 @@ TEST_F(AuthenticatorImplTest, TestGetAssertionTimeout) { ...@@ -661,26 +665,9 @@ TEST_F(AuthenticatorImplTest, TestGetAssertionTimeout) {
GetTestPublicKeyCredentialRequestOptions(); GetTestPublicKeyCredentialRequestOptions();
TestGetAssertionCallback callback_receiver; TestGetAssertionCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
service_manager::Connector::TestApi test_api(connector.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(device::mojom::kServiceName),
device::mojom::HidManager::Name_,
base::Bind(&device::FakeHidManager::AddBinding,
base::Unretained(fake_hid_manager.get())));
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
authenticator->GetAssertion(std::move(options), callback_receiver.callback()); authenticator->GetAssertion(std::move(options), callback_receiver.callback());
// Trigger timer. // Trigger timer.
...@@ -744,25 +731,9 @@ TEST_F(AuthenticatorImplTest, TestCableDiscoveryEnabledWithSwitch) { ...@@ -744,25 +731,9 @@ TEST_F(AuthenticatorImplTest, TestCableDiscoveryEnabledWithSwitch) {
GetTestPublicKeyCredentialRequestOptions(); GetTestPublicKeyCredentialRequestOptions();
TestGetAssertionCallback callback_receiver; TestGetAssertionCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
service_manager::Connector::TestApi test_api(connector.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(device::mojom::kServiceName),
device::mojom::HidManager::Name_,
base::Bind(&device::FakeHidManager::AddBinding,
base::Unretained(fake_hid_manager.get())));
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
authenticator->GetAssertion(std::move(options), callback_receiver.callback()); authenticator->GetAssertion(std::move(options), callback_receiver.callback());
// Trigger timer. // Trigger timer.
...@@ -787,25 +758,9 @@ TEST_F(AuthenticatorImplTest, TestCableDiscoveryDisabledForMakeCredential) { ...@@ -787,25 +758,9 @@ TEST_F(AuthenticatorImplTest, TestCableDiscoveryDisabledForMakeCredential) {
GetTestPublicKeyCredentialCreationOptions(); GetTestPublicKeyCredentialCreationOptions();
TestMakeCredentialCallback callback_receiver; TestMakeCredentialCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
service_manager::Connector::TestApi test_api(connector.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(device::mojom::kServiceName),
device::mojom::HidManager::Name_,
base::Bind(&device::FakeHidManager::AddBinding,
base::Unretained(fake_hid_manager.get())));
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
authenticator->MakeCredential(std::move(options), authenticator->MakeCredential(std::move(options),
callback_receiver.callback()); callback_receiver.callback());
...@@ -828,25 +783,9 @@ TEST_F(AuthenticatorImplTest, TestCableDiscoveryDisabledWithoutSwitch) { ...@@ -828,25 +783,9 @@ TEST_F(AuthenticatorImplTest, TestCableDiscoveryDisabledWithoutSwitch) {
GetTestPublicKeyCredentialRequestOptions(); GetTestPublicKeyCredentialRequestOptions();
TestGetAssertionCallback callback_receiver; TestGetAssertionCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
service_manager::Connector::TestApi test_api(connector.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(device::mojom::kServiceName),
device::mojom::HidManager::Name_,
base::Bind(&device::FakeHidManager::AddBinding,
base::Unretained(fake_hid_manager.get())));
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
authenticator->GetAssertion(std::move(options), callback_receiver.callback()); authenticator->GetAssertion(std::move(options), callback_receiver.callback());
// Trigger timer. // Trigger timer.
...@@ -868,20 +807,9 @@ TEST_F(AuthenticatorImplTest, TestU2fDeviceDoesNotSupportMakeCredential) { ...@@ -868,20 +807,9 @@ TEST_F(AuthenticatorImplTest, TestU2fDeviceDoesNotSupportMakeCredential) {
GetTestPublicKeyCredentialCreationOptions(); GetTestPublicKeyCredentialCreationOptions();
TestMakeCredentialCallback callback_receiver; TestMakeCredentialCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
device::test::ScopedVirtualFidoDevice virtual_device; device::test::ScopedVirtualFidoDevice virtual_device;
authenticator->MakeCredential(std::move(options), authenticator->MakeCredential(std::move(options),
callback_receiver.callback()); callback_receiver.callback());
...@@ -902,20 +830,9 @@ TEST_F(AuthenticatorImplTest, TestU2fDeviceDoesNotSupportGetAssertion) { ...@@ -902,20 +830,9 @@ TEST_F(AuthenticatorImplTest, TestU2fDeviceDoesNotSupportGetAssertion) {
GetTestPublicKeyCredentialRequestOptions(); GetTestPublicKeyCredentialRequestOptions();
TestGetAssertionCallback callback_receiver; TestGetAssertionCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
device::test::ScopedVirtualFidoDevice virtual_device; device::test::ScopedVirtualFidoDevice virtual_device;
authenticator->GetAssertion(std::move(options), callback_receiver.callback()); authenticator->GetAssertion(std::move(options), callback_receiver.callback());
...@@ -936,20 +853,9 @@ TEST_F(AuthenticatorImplTest, Ctap2AcceptsEmptyAllowCredentials) { ...@@ -936,20 +853,9 @@ TEST_F(AuthenticatorImplTest, Ctap2AcceptsEmptyAllowCredentials) {
options->allow_credentials.clear(); options->allow_credentials.clear();
TestGetAssertionCallback callback_receiver; TestGetAssertionCallback callback_receiver;
// Set up service_manager::Connector for tests.
auto fake_hid_manager = std::make_unique<device::FakeHidManager>();
service_manager::mojom::ConnectorRequest request;
auto connector = service_manager::Connector::Create(&request);
// Set up a timer for testing.
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>( auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::Time::Now(), base::TimeTicks::Now()); base::Time::Now(), base::TimeTicks::Now());
auto timer = auto authenticator = ConstructAuthenticatorWithTimer(task_runner);
std::make_unique<base::OneShotTimer>(task_runner->GetMockTickClock());
timer->SetTaskRunner(task_runner);
AuthenticatorPtr authenticator =
ConnectToAuthenticator(connector.get(), std::move(timer));
device::test::ScopedVirtualFidoDevice virtual_device; device::test::ScopedVirtualFidoDevice virtual_device;
authenticator->GetAssertion(std::move(options), callback_receiver.callback()); authenticator->GetAssertion(std::move(options), callback_receiver.callback());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment