File: webauthn_credentials_helper.h

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 (246 lines) | stat: -rw-r--r-- 9,235 bytes parent folder | download | duplicates (5)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 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.

#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_WEBAUTHN_CREDENTIALS_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_WEBAUTHN_CREDENTIALS_HELPER_H_

#include <vector>

#include "base/scoped_observation.h"
#include "chrome/browser/sync/test/integration/fake_server_match_status_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/status_change_checker.h"
#include "components/webauthn/core/browser/passkey_model.h"
#include "components/webauthn/core/browser/passkey_model_change.h"
#include "components/webauthn/core/browser/passkey_sync_bridge.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace sync_pb {
class SyncEntity;
class WebauthnCredentialSpecifics;
}  // namespace sync_pb

namespace webauthn_credentials_helper {

inline constexpr char kTestRpId[] = "example.com";

// Checker to wait until the WEBAUTHN_CREDENTIAL datatype becomes active.
class PasskeySyncActiveChecker : public SingleClientStatusChangeChecker {
 public:
  explicit PasskeySyncActiveChecker(syncer::SyncServiceImpl* service);
  ~PasskeySyncActiveChecker() override;

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override;
};

class LocalPasskeysChangedChecker : public StatusChangeChecker,
                                    public webauthn::PasskeyModel::Observer {
 public:
  explicit LocalPasskeysChangedChecker(int profile);
  ~LocalPasskeysChangedChecker() override;

  // SingleClientStatusChangeChecker:
  bool IsExitConditionSatisfied(std::ostream* os) override;

  // webauthn::PasskeyModel::Observer:
  void OnPasskeysChanged(
      const std::vector<webauthn::PasskeyModelChange>& changes) override;
  void OnPasskeyModelShuttingDown() override;
  void OnPasskeyModelIsReady(bool is_ready) override;

 private:
  int profile_;
  bool satisfied_ = false;
  base::ScopedObservation<webauthn::PasskeyModel,
                          webauthn::PasskeyModel::Observer>
      observation_{this};
};

class LocalPasskeysMatchChecker : public StatusChangeChecker,
                                  public webauthn::PasskeyModel::Observer {
 public:
  using Matcher =
      testing::Matcher<std::vector<sync_pb::WebauthnCredentialSpecifics>>;

  LocalPasskeysMatchChecker(int profile, Matcher matcher);
  ~LocalPasskeysMatchChecker() override;

  // SingleClientStatusChangeChecker:
  bool IsExitConditionSatisfied(std::ostream* os) override;

  // webauthn::PasskeyModel::Observer:
  void OnPasskeysChanged(
      const std::vector<webauthn::PasskeyModelChange>& changes) override;
  void OnPasskeyModelShuttingDown() override;
  void OnPasskeyModelIsReady(bool is_ready) override;

 private:
  const int profile_;
  const Matcher matcher_;
  base::ScopedObservation<webauthn::PasskeyModel,
                          webauthn::PasskeyModel::Observer>
      observation_{this};
};

class ServerPasskeysMatchChecker
    : public fake_server::FakeServerMatchStatusChecker {
 public:
  using Matcher = testing::Matcher<std::vector<sync_pb::SyncEntity>>;

  explicit ServerPasskeysMatchChecker(Matcher matcher);
  ~ServerPasskeysMatchChecker() override;

  // FakeServerMatchStatusChecker:
  bool IsExitConditionSatisfied(std::ostream* os) override;

 private:
  const Matcher matcher_;
};

// Observes PasskeyModel changes and waits until the specified list of
// {ChangeType, sync_id} pairs is observed.
class PasskeyChangeObservationChecker
    : public StatusChangeChecker,
      public webauthn::PasskeyModel::Observer {
 public:
  using ChangeList = std::vector<
      std::pair<webauthn::PasskeyModelChange::ChangeType, std::string>>;
  explicit PasskeyChangeObservationChecker(int profile,
                                           ChangeList expected_changes);
  ~PasskeyChangeObservationChecker() override;

  // SingleClientStatusChangeChecker:
  bool IsExitConditionSatisfied(std::ostream* os) override;

  // webauthn::PasskeyModel::Observer:
  void OnPasskeysChanged(
      const std::vector<webauthn::PasskeyModelChange>& changes) override;
  void OnPasskeyModelShuttingDown() override;
  void OnPasskeyModelIsReady(bool is_ready) override;

 private:
  const int profile_;
  std::vector<webauthn::PasskeyModelChange> changes_observed_;
  const ChangeList expected_changes_;
  base::ScopedObservation<webauthn::PasskeyModel,
                          webauthn::PasskeyModel::Observer>
      observation_{this};
};

class MockPasskeyModelObserver : public webauthn::PasskeyModel::Observer {
 public:
  explicit MockPasskeyModelObserver(webauthn::PasskeyModel* model);
  ~MockPasskeyModelObserver() override;

