device_settings_test_helper.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/run_loop.h"
9#include "base/threading/sequenced_worker_pool.h"
10#include "chrome/browser/chromeos/ownership/owner_settings_service.h"
11#include "chrome/browser/chromeos/ownership/owner_settings_service_factory.h"
12#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
13#include "chrome/browser/chromeos/profiles/profile_helper.h"
14#include "chrome/browser/chromeos/settings/device_settings_service.h"
15#include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
16#include "chrome/test/base/testing_browser_process.h"
17#include "chrome/test/base/testing_profile.h"
18#include "chromeos/dbus/dbus_thread_manager.h"
19#include "chromeos/dbus/fake_dbus_thread_manager.h"
20#include "content/public/browser/browser_thread.h"
21
22namespace chromeos {
23
24DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
25
26DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
27
28void DeviceSettingsTestHelper::FlushLoops() {
29  // DeviceSettingsService may trigger operations that hop back and forth
30  // between the message loop and the blocking pool. 2 iterations are currently
31  // sufficient (key loading, signing).
32  for (int i = 0; i < 2; ++i) {
33    base::MessageLoop::current()->RunUntilIdle();
34    content::BrowserThread::GetBlockingPool()->FlushForTesting();
35  }
36  base::MessageLoop::current()->RunUntilIdle();
37}
38
39void DeviceSettingsTestHelper::FlushStore() {
40  std::vector<StorePolicyCallback> callbacks;
41  callbacks.swap(device_policy_.store_callbacks_);
42  for (std::vector<StorePolicyCallback>::iterator cb(callbacks.begin());
43       cb != callbacks.end(); ++cb) {
44    cb->Run(device_policy_.store_result_);
45  }
46
47  std::map<std::string, PolicyState>::iterator device_local_account_state;
48  for (device_local_account_state = device_local_account_policy_.begin();
49       device_local_account_state != device_local_account_policy_.end();
50       ++device_local_account_state) {
51    callbacks.swap(device_local_account_state->second.store_callbacks_);
52    for (std::vector<StorePolicyCallback>::iterator cb(callbacks.begin());
53         cb != callbacks.end(); ++cb) {
54      cb->Run(device_local_account_state->second.store_result_);
55    }
56  }
57}
58
59void DeviceSettingsTestHelper::FlushRetrieve() {
60  std::vector<RetrievePolicyCallback> callbacks;
61  callbacks.swap(device_policy_.retrieve_callbacks_);
62  for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin());
63       cb != callbacks.end(); ++cb) {
64    cb->Run(device_policy_.policy_blob_);
65  }
66
67  std::map<std::string, PolicyState>::iterator device_local_account_state;
68  for (device_local_account_state = device_local_account_policy_.begin();
69       device_local_account_state != device_local_account_policy_.end();
70       ++device_local_account_state) {
71    std::vector<RetrievePolicyCallback> callbacks;
72    callbacks.swap(device_local_account_state->second.retrieve_callbacks_);
73    for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin());
74         cb != callbacks.end(); ++cb) {
75      cb->Run(device_local_account_state->second.policy_blob_);
76    }
77  }
78}
79
80void DeviceSettingsTestHelper::Flush() {
81  do {
82    FlushLoops();
83    FlushStore();
84    FlushLoops();
85    FlushRetrieve();
86    FlushLoops();
87  } while (HasPendingOperations());
88}
89
90bool DeviceSettingsTestHelper::HasPendingOperations() const {
91  if (device_policy_.HasPendingOperations())
92    return true;
93
94  std::map<std::string, PolicyState>::const_iterator device_local_account_state;
95  for (device_local_account_state = device_local_account_policy_.begin();
96       device_local_account_state != device_local_account_policy_.end();
97       ++device_local_account_state) {
98    if (device_local_account_state->second.HasPendingOperations())
99      return true;
100  }
101
102  return false;
103}
104
105void DeviceSettingsTestHelper::Init(dbus::Bus* bus) {}
106
107void DeviceSettingsTestHelper::SetStubDelegate(
108    SessionManagerClient::StubDelegate* delegate) {}
109
110void DeviceSettingsTestHelper::AddObserver(Observer* observer) {}
111
112void DeviceSettingsTestHelper::RemoveObserver(Observer* observer) {}
113
114bool DeviceSettingsTestHelper::HasObserver(Observer* observer) {
115  return false;
116}
117
118void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
119
120void DeviceSettingsTestHelper::RestartJob(int pid,
121                                          const std::string& command_line) {}
122
123void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {}
124
125void DeviceSettingsTestHelper::StopSession() {}
126
127void DeviceSettingsTestHelper::StartDeviceWipe() {}
128
129void DeviceSettingsTestHelper::RequestLockScreen() {}
130
131void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
132
133void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
134
135void DeviceSettingsTestHelper::RetrieveActiveSessions(
136      const ActiveSessionsCallback& callback) {}
137
138void DeviceSettingsTestHelper::RetrieveDevicePolicy(
139    const RetrievePolicyCallback& callback) {
140  device_policy_.retrieve_callbacks_.push_back(callback);
141}
142
143void DeviceSettingsTestHelper::RetrievePolicyForUser(
144    const std::string& username,
145    const RetrievePolicyCallback& callback) {
146}
147
148std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
149    const std::string& username) {
150  return "";
151}
152
153void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
154    const std::string& account_id,
155    const RetrievePolicyCallback& callback) {
156  device_local_account_policy_[account_id].retrieve_callbacks_.push_back(
157      callback);
158}
159
160void DeviceSettingsTestHelper::StoreDevicePolicy(
161    const std::string& policy_blob,
162    const StorePolicyCallback& callback) {
163  device_policy_.policy_blob_ = policy_blob;
164  device_policy_.store_callbacks_.push_back(callback);
165}
166
167void DeviceSettingsTestHelper::StorePolicyForUser(
168    const std::string& username,
169    const std::string& policy_blob,
170    const StorePolicyCallback& callback) {
171}
172
173void DeviceSettingsTestHelper::StoreDeviceLocalAccountPolicy(
174    const std::string& account_id,
175    const std::string& policy_blob,
176    const StorePolicyCallback& callback) {
177  device_local_account_policy_[account_id].policy_blob_ = policy_blob;
178  device_local_account_policy_[account_id].store_callbacks_.push_back(callback);
179}
180
181void DeviceSettingsTestHelper::SetFlagsForUser(
182    const std::string& account_id,
183    const std::vector<std::string>& flags) {}
184
185void DeviceSettingsTestHelper::GetServerBackedStateKeys(
186    const StateKeysCallback& callback) {}
187
188DeviceSettingsTestHelper::PolicyState::PolicyState()
189    : store_result_(true) {}
190
191DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
192
193ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
194  DeviceSettingsService::Initialize();
195  DeviceSettingsService::Get()->SetSessionManager(this, new MockOwnerKeyUtil());
196  DeviceSettingsService::Get()->Load();
197  Flush();
198}
199
200ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
201  Flush();
202  DeviceSettingsService::Get()->UnsetSessionManager();
203  DeviceSettingsService::Shutdown();
204}
205
206DeviceSettingsTestBase::DeviceSettingsTestBase()
207    : user_manager_(new FakeUserManager()),
208      user_manager_enabler_(user_manager_),
209      owner_key_util_(new MockOwnerKeyUtil()),
210      fake_dbus_thread_manager_(new FakeDBusThreadManager()) {
211}
212
213DeviceSettingsTestBase::~DeviceSettingsTestBase() {
214  base::RunLoop().RunUntilIdle();
215}
216
217void DeviceSettingsTestBase::SetUp() {
218  // Initialize DBusThreadManager with a stub implementation.
219  chromeos::DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager_);
220
221  base::RunLoop().RunUntilIdle();
222
223  device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
224      false);
225  owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
226  device_policy_.Build();
227  device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
228  device_settings_service_.SetSessionManager(&device_settings_test_helper_,
229                                             owner_key_util_);
230  OwnerSettingsService::SetOwnerKeyUtilForTesting(owner_key_util_.get());
231  OwnerSettingsService::SetDeviceSettingsServiceForTesting(
232      &device_settings_service_);
233  profile_.reset(new TestingProfile());
234}
235
236void DeviceSettingsTestBase::TearDown() {
237  OwnerSettingsService::SetOwnerKeyUtilForTesting(NULL);
238  OwnerSettingsService::SetDeviceSettingsServiceForTesting(NULL);
239  FlushDeviceSettings();
240  device_settings_service_.UnsetSessionManager();
241  DBusThreadManager::Shutdown();
242}
243
244void DeviceSettingsTestBase::FlushDeviceSettings() {
245  device_settings_test_helper_.Flush();
246}
247
248void DeviceSettingsTestBase::ReloadDeviceSettings() {
249  device_settings_service_.OwnerKeySet(true);
250  FlushDeviceSettings();
251}
252
253void DeviceSettingsTestBase::InitOwner(const std::string& user_id,
254                                       bool tpm_is_ready) {
255  const user_manager::User* user = user_manager_->FindUser(user_id);
256  if (!user) {
257    user = user_manager_->AddUser(user_id);
258    profile_->set_profile_name(user_id);
259
260    ProfileHelper::Get()->SetUserToProfileMappingForTesting(user,
261                                                            profile_.get());
262  }
263  OwnerSettingsService* service =
264      OwnerSettingsServiceFactory::GetForProfile(profile_.get());
265  CHECK(service);
266  if (tpm_is_ready)
267    service->OnTPMTokenReady();
268  OwnerSettingsServiceFactory::GetInstance()->SetUsername(user_id);
269}
270
271}  // namespace chromeos
272