Commit e8237409 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[DeviceSync vs] Add FakeCryptAuthMetadataSyncer class

Adds fake implementations of CryptAuthMetadataSyncer and
CryptAuthMetadataSyncerImpl::Factory for use in unit tests.

Bug: 951969
Change-Id: I4824dd609edce07767e921f8a4667d03aa9448d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1712605
Commit-Queue: Josh Nohle <nohle@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680103}
parent ffe28625
......@@ -173,6 +173,8 @@ static_library("test_support") {
"fake_cryptauth_key_creator.h",
"fake_cryptauth_key_proof_computer.cc",
"fake_cryptauth_key_proof_computer.h",
"fake_cryptauth_metadata_syncer.cc",
"fake_cryptauth_metadata_syncer.h",
"fake_cryptauth_scheduler.cc",
"fake_cryptauth_scheduler.h",
"fake_cryptauth_v2_enroller.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/services/device_sync/fake_cryptauth_metadata_syncer.h"
namespace chromeos {
namespace device_sync {
FakeCryptAuthMetadataSyncer::FakeCryptAuthMetadataSyncer() = default;
FakeCryptAuthMetadataSyncer::~FakeCryptAuthMetadataSyncer() = default;
void FakeCryptAuthMetadataSyncer::FinishAttempt(
const IdToDeviceMetadataPacketMap& id_to_device_metadata_packet_map,
std::unique_ptr<CryptAuthKey> new_group_key,
const base::Optional<cryptauthv2::EncryptedGroupPrivateKey>&
encrypted_group_private_key,
const CryptAuthDeviceSyncResult& device_sync_result) {
DCHECK(request_context_);
DCHECK(local_device_metadata_);
DCHECK(initial_group_key_);
OnAttemptFinished(id_to_device_metadata_packet_map, std::move(new_group_key),
encrypted_group_private_key, device_sync_result);
}
void FakeCryptAuthMetadataSyncer::OnAttemptStarted(
const cryptauthv2::RequestContext& request_context,
const cryptauthv2::BetterTogetherDeviceMetadata& local_device_metadata,
const CryptAuthKey* initial_group_key) {
request_context_ = request_context;
local_device_metadata_ = local_device_metadata;
initial_group_key_ = initial_group_key;
}
FakeCryptAuthMetadataSyncerFactory::FakeCryptAuthMetadataSyncerFactory() =
default;
FakeCryptAuthMetadataSyncerFactory::~FakeCryptAuthMetadataSyncerFactory() =
default;
std::unique_ptr<CryptAuthMetadataSyncer>
FakeCryptAuthMetadataSyncerFactory::BuildInstance(
CryptAuthClientFactory* client_factory,
std::unique_ptr<base::OneShotTimer> timer) {
last_client_factory_ = client_factory;
auto instance = std::make_unique<FakeCryptAuthMetadataSyncer>();
instances_.push_back(instance.get());
return instance;
}
} // namespace device_sync
} // namespace chromeos
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_SERVICES_DEVICE_SYNC_FAKE_CRYPTAUTH_METADATA_SYNCER_H_
#define CHROMEOS_SERVICES_DEVICE_SYNC_FAKE_CRYPTAUTH_METADATA_SYNCER_H_
#include <memory>
#include <vector>
#include "base/macros.h"
#include "base/optional.h"
#include "base/timer/timer.h"
#include "chromeos/services/device_sync/cryptauth_metadata_syncer.h"
#include "chromeos/services/device_sync/cryptauth_metadata_syncer_impl.h"
#include "chromeos/services/device_sync/proto/cryptauth_better_together_device_metadata.pb.h"
#include "chromeos/services/device_sync/proto/cryptauth_devicesync.pb.h"
namespace chromeos {
namespace device_sync {
class CryptAuthClientFactory;
class CryptAuthDeviceSyncResult;
class CryptAuthKey;
class FakeCryptAuthMetadataSyncer : public CryptAuthMetadataSyncer {
public:
FakeCryptAuthMetadataSyncer();
~FakeCryptAuthMetadataSyncer() override;
// The RequestContext passed to SyncMetadata(). Returns null if
// SyncMetadata() has not been called yet.
const base::Optional<cryptauthv2::RequestContext>& request_context() const {
return request_context_;
}
// The local device's BetterTogetherDeviceMetadata passed to SyncMetadata().
// Returns null if SyncMetadata() has not been called yet.
const base::Optional<cryptauthv2::BetterTogetherDeviceMetadata>&
local_device_metadata() const {
return local_device_metadata_;
}
// The initial group key passed to SyncMetadata(). Returns null if
// SyncMetadata() has not been called yet.
const base::Optional<const CryptAuthKey*>& initial_group_key() const {
return initial_group_key_;
}
void FinishAttempt(
const IdToDeviceMetadataPacketMap& id_to_device_metadata_packet_map,
std::unique_ptr<CryptAuthKey> new_group_key,
const base::Optional<cryptauthv2::EncryptedGroupPrivateKey>&
encrypted_group_private_key,
const CryptAuthDeviceSyncResult& device_sync_result);
private:
// CryptAuthMetadataSyncer:
void OnAttemptStarted(
const cryptauthv2::RequestContext& request_context,
const cryptauthv2::BetterTogetherDeviceMetadata& local_device_metadata,
const CryptAuthKey* initial_group_key) override;
base::Optional<cryptauthv2::RequestContext> request_context_;
base::Optional<cryptauthv2::BetterTogetherDeviceMetadata>
local_device_metadata_;
base::Optional<const CryptAuthKey*> initial_group_key_;
DISALLOW_COPY_AND_ASSIGN(FakeCryptAuthMetadataSyncer);
};
class FakeCryptAuthMetadataSyncerFactory
: public CryptAuthMetadataSyncerImpl::Factory {
public:
FakeCryptAuthMetadataSyncerFactory();
~FakeCryptAuthMetadataSyncerFactory() override;
// Returns a vector of all FakeCryptAuthMetadataSyncer instances created
// by BuildInstance().
const std::vector<FakeCryptAuthMetadataSyncer*>& instances() const {
return instances_;
}
// Returns the most recent CryptAuthClientFactory input into BuildInstance().
const CryptAuthClientFactory* last_client_factory() const {
return last_client_factory_;
}
private:
// CryptAuthMetadataSyncerImpl::Factory:
std::unique_ptr<CryptAuthMetadataSyncer> BuildInstance(
CryptAuthClientFactory* client_factory,
std::unique_ptr<base::OneShotTimer> timer = nullptr) override;
std::vector<FakeCryptAuthMetadataSyncer*> instances_;
CryptAuthClientFactory* last_client_factory_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(FakeCryptAuthMetadataSyncerFactory);
};
} // namespace device_sync
} // namespace chromeos
#endif // CHROMEOS_SERVICES_DEVICE_SYNC_FAKE_CRYPTAUTH_METADATA_SYNCER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment