| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/prefs/pref_value_map.h" | 6 #include "base/prefs/pref_value_map.h" |
| 7 #include "chrome/browser/policy/url_blacklist_policy_handler.h" | 7 #include "chrome/browser/policy/url_blacklist_policy_handler.h" |
| 8 #include "chrome/common/pref_names.h" | |
| 9 #include "components/policy/core/browser/policy_error_map.h" | 8 #include "components/policy/core/browser/policy_error_map.h" |
| 10 #include "components/policy/core/common/policy_map.h" | 9 #include "components/policy/core/common/policy_map.h" |
| 10 #include "components/policy/core/common/policy_pref_names.h" |
| 11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
| 12 #include "policy/policy_constants.h" | 12 #include "policy/policy_constants.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace policy { | 15 namespace policy { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 const char kTestDisabledScheme[] = "kTestDisabledScheme"; | 18 const char kTestDisabledScheme[] = "kTestDisabledScheme"; |
| 19 const char kTestBlacklistValue[] = "kTestBlacklistValue"; | 19 const char kTestBlacklistValue[] = "kTestBlacklistValue"; |
| 20 } // namespace | 20 } // namespace |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist, | 67 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist, |
| 68 base::Value::CreateBooleanValue(false))); | 68 base::Value::CreateBooleanValue(false))); |
| 69 EXPECT_EQ(1U, errors_.size()); | 69 EXPECT_EQ(1U, errors_.size()); |
| 70 const std::string expected = key::kURLBlacklist; | 70 const std::string expected = key::kURLBlacklist; |
| 71 const std::string actual = errors_.begin()->first; | 71 const std::string actual = errors_.begin()->first; |
| 72 EXPECT_EQ(expected, actual); | 72 EXPECT_EQ(expected, actual); |
| 73 } | 73 } |
| 74 | 74 |
| 75 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_NothingSpecified) { | 75 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_NothingSpecified) { |
| 76 ApplyPolicies(); | 76 ApplyPolicies(); |
| 77 EXPECT_FALSE(prefs_.GetValue(prefs::kUrlBlacklist, NULL)); | 77 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 TEST_F(URLBlacklistPolicyHandlerTest, | 80 TEST_F(URLBlacklistPolicyHandlerTest, |
| 81 ApplyPolicySettings_DisabledSchemesWrongType) { | 81 ApplyPolicySettings_DisabledSchemesWrongType) { |
| 82 // The policy expects a list. Give it a boolean. | 82 // The policy expects a list. Give it a boolean. |
| 83 SetPolicy(key::kDisabledSchemes, base::Value::CreateBooleanValue(false)); | 83 SetPolicy(key::kDisabledSchemes, base::Value::CreateBooleanValue(false)); |
| 84 ApplyPolicies(); | 84 ApplyPolicies(); |
| 85 EXPECT_FALSE(prefs_.GetValue(prefs::kUrlBlacklist, NULL)); | 85 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 TEST_F(URLBlacklistPolicyHandlerTest, | 88 TEST_F(URLBlacklistPolicyHandlerTest, |
| 89 ApplyPolicySettings_URLBlacklistWrongType) { | 89 ApplyPolicySettings_URLBlacklistWrongType) { |
| 90 // The policy expects a list. Give it a boolean. | 90 // The policy expects a list. Give it a boolean. |
| 91 SetPolicy(key::kURLBlacklist, base::Value::CreateBooleanValue(false)); | 91 SetPolicy(key::kURLBlacklist, base::Value::CreateBooleanValue(false)); |
| 92 ApplyPolicies(); | 92 ApplyPolicies(); |
| 93 EXPECT_FALSE(prefs_.GetValue(prefs::kUrlBlacklist, NULL)); | 93 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 TEST_F(URLBlacklistPolicyHandlerTest, | 96 TEST_F(URLBlacklistPolicyHandlerTest, |
| 97 ApplyPolicySettings_DisabledSchemesEmpty) { | 97 ApplyPolicySettings_DisabledSchemesEmpty) { |
| 98 SetPolicy(key::kDisabledSchemes, new base::ListValue); | 98 SetPolicy(key::kDisabledSchemes, new base::ListValue); |
| 99 ApplyPolicies(); | 99 ApplyPolicies(); |
| 100 base::Value* out; | 100 base::Value* out; |
| 101 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 101 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 102 base::ListValue* out_list; | 102 base::ListValue* out_list; |
| 103 EXPECT_TRUE(out->GetAsList(&out_list)); | 103 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 104 EXPECT_EQ(0U, out_list->GetSize()); | 104 EXPECT_EQ(0U, out_list->GetSize()); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST_F(URLBlacklistPolicyHandlerTest, | 107 TEST_F(URLBlacklistPolicyHandlerTest, |
| 108 ApplyPolicySettings_URLBlacklistEmpty) { | 108 ApplyPolicySettings_URLBlacklistEmpty) { |
| 109 SetPolicy(key::kURLBlacklist, new base::ListValue); | 109 SetPolicy(key::kURLBlacklist, new base::ListValue); |
| 110 ApplyPolicies(); | 110 ApplyPolicies(); |
| 111 base::Value* out; | 111 base::Value* out; |
| 112 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 112 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 113 base::ListValue* out_list; | 113 base::ListValue* out_list; |
| 114 EXPECT_TRUE(out->GetAsList(&out_list)); | 114 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 115 EXPECT_EQ(0U, out_list->GetSize()); | 115 EXPECT_EQ(0U, out_list->GetSize()); |
| 116 } | 116 } |
| 117 | 117 |
| 118 TEST_F(URLBlacklistPolicyHandlerTest, | 118 TEST_F(URLBlacklistPolicyHandlerTest, |
| 119 ApplyPolicySettings_DisabledSchemesWrongElementType) { | 119 ApplyPolicySettings_DisabledSchemesWrongElementType) { |
| 120 // The policy expects string-valued elements. Give it booleans. | 120 // The policy expects string-valued elements. Give it booleans. |
| 121 scoped_ptr<base::ListValue> in(new base::ListValue); | 121 scoped_ptr<base::ListValue> in(new base::ListValue); |
| 122 in->AppendBoolean(false); | 122 in->AppendBoolean(false); |
| 123 SetPolicy(key::kDisabledSchemes, in.release()); | 123 SetPolicy(key::kDisabledSchemes, in.release()); |
| 124 ApplyPolicies(); | 124 ApplyPolicies(); |
| 125 | 125 |
| 126 // The element should be skipped. | 126 // The element should be skipped. |
| 127 base::Value* out; | 127 base::Value* out; |
| 128 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 128 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 129 base::ListValue* out_list; | 129 base::ListValue* out_list; |
| 130 EXPECT_TRUE(out->GetAsList(&out_list)); | 130 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 131 EXPECT_EQ(0U, out_list->GetSize()); | 131 EXPECT_EQ(0U, out_list->GetSize()); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST_F(URLBlacklistPolicyHandlerTest, | 134 TEST_F(URLBlacklistPolicyHandlerTest, |
| 135 ApplyPolicySettings_URLBlacklistWrongElementType) { | 135 ApplyPolicySettings_URLBlacklistWrongElementType) { |
| 136 // The policy expects string-valued elements. Give it booleans. | 136 // The policy expects string-valued elements. Give it booleans. |
| 137 scoped_ptr<base::ListValue> in(new base::ListValue); | 137 scoped_ptr<base::ListValue> in(new base::ListValue); |
| 138 in->AppendBoolean(false); | 138 in->AppendBoolean(false); |
| 139 SetPolicy(key::kURLBlacklist, in.release()); | 139 SetPolicy(key::kURLBlacklist, in.release()); |
| 140 ApplyPolicies(); | 140 ApplyPolicies(); |
| 141 | 141 |
| 142 // The element should be skipped. | 142 // The element should be skipped. |
| 143 base::Value* out; | 143 base::Value* out; |
| 144 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 144 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 145 base::ListValue* out_list; | 145 base::ListValue* out_list; |
| 146 EXPECT_TRUE(out->GetAsList(&out_list)); | 146 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 147 EXPECT_EQ(0U, out_list->GetSize()); | 147 EXPECT_EQ(0U, out_list->GetSize()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 TEST_F(URLBlacklistPolicyHandlerTest, | 150 TEST_F(URLBlacklistPolicyHandlerTest, |
| 151 ApplyPolicySettings_DisabledSchemesSuccessful) { | 151 ApplyPolicySettings_DisabledSchemesSuccessful) { |
| 152 scoped_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); | 152 scoped_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); |
| 153 in_disabled_schemes->AppendString(kTestDisabledScheme); | 153 in_disabled_schemes->AppendString(kTestDisabledScheme); |
| 154 SetPolicy(key::kDisabledSchemes, in_disabled_schemes.release()); | 154 SetPolicy(key::kDisabledSchemes, in_disabled_schemes.release()); |
| 155 ApplyPolicies(); | 155 ApplyPolicies(); |
| 156 | 156 |
| 157 base::Value* out; | 157 base::Value* out; |
| 158 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 158 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 159 base::ListValue* out_list; | 159 base::ListValue* out_list; |
| 160 EXPECT_TRUE(out->GetAsList(&out_list)); | 160 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 161 EXPECT_EQ(1U, out_list->GetSize()); | 161 EXPECT_EQ(1U, out_list->GetSize()); |
| 162 | 162 |
| 163 std::string out_string; | 163 std::string out_string; |
| 164 EXPECT_TRUE(out_list->GetString(0U, &out_string)); | 164 EXPECT_TRUE(out_list->GetString(0U, &out_string)); |
| 165 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out_string); | 165 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out_string); |
| 166 } | 166 } |
| 167 | 167 |
| 168 TEST_F(URLBlacklistPolicyHandlerTest, | 168 TEST_F(URLBlacklistPolicyHandlerTest, |
| 169 ApplyPolicySettings_URLBlacklistSuccessful) { | 169 ApplyPolicySettings_URLBlacklistSuccessful) { |
| 170 scoped_ptr<base::ListValue> in_url_blacklist(new base::ListValue); | 170 scoped_ptr<base::ListValue> in_url_blacklist(new base::ListValue); |
| 171 in_url_blacklist->AppendString(kTestBlacklistValue); | 171 in_url_blacklist->AppendString(kTestBlacklistValue); |
| 172 SetPolicy(key::kURLBlacklist, in_url_blacklist.release()); | 172 SetPolicy(key::kURLBlacklist, in_url_blacklist.release()); |
| 173 ApplyPolicies(); | 173 ApplyPolicies(); |
| 174 | 174 |
| 175 base::Value* out; | 175 base::Value* out; |
| 176 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 176 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 177 base::ListValue* out_list; | 177 base::ListValue* out_list; |
| 178 EXPECT_TRUE(out->GetAsList(&out_list)); | 178 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 179 EXPECT_EQ(1U, out_list->GetSize()); | 179 EXPECT_EQ(1U, out_list->GetSize()); |
| 180 | 180 |
| 181 std::string out_string; | 181 std::string out_string; |
| 182 EXPECT_TRUE(out_list->GetString(0U, &out_string)); | 182 EXPECT_TRUE(out_list->GetString(0U, &out_string)); |
| 183 EXPECT_EQ(kTestBlacklistValue, out_string); | 183 EXPECT_EQ(kTestBlacklistValue, out_string); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_MergeSuccessful) { | 186 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_MergeSuccessful) { |
| 187 scoped_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); | 187 scoped_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); |
| 188 in_disabled_schemes->AppendString(kTestDisabledScheme); | 188 in_disabled_schemes->AppendString(kTestDisabledScheme); |
| 189 SetPolicy(key::kDisabledSchemes, in_disabled_schemes.release()); | 189 SetPolicy(key::kDisabledSchemes, in_disabled_schemes.release()); |
| 190 | 190 |
| 191 scoped_ptr<base::ListValue> in_url_blacklist(new base::ListValue); | 191 scoped_ptr<base::ListValue> in_url_blacklist(new base::ListValue); |
| 192 in_url_blacklist->AppendString(kTestBlacklistValue); | 192 in_url_blacklist->AppendString(kTestBlacklistValue); |
| 193 SetPolicy(key::kURLBlacklist, in_url_blacklist.release()); | 193 SetPolicy(key::kURLBlacklist, in_url_blacklist.release()); |
| 194 | 194 |
| 195 ApplyPolicies(); | 195 ApplyPolicies(); |
| 196 | 196 |
| 197 base::Value* out; | 197 base::Value* out; |
| 198 EXPECT_TRUE(prefs_.GetValue(prefs::kUrlBlacklist, &out)); | 198 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 199 base::ListValue* out_list; | 199 base::ListValue* out_list; |
| 200 EXPECT_TRUE(out->GetAsList(&out_list)); | 200 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 201 EXPECT_EQ(2U, out_list->GetSize()); | 201 EXPECT_EQ(2U, out_list->GetSize()); |
| 202 | 202 |
| 203 std::string out1; | 203 std::string out1; |
| 204 EXPECT_TRUE(out_list->GetString(0U, &out1)); | 204 EXPECT_TRUE(out_list->GetString(0U, &out1)); |
| 205 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out1); | 205 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out1); |
| 206 | 206 |
| 207 std::string out2; | 207 std::string out2; |
| 208 EXPECT_TRUE(out_list->GetString(1U, &out2)); | 208 EXPECT_TRUE(out_list->GetString(1U, &out2)); |
| 209 EXPECT_EQ(kTestBlacklistValue, out2); | 209 EXPECT_EQ(kTestBlacklistValue, out2); |
| 210 } | 210 } |
| 211 | 211 |
| 212 } // namespace policy | 212 } // namespace policy |
| OLD | NEW |