Commit 3cb4b2b0 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Refactor CryptAuthDeviceManager.

Now, it is a pure virtual class with a test double. This is in
preparation for using it in the MultiDevice service.

Bug: 752273
Change-Id: I01f79a0361ec71e51a30dc3a56b28555dc594712
Reviewed-on: https://chromium-review.googlesource.com/910118Reviewed-by: default avatarRamin Halavati <rhalavati@chromium.org>
Reviewed-by: default avatarTim Song <tengs@chromium.org>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536750}
parent 95c4334c
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/cryptauth/cryptauth_client.h" #include "components/cryptauth/cryptauth_client.h"
#include "components/cryptauth/cryptauth_client_impl.h" #include "components/cryptauth/cryptauth_client_impl.h"
#include "components/cryptauth/cryptauth_device_manager.h" #include "components/cryptauth/cryptauth_device_manager_impl.h"
#include "components/cryptauth/cryptauth_enroller.h" #include "components/cryptauth/cryptauth_enroller.h"
#include "components/cryptauth/cryptauth_enroller_impl.h" #include "components/cryptauth/cryptauth_enroller_impl.h"
#include "components/cryptauth/cryptauth_enrollment_utils.h" #include "components/cryptauth/cryptauth_enrollment_utils.h"
...@@ -172,7 +172,7 @@ std::unique_ptr<ChromeCryptAuthService> ChromeCryptAuthService::Create( ...@@ -172,7 +172,7 @@ std::unique_ptr<ChromeCryptAuthService> ChromeCryptAuthService::Create(
profile->GetPrefs()); profile->GetPrefs());
std::unique_ptr<cryptauth::CryptAuthDeviceManager> device_manager = std::unique_ptr<cryptauth::CryptAuthDeviceManager> device_manager =
base::MakeUnique<cryptauth::CryptAuthDeviceManager>( base::MakeUnique<cryptauth::CryptAuthDeviceManagerImpl>(
base::DefaultClock::GetInstance(), base::DefaultClock::GetInstance(),
CreateCryptAuthClientFactoryImpl(profile), gcm_manager.get(), CreateCryptAuthClientFactoryImpl(profile), gcm_manager.get(),
profile->GetPrefs()); profile->GetPrefs());
......
...@@ -24,6 +24,8 @@ static_library("cryptauth") { ...@@ -24,6 +24,8 @@ static_library("cryptauth") {
"cryptauth_client_impl.h", "cryptauth_client_impl.h",
"cryptauth_device_manager.cc", "cryptauth_device_manager.cc",
"cryptauth_device_manager.h", "cryptauth_device_manager.h",
"cryptauth_device_manager_impl.cc",
"cryptauth_device_manager_impl.h",
"cryptauth_enroller.h", "cryptauth_enroller.h",
"cryptauth_enroller_impl.cc", "cryptauth_enroller_impl.cc",
"cryptauth_enroller_impl.h", "cryptauth_enroller_impl.h",
...@@ -116,6 +118,8 @@ static_library("test_support") { ...@@ -116,6 +118,8 @@ static_library("test_support") {
"fake_authenticator.h", "fake_authenticator.h",
"fake_connection.cc", "fake_connection.cc",
"fake_connection.h", "fake_connection.h",
"fake_cryptauth_device_manager.cc",
"fake_cryptauth_device_manager.h",
"fake_cryptauth_gcm_manager.cc", "fake_cryptauth_gcm_manager.cc",
"fake_cryptauth_gcm_manager.h", "fake_cryptauth_gcm_manager.h",
"fake_cryptauth_service.cc", "fake_cryptauth_service.cc",
...@@ -164,7 +168,7 @@ source_set("unit_tests") { ...@@ -164,7 +168,7 @@ source_set("unit_tests") {
"cryptauth_access_token_fetcher_impl_unittest.cc", "cryptauth_access_token_fetcher_impl_unittest.cc",
"cryptauth_api_call_flow_unittest.cc", "cryptauth_api_call_flow_unittest.cc",
"cryptauth_client_impl_unittest.cc", "cryptauth_client_impl_unittest.cc",
"cryptauth_device_manager_unittest.cc", "cryptauth_device_manager_impl_unittest.cc",
"cryptauth_enroller_impl_unittest.cc", "cryptauth_enroller_impl_unittest.cc",
"cryptauth_enrollment_manager_unittest.cc", "cryptauth_enrollment_manager_unittest.cc",
"cryptauth_gcm_manager_impl_unittest.cc", "cryptauth_gcm_manager_impl_unittest.cc",
......
...@@ -8,31 +8,24 @@ ...@@ -8,31 +8,24 @@
#include <memory> #include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "components/cryptauth/cryptauth_gcm_manager.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h" #include "components/cryptauth/proto/cryptauth_api.pb.h"
#include "components/cryptauth/sync_scheduler.h"
class PrefService;
class PrefRegistrySimple; class PrefRegistrySimple;
namespace cryptauth { namespace cryptauth {
class CryptAuthClient; // Manages syncing and storing the user's devices that are registered with
class CryptAuthClientFactory; // CryptAuth.
//
// This class manages syncing and storing the user's phones that are registered // CryptAuthDeviceManager periodically syncs the user's devices from CryptAuth
// with CryptAuth and are capable of unlocking the user's other devices. These // to keep the list of unlock keys fresh. If a sync attempts fails, the manager
// phones are called "unlock keys". // will schedule the next sync more aggressively to recover.
// The manager periodically syncs the user's devices from CryptAuth to keep the class CryptAuthDeviceManager {
// list of unlock keys fresh. If a sync attempts fails, the manager will
// schedule the next sync more aggressively to recover.
class CryptAuthDeviceManager : public SyncScheduler::Delegate,
public CryptAuthGCMManager::Observer {
public: public:
// Registers the prefs used by this class to the given |registry|.
static void RegisterPrefs(PrefRegistrySimple* registry);
// Respresents the success result of a sync attempt. // Respresents the success result of a sync attempt.
enum class SyncResult { SUCCESS, FAILURE }; enum class SyncResult { SUCCESS, FAILURE };
...@@ -54,77 +47,53 @@ class CryptAuthDeviceManager : public SyncScheduler::Delegate, ...@@ -54,77 +47,53 @@ class CryptAuthDeviceManager : public SyncScheduler::Delegate,
virtual void OnSyncFinished(SyncResult sync_result, virtual void OnSyncFinished(SyncResult sync_result,
DeviceChangeResult device_change_result) {} DeviceChangeResult device_change_result) {}
virtual ~Observer() {} virtual ~Observer() = default;
}; };
// Creates the manager: CryptAuthDeviceManager();
// |clock|: Used to determine the time between sync attempts. virtual ~CryptAuthDeviceManager();
// |client_factory|: Creates CryptAuthClient instances to perform each sync.
// |gcm_manager|: Notifies when GCM push messages trigger device syncs.
// Not owned and must outlive this instance.
// |pref_service|: Stores syncing metadata and unlock key information to
// persist across browser restarts. Must already be registered
// with RegisterPrefs().
CryptAuthDeviceManager(base::Clock* clock,
std::unique_ptr<CryptAuthClientFactory> client_factory,
CryptAuthGCMManager* gcm_manager,
PrefService* pref_service);
~CryptAuthDeviceManager() override;
// Registers the prefs used by this class to the given |registry|.
static void RegisterPrefs(PrefRegistrySimple* registry);
// Starts device manager to begin syncing devices.
void Start();
// Adds an observer.
void AddObserver(Observer* observer); void AddObserver(Observer* observer);
// Removes an observer.
void RemoveObserver(Observer* observer); void RemoveObserver(Observer* observer);
// Starts device manager to begin syncing devices.
virtual void Start() = 0;
// Skips the waiting period and forces a sync immediately. If a // Skips the waiting period and forces a sync immediately. If a
// sync attempt is already in progress, this function does nothing. // sync attempt is already in progress, this function does nothing.
// |invocation_reason| specifies the reason that the sync was triggered, // |invocation_reason| specifies the reason that the sync was triggered,
// which is upload to the server. // which is upload to the server.
void ForceSyncNow(InvocationReason invocation_reason); virtual void ForceSyncNow(InvocationReason invocation_reason) = 0;
// Returns the timestamp of the last successful sync. If no sync // Returns the timestamp of the last successful sync. If no sync
// has ever been made, then returns a null base::Time object. // has ever been made, then returns a null base::Time object.
base::Time GetLastSyncTime() const; virtual base::Time GetLastSyncTime() const = 0;
// Returns the time to the next sync attempt. // Returns the time to the next sync attempt.
base::TimeDelta GetTimeToNextAttempt() const; virtual base::TimeDelta GetTimeToNextAttempt() const = 0;
// Returns true if a device sync attempt is currently in progress. // Returns true if a device sync attempt is currently in progress.
bool IsSyncInProgress() const; virtual bool IsSyncInProgress() const = 0;
// Returns true if the last device sync failed and the manager is now // Returns true if the last device sync failed and the manager is now
// scheduling sync attempts more aggressively to recover. If no enrollment // scheduling sync attempts more aggressively to recover. If no enrollment
// has ever been recorded, then this function will also return true. // has ever been recorded, then this function will also return true.
bool IsRecoveringFromFailure() const; virtual bool IsRecoveringFromFailure() const = 0;
// Returns a list of all remote devices that have been synced. // Returns a list of all remote devices that have been synced.
virtual std::vector<ExternalDeviceInfo> GetSyncedDevices() const; virtual std::vector<ExternalDeviceInfo> GetSyncedDevices() const = 0;
// Returns a list of remote devices that can unlock the user's other devices. // Returns a list of remote devices that can unlock the user's other devices.
virtual std::vector<ExternalDeviceInfo> GetUnlockKeys() const; virtual std::vector<ExternalDeviceInfo> GetUnlockKeys() const = 0;
// Like GetUnlockKeys(), but only returns Pixel devices. // Like GetUnlockKeys(), but only returns Pixel devices.
virtual std::vector<ExternalDeviceInfo> GetPixelUnlockKeys() const; virtual std::vector<ExternalDeviceInfo> GetPixelUnlockKeys() const = 0;
// Returns a list of remote devices that can host tether hotspots. // Returns a list of remote devices that can host tether hotspots.
virtual std::vector<ExternalDeviceInfo> GetTetherHosts() const; virtual std::vector<ExternalDeviceInfo> GetTetherHosts() const = 0;
// Like GetTetherHosts(), but only returns Pixel devices. // Like GetTetherHosts(), but only returns Pixel devices.
virtual std::vector<ExternalDeviceInfo> GetPixelTetherHosts() const; virtual std::vector<ExternalDeviceInfo> GetPixelTetherHosts() const = 0;
protected: protected:
// Empty constructor, to be used by tests to mock the device manager. Do not
// use this constructor outside of tests.
CryptAuthDeviceManager();
void SetSyncSchedulerForTest(std::unique_ptr<SyncScheduler> sync_scheduler);
// Invokes OnSyncStarted() on all observers. // Invokes OnSyncStarted() on all observers.
void NotifySyncStarted(); void NotifySyncStarted();
...@@ -134,54 +103,8 @@ class CryptAuthDeviceManager : public SyncScheduler::Delegate, ...@@ -134,54 +103,8 @@ class CryptAuthDeviceManager : public SyncScheduler::Delegate,
DeviceChangeResult device_change_result); DeviceChangeResult device_change_result);
private: private:
// CryptAuthGCMManager::Observer:
void OnResyncMessage() override;
// Updates |unlock_keys_| by fetching the list stored in |pref_service_|.
void UpdateUnlockKeysFromPrefs();
// SyncScheduler::Delegate:
void OnSyncRequested(
std::unique_ptr<SyncScheduler::SyncRequest> sync_request) override;
// Callback when |cryptauth_client_| completes with the response.
void OnGetMyDevicesSuccess(const GetMyDevicesResponse& response);
void OnGetMyDevicesFailure(const std::string& error);
// Used to determine the time.
base::Clock* clock_;
// Creates CryptAuthClient instances for each sync attempt.
std::unique_ptr<CryptAuthClientFactory> client_factory_;
// Notifies when GCM push messages trigger device sync. Not owned and must
// outlive this instance.
CryptAuthGCMManager* gcm_manager_;
// Contains preferences that outlive the lifetime of this object and across
// process restarts. |pref_service_| must outlive the lifetime of this
// instance.
PrefService* const pref_service_;
// All devices currently synced from CryptAuth.
std::vector<ExternalDeviceInfo> synced_devices_;
// Schedules the time between device sync attempts.
std::unique_ptr<SyncScheduler> scheduler_;
// Contains the SyncRequest that |scheduler_| requests when a device sync
// attempt is made.
std::unique_ptr<SyncScheduler::SyncRequest> sync_request_;
// The CryptAuthEnroller instance for the current sync attempt. A new
// instance will be created for each individual attempt.
std::unique_ptr<CryptAuthClient> cryptauth_client_;
// List of observers.
base::ObserverList<Observer> observers_; base::ObserverList<Observer> observers_;
base::WeakPtrFactory<CryptAuthDeviceManager> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManager); DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManager);
}; };
......
This diff is collapsed.
// Copyright 2018 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 COMPONENTS_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_IMPL_H_
#define COMPONENTS_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_IMPL_H_
#include <memory>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "components/cryptauth/cryptauth_device_manager.h"
#include "components/cryptauth/cryptauth_gcm_manager.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h"
#include "components/cryptauth/sync_scheduler.h"
class PrefService;
namespace cryptauth {
class CryptAuthClient;
class CryptAuthClientFactory;
class CryptAuthDeviceManagerImpl : public CryptAuthDeviceManager,
public SyncScheduler::Delegate,
public CryptAuthGCMManager::Observer {
public:
// Creates the manager:
// |clock|: Used to determine the time between sync attempts.
// |client_factory|: Creates CryptAuthClient instances to perform each sync.
// |gcm_manager|: Notifies when GCM push messages trigger device syncs.
// Not owned and must outlive this instance.
// |pref_service|: Stores syncing metadata and unlock key information to
// persist across browser restarts. Must already be registered
// with RegisterPrefs().
CryptAuthDeviceManagerImpl(
base::Clock* clock,
std::unique_ptr<CryptAuthClientFactory> client_factory,
CryptAuthGCMManager* gcm_manager,
PrefService* pref_service);
~CryptAuthDeviceManagerImpl() override;
// CryptAuthDeviceManager:
void Start() override;
void ForceSyncNow(InvocationReason invocation_reason) override;
base::Time GetLastSyncTime() const override;
base::TimeDelta GetTimeToNextAttempt() const override;
bool IsSyncInProgress() const override;
bool IsRecoveringFromFailure() const override;
std::vector<ExternalDeviceInfo> GetSyncedDevices() const override;
std::vector<ExternalDeviceInfo> GetUnlockKeys() const override;
std::vector<ExternalDeviceInfo> GetPixelUnlockKeys() const override;
std::vector<ExternalDeviceInfo> GetTetherHosts() const override;
std::vector<ExternalDeviceInfo> GetPixelTetherHosts() const override;
protected:
void SetSyncSchedulerForTest(std::unique_ptr<SyncScheduler> sync_scheduler);
private:
// CryptAuthGCMManager::Observer:
void OnResyncMessage() override;
// Updates |unlock_keys_| by fetching the list stored in |pref_service_|.
void UpdateUnlockKeysFromPrefs();
// SyncScheduler::Delegate:
void OnSyncRequested(
std::unique_ptr<SyncScheduler::SyncRequest> sync_request) override;
// Callback when |cryptauth_client_| completes with the response.
void OnGetMyDevicesSuccess(const GetMyDevicesResponse& response);
void OnGetMyDevicesFailure(const std::string& error);
// Used to determine the time.
base::Clock* clock_;
// Creates CryptAuthClient instances for each sync attempt.
std::unique_ptr<CryptAuthClientFactory> client_factory_;
// Notifies when GCM push messages trigger device sync. Not owned and must
// outlive this instance.
CryptAuthGCMManager* gcm_manager_;
// Contains preferences that outlive the lifetime of this object and across
// process restarts. |pref_service_| must outlive the lifetime of this
// instance.
PrefService* const pref_service_;
// All devices currently synced from CryptAuth.
std::vector<ExternalDeviceInfo> synced_devices_;
// Schedules the time between device sync attempts.
std::unique_ptr<SyncScheduler> scheduler_;
// Contains the SyncRequest that |scheduler_| requests when a device sync
// attempt is made.
std::unique_ptr<SyncScheduler::SyncRequest> sync_request_;
// The CryptAuthEnroller instance for the current sync attempt. A new
// instance will be created for each individual attempt.
std::unique_ptr<CryptAuthClient> cryptauth_client_;
base::WeakPtrFactory<CryptAuthDeviceManagerImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManagerImpl);
};
} // namespace cryptauth
#endif // COMPONENTS_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_IMPL_H_
// Copyright 2018 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 "components/cryptauth/fake_cryptauth_device_manager.h"
#include <memory>
namespace cryptauth {
FakeCryptAuthDeviceManager::FakeCryptAuthDeviceManager() = default;
FakeCryptAuthDeviceManager::~FakeCryptAuthDeviceManager() = default;
void FakeCryptAuthDeviceManager::FinishActiveSync(
SyncResult sync_result,
DeviceChangeResult device_change_result,
const base::Time& sync_finish_time) {
DCHECK(is_sync_in_progress_);
is_sync_in_progress_ = false;
if (sync_result == CryptAuthDeviceManager::SyncResult::SUCCESS) {
last_sync_time_ = sync_finish_time;
is_recovering_from_failure_ = false;
} else {
is_recovering_from_failure_ = true;
}
NotifySyncFinished(sync_result, device_change_result);
}
void FakeCryptAuthDeviceManager::Start() {
has_started_ = true;
}
void FakeCryptAuthDeviceManager::ForceSyncNow(
InvocationReason invocation_reason) {
is_sync_in_progress_ = true;
NotifySyncStarted();
}
base::Time FakeCryptAuthDeviceManager::GetLastSyncTime() const {
return last_sync_time_;
}
base::TimeDelta FakeCryptAuthDeviceManager::GetTimeToNextAttempt() const {
return time_to_next_attempt_;
}
bool FakeCryptAuthDeviceManager::IsSyncInProgress() const {
return is_sync_in_progress_;
}
bool FakeCryptAuthDeviceManager::IsRecoveringFromFailure() const {
return is_recovering_from_failure_;
}
std::vector<ExternalDeviceInfo> FakeCryptAuthDeviceManager::GetSyncedDevices()
const {
return synced_devices_;
}
std::vector<ExternalDeviceInfo> FakeCryptAuthDeviceManager::GetUnlockKeys()
const {
return unlock_keys_;
}
std::vector<ExternalDeviceInfo> FakeCryptAuthDeviceManager::GetPixelUnlockKeys()
const {
return pixel_unlock_keys_;
}
std::vector<ExternalDeviceInfo> FakeCryptAuthDeviceManager::GetTetherHosts()
const {
return tether_hosts_;
}
std::vector<ExternalDeviceInfo>
FakeCryptAuthDeviceManager::GetPixelTetherHosts() const {
return pixel_tether_hosts_;
}
} // namespace cryptauth
// Copyright 2018 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 COMPONENTS_CRYPTAUTH_FAKE_CRYPTAUTH_DEVICE_MANAGER_H_
#define COMPONENTS_CRYPTAUTH_FAKE_CRYPTAUTH_DEVICE_MANAGER_H_
#include <memory>
#include "base/macros.h"
#include "components/cryptauth/cryptauth_device_manager.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h"
namespace cryptauth {
// Test double for CryptAuthDeviceManager.
class FakeCryptAuthDeviceManager : public CryptAuthDeviceManager {
public:
FakeCryptAuthDeviceManager();
~FakeCryptAuthDeviceManager() override;
bool has_started() { return has_started_; }
void set_time_to_next_attempt(base::TimeDelta time_to_next_attempt) {
time_to_next_attempt_ = time_to_next_attempt;
}
std::vector<ExternalDeviceInfo>& synced_devices() { return synced_devices_; }
void set_synced_devices(std::vector<ExternalDeviceInfo> synced_devices) {
synced_devices_ = synced_devices;
}
std::vector<ExternalDeviceInfo>& unlock_keys() { return unlock_keys_; }
void set_unlock_keys(std::vector<ExternalDeviceInfo> unlock_keys) {
unlock_keys_ = unlock_keys;
}
std::vector<ExternalDeviceInfo>& pixel_unlock_keys() {
return pixel_unlock_keys_;
}
void set_pixel_unlock_keys(
std::vector<ExternalDeviceInfo> pixel_unlock_keys) {
pixel_unlock_keys_ = pixel_unlock_keys;
}
std::vector<ExternalDeviceInfo>& tether_hosts() { return tether_hosts_; }
void set_tether_hosts(std::vector<ExternalDeviceInfo> tether_hosts) {
tether_hosts_ = tether_hosts;
}
std::vector<ExternalDeviceInfo>& pixel_tether_hosts() {
return pixel_tether_hosts_;
}
void set_pixel_tether_hosts(
std::vector<ExternalDeviceInfo> pixel_tether_hosts) {
pixel_tether_hosts_ = pixel_tether_hosts;
}
// Finishes the active sync; should only be called if a sync is in progress
// due to a previous call to ForceSyncNow(). If |sync_result| is SUCCESS,
// |sync_finish_time| will be stored as the last sync time and will be
// returned by future calls to GetLastSyncTime().
void FinishActiveSync(SyncResult sync_result,
DeviceChangeResult device_change_result,
const base::Time& sync_finish_time = base::Time());
// Make these functions public for testing.
using CryptAuthDeviceManager::NotifySyncStarted;
using CryptAuthDeviceManager::NotifySyncFinished;
// CryptAuthDeviceManager:
void Start() override;
void ForceSyncNow(InvocationReason invocation_reason) override;
base::Time GetLastSyncTime() const override;
base::TimeDelta GetTimeToNextAttempt() const override;
bool IsSyncInProgress() const override;
bool IsRecoveringFromFailure() const override;
std::vector<ExternalDeviceInfo> GetSyncedDevices() const override;
std::vector<ExternalDeviceInfo> GetUnlockKeys() const override;
std::vector<ExternalDeviceInfo> GetPixelUnlockKeys() const override;
std::vector<ExternalDeviceInfo> GetTetherHosts() const override;
std::vector<ExternalDeviceInfo> GetPixelTetherHosts() const override;
private:
bool has_started_ = false;
bool is_sync_in_progress_ = false;
bool is_recovering_from_failure_ = false;
base::Time last_sync_time_;
base::TimeDelta time_to_next_attempt_;
std::vector<ExternalDeviceInfo> synced_devices_;
std::vector<ExternalDeviceInfo> unlock_keys_;
std::vector<ExternalDeviceInfo> pixel_unlock_keys_;
std::vector<ExternalDeviceInfo> tether_hosts_;
std::vector<ExternalDeviceInfo> pixel_tether_hosts_;
DISALLOW_COPY_AND_ASSIGN(FakeCryptAuthDeviceManager);
};
} // namespace cryptauth
#endif // COMPONENTS_CRYPTAUTH_FAKE_CRYPTAUTH_DEVICE_MANAGER_H_
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
#include <vector> #include <vector>
#include "base/logging.h" #include "base/logging.h"
#include "components/cryptauth/cryptauth_device_manager.h"
#include "components/cryptauth/cryptauth_enroller.h" #include "components/cryptauth/cryptauth_enroller.h"
#include "components/cryptauth/cryptauth_enrollment_manager.h" #include "components/cryptauth/cryptauth_enrollment_manager.h"
#include "components/cryptauth/fake_cryptauth_device_manager.h"
#include "components/cryptauth/fake_cryptauth_gcm_manager.h" #include "components/cryptauth/fake_cryptauth_gcm_manager.h"
#include "components/cryptauth/fake_cryptauth_service.h" #include "components/cryptauth/fake_cryptauth_service.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h" #include "components/cryptauth/proto/cryptauth_api.pb.h"
...@@ -36,14 +36,6 @@ const char kBeaconSeed2Data[] = "beaconSeed2Data"; ...@@ -36,14 +36,6 @@ const char kBeaconSeed2Data[] = "beaconSeed2Data";
const int64_t kBeaconSeed2StartMs = 2000L; const int64_t kBeaconSeed2StartMs = 2000L;
const int64_t kBeaconSeed2EndMs = 3000L; const int64_t kBeaconSeed2EndMs = 3000L;
class MockCryptAuthDeviceManager : public CryptAuthDeviceManager {
public:
MockCryptAuthDeviceManager() {}
~MockCryptAuthDeviceManager() override {}
MOCK_CONST_METHOD0(GetSyncedDevices, std::vector<ExternalDeviceInfo>());
};
class MockCryptAuthEnrollmentManager : public CryptAuthEnrollmentManager { class MockCryptAuthEnrollmentManager : public CryptAuthEnrollmentManager {
public: public:
explicit MockCryptAuthEnrollmentManager( explicit MockCryptAuthEnrollmentManager(
...@@ -121,8 +113,7 @@ class LocalDeviceDataProviderTest : public testing::Test { ...@@ -121,8 +113,7 @@ class LocalDeviceDataProviderTest : public testing::Test {
} }
void SetUp() override { void SetUp() override {
mock_device_manager_ = fake_device_manager_ = std::make_unique<FakeCryptAuthDeviceManager>();
base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>());
fake_cryptauth_gcm_manager_ = fake_cryptauth_gcm_manager_ =
std::make_unique<FakeCryptAuthGCMManager>("registrationId"); std::make_unique<FakeCryptAuthGCMManager>("registrationId");
mock_enrollment_manager_ = mock_enrollment_manager_ =
...@@ -131,7 +122,7 @@ class LocalDeviceDataProviderTest : public testing::Test { ...@@ -131,7 +122,7 @@ class LocalDeviceDataProviderTest : public testing::Test {
fake_cryptauth_service_ = std::make_unique<FakeCryptAuthService>(); fake_cryptauth_service_ = std::make_unique<FakeCryptAuthService>();
fake_cryptauth_service_->set_cryptauth_device_manager( fake_cryptauth_service_->set_cryptauth_device_manager(
mock_device_manager_.get()); fake_device_manager_.get());
fake_cryptauth_service_->set_cryptauth_enrollment_manager( fake_cryptauth_service_->set_cryptauth_enrollment_manager(
mock_enrollment_manager_.get()); mock_enrollment_manager_.get());
...@@ -143,7 +134,7 @@ class LocalDeviceDataProviderTest : public testing::Test { ...@@ -143,7 +134,7 @@ class LocalDeviceDataProviderTest : public testing::Test {
std::vector<ExternalDeviceInfo> fake_synced_devices_; std::vector<ExternalDeviceInfo> fake_synced_devices_;
std::unique_ptr<FakeCryptAuthGCMManager> fake_cryptauth_gcm_manager_; std::unique_ptr<FakeCryptAuthGCMManager> fake_cryptauth_gcm_manager_;
std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager_; std::unique_ptr<FakeCryptAuthDeviceManager> fake_device_manager_;
std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>> std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>>
mock_enrollment_manager_; mock_enrollment_manager_;
std::unique_ptr<FakeCryptAuthService> fake_cryptauth_service_; std::unique_ptr<FakeCryptAuthService> fake_cryptauth_service_;
...@@ -157,8 +148,7 @@ class LocalDeviceDataProviderTest : public testing::Test { ...@@ -157,8 +148,7 @@ class LocalDeviceDataProviderTest : public testing::Test {
TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) {
ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
.WillByDefault(Return(std::string())); .WillByDefault(Return(std::string()));
ON_CALL(*mock_device_manager_, GetSyncedDevices()) fake_device_manager_->set_synced_devices(fake_synced_devices_);
.WillByDefault(Return(fake_synced_devices_));
std::string public_key; std::string public_key;
std::vector<BeaconSeed> beacon_seeds; std::vector<BeaconSeed> beacon_seeds;
...@@ -169,8 +159,6 @@ TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { ...@@ -169,8 +159,6 @@ TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) {
TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) { TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) {
ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
.WillByDefault(Return(kDefaultPublicKey)); .WillByDefault(Return(kDefaultPublicKey));
ON_CALL(*mock_device_manager_, GetSyncedDevices())
.WillByDefault(Return(std::vector<ExternalDeviceInfo>()));
std::string public_key; std::string public_key;
std::vector<BeaconSeed> beacon_seeds; std::vector<BeaconSeed> beacon_seeds;
...@@ -182,10 +170,9 @@ TEST_F(LocalDeviceDataProviderTest, ...@@ -182,10 +170,9 @@ TEST_F(LocalDeviceDataProviderTest,
TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) { TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) {
ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
.WillByDefault(Return(kDefaultPublicKey)); .WillByDefault(Return(kDefaultPublicKey));
ON_CALL(*mock_device_manager_, GetSyncedDevices()) fake_device_manager_->set_synced_devices(std::vector<ExternalDeviceInfo>{
.WillByDefault(Return(std::vector<ExternalDeviceInfo>{ fake_synced_devices_[0], fake_synced_devices_[1], fake_synced_devices_[2],
fake_synced_devices_[0], fake_synced_devices_[1], fake_synced_devices_[3]});
fake_synced_devices_[2], fake_synced_devices_[3]}));
std::string public_key; std::string public_key;
std::vector<BeaconSeed> beacon_seeds; std::vector<BeaconSeed> beacon_seeds;
...@@ -197,10 +184,7 @@ TEST_F(LocalDeviceDataProviderTest, ...@@ -197,10 +184,7 @@ TEST_F(LocalDeviceDataProviderTest,
TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) { TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) {
ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
.WillByDefault(Return(kDefaultPublicKey)); .WillByDefault(Return(kDefaultPublicKey));
ON_CALL(*mock_device_manager_, GetSyncedDevices()) fake_device_manager_->synced_devices().push_back(fake_synced_devices_[4]);
.WillByDefault(Return(std::vector<ExternalDeviceInfo>{
fake_synced_devices_[4],
}));
std::string public_key; std::string public_key;
std::vector<BeaconSeed> beacon_seeds; std::vector<BeaconSeed> beacon_seeds;
...@@ -211,8 +195,7 @@ TEST_F(LocalDeviceDataProviderTest, ...@@ -211,8 +195,7 @@ TEST_F(LocalDeviceDataProviderTest,
TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) { TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) {
ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
.WillByDefault(Return(kDefaultPublicKey)); .WillByDefault(Return(kDefaultPublicKey));
ON_CALL(*mock_device_manager_, GetSyncedDevices()) fake_device_manager_->set_synced_devices(fake_synced_devices_);
.WillByDefault(Return(fake_synced_devices_));
std::string public_key; std::string public_key;
std::vector<BeaconSeed> beacon_seeds; std::vector<BeaconSeed> beacon_seeds;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "components/cryptauth/cryptauth_client.h" #include "components/cryptauth/cryptauth_client.h"
#include "components/cryptauth/cryptauth_device_manager.h" #include "components/cryptauth/fake_cryptauth_device_manager.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -39,14 +39,6 @@ const int64_t fake_beacon_seed4_end_ms = 3000L; ...@@ -39,14 +39,6 @@ const int64_t fake_beacon_seed4_end_ms = 3000L;
const std::string public_key1 = "publicKey1"; const std::string public_key1 = "publicKey1";
const std::string public_key2 = "publicKey2"; const std::string public_key2 = "publicKey2";
class MockDeviceManager : public CryptAuthDeviceManager {
public:
MockDeviceManager() {}
~MockDeviceManager() override {}
MOCK_CONST_METHOD0(GetSyncedDevices, std::vector<ExternalDeviceInfo>());
};
ExternalDeviceInfo CreateFakeInfo1() { ExternalDeviceInfo CreateFakeInfo1() {
BeaconSeed seed1; BeaconSeed seed1;
seed1.set_data(fake_beacon_seed1_data); seed1.set_data(fake_beacon_seed1_data);
...@@ -91,13 +83,13 @@ class CryptAuthRemoteBeaconSeedFetcherTest : public testing::Test { ...@@ -91,13 +83,13 @@ class CryptAuthRemoteBeaconSeedFetcherTest : public testing::Test {
: fake_info1_(CreateFakeInfo1()), fake_info2_(CreateFakeInfo2()) {} : fake_info1_(CreateFakeInfo1()), fake_info2_(CreateFakeInfo2()) {}
void SetUp() override { void SetUp() override {
mock_device_manager_ = std::make_unique<MockDeviceManager>(); fake_device_manager_ = std::make_unique<FakeCryptAuthDeviceManager>();
fetcher_ = std::make_unique<StrictMock<RemoteBeaconSeedFetcher>>( fetcher_ = std::make_unique<StrictMock<RemoteBeaconSeedFetcher>>(
mock_device_manager_.get()); fake_device_manager_.get());
} }
std::unique_ptr<RemoteBeaconSeedFetcher> fetcher_; std::unique_ptr<RemoteBeaconSeedFetcher> fetcher_;
std::unique_ptr<MockDeviceManager> mock_device_manager_; std::unique_ptr<FakeCryptAuthDeviceManager> fake_device_manager_;
const ExternalDeviceInfo fake_info1_; const ExternalDeviceInfo fake_info1_;
const ExternalDeviceInfo fake_info2_; const ExternalDeviceInfo fake_info2_;
...@@ -112,18 +104,14 @@ TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestRemoteDeviceWithNoPublicKey) { ...@@ -112,18 +104,14 @@ TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestRemoteDeviceWithNoPublicKey) {
} }
TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestNoSyncedDevices) { TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestNoSyncedDevices) {
EXPECT_CALL(*mock_device_manager_, GetSyncedDevices())
.WillOnce(Return(std::vector<ExternalDeviceInfo>()));
std::vector<BeaconSeed> seeds; std::vector<BeaconSeed> seeds;
EXPECT_FALSE(fetcher_->FetchSeedsForDeviceId( EXPECT_FALSE(fetcher_->FetchSeedsForDeviceId(
RemoteDevice::GenerateDeviceId(public_key1), &seeds)); RemoteDevice::GenerateDeviceId(public_key1), &seeds));
} }
TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestDeviceHasDifferentPublicKey) { TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestDeviceHasDifferentPublicKey) {
std::vector<ExternalDeviceInfo> device_infos = {fake_info1_, fake_info2_}; fake_device_manager_->set_synced_devices(
EXPECT_CALL(*mock_device_manager_, GetSyncedDevices()) std::vector<ExternalDeviceInfo>{fake_info1_, fake_info2_});
.WillOnce(Return(device_infos));
std::vector<BeaconSeed> seeds; std::vector<BeaconSeed> seeds;
EXPECT_FALSE(fetcher_->FetchSeedsForDeviceId( EXPECT_FALSE(fetcher_->FetchSeedsForDeviceId(
...@@ -131,10 +119,8 @@ TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestDeviceHasDifferentPublicKey) { ...@@ -131,10 +119,8 @@ TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestDeviceHasDifferentPublicKey) {
} }
TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestSuccess) { TEST_F(CryptAuthRemoteBeaconSeedFetcherTest, TestSuccess) {
std::vector<ExternalDeviceInfo> device_infos = {fake_info1_, fake_info2_}; fake_device_manager_->set_synced_devices(
EXPECT_CALL(*mock_device_manager_, GetSyncedDevices()) std::vector<ExternalDeviceInfo>{fake_info1_, fake_info2_});
.Times(2)
.WillRepeatedly(Return(device_infos));
std::vector<BeaconSeed> seeds1; std::vector<BeaconSeed> seeds1;
ASSERT_TRUE(fetcher_->FetchSeedsForDeviceId( ASSERT_TRUE(fetcher_->FetchSeedsForDeviceId(
......
...@@ -57,7 +57,7 @@ Refer to README.md for content description and update process. ...@@ -57,7 +57,7 @@ Refer to README.md for content description and update process.
<item id="cryptauth_enrollment_flow_setup" hash_code="84889397" type="1" second_id="29188932" content_hash_code="128348931" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/> <item id="cryptauth_enrollment_flow_setup" hash_code="84889397" type="1" second_id="29188932" content_hash_code="128348931" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/>
<item id="cryptauth_find_eligible_for_promotion" hash_code="20053290" type="1" second_id="29188932" content_hash_code="93687383" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/> <item id="cryptauth_find_eligible_for_promotion" hash_code="20053290" type="1" second_id="29188932" content_hash_code="93687383" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/>
<item id="cryptauth_find_eligible_unlock_devices" hash_code="120000562" type="1" second_id="29188932" content_hash_code="46773475" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/> <item id="cryptauth_find_eligible_unlock_devices" hash_code="120000562" type="1" second_id="29188932" content_hash_code="46773475" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/>
<item id="cryptauth_get_my_devices" hash_code="136498680" type="1" second_id="29188932" content_hash_code="73842435" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_device_manager.cc"/> <item id="cryptauth_get_my_devices" hash_code="136498680" type="1" second_id="29188932" content_hash_code="73842435" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_device_manager_impl.cc"/>
<item id="cryptauth_toggle_easyunlock" hash_code="25204343" type="1" second_id="29188932" content_hash_code="13570943" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/> <item id="cryptauth_toggle_easyunlock" hash_code="25204343" type="1" second_id="29188932" content_hash_code="13570943" os_list="linux,windows" semantics_fields="1,2,3,4,5" policy_fields="3,4" file_path="components/cryptauth/cryptauth_client_impl.cc"/>
<item id="data_reduction_proxy_config" hash_code="485305" type="0" content_hash_code="134075813" os_list="linux,windows" file_path="components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service_client.cc"/> <item id="data_reduction_proxy_config" hash_code="485305" type="0" content_hash_code="134075813" os_list="linux,windows" file_path="components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service_client.cc"/>
<item id="data_reduction_proxy_pingback" hash_code="68561428" type="0" content_hash_code="78407792" os_list="linux,windows" file_path="components/data_reduction_proxy/content/browser/data_reduction_proxy_pingback_client_impl.cc"/> <item id="data_reduction_proxy_pingback" hash_code="68561428" type="0" content_hash_code="78407792" os_list="linux,windows" file_path="components/data_reduction_proxy/content/browser/data_reduction_proxy_pingback_client_impl.cc"/>
......
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