Commit 651418c8 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[DeviceSync v2] Add FakeCryptAuthV2DeviceManager class

This is an implementation of CryptAuthV2DeviceManager used for tests.
It queues DeviceSync requests made via ForceDeviceSyncNow(). These
requests are sequentially processed by calls to
FinishNextForcedDeviceSync(), which also updates parameters such as
the last DeviceSync time.

Bug: 951969
Change-Id: I964425fcab12a25d58082f882d344146f324bacf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1874196Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Josh Nohle <nohle@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708677}
parent 733e377c
......@@ -194,6 +194,8 @@ static_library("test_support") {
"fake_cryptauth_metadata_syncer.h",
"fake_cryptauth_scheduler.cc",
"fake_cryptauth_scheduler.h",
"fake_cryptauth_v2_device_manager.cc",
"fake_cryptauth_v2_device_manager.h",
"fake_cryptauth_v2_enroller.cc",
"fake_cryptauth_v2_enroller.h",
"fake_device_sync.cc",
......
......@@ -56,7 +56,7 @@ class CryptAuthV2DeviceManager {
// by a GCM message or if no session ID was included in the GCM
// message.
virtual void ForceDeviceSyncNow(
const cryptauthv2::ClientMetadata::InvocationReason&,
const cryptauthv2::ClientMetadata::InvocationReason& invocation_reason,
const base::Optional<std::string>& session_id) = 0;
// Returns true if a v2 DeviceSync attempt is currently in progress.
......
// 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_v2_device_manager.h"
namespace chromeos {
namespace device_sync {
FakeCryptAuthV2DeviceManager::FakeCryptAuthV2DeviceManager() = default;
FakeCryptAuthV2DeviceManager::~FakeCryptAuthV2DeviceManager() = default;
void FakeCryptAuthV2DeviceManager::Start() {
has_started_ = true;
}
const CryptAuthDeviceRegistry::InstanceIdToDeviceMap&
FakeCryptAuthV2DeviceManager::GetSyncedDevices() const {
return synced_devices_;
}
void FakeCryptAuthV2DeviceManager::ForceDeviceSyncNow(
const cryptauthv2::ClientMetadata::InvocationReason& invocation_reason,
const base::Optional<std::string>& session_id) {
DCHECK(has_started_);
cryptauthv2::ClientMetadata client_metadata;
client_metadata.set_invocation_reason(invocation_reason);
if (session_id)
client_metadata.set_session_id(*session_id);
force_device_sync_now_requests_.push(client_metadata);
NotifyDeviceSyncStarted(client_metadata);
}
bool FakeCryptAuthV2DeviceManager::IsDeviceSyncInProgress() const {
return !force_device_sync_now_requests_.empty();
}
bool FakeCryptAuthV2DeviceManager::IsRecoveringFromFailure() const {
return is_recovering_from_failure_;
}
base::Optional<base::Time> FakeCryptAuthV2DeviceManager::GetLastDeviceSyncTime()
const {
return last_device_sync_time_;
}
base::Optional<base::TimeDelta>
FakeCryptAuthV2DeviceManager::GetTimeToNextAttempt() const {
return time_to_next_attempt_;
}
void FakeCryptAuthV2DeviceManager::FinishNextForcedDeviceSync(
const CryptAuthDeviceSyncResult& device_sync_result,
base::Time device_sync_finish_time) {
DCHECK(IsDeviceSyncInProgress());
if (device_sync_result.IsSuccess()) {
last_device_sync_time_ = device_sync_finish_time;
is_recovering_from_failure_ = false;
} else {
is_recovering_from_failure_ = true;
}
force_device_sync_now_requests_.pop();
NotifyDeviceSyncFinished(device_sync_result);
}
} // 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_V2_DEVICE_MANAGER_H_
#define CHROMEOS_SERVICES_DEVICE_SYNC_FAKE_CRYPTAUTH_V2_DEVICE_MANAGER_H_
#include <string>
#include "base/containers/queue.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/time/time.h"
#include "chromeos/services/device_sync/cryptauth_device.h"
#include "chromeos/services/device_sync/cryptauth_device_registry.h"
#include "chromeos/services/device_sync/cryptauth_device_sync_result.h"
#include "chromeos/services/device_sync/cryptauth_v2_device_manager.h"
#include "chromeos/services/device_sync/proto/cryptauth_common.pb.h"
namespace chromeos {
namespace device_sync {
// An implementation of CryptAuthV2DeviceManager used for tests. This
// implementation queues DeviceSync requests made via ForceDeviceSyncNow().
// These requests are sequentially processed by calls to
// FinishNextForcedDeviceSync(), which also updates parameters such as the last
// DeviceSync time.
class FakeCryptAuthV2DeviceManager : public CryptAuthV2DeviceManager {
public:
FakeCryptAuthV2DeviceManager();
~FakeCryptAuthV2DeviceManager() override;
// CryptAuthV2DeviceManager:
void Start() override;
const CryptAuthDeviceRegistry::InstanceIdToDeviceMap& GetSyncedDevices()
const override;
void ForceDeviceSyncNow(
const cryptauthv2::ClientMetadata::InvocationReason& invocation_reason,
const base::Optional<std::string>& session_id) override;
bool IsDeviceSyncInProgress() const override;
bool IsRecoveringFromFailure() const override;
base::Optional<base::Time> GetLastDeviceSyncTime() const override;
base::Optional<base::TimeDelta> GetTimeToNextAttempt() const override;
bool has_started() const { return has_started_; }
const base::queue<cryptauthv2::ClientMetadata>&
force_device_sync_now_requests() const {
return force_device_sync_now_requests_;
}
void set_synced_devices(
const CryptAuthDeviceRegistry::InstanceIdToDeviceMap& synced_devices) {
synced_devices_ = synced_devices;
}
void set_time_to_next_attempt(
const base::Optional<base::TimeDelta>& time_to_next_attempt) {
time_to_next_attempt_ = time_to_next_attempt;
}
// Finishes the next forced DeviceSync request in the queue. Should only be
// called if the queue of requests if not empty. If |device_sync_result|
// indicates success, |device_sync_finish_time| will be stored as the last
// DeviceSync time and will be returned by future calls to
// GetLastDeviceSyncTime().
void FinishNextForcedDeviceSync(
const CryptAuthDeviceSyncResult& device_sync_result,
base::Time device_sync_finish_time);
private:
bool has_started_ = false;
bool is_recovering_from_failure_ = false;
base::Optional<base::Time> last_device_sync_time_;
base::Optional<base::TimeDelta> time_to_next_attempt_;
CryptAuthDeviceRegistry::InstanceIdToDeviceMap synced_devices_;
base::queue<cryptauthv2::ClientMetadata> force_device_sync_now_requests_;
DISALLOW_COPY_AND_ASSIGN(FakeCryptAuthV2DeviceManager);
};
} // namespace device_sync
} // namespace chromeos
#endif // CHROMEOS_SERVICES_DEVICE_SYNC_FAKE_CRYPTAUTH_V2_DEVICE_MANAGER_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