1 // Copyright 2014 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 "base/json/json_reader.h" 6 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h" 7 #include "components/bookmarks/common/bookmark_pref_names.h" 8 #include "components/policy/core/browser/configuration_policy_pref_store.h" 9 #include "components/policy/core/browser/configuration_policy_pref_store_test.h" 10 #include "components/policy/core/common/policy_map.h" 11 #include "components/policy/core/common/schema.h" 12 #include "policy/policy_constants.h" 13 14 #if defined(ENABLE_EXTENSIONS) 15 #include "extensions/common/value_builder.h" 16 #endif 17 18 namespace policy { 19 20 class ManagedBookmarksPolicyHandlerTest 21 : public ConfigurationPolicyPrefStoreTest { 22 virtual void SetUp() OVERRIDE { 23 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); 24 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( 25 new ManagedBookmarksPolicyHandler(chrome_schema))); 26 } 27 }; 28 29 #if defined(ENABLE_EXTENSIONS) 30 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) { 31 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); 32 33 PolicyMap policy; 34 policy.Set(key::kManagedBookmarks, 35 POLICY_LEVEL_MANDATORY, 36 POLICY_SCOPE_USER, 37 base::JSONReader::Read( 38 "[" 39 " {" 40 " \"name\": \"Google\"," 41 " \"url\": \"google.com\"" 42 " }," 43 " {" 44 " \"name\": \"Empty Folder\"," 45 " \"children\": []" 46 " }," 47 " {" 48 " \"name\": \"Big Folder\"," 49 " \"children\": [" 50 " {" 51 " \"name\": \"Youtube\"," 52 " \"url\": \"youtube.com\"" 53 " }," 54 " {" 55 " \"name\": \"Chromium\"," 56 " \"url\": \"chromium.org\"" 57 " }," 58 " {" 59 " \"name\": \"More Stuff\"," 60 " \"children\": [" 61 " {" 62 " \"name\": \"Bugs\"," 63 " \"url\": \"crbug.com\"" 64 " }" 65 " ]" 66 " }" 67 " ]" 68 " }" 69 "]"), 70 NULL); 71 UpdateProviderPolicy(policy); 72 const base::Value* pref_value = NULL; 73 EXPECT_TRUE( 74 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); 75 ASSERT_TRUE(pref_value); 76 77 scoped_ptr<base::Value> expected( 78 extensions::ListBuilder() 79 .Append(extensions::DictionaryBuilder() 80 .Set("name", "Google") 81 .Set("url", "http://google.com/")) 82 .Append(extensions::DictionaryBuilder() 83 .Set("name", "Empty Folder") 84 .Set("children", extensions::ListBuilder().Pass())) 85 .Append(extensions::DictionaryBuilder() 86 .Set("name", "Big Folder") 87 .Set("children", extensions::ListBuilder() 88 .Append(extensions::DictionaryBuilder() 89 .Set("name", "Youtube") 90 .Set("url", "http://youtube.com/")) 91 .Append(extensions::DictionaryBuilder() 92 .Set("name", "Chromium") 93 .Set("url", "http://chromium.org/")) 94 .Append(extensions::DictionaryBuilder() 95 .Set("name", "More Stuff") 96 .Set("children", extensions::ListBuilder() 97 .Append(extensions::DictionaryBuilder() 98 .Set("name", "Bugs") 99 .Set("url", "http://crbug.com/") 100 .Pass()) 101 .Pass()) 102 .Pass()) 103 .Pass()) 104 .Pass()) 105 .Build()); 106 EXPECT_TRUE(pref_value->Equals(expected.get())); 107 } 108 #endif // defined(ENABLE_EXTENSIONS) 109 110 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) { 111 PolicyMap policy; 112 // The expected type is base::ListValue, but this policy sets it as an 113 // unparsed base::StringValue. Any type other than ListValue should fail. 114 policy.Set(key::kManagedBookmarks, 115 POLICY_LEVEL_MANDATORY, 116 POLICY_SCOPE_USER, 117 new base::StringValue( 118 "[" 119 " {" 120 " \"name\": \"Google\"," 121 " \"url\": \"google.com\"" 122 " }," 123 "]"), 124 NULL); 125 UpdateProviderPolicy(policy); 126 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); 127 } 128 129 #if defined(ENABLE_EXTENSIONS) 130 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) { 131 PolicyMap policy; 132 policy.Set(key::kManagedBookmarks, 133 POLICY_LEVEL_MANDATORY, 134 POLICY_SCOPE_USER, 135 base::JSONReader::Read( 136 "[" 137 " {" 138 " \"name\": \"Google\"," 139 " \"unknown\": \"should be ignored\"," 140 " \"url\": \"google.com\"" 141 " }" 142 "]"), 143 NULL); 144 UpdateProviderPolicy(policy); 145 const base::Value* pref_value = NULL; 146 EXPECT_TRUE( 147 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); 148 ASSERT_TRUE(pref_value); 149 150 scoped_ptr<base::Value> expected( 151 extensions::ListBuilder() 152 .Append(extensions::DictionaryBuilder() 153 .Set("name", "Google") 154 .Set("url", "http://google.com/")) 155 .Build()); 156 EXPECT_TRUE(pref_value->Equals(expected.get())); 157 } 158 #endif 159 160 #if defined(ENABLE_EXTENSIONS) 161 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) { 162 PolicyMap policy; 163 policy.Set(key::kManagedBookmarks, 164 POLICY_LEVEL_MANDATORY, 165 POLICY_SCOPE_USER, 166 base::JSONReader::Read( 167 "[" 168 " {" 169 " \"name\": \"Empty\"," 170 " \"url\": \"\"" 171 " }," 172 " {" 173 " \"name\": \"Invalid type\"," 174 " \"url\": 4" 175 " }," 176 " {" 177 " \"name\": \"Invalid URL\"," 178 " \"url\": \"?\"" 179 " }," 180 " {" 181 " \"name\": \"Google\"," 182 " \"url\": \"google.com\"" 183 " }" 184 "]"), 185 NULL); 186 UpdateProviderPolicy(policy); 187 const base::Value* pref_value = NULL; 188 EXPECT_TRUE( 189 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); 190 ASSERT_TRUE(pref_value); 191 192 scoped_ptr<base::Value> expected( 193 extensions::ListBuilder() 194 .Append(extensions::DictionaryBuilder() 195 .Set("name", "Google") 196 .Set("url", "http://google.com/")) 197 .Build()); 198 EXPECT_TRUE(pref_value->Equals(expected.get())); 199 } 200 #endif 201 202 } // namespace policy 203