mock_fido_device.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2017 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. #ifndef DEVICE_FIDO_MOCK_FIDO_DEVICE_H_
  5. #define DEVICE_FIDO_MOCK_FIDO_DEVICE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "base/containers/span.h"
  12. #include "base/time/time.h"
  13. #include "device/fido/fido_constants.h"
  14. #include "device/fido/fido_device.h"
  15. #include "device/fido/fido_transport_protocol.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. namespace cbor {
  19. class Value;
  20. }
  21. namespace device {
  22. class MockFidoDevice : public ::testing::StrictMock<FidoDevice> {
  23. public:
  24. // MakeU2f returns a fully initialized U2F device. This represents the state
  25. // after |DiscoverSupportedProtocolAndDeviceInfo| has been called by the
  26. // FidoDeviceDiscovery.
  27. static std::unique_ptr<MockFidoDevice> MakeU2f();
  28. // MakeCtap returns a fully initialized CTAP device. This represents the
  29. // state after |DiscoverSupportedProtocolAndDeviceInfo| has been called by
  30. // the FidoDeviceDiscovery.
  31. static std::unique_ptr<MockFidoDevice> MakeCtap(
  32. absl::optional<AuthenticatorGetInfoResponse> device_info = absl::nullopt);
  33. // MakeU2fWithDeviceInfoExpectation returns a uninitialized U2F device
  34. // suitable for injecting into a FidoDeviceDiscovery, which will determine its
  35. // protocol version by invoking |DiscoverSupportedProtocolAndDeviceInfo|.
  36. static std::unique_ptr<MockFidoDevice> MakeU2fWithGetInfoExpectation();
  37. // MakeCtapWithDeviceInfoExpectation returns a uninitialized CTAP device
  38. // suitable for injecting into a FidoDeviceDiscovery, which will determine its
  39. // protocol version by invoking |DiscoverSupportedProtocolAndDeviceInfo|. If a
  40. // response is supplied, the mock will use that to reply; otherwise it will
  41. // use |test_data::kTestAuthenticatorGetInfoResponse|.
  42. static std::unique_ptr<MockFidoDevice> MakeCtapWithGetInfoExpectation(
  43. absl::optional<base::span<const uint8_t>> get_info_response =
  44. absl::nullopt);
  45. // EncodeCBORRequest is a helper function for use with the |Expect*|
  46. // functions, below, that take a serialised request.
  47. static std::vector<uint8_t> EncodeCBORRequest(
  48. std::pair<CtapRequestCommand, absl::optional<cbor::Value>> request);
  49. MockFidoDevice();
  50. MockFidoDevice(ProtocolVersion protocol_version,
  51. absl::optional<AuthenticatorGetInfoResponse> device_info);
  52. MockFidoDevice(const MockFidoDevice&) = delete;
  53. MockFidoDevice& operator=(const MockFidoDevice&) = delete;
  54. ~MockFidoDevice() override;
  55. // TODO(crbug.com/729950): Remove these workarounds once support for move-only
  56. // types is added to GMock.
  57. MOCK_METHOD1(TryWinkRef, void(base::OnceClosure& cb));
  58. void TryWink(base::OnceClosure cb) override;
  59. // GMock cannot mock a method taking a move-only type.
  60. // TODO(crbug.com/729950): Remove these workarounds once support for move-only
  61. // types is added to GMock.
  62. MOCK_METHOD2(DeviceTransactPtr,
  63. CancelToken(const std::vector<uint8_t>& command,
  64. DeviceCallback& cb));
  65. // FidoDevice:
  66. CancelToken DeviceTransact(std::vector<uint8_t> command,
  67. DeviceCallback cb) override;
  68. MOCK_METHOD1(Cancel, void(FidoDevice::CancelToken));
  69. MOCK_CONST_METHOD0(GetId, std::string(void));
  70. MOCK_CONST_METHOD0(GetDisplayName, std::string(void));
  71. FidoTransportProtocol DeviceTransport() const override;
  72. base::WeakPtr<FidoDevice> GetWeakPtr() override;
  73. void ExpectWinkedAtLeastOnce();
  74. void ExpectCtap2CommandAndRespondWith(
  75. CtapRequestCommand command,
  76. absl::optional<base::span<const uint8_t>> response,
  77. base::TimeDelta delay = base::TimeDelta(),
  78. testing::Matcher<base::span<const uint8_t>> request_matcher =
  79. testing::A<base::span<const uint8_t>>());
  80. void ExpectCtap2CommandAndRespondWithError(
  81. CtapRequestCommand command,
  82. CtapDeviceResponseCode response_code,
  83. base::TimeDelta delay = base::TimeDelta());
  84. void ExpectRequestAndRespondWith(
  85. base::span<const uint8_t> request,
  86. absl::optional<base::span<const uint8_t>> response,
  87. base::TimeDelta delay = base::TimeDelta());
  88. void ExpectCtap2CommandAndDoNotRespond(CtapRequestCommand command);
  89. void ExpectRequestAndDoNotRespond(base::span<const uint8_t> request);
  90. void StubGetId();
  91. void SetDeviceTransport(FidoTransportProtocol transport_protocol);
  92. void StubGetDisplayName();
  93. private:
  94. FidoTransportProtocol transport_protocol_ =
  95. FidoTransportProtocol::kUsbHumanInterfaceDevice;
  96. base::WeakPtrFactory<FidoDevice> weak_factory_{this};
  97. };
  98. } // namespace device
  99. #endif // DEVICE_FIDO_MOCK_FIDO_DEVICE_H_