File: cable_registration_state_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 4,742 bytes parent folder | download | duplicates (6)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/webauthn/android/cable_registration_state.h"

#include "base/functional/callback.h"
#include "base/logging.h"
#include "device/fido/cable/v2_handshake.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/boringssl/src/include/openssl/ec.h"
#include "third_party/boringssl/src/include/openssl/ec_key.h"
#include "third_party/boringssl/src/include/openssl/obj.h"

using device::cablev2::authenticator::Registration;

namespace webauthn::authenticator {
namespace {

class TestRegistration : public Registration {
 public:
  TestRegistration(
      base::OnceCallback<void()> on_ready,
      base::RepeatingCallback<void(std::unique_ptr<Registration::Event>)>
          event_callback)
      : on_ready_(std::move(on_ready)), on_event_(std::move(event_callback)) {}

  ~TestRegistration() override = default;

  void PrepareContactID() override {}
  void RotateContactID() override {}
  std::optional<std::vector<uint8_t>> contact_id() const override {
    return contact_id_;
  }

  static constexpr std::vector<uint8_t> ContactID() { return {1, 2, 3, 4}; }

  base::OnceCallback<void()> on_ready_;
  const base::RepeatingCallback<void(std::unique_ptr<Registration::Event>)>
      on_event_;
  std::optional<std::vector<uint8_t>> contact_id_ = ContactID();
};

class TestSystemInterface : public RegistrationState::SystemInterface {
 public:
  ~TestSystemInterface() override = default;

  std::unique_ptr<Registration> NewRegistration(
      Registration::Type type,
      base::OnceCallback<void()> on_ready,
      base::RepeatingCallback<void(std::unique_ptr<Registration::Event>)>
          event_callback) override {
    auto registration = std::make_unique<TestRegistration>(
        std::move(on_ready), std::move(event_callback));
    if (type == Registration::Type::LINKING) {
      linking_registration_ = registration.get();
    } else {
      syncing_registration_ = registration.get();
    }
    return registration;
  }

  void CanDeviceSupportCable(base::OnceCallback<void(bool)> callback) override {
    CHECK(!support_callback_);
    support_callback_ = std::move(callback);
  }

  void AmInWorkProfile(base::OnceCallback<void(bool)> callback) override {
    CHECK(!work_profile_callback_);
    work_profile_callback_ = std::move(callback);
  }

  void RefreshLocalDeviceInfo() override {
    refresh_local_device_info_called_ = true;
  }

  void GetPrelinkFromPlayServices(
      base::OnceCallback<void(std::optional<std::vector<uint8_t>>)> callback)
      override {
    CHECK(!prelink_callback_);
    prelink_callback_ = std::move(callback);
  }

  raw_ptr<TestRegistration> linking_registration_ = nullptr;
  raw_ptr<TestRegistration> syncing_registration_ = nullptr;
  bool refresh_local_device_info_called_ = false;

  base::OnceCallback<void(bool)> support_callback_;
  base::OnceCallback<void(bool)> work_profile_callback_;
  base::OnceCallback<void(std::optional<std::vector<uint8_t>>)>
      prelink_callback_;
};

class CableRegistrationStateTest : public testing::Test {
  void SetUp() override {
    auto interface = std::make_unique<TestSystemInterface>();
    interface_ = interface.get();
    state_ = std::make_unique<RegistrationState>(std::move(interface));
  }

 protected:
  static bssl::UniquePtr<EC_KEY> FakeKey() {
    bssl::UniquePtr<EC_GROUP> p256(
        EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
    return bssl::UniquePtr<EC_KEY>(
        EC_KEY_derive_from_secret(p256.get(), nullptr, 0));
  }

  raw_ptr<TestSystemInterface> interface_ = nullptr;
  std::unique_ptr<RegistrationState> state_;
};

TEST_F(CableRegistrationStateTest, HaveDataForSync) {
  state_->Register();
  EXPECT_FALSE(state_->have_data_for_sync());
  std::move(interface_->support_callback_).Run(true);
  EXPECT_FALSE(state_->have_data_for_sync());
  EXPECT_FALSE(interface_->refresh_local_device_info_called_);
  state_->SignalSyncWhenReady();
  EXPECT_FALSE(state_->have_data_for_sync());
  state_->SignalSyncWhenReady();
  EXPECT_FALSE(state_->have_data_for_sync());
  std::move(interface_->prelink_callback_).Run(std::nullopt);
  EXPECT_FALSE(interface_->refresh_local_device_info_called_);
  EXPECT_FALSE(state_->have_data_for_sync());
  std::move(interface_->work_profile_callback_).Run(true);
  EXPECT_TRUE(interface_->refresh_local_device_info_called_);
  EXPECT_TRUE(state_->have_data_for_sync());
  EXPECT_TRUE(state_->device_supports_cable());
  EXPECT_TRUE(state_->am_in_work_profile());
  EXPECT_FALSE(state_->link_data_from_play_services().has_value());
}

}  // namespace

}  // namespace webauthn::authenticator