  MOCK_METHOD(void,
              OnPasskeysChanged,
              (const std::vector<webauthn::PasskeyModelChange>&),
              (override));
  MOCK_METHOD(void, OnPasskeyModelShuttingDown, (), (override));
  MOCK_METHOD(void, OnPasskeyModelIsReady, (bool), (override));

 private:
  base::ScopedObservation<webauthn::PasskeyModel,
                          webauthn::PasskeyModel::Observer>
      observation_{this};
};

webauthn::PasskeySyncBridge& GetModel(int profile_idx);

bool AwaitAllModelsMatch();

// Returns a new WebauthnCredentialSpecifics entity with a random sync ID,
// credential ID and user ID, and a fixed RP ID.
sync_pb::WebauthnCredentialSpecifics NewPasskey();

// Returns a new WebauthnCredentialSpecifics entity shadowing another one. Sync
// ID and credential ID are random, while user ID and RP ID will match the other
// credential.
sync_pb::WebauthnCredentialSpecifics NewShadowingPasskey(
    const sync_pb::WebauthnCredentialSpecifics& shadowed);

// Tests that a `sync_pb::SyncEntity` has WebauthnCredentialSpecifics with the
// given `sync_id`. Use with `ServerPasskeysMatchChecker`.
MATCHER_P(EntityHasSyncId, expected_sync_id, "") {
  return arg.specifics().webauthn_credential().sync_id() == expected_sync_id;
}

MATCHER_P(EntityHasUsername, expected_username, "") {
  return arg.specifics().webauthn_credential().user_name() == expected_username;
}

MATCHER_P(EntityHasDisplayName, expected_display_name, "") {
  return arg.specifics().webauthn_credential().user_display_name() ==
         expected_display_name;
}

MATCHER_P(EntityHasHidden, expected_hidden, "") {
  return arg.specifics().webauthn_credential().hidden() == expected_hidden;
}

MATCHER(EntityHasCurrentHiddenTime, "") {
  base::Time hidden_time = base::Time::FromMillisecondsSinceUnixEpoch(
      arg.specifics().webauthn_credential().hidden_time());
  return (base::Time::Now() - hidden_time) < base::Seconds(5);
}

MATCHER_P(EntityHasLastUsedTime, expected_last_used_time, "") {
  return arg.specifics()
             .webauthn_credential()
             .last_used_time_windows_epoch_micros() == expected_last_used_time;
}

// Matches a `sync_pb::WebauthnCredentialSpecifics` against another field by
// field.
MATCHER_P(PasskeySpecificsEq, expected, "") {
  return arg.sync_id() == expected.sync_id() &&
         arg.credential_id() == expected.credential_id() &&
         arg.rp_id() == expected.rp_id() &&
         std::ranges::equal(arg.newly_shadowed_credential_ids().begin(),
                            arg.newly_shadowed_credential_ids().end(),
                            expected.newly_shadowed_credential_ids().begin(),
                            expected.newly_shadowed_credential_ids().end()) &&
         arg.creation_time() == expected.creation_time() &&
         arg.user_name() == expected.user_name() &&
         arg.user_display_name() == expected.user_display_name() &&
         arg.third_party_payments_support() ==
             expected.third_party_payments_support() &&
         arg.last_used_time_windows_epoch_micros() ==
             expected.last_used_time_windows_epoch_micros() &&
         arg.key_version() == expected.key_version() &&
         arg.has_private_key() == expected.has_private_key() &&
         arg.private_key() == expected.private_key() &&
         arg.has_encrypted() == expected.has_encrypted() &&
         arg.encrypted() == expected.encrypted();
}

// Matches the `sync_id` of a `sync_pb::WebauthnCredentialSpecifics`. Use with
// `LocalPasskeysMatchChecker`.
MATCHER_P(PasskeyHasSyncId, expected_sync_id, "") {
  return arg.sync_id() == expected_sync_id;
}

// Matches the `rp_id` of a `sync_pb::WebauthnCredentialSpecifics`. Use with
// `LocalPasskeysMatchChecker`.
MATCHER_P(PasskeyHasRpId, expected_rp_id, "") {
  return arg.rp_id() == expected_rp_id;
}

// Matches the `user_id` of a `sync_pb::WebauthnCredentialSpecifics`. Use with
// `LocalPasskeysMatchChecker`.
MATCHER_P(PasskeyHasUserId, expected_user_id, "") {
  return arg.user_id() == expected_user_id;
}

// Matches the `display_name` of a `sync_pb::WebauthnCredentialSpecifics`. Use
// with `LocalPasskeysMatchChecker`.
MATCHER_P(PasskeyHasDisplayName, expected_display_name, "") {
  return arg.user_display_name() == expected_display_name;
}

}  // namespace webauthn_credentials_helper

#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_WEBAUTHN_CREDENTIALS_HELPER_H_