v2_test_util.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2020 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_CABLE_V2_TEST_UTIL_H_
  5. #define DEVICE_FIDO_CABLE_V2_TEST_UTIL_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/containers/span.h"
  9. #include "device/fido/cable/v2_authenticator.h"
  10. #include "device/fido/cable/v2_constants.h"
  11. #include "device/fido/cable/v2_discovery.h"
  12. #include "services/network/public/mojom/network_context.mojom-forward.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace device {
  15. class VirtualCtap2Device;
  16. namespace cablev2 {
  17. // ContactCallback is called when a mock tunnel server (see
  18. // |NewMockTunnelServer|) is asked to contact a phone. This simulates a tunnel
  19. // server using a cloud messaging solution to wake a device.
  20. using ContactCallback = base::RepeatingCallback<void(
  21. base::span<const uint8_t, kTunnelIdSize> tunnel_id,
  22. base::span<const uint8_t, kPairingIDSize> pairing_id,
  23. base::span<const uint8_t, kClientNonceSize> client_nonce,
  24. const std::string& request_type_hint)>;
  25. // NewMockTunnelServer returns a |NetworkContext| that implements WebSocket
  26. // requests and simulates a tunnel server. If the given |contact_callback| is
  27. // |nullopt| then all contact requests will be rejected with an HTTP 410 status
  28. // to indicate that the contact ID is disabled.
  29. std::unique_ptr<network::mojom::NetworkContext> NewMockTunnelServer(
  30. absl::optional<ContactCallback> contact_callback);
  31. namespace authenticator {
  32. // Observer is an interface that can be implemented by tests that wish to see
  33. // certain platform events.
  34. class Observer {
  35. public:
  36. // See `Platform::OnStatus`.
  37. virtual void OnStatus(Platform::Status) = 0;
  38. // See `Platform::OnCompleted`.
  39. virtual void OnCompleted(absl::optional<Platform::Error>) = 0;
  40. };
  41. // NewMockPlatform returns a |Platform| that implements the makeCredential
  42. // operation by forwarding it to |ctap2_device|. Transmitted BLE adverts are
  43. // forwarded to |ble_advert_callback|. |observer| may be |nullptr| but, if not,
  44. // then corresponding calls to the mock `Platform` are forwarded to the
  45. // observer.
  46. std::unique_ptr<Platform> NewMockPlatform(
  47. Discovery::AdvertEventStream::Callback ble_advert_callback,
  48. device::VirtualCtap2Device* ctap2_device,
  49. Observer* observer);
  50. // NewLateLinkingDevice returns a caBLEv2 device that fails all CTAP requests
  51. // but sends linking information after the transaction.
  52. std::unique_ptr<Transaction> NewLateLinkingDevice(
  53. CtapDeviceResponseCode ctap_error,
  54. std::unique_ptr<Platform> platform,
  55. network::mojom::NetworkContext* network_context,
  56. base::span<const uint8_t> qr_secret,
  57. base::span<const uint8_t, kP256X962Length> peer_identity);
  58. } // namespace authenticator
  59. } // namespace cablev2
  60. } // namespace device
  61. #endif // DEVICE_FIDO_CABLE_V2_TEST_UTIL_H_