blob: eb59a1379088ad7e7a033b09b1b3c5872c7ad7ff [file] [log] [blame]
Jun Choi4fc8b7812018-04-05 07:39:071// 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 Choi19b944e92018-04-23 20:20:2013#include "device/fido/fido_parsing_utils.h"
Jun Choi22af8b372018-04-09 04:29:1814#include "device/fido/fido_test_data.h"
Jun Choib60937e2018-04-12 17:02:3815#include "device/fido/fido_transport_protocol.h"
Jun Choi4fc8b7812018-04-05 07:39:0716#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 Choi4fc8b7812018-04-05 07:39:0719#include "testing/gmock/include/gmock/gmock.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22namespace device {
23
24namespace {
25
Jun Choif7ab0df2018-04-05 21:48:1626using TestGetAssertionRequestCallback = test::StatusAndValueCallbackReceiver<
27 FidoReturnCode,
28 base::Optional<AuthenticatorGetAssertionResponse>>;
Jun Choi4fc8b7812018-04-05 07:39:0729
30} // namespace
31
32class 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 Choi03ba9b32018-05-25 18:33:3942 test_data::kRelyingPartyId,
43 fido_parsing_utils::Materialize(test_data::kClientDataHash));
Jun Choi4fc8b7812018-04-05 07:39:0744 request_param.SetAllowList(
Jan Wilken Doerrie726e197e2018-05-14 12:53:2545 {{CredentialType::kPublicKey,
Jun Choi19b944e92018-04-23 20:20:2046 fido_parsing_utils::Materialize(
Jun Choif7be8a72018-04-07 02:59:3247 test_data::kTestGetAssertionCredentialId)}});
Jun Choi4fc8b7812018-04-05 07:39:0748
49 return std::make_unique<GetAssertionRequestHandler>(
50 nullptr /* connector */,
Jun Choib60937e2018-04-12 17:02:3851 base::flat_set<FidoTransportProtocol>(
52 {FidoTransportProtocol::kUsbHumanInterfaceDevice}),
Jun Choi4fc8b7812018-04-05 07:39:0753 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
70TEST_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.
93TEST_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