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