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 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 8 | #include "base/test/scoped_feature_list.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 9 | #include "base/test/scoped_task_environment.h" |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 10 | #include "device/base/features.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 11 | #include "device/fido/authenticator_get_assertion_response.h" |
| 12 | #include "device/fido/ctap_get_assertion_request.h" |
| 13 | #include "device/fido/fake_fido_discovery.h" |
| 14 | #include "device/fido/fido_constants.h" |
Jun Choi | 19b944e9 | 2018-04-23 20:20:20 | [diff] [blame] | 15 | #include "device/fido/fido_parsing_utils.h" |
Jun Choi | 22af8b37 | 2018-04-09 04:29:18 | [diff] [blame] | 16 | #include "device/fido/fido_test_data.h" |
Jun Choi | b60937e | 2018-04-12 17:02:38 | [diff] [blame] | 17 | #include "device/fido/fido_transport_protocol.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 18 | #include "device/fido/get_assertion_request_handler.h" |
| 19 | #include "device/fido/mock_fido_device.h" |
| 20 | #include "device/fido/test_callback_receiver.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
| 24 | namespace device { |
| 25 | |
| 26 | namespace { |
| 27 | |
Jun Choi | f7ab0df | 2018-04-05 21:48:16 | [diff] [blame] | 28 | using TestGetAssertionRequestCallback = test::StatusAndValueCallbackReceiver< |
| 29 | FidoReturnCode, |
| 30 | base::Optional<AuthenticatorGetAssertionResponse>>; |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | class FidoGetAssertionHandlerTest : public ::testing::Test { |
| 35 | public: |
| 36 | void ForgeNextHidDiscovery() { |
| 37 | discovery_ = scoped_fake_discovery_factory_.ForgeNextHidDiscovery(); |
| 38 | } |
| 39 | |
| 40 | std::unique_ptr<GetAssertionRequestHandler> CreateGetAssertionHandler() { |
| 41 | ForgeNextHidDiscovery(); |
| 42 | |
| 43 | CtapGetAssertionRequest request_param( |
Jun Choi | 03ba9b3 | 2018-05-25 18:33:39 | [diff] [blame] | 44 | test_data::kRelyingPartyId, |
| 45 | fido_parsing_utils::Materialize(test_data::kClientDataHash)); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 46 | request_param.SetAllowList( |
Jan Wilken Doerrie | 726e197e | 2018-05-14 12:53:25 | [diff] [blame] | 47 | {{CredentialType::kPublicKey, |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 48 | fido_parsing_utils::Materialize(test_data::kU2fSignKeyHandle)}}); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 49 | |
| 50 | return std::make_unique<GetAssertionRequestHandler>( |
| 51 | nullptr /* connector */, |
Jun Choi | b60937e | 2018-04-12 17:02:38 | [diff] [blame] | 52 | base::flat_set<FidoTransportProtocol>( |
| 53 | {FidoTransportProtocol::kUsbHumanInterfaceDevice}), |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 54 | std::move(request_param), get_assertion_cb_.callback()); |
| 55 | } |
| 56 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 57 | void InitFeatureListWithCtapFlag() { |
| 58 | scoped_feature_list_.InitAndEnableFeature(kNewCtap2Device); |
| 59 | } |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 60 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 61 | test::FakeFidoDiscovery* discovery() const { return discovery_; } |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 62 | TestGetAssertionRequestCallback& get_assertion_callback() { |
| 63 | return get_assertion_cb_; |
| 64 | } |
| 65 | |
| 66 | protected: |
| 67 | base::test::ScopedTaskEnvironment scoped_task_environment_{ |
| 68 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME}; |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 69 | base::test::ScopedFeatureList scoped_feature_list_; |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 70 | test::ScopedFakeFidoDiscoveryFactory scoped_fake_discovery_factory_; |
| 71 | test::FakeFidoDiscovery* discovery_; |
| 72 | TestGetAssertionRequestCallback get_assertion_cb_; |
| 73 | }; |
| 74 | |
| 75 | TEST_F(FidoGetAssertionHandlerTest, TestGetAssertionRequestOnSingleDevice) { |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 76 | InitFeatureListWithCtapFlag(); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 77 | auto request_handler = CreateGetAssertionHandler(); |
| 78 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 79 | auto device = std::make_unique<MockFidoDevice>(); |
| 80 | |
| 81 | EXPECT_CALL(*device, GetId()).WillRepeatedly(testing::Return("device0")); |
| 82 | device->ExpectCtap2CommandAndRespondWith( |
| 83 | CtapRequestCommand::kAuthenticatorGetInfo, |
| 84 | test_data::kTestAuthenticatorGetInfoResponse); |
| 85 | device->ExpectCtap2CommandAndRespondWith( |
| 86 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 87 | test_data::kTestGetAssertionResponse); |
| 88 | |
| 89 | discovery()->AddDevice(std::move(device)); |
| 90 | get_assertion_callback().WaitForCallback(); |
| 91 | |
| 92 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
| 93 | EXPECT_TRUE(get_assertion_callback().value()); |
| 94 | EXPECT_TRUE(request_handler->is_complete()); |
| 95 | } |
| 96 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 97 | // Test a scenario where the connected authenticator is a U2F device. |
| 98 | TEST_F(FidoGetAssertionHandlerTest, TestU2fSign) { |
| 99 | InitFeatureListWithCtapFlag(); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 100 | auto request_handler = CreateGetAssertionHandler(); |
| 101 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 102 | |
| 103 | auto device = std::make_unique<MockFidoDevice>(); |
| 104 | EXPECT_CALL(*device, GetId()).WillRepeatedly(testing::Return("device0")); |
| 105 | device->ExpectCtap2CommandAndRespondWith( |
| 106 | CtapRequestCommand::kAuthenticatorGetInfo, base::nullopt); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 107 | device->ExpectRequestAndRespondWith( |
| 108 | test_data::kU2fCheckOnlySignCommandApdu, |
| 109 | test_data::kApduEncodedNoErrorSignResponse); |
| 110 | device->ExpectRequestAndRespondWith( |
| 111 | test_data::kU2fSignCommandApdu, |
| 112 | test_data::kApduEncodedNoErrorSignResponse); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 113 | |
| 114 | discovery()->AddDevice(std::move(device)); |
| 115 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame^] | 116 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
| 117 | EXPECT_TRUE(get_assertion_callback().value()); |
| 118 | EXPECT_TRUE(request_handler->is_complete()); |
| 119 | } |
| 120 | |
| 121 | // Test a scenario where the connected authenticator is a U2F device and |
| 122 | // "WebAuthenticationCtap2" flag is not enabled. |
| 123 | TEST_F(FidoGetAssertionHandlerTest, TestU2fSignWithoutCtapFlag) { |
| 124 | auto request_handler = CreateGetAssertionHandler(); |
| 125 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 126 | |
| 127 | auto device = std::make_unique<MockFidoDevice>(); |
| 128 | EXPECT_CALL(*device, GetId()).WillRepeatedly(testing::Return("device0")); |
| 129 | device->ExpectRequestAndRespondWith( |
| 130 | test_data::kU2fCheckOnlySignCommandApdu, |
| 131 | test_data::kApduEncodedNoErrorSignResponse); |
| 132 | device->ExpectRequestAndRespondWith( |
| 133 | test_data::kU2fSignCommandApdu, |
| 134 | test_data::kApduEncodedNoErrorSignResponse); |
| 135 | |
| 136 | discovery()->AddDevice(std::move(device)); |
| 137 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 138 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
| 139 | EXPECT_TRUE(get_assertion_callback().value()); |
| 140 | EXPECT_TRUE(request_handler->is_complete()); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | } // namespace device |