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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// Copyright 2014 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 "chrome/browser/supervised_user/legacy/supervised_user_registration_utility.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/supervised_user/legacy/supervised_user_refresh_token_fetcher.h"
#include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
#include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service_factory.h"
#include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h"
#include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_utils.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "sync/api/attachments/attachment_id.h"
#include "sync/api/sync_change.h"
#include "sync/api/sync_error_factory_mock.h"
#include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
#include "sync/protocol/sync.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
using sync_pb::ManagedUserSpecifics;
using syncer::SUPERVISED_USERS;
using syncer::SyncChange;
using syncer::SyncChangeList;
using syncer::SyncChangeProcessor;
using syncer::SyncData;
using syncer::SyncDataList;
using syncer::SyncError;
using syncer::SyncErrorFactory;
using syncer::SyncMergeResult;
namespace {
const char kSupervisedUserToken[] = "supervisedusertoken";
class MockChangeProcessor : public SyncChangeProcessor {
public:
MockChangeProcessor() {}
~MockChangeProcessor() override {}
// SyncChangeProcessor implementation:
SyncError ProcessSyncChanges(const tracked_objects::Location& from_here,
const SyncChangeList& change_list) override;
SyncDataList GetAllSyncData(syncer::ModelType type) const override {
return SyncDataList();
}
const SyncChangeList& changes() const { return change_list_; }
private:
SyncChangeList change_list_;
};
SyncError MockChangeProcessor::ProcessSyncChanges(
const tracked_objects::Location& from_here,
const SyncChangeList& change_list) {
change_list_ = change_list;
return SyncError();
}
class MockSupervisedUserRefreshTokenFetcher
: public SupervisedUserRefreshTokenFetcher {
public:
MockSupervisedUserRefreshTokenFetcher() {}
~MockSupervisedUserRefreshTokenFetcher() override {}
// SupervisedUserRefreshTokenFetcher implementation:
void Start(const std::string& supervised_user_id,
const std::string& device_name,
const TokenCallback& callback) override {
GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
callback.Run(error, kSupervisedUserToken);
}
};
} // namespace
class SupervisedUserRegistrationUtilityTest : public ::testing::Test {
public:
SupervisedUserRegistrationUtilityTest();
~SupervisedUserRegistrationUtilityTest() override;
void TearDown() override;
protected:
scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
scoped_ptr<SyncErrorFactory> CreateErrorFactory();
SyncData CreateRemoteData(const std::string& id, const std::string& name);
SyncMergeResult StartInitialSync();
SupervisedUserRegistrationUtility::RegistrationCallback
GetRegistrationCallback();
SupervisedUserRegistrationUtility* GetRegistrationUtility();
void Acknowledge();
PrefService* prefs() { return profile_.GetTestingPrefService(); }
SupervisedUserSyncService* service() { return service_; }
SupervisedUserSharedSettingsService* shared_settings_service() {
return shared_settings_service_;
}
MockChangeProcessor* change_processor() { return change_processor_; }
bool received_callback() const { return received_callback_; }
const GoogleServiceAuthError& error() const { return error_; }
const std::string& token() const { return token_; }
private:
void OnSupervisedUserRegistered(const GoogleServiceAuthError& error,
const std::string& token);
base::MessageLoop message_loop_;
base::RunLoop run_loop_;
TestingProfile profile_;
SupervisedUserSyncService* service_;
SupervisedUserSharedSettingsService* shared_settings_service_;
scoped_ptr<SupervisedUserRegistrationUtility> registration_utility_;
// Owned by the SupervisedUserSyncService.
MockChangeProcessor* change_processor_;
// A unique ID for creating "remote" Sync data.
int64 sync_data_id_;
// Whether OnSupervisedUserRegistered has been called.
bool received_callback_;
// Hold the registration result (either an error, or a token).
GoogleServiceAuthError error_;
std::string token_;
base::WeakPtrFactory<SupervisedUserRegistrationUtilityTest> weak_ptr_factory_;
};
SupervisedUserRegistrationUtilityTest::SupervisedUserRegistrationUtilityTest()
: change_processor_(NULL),
sync_data_id_(0),
received_callback_(false),
error_(GoogleServiceAuthError::NUM_STATES),
weak_ptr_factory_(this) {
service_ = SupervisedUserSyncServiceFactory::GetForProfile(&profile_);
shared_settings_service_ =
SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
&profile_);
}
SupervisedUserRegistrationUtilityTest::
~SupervisedUserRegistrationUtilityTest() {
EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
}
void SupervisedUserRegistrationUtilityTest::TearDown() {
content::RunAllBlockingPoolTasksUntilIdle();
}
scoped_ptr<SyncChangeProcessor>
SupervisedUserRegistrationUtilityTest::CreateChangeProcessor() {
EXPECT_FALSE(change_processor_);
change_processor_ = new MockChangeProcessor();
return scoped_ptr<SyncChangeProcessor>(change_processor_);
}
scoped_ptr<SyncErrorFactory>
SupervisedUserRegistrationUtilityTest::CreateErrorFactory() {
return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
}
SyncMergeResult SupervisedUserRegistrationUtilityTest::StartInitialSync() {
SyncDataList initial_sync_data;
SyncMergeResult result =
service()->MergeDataAndStartSyncing(SUPERVISED_USERS,
initial_sync_data,
CreateChangeProcessor(),
CreateErrorFactory());
EXPECT_FALSE(result.error().IsSet());
return result;
}
SupervisedUserRegistrationUtility::RegistrationCallback
SupervisedUserRegistrationUtilityTest::GetRegistrationCallback() {
return base::Bind(
&SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered,
weak_ptr_factory_.GetWeakPtr());
}
SupervisedUserRegistrationUtility*
SupervisedUserRegistrationUtilityTest::GetRegistrationUtility() {
if (registration_utility_.get())
return registration_utility_.get();
scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher(
new MockSupervisedUserRefreshTokenFetcher);
registration_utility_.reset(
SupervisedUserRegistrationUtility::CreateImpl(prefs(),
token_fetcher.Pass(),
service(),
shared_settings_service()));
return registration_utility_.get();
}
void SupervisedUserRegistrationUtilityTest::Acknowledge() {
SyncChangeList new_changes;
for (const SyncChange& sync_change : change_processor()->changes()) {
EXPECT_EQ(SyncChange::ACTION_ADD, sync_change.change_type());
::sync_pb::EntitySpecifics specifics =
sync_change.sync_data().GetSpecifics();
EXPECT_FALSE(specifics.managed_user().acknowledged());
specifics.mutable_managed_user()->set_acknowledged(true);
new_changes.push_back(
SyncChange(FROM_HERE,
SyncChange::ACTION_UPDATE,
SyncData::CreateRemoteData(
++sync_data_id_,
specifics,
base::Time(),
syncer::AttachmentIdList(),
syncer::AttachmentServiceProxyForTest::Create())));
}
service()->ProcessSyncChanges(FROM_HERE, new_changes);
run_loop_.Run();
}
void SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered(
const GoogleServiceAuthError& error,
const std::string& token) {
received_callback_ = true;
error_ = error;
token_ = token;
run_loop_.Quit();
}
TEST_F(SupervisedUserRegistrationUtilityTest, Register) {
StartInitialSync();
GetRegistrationUtility()->Register(
SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
GetRegistrationCallback());
EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
Acknowledge();
EXPECT_TRUE(received_callback());
EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
EXPECT_FALSE(token().empty());
}
TEST_F(SupervisedUserRegistrationUtilityTest, RegisterBeforeInitialSync) {
GetRegistrationUtility()->Register(
SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Nemo"), 5),
GetRegistrationCallback());
EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
StartInitialSync();
Acknowledge();
EXPECT_TRUE(received_callback());
EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
EXPECT_FALSE(token().empty());
}
TEST_F(SupervisedUserRegistrationUtilityTest,
SyncServiceShutdownBeforeRegFinish) {
StartInitialSync();
GetRegistrationUtility()->Register(
SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Remy"), 12),
GetRegistrationCallback());
EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
service()->Shutdown();
EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
EXPECT_TRUE(received_callback());
EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
EXPECT_EQ(std::string(), token());
}
TEST_F(SupervisedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
StartInitialSync();
GetRegistrationUtility()->Register(
SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Mike"), 17),
GetRegistrationCallback());
EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
service()->StopSyncing(SUPERVISED_USERS);
EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
EXPECT_TRUE(received_callback());
EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
EXPECT_EQ(std::string(), token());
}
|