Home | History | Annotate | Download | only in settings
      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 
     21 namespace chromeos {
     22 
     23 DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
     24 
     25 DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
     26 
     27 void 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 
     38 void 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 
     58 void 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 
     79 void DeviceSettingsTestHelper::Flush() {
     80   do {
     81     FlushLoops();
     82     FlushStore();
     83     FlushLoops();
     84     FlushRetrieve();
     85     FlushLoops();
     86   } while (HasPendingOperations());
     87 }
     88 
     89 bool 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 
    104 void DeviceSettingsTestHelper::Init(dbus::Bus* bus) {}
    105 
    106 void DeviceSettingsTestHelper::SetStubDelegate(
    107     SessionManagerClient::StubDelegate* delegate) {}
    108 
    109 void DeviceSettingsTestHelper::AddObserver(Observer* observer) {}
    110 
    111 void DeviceSettingsTestHelper::RemoveObserver(Observer* observer) {}
    112 
    113 bool DeviceSettingsTestHelper::HasObserver(Observer* observer) {
    114   return false;
    115 }
    116 
    117 void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
    118 
    119 void DeviceSettingsTestHelper::RestartJob(int pid,
    120                                           const std::string& command_line) {}
    121 
    122 void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {}
    123 
    124 void DeviceSettingsTestHelper::StopSession() {}
    125 
    126 void DeviceSettingsTestHelper::StartDeviceWipe() {}
    127 
    128 void DeviceSettingsTestHelper::RequestLockScreen() {}
    129 
    130 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
    131 
    132 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
    133 
    134 void DeviceSettingsTestHelper::RetrieveActiveSessions(
    135       const ActiveSessionsCallback& callback) {}
    136 
    137 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
    138     const RetrievePolicyCallback& callback) {
    139   device_policy_.retrieve_callbacks_.push_back(callback);
    140 }
    141 
    142 void DeviceSettingsTestHelper::RetrievePolicyForUser(
    143     const std::string& username,
    144     const RetrievePolicyCallback& callback) {
    145 }
    146 
    147 std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
    148     const std::string& username) {
    149   return "";
    150 }
    151 
    152 void 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 
    159 void 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 
    166 void DeviceSettingsTestHelper::StorePolicyForUser(
    167     const std::string& username,
    168     const std::string& policy_blob,
    169     const StorePolicyCallback& callback) {
    170 }
    171 
    172 void 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 
    180 void DeviceSettingsTestHelper::SetFlagsForUser(
    181     const std::string& account_id,
    182     const std::vector<std::string>& flags) {}
    183 
    184 void DeviceSettingsTestHelper::GetServerBackedStateKeys(
    185     const StateKeysCallback& callback) {}
    186 
    187 DeviceSettingsTestHelper::PolicyState::PolicyState()
    188     : store_result_(true) {}
    189 
    190 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
    191 
    192 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
    193   DeviceSettingsService::Initialize();
    194   DeviceSettingsService::Get()->SetSessionManager(this, new MockOwnerKeyUtil());
    195   DeviceSettingsService::Get()->Load();
    196   Flush();
    197 }
    198 
    199 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
    200   Flush();
    201   DeviceSettingsService::Get()->UnsetSessionManager();
    202   DeviceSettingsService::Shutdown();
    203 }
    204 
    205 DeviceSettingsTestBase::DeviceSettingsTestBase()
    206     : user_manager_(new FakeUserManager()),
    207       user_manager_enabler_(user_manager_),
    208       owner_key_util_(new MockOwnerKeyUtil()),
    209       fake_dbus_thread_manager_(new FakeDBusThreadManager()) {
    210   fake_dbus_thread_manager_->SetFakeClients();
    211 }
    212 
    213 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
    214   base::RunLoop().RunUntilIdle();
    215 }
    216 
    217 void DeviceSettingsTestBase::SetUp() {
    218   // Initialize DBusThreadManager with a stub implementation.
    219   chromeos::DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager_);
    220 
    221   NetworkHandler::Initialize();
    222   base::RunLoop().RunUntilIdle();
    223 
    224   device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
    225       false);
    226   owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
    227   device_policy_.Build();
    228   device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
    229   device_settings_service_.SetSessionManager(&device_settings_test_helper_,
    230                                              owner_key_util_);
    231   OwnerSettingsService::SetOwnerKeyUtilForTesting(owner_key_util_.get());
    232   OwnerSettingsService::SetDeviceSettingsServiceForTesting(
    233       &device_settings_service_);
    234   profile_.reset(new TestingProfile());
    235 }
    236 
    237 void DeviceSettingsTestBase::TearDown() {
    238   OwnerSettingsService::SetOwnerKeyUtilForTesting(NULL);
    239   OwnerSettingsService::SetDeviceSettingsServiceForTesting(NULL);
    240   FlushDeviceSettings();
    241   device_settings_service_.UnsetSessionManager();
    242   NetworkHandler::Shutdown();
    243   DBusThreadManager::Shutdown();
    244 }
    245 
    246 void DeviceSettingsTestBase::FlushDeviceSettings() {
    247   device_settings_test_helper_.Flush();
    248 }
    249 
    250 void DeviceSettingsTestBase::ReloadDeviceSettings() {
    251   device_settings_service_.OwnerKeySet(true);
    252   FlushDeviceSettings();
    253 }
    254 
    255 void DeviceSettingsTestBase::InitOwner(const std::string& user_id,
    256                                        bool tpm_is_ready) {
    257   const User* user = user_manager_->FindUser(user_id);
    258   if (!user) {
    259     user = user_manager_->AddUser(user_id);
    260     profile_->set_profile_name(user_id);
    261     user_manager_->SetProfileForUser(user, 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