Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 1 | // Copyright 2018 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 <memory> |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "base/test/scoped_task_environment.h" |
| 9 | #include "device/fido/authenticator_get_assertion_response.h" |
| 10 | #include "device/fido/ctap_get_assertion_request.h" |
| 11 | #include "device/fido/fake_fido_discovery.h" |
| 12 | #include "device/fido/fido_constants.h" |
Jun Choi | 19b944e9 | 2018-04-23 20:20:20 | [diff] [blame] | 13 | #include "device/fido/fido_parsing_utils.h" |
Jun Choi | 22af8b37 | 2018-04-09 04:29:18 | [diff] [blame] | 14 | #include "device/fido/fido_test_data.h" |
Jun Choi | b60937e | 2018-04-12 17:02:38 | [diff] [blame] | 15 | #include "device/fido/fido_transport_protocol.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 16 | #include "device/fido/get_assertion_request_handler.h" |
| 17 | #include "device/fido/mock_fido_device.h" |
| 18 | #include "device/fido/test_callback_receiver.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
| 22 | namespace device { |
| 23 | |
| 24 | namespace { |
| 25 | |
Jun Choi | f7ab0df | 2018-04-05 21:48:16 | [diff] [blame] | 26 | using TestGetAssertionRequestCallback = test::StatusAndValueCallbackReceiver< |
| 27 | FidoReturnCode, |
| 28 | base::Optional<AuthenticatorGetAssertionResponse>>; |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | class FidoGetAssertionHandlerTest : public ::testing::Test { |
| 33 | public: |
| 34 | void ForgeNextHidDiscovery() { |
| 35 | discovery_ = scoped_fake_discovery_factory_.ForgeNextHidDiscovery(); |
| 36 | } |
| 37 | |
| 38 | std::unique_ptr<GetAssertionRequestHandler> CreateGetAssertionHandler() { |
| 39 | ForgeNextHidDiscovery(); |
| 40 | |
| 41 | CtapGetAssertionRequest request_param( |
Jun Choi | 03ba9b3 | 2018-05-25 18:33:39 | [diff] [blame^] | 42 | test_data::kRelyingPartyId, |
| 43 | fido_parsing_utils::Materialize(test_data::kClientDataHash)); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 44 | request_param.SetAllowList( |
Jan Wilken Doerrie | 726e197e | 2018-05-14 12:53:25 | [diff] [blame] | 45 | {{CredentialType::kPublicKey, |
Jun Choi | 19b944e9 | 2018-04-23 20:20:20 | [diff] [blame] | 46 | fido_parsing_utils::Materialize( |
Jun Choi | f7be8a7 | 2018-04-07 02:59:32 | [diff] [blame] | 47 | test_data::kTestGetAssertionCredentialId)}}); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 48 | |
| 49 | return std::make_unique<GetAssertionRequestHandler>( |
| 50 | nullptr /* connector */, |
Jun Choi | b60937e | 2018-04-12 17:02:38 | [diff] [blame] | 51 | base::flat_set<FidoTransportProtocol>( |
| 52 | {FidoTransportProtocol::kUsbHumanInterfaceDevice}), |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 53 | std::move(request_param), get_assertion_cb_.callback()); |
| 54 | } |
| 55 | |
| 56 | test::FakeFidoDiscovery* discovery() const { return discovery_; } |
| 57 | |
| 58 | TestGetAssertionRequestCallback& get_assertion_callback() { |
| 59 | return get_assertion_cb_; |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | base::test::ScopedTaskEnvironment scoped_task_environment_{ |
| 64 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME}; |
| 65 | test::ScopedFakeFidoDiscoveryFactory scoped_fake_discovery_factory_; |
| 66 | test::FakeFidoDiscovery* discovery_; |
| 67 | TestGetAssertionRequestCallback get_assertion_cb_; |
| 68 | }; |
| 69 | |
| 70 | TEST_F(FidoGetAssertionHandlerTest, TestGetAssertionRequestOnSingleDevice) { |
| 71 | auto request_handler = CreateGetAssertionHandler(); |
| 72 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 73 | auto device = std::make_unique<MockFidoDevice>(); |
| 74 | |
| 75 | EXPECT_CALL(*device, GetId()).WillRepeatedly(testing::Return("device0")); |
| 76 | device->ExpectCtap2CommandAndRespondWith( |
| 77 | CtapRequestCommand::kAuthenticatorGetInfo, |
| 78 | test_data::kTestAuthenticatorGetInfoResponse); |
| 79 | device->ExpectCtap2CommandAndRespondWith( |
| 80 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 81 | test_data::kTestGetAssertionResponse); |
| 82 | |
| 83 | discovery()->AddDevice(std::move(device)); |
| 84 | get_assertion_callback().WaitForCallback(); |
| 85 | |
| 86 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
| 87 | EXPECT_TRUE(get_assertion_callback().value()); |
| 88 | EXPECT_TRUE(request_handler->is_complete()); |
| 89 | } |
| 90 | |
| 91 | // Test a scenario where the connected authenticator is a U2F device. Request |
| 92 | // be silently dropped and request should remain in incomplete state. |
| 93 | TEST_F(FidoGetAssertionHandlerTest, TestGetAssertionIncorrectGetInfoResponse) { |
| 94 | auto request_handler = CreateGetAssertionHandler(); |
| 95 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 96 | |
| 97 | auto device = std::make_unique<MockFidoDevice>(); |
| 98 | EXPECT_CALL(*device, GetId()).WillRepeatedly(testing::Return("device0")); |
| 99 | device->ExpectCtap2CommandAndRespondWith( |
| 100 | CtapRequestCommand::kAuthenticatorGetInfo, base::nullopt); |
| 101 | |
| 102 | discovery()->AddDevice(std::move(device)); |
| 103 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 104 | EXPECT_FALSE(request_handler->is_complete()); |
| 105 | } |
| 106 | |
| 107 | } // namespace device |