chromium/chrome/browser/sync/test/integration/webauthn_credentials_helper.h

// 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[] =;

// Checker to wait until the WEBAUTHN_CREDENTIAL datatype becomes active.
class PasskeySyncActiveChecker : public SingleClientStatusChangeChecker {};

class LocalPasskeysChangedChecker : public StatusChangeChecker,
                                    public webauthn::PasskeyModel::Observer {};

class LocalPasskeysMatchChecker : public StatusChangeChecker,
                                  public webauthn::PasskeyModel::Observer {};

class ServerPasskeysMatchChecker
    : public fake_server::FakeServerMatchStatusChecker {};

// Observes PasskeyModel changes and waits until the specified list of
// {ChangeType, sync_id} pairs is observed.
class PasskeyChangeObservationChecker
    : public StatusChangeChecker,
      public webauthn::PasskeyModel::Observer {};

class MockPasskeyModelObserver : public webauthn::PasskeyModel::Observer {};

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, "") {}

MATCHER_P(EntityHasUsername, expected_username, "") {}

MATCHER_P(EntityHasDisplayName, expected_display_name, "") {}

// Matches a `sync_pb::WebauthnCredentialSpecifics` against another field by
// field.
MATCHER_P(PasskeySpecificsEq, expected, "") {}

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

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

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

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

}  // namespace webauthn_credentials_helper

#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_WEBAUTHN_CREDENTIALS_HELPER_H_