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
|
// Copyright (c) 2012 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/chromeos/settings/device_settings_test_helper.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_dbus_thread_manager.h"
#include "chromeos/network/network_handler.h"
#include "content/public/browser/browser_thread.h"
namespace chromeos {
DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
void DeviceSettingsTestHelper::FlushLoops() {
// DeviceSettingsService may trigger operations that hop back and forth
// between the message loop and the blocking pool. 2 iterations are currently
// sufficient (key loading, signing).
for (int i = 0; i < 2; ++i) {
base::MessageLoop::current()->RunUntilIdle();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
}
base::MessageLoop::current()->RunUntilIdle();
}
void DeviceSettingsTestHelper::FlushStore() {
std::vector<StorePolicyCallback> callbacks;
callbacks.swap(device_policy_.store_callbacks_);
for (std::vector<StorePolicyCallback>::iterator cb(callbacks.begin());
cb != callbacks.end(); ++cb) {
cb->Run(device_policy_.store_result_);
}
std::map<std::string, PolicyState>::iterator device_local_account_state;
for (device_local_account_state = device_local_account_policy_.begin();
device_local_account_state != device_local_account_policy_.end();
++device_local_account_state) {
callbacks.swap(device_local_account_state->second.store_callbacks_);
for (std::vector<StorePolicyCallback>::iterator cb(callbacks.begin());
cb != callbacks.end(); ++cb) {
cb->Run(device_local_account_state->second.store_result_);
}
}
}
void DeviceSettingsTestHelper::FlushRetrieve() {
std::vector<RetrievePolicyCallback> callbacks;
callbacks.swap(device_policy_.retrieve_callbacks_);
for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin());
cb != callbacks.end(); ++cb) {
cb->Run(device_policy_.policy_blob_);
}
std::map<std::string, PolicyState>::iterator device_local_account_state;
for (device_local_account_state = device_local_account_policy_.begin();
device_local_account_state != device_local_account_policy_.end();
++device_local_account_state) {
std::vector<RetrievePolicyCallback> callbacks;
callbacks.swap(device_local_account_state->second.retrieve_callbacks_);
for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin());
cb != callbacks.end(); ++cb) {
cb->Run(device_local_account_state->second.policy_blob_);
}
}
}
void DeviceSettingsTestHelper::Flush() {
do {
FlushLoops();
FlushStore();
FlushLoops();
FlushRetrieve();
FlushLoops();
} while (HasPendingOperations());
}
bool DeviceSettingsTestHelper::HasPendingOperations() const {
if (device_policy_.HasPendingOperations())
return true;
std::map<std::string, PolicyState>::const_iterator device_local_account_state;
for (device_local_account_state = device_local_account_policy_.begin();
device_local_account_state != device_local_account_policy_.end();
++device_local_account_state) {
if (device_local_account_state->second.HasPendingOperations())
return true;
}
return false;
}
void DeviceSettingsTestHelper::Init(dbus::Bus* bus) {}
void DeviceSettingsTestHelper::SetStubDelegate(
SessionManagerClient::StubDelegate* delegate) {}
void DeviceSettingsTestHelper::AddObserver(Observer* observer) {}
void DeviceSettingsTestHelper::RemoveObserver(Observer* observer) {}
bool DeviceSettingsTestHelper::HasObserver(Observer* observer) {
return false;
}
void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
void DeviceSettingsTestHelper::RestartJob(int pid,
const std::string& command_line) {}
void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {}
void DeviceSettingsTestHelper::StopSession() {}
void DeviceSettingsTestHelper::StartDeviceWipe() {}
void DeviceSettingsTestHelper::RequestLockScreen() {}
void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
void DeviceSettingsTestHelper::RetrieveActiveSessions(
const ActiveSessionsCallback& callback) {}
void DeviceSettingsTestHelper::RetrieveDevicePolicy(
const RetrievePolicyCallback& callback) {
device_policy_.retrieve_callbacks_.push_back(callback);
}
void DeviceSettingsTestHelper::RetrievePolicyForUser(
const std::string& username,
const RetrievePolicyCallback& callback) {
}
std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
const std::string& username) {
return "";
}
void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
const std::string& account_id,
const RetrievePolicyCallback& callback) {
device_local_account_policy_[account_id].retrieve_callbacks_.push_back(
callback);
}
void DeviceSettingsTestHelper::StoreDevicePolicy(
const std::string& policy_blob,
const StorePolicyCallback& callback) {
device_policy_.policy_blob_ = policy_blob;
device_policy_.store_callbacks_.push_back(callback);
}
void DeviceSettingsTestHelper::StorePolicyForUser(
const std::string& username,
const std::string& policy_blob,
const StorePolicyCallback& callback) {
}
void DeviceSettingsTestHelper::StoreDeviceLocalAccountPolicy(
const std::string& account_id,
const std::string& policy_blob,
const StorePolicyCallback& callback) {
device_local_account_policy_[account_id].policy_blob_ = policy_blob;
device_local_account_policy_[account_id].store_callbacks_.push_back(callback);
}
void DeviceSettingsTestHelper::SetFlagsForUser(
const std::string& account_id,
const std::vector<std::string>& flags) {}
void DeviceSettingsTestHelper::GetServerBackedStateKeys(
const StateKeysCallback& callback) {}
DeviceSettingsTestHelper::PolicyState::PolicyState()
: store_result_(true) {}
DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
DeviceSettingsService::Initialize();
DeviceSettingsService::Get()->SetSessionManager(this, new MockOwnerKeyUtil());
DeviceSettingsService::Get()->Load();
Flush();
}
ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
Flush();
DeviceSettingsService::Get()->UnsetSessionManager();
DeviceSettingsService::Shutdown();
}
DeviceSettingsTestBase::DeviceSettingsTestBase()
: ui_thread_(content::BrowserThread::UI, &loop_),
file_thread_(content::BrowserThread::FILE, &loop_),
owner_key_util_(new MockOwnerKeyUtil()),
fake_dbus_thread_manager_(new FakeDBusThreadManager()) {
fake_dbus_thread_manager_->SetFakeClients();
}
DeviceSettingsTestBase::~DeviceSettingsTestBase() {
base::RunLoop().RunUntilIdle();
}
void DeviceSettingsTestBase::SetUp() {
// Initialize DBusThreadManager with a stub implementation.
chromeos::DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager_);
NetworkHandler::Initialize();
loop_.RunUntilIdle();
device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
false);
owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
device_policy_.Build();
device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
device_settings_service_.SetSessionManager(&device_settings_test_helper_,
owner_key_util_);
}
void DeviceSettingsTestBase::TearDown() {
FlushDeviceSettings();
device_settings_service_.UnsetSessionManager();
NetworkHandler::Shutdown();
DBusThreadManager::Shutdown();
}
void DeviceSettingsTestBase::FlushDeviceSettings() {
device_settings_test_helper_.Flush();
}
void DeviceSettingsTestBase::ReloadDeviceSettings() {
device_settings_service_.OwnerKeySet(true);
FlushDeviceSettings();
}
} // namespace chromeos
|