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/policy/policy_map.h" 6 7 #include "base/callback.h" 8 #include "base/memory/weak_ptr.h" 9 #include "chrome/browser/policy/external_data_manager.h" 10 #include "policy/policy_constants.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace policy { 14 15 namespace { 16 17 // Utility functions for the tests. 18 void SetPolicy(PolicyMap* map, const char* name, Value* value) { 19 map->Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value, NULL); 20 } 21 22 void SetPolicy(PolicyMap* map, 23 const char* name, 24 ExternalDataFetcher* external_data_fetcher) { 25 map->Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 26 NULL, external_data_fetcher); 27 } 28 29 } // namespace 30 31 class PolicyMapTest : public testing::Test { 32 protected: 33 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher( 34 const std::string& policy) const; 35 }; 36 37 scoped_ptr<ExternalDataFetcher> PolicyMapTest::CreateExternalDataFetcher( 38 const std::string& policy) const { 39 return make_scoped_ptr( 40 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), policy)); 41 } 42 43 TEST_F(PolicyMapTest, SetAndGet) { 44 PolicyMap map; 45 SetPolicy(&map, key::kHomepageLocation, Value::CreateStringValue("aaa")); 46 StringValue expected("aaa"); 47 EXPECT_TRUE(expected.Equals(map.GetValue(key::kHomepageLocation))); 48 SetPolicy(&map, key::kHomepageLocation, Value::CreateStringValue("bbb")); 49 StringValue expected_b("bbb"); 50 EXPECT_TRUE(expected_b.Equals(map.GetValue(key::kHomepageLocation))); 51 SetPolicy(&map, key::kHomepageLocation, 52 CreateExternalDataFetcher("dummy").release()); 53 EXPECT_FALSE(map.GetValue(key::kHomepageLocation)); 54 const PolicyMap::Entry* entry = map.Get(key::kHomepageLocation); 55 ASSERT_TRUE(entry != NULL); 56 EXPECT_EQ(POLICY_LEVEL_MANDATORY, entry->level); 57 EXPECT_EQ(POLICY_SCOPE_USER, entry->scope); 58 EXPECT_TRUE(ExternalDataFetcher::Equals( 59 entry->external_data_fetcher, CreateExternalDataFetcher("dummy").get())); 60 map.Set(key::kHomepageLocation, POLICY_LEVEL_RECOMMENDED, 61 POLICY_SCOPE_MACHINE, NULL, NULL); 62 EXPECT_FALSE(map.GetValue(key::kHomepageLocation)); 63 entry = map.Get(key::kHomepageLocation); 64 ASSERT_TRUE(entry != NULL); 65 EXPECT_EQ(POLICY_LEVEL_RECOMMENDED, entry->level); 66 EXPECT_EQ(POLICY_SCOPE_MACHINE, entry->scope); 67 EXPECT_FALSE(entry->external_data_fetcher); 68 } 69 70 TEST_F(PolicyMapTest, Equals) { 71 PolicyMap a; 72 SetPolicy(&a, key::kHomepageLocation, Value::CreateStringValue("aaa")); 73 PolicyMap a2; 74 SetPolicy(&a2, key::kHomepageLocation, Value::CreateStringValue("aaa")); 75 PolicyMap b; 76 SetPolicy(&b, key::kHomepageLocation, Value::CreateStringValue("bbb")); 77 PolicyMap c; 78 SetPolicy(&c, key::kHomepageLocation, Value::CreateStringValue("aaa")); 79 SetPolicy(&c, key::kHomepageIsNewTabPage, Value::CreateBooleanValue(true)); 80 PolicyMap d; 81 SetPolicy(&d, key::kHomepageLocation, 82 CreateExternalDataFetcher("ddd").release()); 83 PolicyMap d2; 84 SetPolicy(&d2, key::kHomepageLocation, 85 CreateExternalDataFetcher("ddd").release()); 86 PolicyMap e; 87 SetPolicy(&e, key::kHomepageLocation, 88 CreateExternalDataFetcher("eee").release()); 89 EXPECT_FALSE(a.Equals(b)); 90 EXPECT_FALSE(a.Equals(c)); 91 EXPECT_FALSE(a.Equals(d)); 92 EXPECT_FALSE(a.Equals(e)); 93 EXPECT_FALSE(b.Equals(a)); 94 EXPECT_FALSE(b.Equals(c)); 95 EXPECT_FALSE(b.Equals(d)); 96 EXPECT_FALSE(b.Equals(e)); 97 EXPECT_FALSE(c.Equals(a)); 98 EXPECT_FALSE(c.Equals(b)); 99 EXPECT_FALSE(c.Equals(d)); 100 EXPECT_FALSE(c.Equals(e)); 101 EXPECT_FALSE(d.Equals(a)); 102 EXPECT_FALSE(d.Equals(b)); 103 EXPECT_FALSE(d.Equals(c)); 104 EXPECT_FALSE(d.Equals(e)); 105 EXPECT_FALSE(e.Equals(a)); 106 EXPECT_FALSE(e.Equals(b)); 107 EXPECT_FALSE(e.Equals(c)); 108 EXPECT_FALSE(e.Equals(d)); 109 EXPECT_TRUE(a.Equals(a2)); 110 EXPECT_TRUE(a2.Equals(a)); 111 EXPECT_TRUE(d.Equals(d2)); 112 EXPECT_TRUE(d2.Equals(d)); 113 PolicyMap empty1; 114 PolicyMap empty2; 115 EXPECT_TRUE(empty1.Equals(empty2)); 116 EXPECT_TRUE(empty2.Equals(empty1)); 117 EXPECT_FALSE(empty1.Equals(a)); 118 EXPECT_FALSE(a.Equals(empty1)); 119 } 120 121 TEST_F(PolicyMapTest, Swap) { 122 PolicyMap a; 123 SetPolicy(&a, key::kHomepageLocation, Value::CreateStringValue("aaa")); 124 SetPolicy(&a, key::kAlternateErrorPagesEnabled, 125 CreateExternalDataFetcher("dummy").release()); 126 PolicyMap b; 127 SetPolicy(&b, key::kHomepageLocation, Value::CreateStringValue("bbb")); 128 SetPolicy(&b, key::kHomepageIsNewTabPage, Value::CreateBooleanValue(true)); 129 130 a.Swap(&b); 131 base::StringValue expected("bbb"); 132 EXPECT_TRUE(expected.Equals(a.GetValue(key::kHomepageLocation))); 133 base::FundamentalValue expected_bool(true); 134 EXPECT_TRUE(expected_bool.Equals(a.GetValue(key::kHomepageIsNewTabPage))); 135 EXPECT_FALSE(a.GetValue(key::kAlternateErrorPagesEnabled)); 136 EXPECT_FALSE(a.Get(key::kAlternateErrorPagesEnabled)); 137 StringValue expected_a("aaa"); 138 EXPECT_TRUE(expected_a.Equals(b.GetValue(key::kHomepageLocation))); 139 EXPECT_FALSE(b.GetValue(key::kHomepageIsNewTabPage)); 140 EXPECT_FALSE(a.GetValue(key::kAlternateErrorPagesEnabled)); 141 const PolicyMap::Entry* entry = b.Get(key::kAlternateErrorPagesEnabled); 142 ASSERT_TRUE(entry); 143 EXPECT_TRUE(ExternalDataFetcher::Equals( 144 CreateExternalDataFetcher("dummy").get(), entry->external_data_fetcher)); 145 146 b.Clear(); 147 a.Swap(&b); 148 PolicyMap empty; 149 EXPECT_TRUE(a.Equals(empty)); 150 EXPECT_FALSE(b.Equals(empty)); 151 } 152 153 TEST_F(PolicyMapTest, MergeFrom) { 154 PolicyMap a; 155 a.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 156 Value::CreateStringValue("google.com"), NULL); 157 a.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 158 Value::CreateBooleanValue(true), NULL); 159 a.Set(key::kAlternateErrorPagesEnabled, 160 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 161 NULL, CreateExternalDataFetcher("a").release()); 162 a.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 163 Value::CreateBooleanValue(false), NULL); 164 a.Set(key::kDefaultSearchProviderSearchURL, 165 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, 166 Value::CreateStringValue("google.com/q={x}"), NULL); 167 168 PolicyMap b; 169 b.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 170 Value::CreateStringValue("chromium.org"), NULL); 171 b.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 172 Value::CreateBooleanValue(false), NULL); 173 b.Set(key::kAlternateErrorPagesEnabled, 174 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 175 NULL, CreateExternalDataFetcher("b").release()); 176 b.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_RECOMMENDED, 177 POLICY_SCOPE_MACHINE, Value::CreateBooleanValue(true), NULL); 178 b.Set(key::kDefaultSearchProviderSearchURL, 179 POLICY_LEVEL_MANDATORY, 180 POLICY_SCOPE_MACHINE, 181 Value::CreateStringValue(std::string()), 182 NULL); 183 b.Set(key::kDisableSpdy, 184 POLICY_LEVEL_RECOMMENDED, 185 POLICY_SCOPE_USER, 186 Value::CreateBooleanValue(true), 187 NULL); 188 189 a.MergeFrom(b); 190 191 PolicyMap c; 192 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER. 193 c.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 194 Value::CreateStringValue("chromium.org"), NULL); 195 // |a| has precedence over |b|. 196 c.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 197 Value::CreateBooleanValue(true), NULL); 198 c.Set(key::kAlternateErrorPagesEnabled, 199 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 200 NULL, CreateExternalDataFetcher("a").release()); 201 // POLICY_SCOPE_MACHINE over POLICY_SCOPE_USER for POLICY_LEVEL_RECOMMENDED. 202 c.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_RECOMMENDED, 203 POLICY_SCOPE_MACHINE, Value::CreateBooleanValue(true), NULL); 204 // POLICY_LEVEL_MANDATORY over POLICY_LEVEL_RECOMMENDED. 205 c.Set(key::kDefaultSearchProviderSearchURL, 206 POLICY_LEVEL_MANDATORY, 207 POLICY_SCOPE_MACHINE, 208 Value::CreateStringValue(std::string()), 209 NULL); 210 // Merge new ones. 211 c.Set(key::kDisableSpdy, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 212 Value::CreateBooleanValue(true), NULL); 213 214 EXPECT_TRUE(a.Equals(c)); 215 } 216 217 TEST_F(PolicyMapTest, GetDifferingKeys) { 218 PolicyMap a; 219 a.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 220 Value::CreateStringValue("google.com"), NULL); 221 a.Set(key::kSearchSuggestEnabled, 222 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 223 NULL, CreateExternalDataFetcher("dummy").release()); 224 a.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 225 Value::CreateBooleanValue(true), NULL); 226 a.Set(key::kAlternateErrorPagesEnabled, 227 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 228 NULL, CreateExternalDataFetcher("a").release()); 229 a.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 230 Value::CreateBooleanValue(false), NULL); 231 a.Set(key::kDefaultSearchProviderSearchURL, 232 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, 233 Value::CreateStringValue("google.com/q={x}"), NULL); 234 a.Set(key::kDisable3DAPIs, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 235 Value::CreateBooleanValue(true), NULL); 236 237 PolicyMap b; 238 b.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 239 Value::CreateStringValue("google.com"), NULL); 240 b.Set(key::kSearchSuggestEnabled, 241 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 242 NULL, CreateExternalDataFetcher("dummy").release()); 243 b.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 244 Value::CreateBooleanValue(false), NULL); 245 b.Set(key::kAlternateErrorPagesEnabled, 246 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 247 NULL, CreateExternalDataFetcher("b").release()); 248 b.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 249 Value::CreateBooleanValue(false), NULL); 250 b.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_RECOMMENDED, 251 POLICY_SCOPE_USER, Value::CreateStringValue("google.com/q={x}"), NULL); 252 b.Set(key::kDisableSpdy, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 253 Value::CreateBooleanValue(true), NULL); 254 255 std::set<std::string> diff; 256 std::set<std::string> diff2; 257 a.GetDifferingKeys(b, &diff); 258 b.GetDifferingKeys(a, &diff2); 259 // Order shouldn't matter. 260 EXPECT_EQ(diff, diff2); 261 // No change. 262 EXPECT_TRUE(diff.find(key::kHomepageLocation) == diff.end()); 263 EXPECT_TRUE(diff.find(key::kSearchSuggestEnabled) == diff.end()); 264 // Different values. 265 EXPECT_TRUE(diff.find(key::kShowHomeButton) != diff.end()); 266 // Different external data references. 267 EXPECT_TRUE(diff.find(key::kAlternateErrorPagesEnabled) != diff.end()); 268 // Different levels. 269 EXPECT_TRUE(diff.find(key::kBookmarkBarEnabled) != diff.end()); 270 // Different scopes. 271 EXPECT_TRUE(diff.find(key::kDefaultSearchProviderSearchURL) != diff.end()); 272 // Not in |a|. 273 EXPECT_TRUE(diff.find(key::kDisableSpdy) != diff.end()); 274 // Not in |b|. 275 EXPECT_TRUE(diff.find(key::kDisable3DAPIs) != diff.end()); 276 // No surprises. 277 EXPECT_EQ(6u, diff.size()); 278 } 279 280 } // namespace policy 281