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 <map>
      6 #include <string>
      7 
      8 #include "base/bind.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/stl_util.h"
     13 #include "base/values.h"
     14 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
     15 #include "chrome/browser/chromeos/settings/cros_settings.h"
     16 #include "chrome/browser/chromeos/settings/device_settings_service.h"
     17 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
     18 #include "chrome/test/base/scoped_testing_local_state.h"
     19 #include "chrome/test/base/testing_browser_process.h"
     20 #include "chromeos/settings/cros_settings_names.h"
     21 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
     22 #include "content/public/test/test_browser_thread.h"
     23 #include "policy/proto/device_management_backend.pb.h"
     24 #include "testing/gtest/include/gtest/gtest.h"
     25 
     26 namespace em = enterprise_management;
     27 
     28 namespace chromeos {
     29 
     30 class CrosSettingsTest : public testing::Test {
     31  protected:
     32   CrosSettingsTest()
     33       : message_loop_(base::MessageLoop::TYPE_UI),
     34         ui_thread_(content::BrowserThread::UI, &message_loop_),
     35         local_state_(TestingBrowserProcess::GetGlobal()),
     36         settings_(DeviceSettingsService::Get()),
     37         weak_factory_(this) {}
     38 
     39   virtual ~CrosSettingsTest() {}
     40 
     41   virtual void TearDown() OVERRIDE {
     42     ASSERT_TRUE(expected_props_.empty());
     43     STLDeleteValues(&expected_props_);
     44     expected_props_.clear();
     45   }
     46 
     47   void FetchPref(const std::string& pref) {
     48     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     49     if (expected_props_.find(pref) == expected_props_.end())
     50       return;
     51 
     52     if (CrosSettingsProvider::TRUSTED ==
     53             settings_.PrepareTrustedValues(
     54                 base::Bind(&CrosSettingsTest::FetchPref,
     55                            weak_factory_.GetWeakPtr(), pref))) {
     56       scoped_ptr<base::Value> expected_value(
     57           expected_props_.find(pref)->second);
     58       const base::Value* pref_value = settings_.GetPref(pref);
     59       if (expected_value.get()) {
     60         ASSERT_TRUE(pref_value);
     61         ASSERT_TRUE(expected_value->Equals(pref_value));
     62       } else {
     63         ASSERT_FALSE(pref_value);
     64       }
     65       expected_props_.erase(pref);
     66     }
     67   }
     68 
     69   void SetPref(const std::string& pref_name, const base::Value* value) {
     70     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     71     settings_.Set(pref_name, *value);
     72   }
     73 
     74   void AddExpectation(const std::string& pref_name, base::Value* value) {
     75     base::Value*& entry = expected_props_[pref_name];
     76     delete entry;
     77     entry = value;
     78   }
     79 
     80   void PrepareEmptyPolicy(em::PolicyData* policy) {
     81     // Prepare some policy blob.
     82     em::PolicyFetchResponse response;
     83     em::ChromeDeviceSettingsProto pol;
     84     policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType);
     85     policy->set_username("me@owner");
     86     policy->set_policy_value(pol.SerializeAsString());
     87     // Wipe the signed settings store.
     88     response.set_policy_data(policy->SerializeAsString());
     89     response.set_policy_data_signature("false");
     90   }
     91 
     92   static bool IsWhitelisted(CrosSettings* cs, const std::string& username) {
     93     return cs->FindEmailInList(kAccountsPrefUsers, username, NULL);
     94   }
     95 
     96   base::MessageLoop message_loop_;
     97   content::TestBrowserThread ui_thread_;
     98 
     99   ScopedTestingLocalState local_state_;
    100   ScopedDeviceSettingsTestHelper device_settings_test_helper_;
    101   CrosSettings settings_;
    102 
    103   base::WeakPtrFactory<CrosSettingsTest> weak_factory_;
    104 
    105   std::map<std::string, base::Value*> expected_props_;
    106 };
    107 
    108 TEST_F(CrosSettingsTest, SetPref) {
    109   // Change to something that is not the default.
    110   AddExpectation(kAccountsPrefAllowGuest,
    111                  base::Value::CreateBooleanValue(false));
    112   SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]);
    113   FetchPref(kAccountsPrefAllowGuest);
    114   ASSERT_TRUE(expected_props_.empty());
    115 }
    116 
    117 TEST_F(CrosSettingsTest, GetPref) {
    118   // We didn't change the default so look for it.
    119   AddExpectation(kAccountsPrefAllowGuest,
    120                  base::Value::CreateBooleanValue(true));
    121   FetchPref(kAccountsPrefAllowGuest);
    122 }
    123 
    124 TEST_F(CrosSettingsTest, SetWhitelist) {
    125   // Setting the whitelist should also switch the value of
    126   // kAccountsPrefAllowNewUser to false.
    127   base::ListValue whitelist;
    128   whitelist.Append(new base::StringValue("me@owner"));
    129   AddExpectation(kAccountsPrefAllowNewUser,
    130                  base::Value::CreateBooleanValue(false));
    131   AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
    132   SetPref(kAccountsPrefUsers, &whitelist);
    133   FetchPref(kAccountsPrefAllowNewUser);
    134   FetchPref(kAccountsPrefUsers);
    135 }
    136 
    137 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
    138   base::ListValue* whitelist = new base::ListValue();
    139   base::StringValue hacky_user("h@xxor");
    140   whitelist->Append(hacky_user.DeepCopy());
    141   AddExpectation(kAccountsPrefAllowNewUser,
    142                  base::Value::CreateBooleanValue(false));
    143   AddExpectation(kAccountsPrefUsers, whitelist);
    144   // Add some user to the whitelist.
    145   settings_.AppendToList(kAccountsPrefUsers, &hacky_user);
    146   FetchPref(kAccountsPrefAllowNewUser);
    147   FetchPref(kAccountsPrefUsers);
    148 }
    149 
    150 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) {
    151   base::ListValue whitelist;
    152   base::StringValue hacky_user("h@xxor");
    153   base::StringValue lamy_user("l@mer");
    154   whitelist.Append(hacky_user.DeepCopy());
    155   base::ListValue* expected_list = whitelist.DeepCopy();
    156   whitelist.Append(lamy_user.DeepCopy());
    157   AddExpectation(kAccountsPrefAllowNewUser,
    158                  base::Value::CreateBooleanValue(false));
    159   AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
    160   SetPref(kAccountsPrefUsers, &whitelist);
    161   FetchPref(kAccountsPrefAllowNewUser);
    162   FetchPref(kAccountsPrefUsers);
    163   ASSERT_TRUE(expected_props_.empty());
    164   // Now try to remove one element from that list.
    165   AddExpectation(kAccountsPrefUsers, expected_list);
    166   settings_.RemoveFromList(kAccountsPrefUsers, &lamy_user);
    167   FetchPref(kAccountsPrefAllowNewUser);
    168   FetchPref(kAccountsPrefUsers);
    169 }
    170 
    171 TEST_F(CrosSettingsTest, SetEmptyWhitelist) {
    172   // Setting the whitelist empty should switch the value of
    173   // kAccountsPrefAllowNewUser to true.
    174   base::ListValue whitelist;
    175   AddExpectation(kAccountsPrefAllowNewUser,
    176                  base::Value::CreateBooleanValue(true));
    177   SetPref(kAccountsPrefUsers, &whitelist);
    178   FetchPref(kAccountsPrefAllowNewUser);
    179   FetchPref(kAccountsPrefUsers);
    180 }
    181 
    182 TEST_F(CrosSettingsTest, SetEmptyWhitelistAndNoNewUsers) {
    183   // Setting the whitelist empty and disallowing new users should result in no
    184   // new users allowed.
    185   base::ListValue whitelist;
    186   base::FundamentalValue disallow_new(false);
    187   AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
    188   AddExpectation(kAccountsPrefAllowNewUser,
    189                  base::Value::CreateBooleanValue(false));
    190   SetPref(kAccountsPrefUsers, &whitelist);
    191   SetPref(kAccountsPrefAllowNewUser, &disallow_new);
    192   FetchPref(kAccountsPrefAllowNewUser);
    193   FetchPref(kAccountsPrefUsers);
    194 }
    195 
    196 TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) {
    197   // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
    198   // false (which is the implicit value too).
    199   base::ListValue whitelist;
    200   whitelist.Append(new base::StringValue("me@owner"));
    201   AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
    202   AddExpectation(kAccountsPrefAllowNewUser,
    203                  base::Value::CreateBooleanValue(false));
    204   SetPref(kAccountsPrefUsers, &whitelist);
    205   SetPref(kAccountsPrefAllowNewUser,
    206           expected_props_[kAccountsPrefAllowNewUser]);
    207   FetchPref(kAccountsPrefAllowNewUser);
    208   FetchPref(kAccountsPrefUsers);
    209 }
    210 
    211 TEST_F(CrosSettingsTest, SetAllowNewUsers) {
    212   // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
    213   AddExpectation(kAccountsPrefAllowNewUser,
    214                  base::Value::CreateBooleanValue(true));
    215   SetPref(kAccountsPrefAllowNewUser,
    216           expected_props_[kAccountsPrefAllowNewUser]);
    217   FetchPref(kAccountsPrefAllowNewUser);
    218 }
    219 
    220 TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) {
    221   base::FundamentalValue ephemeral_users_enabled(true);
    222   AddExpectation(kAccountsPrefEphemeralUsersEnabled,
    223                  base::Value::CreateBooleanValue(true));
    224   SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled);
    225   FetchPref(kAccountsPrefEphemeralUsersEnabled);
    226 }
    227 
    228 TEST_F(CrosSettingsTest, FindEmailInList) {
    229   base::ListValue list;
    230   list.Append(new base::StringValue("user (at) example.com"));
    231   list.Append(new base::StringValue("nodomain"));
    232   list.Append(new base::StringValue("with.dots (at) gmail.com"));
    233   list.Append(new base::StringValue("Upper (at) example.com"));
    234 
    235   CrosSettings* cs = &settings_;
    236   cs->Set(kAccountsPrefUsers, list);
    237 
    238   EXPECT_TRUE(IsWhitelisted(cs, "user (at) example.com"));
    239   EXPECT_FALSE(IsWhitelisted(cs, "us.er (at) example.com"));
    240   EXPECT_TRUE(IsWhitelisted(cs, "USER (at) example.com"));
    241   EXPECT_FALSE(IsWhitelisted(cs, "user"));
    242 
    243   EXPECT_TRUE(IsWhitelisted(cs, "nodomain"));
    244   EXPECT_TRUE(IsWhitelisted(cs, "nodomain (at) gmail.com"));
    245   EXPECT_TRUE(IsWhitelisted(cs, "no.domain (at) gmail.com"));
    246   EXPECT_TRUE(IsWhitelisted(cs, "NO.DOMAIN"));
    247 
    248   EXPECT_TRUE(IsWhitelisted(cs, "with.dots (at) gmail.com"));
    249   EXPECT_TRUE(IsWhitelisted(cs, "withdots (at) gmail.com"));
    250   EXPECT_TRUE(IsWhitelisted(cs, "WITH.DOTS (at) gmail.com"));
    251   EXPECT_TRUE(IsWhitelisted(cs, "WITHDOTS"));
    252 
    253   EXPECT_TRUE(IsWhitelisted(cs, "Upper (at) example.com"));
    254   EXPECT_FALSE(IsWhitelisted(cs, "U.pper (at) example.com"));
    255   EXPECT_FALSE(IsWhitelisted(cs, "Upper"));
    256   EXPECT_TRUE(IsWhitelisted(cs, "upper (at) example.com"));
    257 }
    258 
    259 TEST_F(CrosSettingsTest, FindEmailInListWildcard) {
    260   base::ListValue list;
    261   list.Append(new base::StringValue("user (at) example.com"));
    262   list.Append(new base::StringValue("*@example.com"));
    263 
    264   CrosSettings* cs = &settings_;
    265   cs->Set(kAccountsPrefUsers, list);
    266 
    267   bool wildcard_match = false;
    268   EXPECT_TRUE(cs->FindEmailInList(
    269       kAccountsPrefUsers, "test (at) example.com", &wildcard_match));
    270   EXPECT_TRUE(wildcard_match);
    271   EXPECT_TRUE(cs->FindEmailInList(
    272       kAccountsPrefUsers, "user (at) example.com", &wildcard_match));
    273   EXPECT_FALSE(wildcard_match);
    274   EXPECT_TRUE(cs->FindEmailInList(
    275       kAccountsPrefUsers, "*@example.com", &wildcard_match));
    276   EXPECT_TRUE(wildcard_match);
    277 }
    278 
    279 }  // namespace chromeos
    280