Home | History | Annotate | Download | only in login
      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 <cstdlib>
      6 #include <cstring>
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/prefs/pref_service.h"
     10 #include "base/run_loop.h"
     11 #include "base/values.h"
     12 #include "chrome/browser/browser_process.h"
     13 #include "chrome/browser/chromeos/login/user.h"
     14 #include "chrome/browser/chromeos/login/user_manager.h"
     15 #include "chrome/browser/chromeos/login/user_manager_impl.h"
     16 #include "chrome/browser/chromeos/settings/cros_settings.h"
     17 #include "chrome/browser/chromeos/settings/device_settings_service.h"
     18 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
     19 #include "chrome/test/base/scoped_testing_local_state.h"
     20 #include "chrome/test/base/testing_browser_process.h"
     21 #include "chromeos/settings/cros_settings_names.h"
     22 #include "chromeos/settings/cros_settings_provider.h"
     23 #include "content/public/test/test_browser_thread_bundle.h"
     24 #include "testing/gtest/include/gtest/gtest.h"
     25 
     26 namespace chromeos {
     27 
     28 class UserManagerTest : public testing::Test {
     29  protected:
     30   virtual void SetUp() OVERRIDE {
     31     cros_settings_ = CrosSettings::Get();
     32 
     33     // Replace the real DeviceSettingsProvider with a stub.
     34     device_settings_provider_ =
     35         cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo);
     36     EXPECT_TRUE(device_settings_provider_);
     37     EXPECT_TRUE(
     38         cros_settings_->RemoveSettingsProvider(device_settings_provider_));
     39     cros_settings_->AddSettingsProvider(&stub_settings_provider_);
     40 
     41     // Populate the stub DeviceSettingsProvider with valid values.
     42     SetDeviceSettings(false, "", false);
     43 
     44     // Register an in-memory local settings instance.
     45     local_state_.reset(
     46         new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
     47 
     48     ResetUserManager();
     49   }
     50 
     51   virtual void TearDown() OVERRIDE {
     52     // Unregister the in-memory local settings instance.
     53     local_state_.reset();
     54 
     55     // Restore the real DeviceSettingsProvider.
     56     EXPECT_TRUE(
     57       cros_settings_->RemoveSettingsProvider(&stub_settings_provider_));
     58     cros_settings_->AddSettingsProvider(device_settings_provider_);
     59 
     60     // Shut down the DeviceSettingsService.
     61     DeviceSettingsService::Get()->UnsetSessionManager();
     62 
     63     base::RunLoop().RunUntilIdle();
     64   }
     65 
     66   UserManagerImpl* GetUserManagerImpl() const {
     67     return static_cast<UserManagerImpl*>(UserManager::Get());
     68   }
     69 
     70   bool GetUserManagerEphemeralUsersEnabled() const {
     71     return GetUserManagerImpl()->ephemeral_users_enabled_;
     72   }
     73 
     74   void SetUserManagerEphemeralUsersEnabled(bool ephemeral_users_enabled) {
     75     GetUserManagerImpl()->ephemeral_users_enabled_ = ephemeral_users_enabled;
     76   }
     77 
     78   const std::string& GetUserManagerOwnerEmail() const {
     79     return GetUserManagerImpl()-> owner_email_;
     80   }
     81 
     82   void SetUserManagerOwnerEmail(const std::string& owner_email) {
     83     GetUserManagerImpl()->owner_email_ = owner_email;
     84   }
     85 
     86   void ResetUserManager() {
     87     // Reset the UserManager singleton.
     88     user_manager_enabler_.reset();
     89     // Initialize the UserManager singleton to a fresh UserManagerImpl instance.
     90     user_manager_enabler_.reset(
     91         new ScopedUserManagerEnabler(new UserManagerImpl));
     92   }
     93 
     94   void SetDeviceSettings(bool ephemeral_users_enabled,
     95                          const std::string &owner,
     96                          bool locally_managed_users_enabled) {
     97     base::FundamentalValue
     98         ephemeral_users_enabled_value(ephemeral_users_enabled);
     99     stub_settings_provider_.Set(kAccountsPrefEphemeralUsersEnabled,
    100         ephemeral_users_enabled_value);
    101     base::StringValue owner_value(owner);
    102     stub_settings_provider_.Set(kDeviceOwner, owner_value);
    103     stub_settings_provider_.Set(kAccountsPrefSupervisedUsersEnabled,
    104         base::FundamentalValue(locally_managed_users_enabled));
    105   }
    106 
    107   void RetrieveTrustedDevicePolicies() {
    108     GetUserManagerImpl()->RetrieveTrustedDevicePolicies();
    109   }
    110 
    111  protected:
    112   content::TestBrowserThreadBundle thread_bundle_;
    113 
    114   CrosSettings* cros_settings_;
    115   CrosSettingsProvider* device_settings_provider_;
    116   StubCrosSettingsProvider stub_settings_provider_;
    117   scoped_ptr<ScopedTestingLocalState> local_state_;
    118 
    119   ScopedTestDeviceSettingsService test_device_settings_service_;
    120   ScopedTestCrosSettings test_cros_settings_;
    121 
    122   scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
    123 };
    124 
    125 TEST_F(UserManagerTest, RetrieveTrustedDevicePolicies) {
    126   SetUserManagerEphemeralUsersEnabled(true);
    127   SetUserManagerOwnerEmail("");
    128 
    129   SetDeviceSettings(false, "owner (at) invalid.domain", false);
    130   RetrieveTrustedDevicePolicies();
    131 
    132   EXPECT_FALSE(GetUserManagerEphemeralUsersEnabled());
    133   EXPECT_EQ(GetUserManagerOwnerEmail(), "owner (at) invalid.domain");
    134 }
    135 
    136 TEST_F(UserManagerTest, RemoveAllExceptOwnerFromList) {
    137   UserManager::Get()->UserLoggedIn(
    138       "owner (at) invalid.domain", "owner (at) invalid.domain", false);
    139   ResetUserManager();
    140   UserManager::Get()->UserLoggedIn(
    141       "user0 (at) invalid.domain", "owner (at) invalid.domain", false);
    142   ResetUserManager();
    143   UserManager::Get()->UserLoggedIn(
    144       "user1 (at) invalid.domain", "owner (at) invalid.domain", false);
    145   ResetUserManager();
    146 
    147   const UserList* users = &UserManager::Get()->GetUsers();
    148   ASSERT_EQ(3U, users->size());
    149   EXPECT_EQ((*users)[0]->email(), "user1 (at) invalid.domain");
    150   EXPECT_EQ((*users)[1]->email(), "user0 (at) invalid.domain");
    151   EXPECT_EQ((*users)[2]->email(), "owner (at) invalid.domain");
    152 
    153   SetDeviceSettings(true, "owner (at) invalid.domain", false);
    154   RetrieveTrustedDevicePolicies();
    155 
    156   users = &UserManager::Get()->GetUsers();
    157   EXPECT_EQ(1U, users->size());
    158   EXPECT_EQ((*users)[0]->email(), "owner (at) invalid.domain");
    159 }
    160 
    161 TEST_F(UserManagerTest, RegularUserLoggedInAsEphemeral) {
    162   SetDeviceSettings(true, "owner (at) invalid.domain", false);
    163   RetrieveTrustedDevicePolicies();
    164 
    165   UserManager::Get()->UserLoggedIn(
    166       "owner (at) invalid.domain", "user0 (at) invalid.domain", false);
    167   ResetUserManager();
    168   UserManager::Get()->UserLoggedIn(
    169       "user0 (at) invalid.domain", "user0 (at) invalid.domain", false);
    170   ResetUserManager();
    171 
    172   const UserList* users = &UserManager::Get()->GetUsers();
    173   EXPECT_EQ(1U, users->size());
    174   EXPECT_EQ((*users)[0]->email(), "owner (at) invalid.domain");
    175 }
    176 
    177 }  // namespace chromeos
    178