summaryrefslogtreecommitdiff
path: root/chromium/device/fido/mock_fido_device.h
blob: 90fa9956ed04120f047abd9f223af2b87ce648dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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.

#ifndef DEVICE_FIDO_MOCK_FIDO_DEVICE_H_
#define DEVICE_FIDO_MOCK_FIDO_DEVICE_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/component_export.h"
#include "base/containers/span.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/time/time.h"
#include "device/fido/fido_constants.h"
#include "device/fido/fido_device.h"
#include "device/fido/fido_transport_protocol.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace cbor {
class Value;
}

namespace device {

class MockFidoDevice : public ::testing::StrictMock<FidoDevice> {
 public:
  // MakeU2f returns a fully initialized U2F device. This represents the state
  // after |DiscoverSupportedProtocolAndDeviceInfo| has been called by the
  // FidoDeviceDiscovery.
  static std::unique_ptr<MockFidoDevice> MakeU2f();
  // MakeCtap returns a fully initialized CTAP device. This represents the
  // state after |DiscoverSupportedProtocolAndDeviceInfo| has been called by
  // the FidoDeviceDiscovery.
  static std::unique_ptr<MockFidoDevice> MakeCtap(
      base::Optional<AuthenticatorGetInfoResponse> device_info = base::nullopt);
  // MakeU2fWithDeviceInfoExpectation returns a uninitialized U2F device
  // suitable for injecting into a FidoDeviceDiscovery, which will determine its
  // protocol version by invoking |DiscoverSupportedProtocolAndDeviceInfo|.
  static std::unique_ptr<MockFidoDevice> MakeU2fWithGetInfoExpectation();
  // MakeCtapWithDeviceInfoExpectation returns a uninitialized CTAP device
  // suitable for injecting into a FidoDeviceDiscovery, which will determine its
  // protocol version by invoking |DiscoverSupportedProtocolAndDeviceInfo|. If a
  // response is supplied, the mock will use that to reply; otherwise it will
  // use |test_data::kTestAuthenticatorGetInfoResponse|.
  static std::unique_ptr<MockFidoDevice> MakeCtapWithGetInfoExpectation(
      base::Optional<base::span<const uint8_t>> get_info_response =
          base::nullopt);
  // EncodeCBORRequest is a helper function for use with the |Expect*|
  // functions, below, that take a serialised request.
  static std::vector<uint8_t> EncodeCBORRequest(
      std::pair<CtapRequestCommand, base::Optional<cbor::Value>> request);

  MockFidoDevice();
  MockFidoDevice(ProtocolVersion protocol_version,
                 base::Optional<AuthenticatorGetInfoResponse> device_info);
  ~MockFidoDevice() override;

  // TODO(crbug.com/729950): Remove these workarounds once support for move-only
  // types is added to GMock.
  MOCK_METHOD1(TryWinkRef, void(base::OnceClosure& cb));
  void TryWink(base::OnceClosure cb) override;

  // GMock cannot mock a method taking a move-only type.
  // TODO(crbug.com/729950): Remove these workarounds once support for move-only
  // types is added to GMock.
  MOCK_METHOD2(DeviceTransactPtr,
               CancelToken(const std::vector<uint8_t>& command,
                           DeviceCallback& cb));

  // FidoDevice:
  CancelToken DeviceTransact(std::vector<uint8_t> command,
                             DeviceCallback cb) override;
  MOCK_METHOD1(Cancel, void(FidoDevice::CancelToken));
  MOCK_CONST_METHOD0(GetId, std::string(void));
  MOCK_CONST_METHOD0(GetDisplayName, base::string16(void));
  FidoTransportProtocol DeviceTransport() const override;
  base::WeakPtr<FidoDevice> GetWeakPtr() override;

  void ExpectWinkedAtLeastOnce();
  void ExpectCtap2CommandAndRespondWith(
      CtapRequestCommand command,
      base::Optional<base::span<const uint8_t>> response,
      base::TimeDelta delay = base::TimeDelta(),
      testing::Matcher<base::span<const uint8_t>> request_matcher =
          testing::A<base::span<const uint8_t>>());
  void ExpectCtap2CommandAndRespondWithError(
      CtapRequestCommand command,
      CtapDeviceResponseCode response_code,
      base::TimeDelta delay = base::TimeDelta());
  void ExpectRequestAndRespondWith(
      base::span<const uint8_t> request,
      base::Optional<base::span<const uint8_t>> response,
      base::TimeDelta delay = base::TimeDelta());
  void ExpectCtap2CommandAndDoNotRespond(CtapRequestCommand command);
  void ExpectRequestAndDoNotRespond(base::span<const uint8_t> request);
  void StubGetId();
  void SetDeviceTransport(FidoTransportProtocol transport_protocol);
  void StubGetDisplayName();

 private:
  FidoTransportProtocol transport_protocol_ =
      FidoTransportProtocol::kUsbHumanInterfaceDevice;
  base::WeakPtrFactory<FidoDevice> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(MockFidoDevice);
};

}  // namespace device

#endif  // DEVICE_FIDO_MOCK_FIDO_DEVICE_H_