Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: chrome/browser/extensions/extension_management_unittest.cc

Issue 499313002: Add ExtensionManagement class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: fixes to #5 Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 <algorithm>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_management.h"
12 #include "chrome/browser/extensions/external_policy_loader.h"
13 #include "extensions/browser/pref_names.h"
14 #include "extensions/common/url_pattern.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace extensions {
18
19 namespace {
20 const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop";
21 const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
22 const char kExampleUpdateUrl[] = "http://example.com/update_url";
23 } // namespace
24
25 class ExtensionManagementTest : public testing::Test {
26 public:
27 ExtensionManagementTest() {}
28 virtual ~ExtensionManagementTest() {}
29
30 // testing::Test:
31 virtual void SetUp() OVERRIDE {
32 pref_service_.reset(new TestingPrefServiceSimple());
33 pref_service_->registry()->RegisterListPref(
34 pref_names::kAllowedInstallSites);
35 pref_service_->registry()->RegisterListPref(pref_names::kAllowedTypes);
36 pref_service_->registry()->RegisterListPref(pref_names::kInstallDenyList);
37 pref_service_->registry()->RegisterListPref(pref_names::kInstallAllowList);
38 pref_service_->registry()->RegisterDictionaryPref(
39 pref_names::kInstallForceList);
40 extension_management_.reset(new ExtensionManagement(pref_service_.get()));
41 }
42
43 void SetPref(bool managed, const char* path, base::Value* value) {
44 if (managed)
45 pref_service_->SetManagedPref(path, value);
46 else
47 pref_service_->SetUserPref(path, value);
48 }
49
50 void RemovePref(bool managed, const char* path) {
51 if (managed)
52 pref_service_->RemoveManagedPref(path);
53 else
54 pref_service_->RemoveUserPref(path);
55 }
56
57 void Refresh() {
58 extension_management_->Refresh();
59 }
60
61 protected:
62 scoped_ptr<TestingPrefServiceSimple> pref_service_;
63 scoped_ptr<ExtensionManagement> extension_management_;
64 };
65
66 // Verify that preference controlled by legacy ExtensionInstallSources policy is
67 // handled well.
68 TEST_F(ExtensionManagementTest, LegacyInstallSources) {
69 base::ListValue allowed_sites_pref;
70 allowed_sites_pref.AppendString("https://www.example.com/foo");
71 allowed_sites_pref.AppendString("https://corp.mycompany.com/*");
72 SetPref(
73 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy());
74 Refresh();
75 const URLPatternSet& allowed_sites =
76 extension_management_->ReadGlobalSettings().install_sources;
77 ASSERT_TRUE(extension_management_->ReadGlobalSettings()
78 .has_restricted_install_sources);
79 EXPECT_FALSE(allowed_sites.is_empty());
80 EXPECT_TRUE(allowed_sites.MatchesURL(GURL("https://www.example.com/foo")));
81 EXPECT_FALSE(allowed_sites.MatchesURL(GURL("https://www.example.com/bar")));
82 EXPECT_TRUE(
83 allowed_sites.MatchesURL(GURL("https://corp.mycompany.com/entry")));
84 EXPECT_FALSE(
85 allowed_sites.MatchesURL(GURL("https://www.mycompany.com/entry")));
86 }
87
88 // Verify that preference controlled by legacy ExtensionAllowedTypes policy is
89 // handled well.
90 TEST_F(ExtensionManagementTest, LegacyAllowedTypes) {
91 base::ListValue allowed_types_pref;
92 allowed_types_pref.AppendInteger(Manifest::TYPE_THEME);
93 allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT);
94
95 SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy());
96 Refresh();
97 const std::vector<Manifest::Type>& allowed_types =
98 extension_management_->ReadGlobalSettings().allowed_types;
99 ASSERT_TRUE(
100 extension_management_->ReadGlobalSettings().has_restricted_allowed_types);
101 EXPECT_TRUE(allowed_types.size() == 2);
102 EXPECT_FALSE(std::find(allowed_types.begin(),
103 allowed_types.end(),
104 Manifest::TYPE_EXTENSION) != allowed_types.end());
105 EXPECT_TRUE(std::find(allowed_types.begin(),
106 allowed_types.end(),
107 Manifest::TYPE_THEME) != allowed_types.end());
108 EXPECT_TRUE(std::find(allowed_types.begin(),
109 allowed_types.end(),
110 Manifest::TYPE_USER_SCRIPT) != allowed_types.end());
111 }
112
113 // Verify that preference controlled by legacy ExtensionInstallBlacklist policy
114 // is handled well.
115 TEST_F(ExtensionManagementTest, LegacyInstallBlacklist) {
116 base::ListValue denied_list_pref;
117 denied_list_pref.AppendString(kTargetExtension);
118
119 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
120 Refresh();
121 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
122 ExtensionManagement::INSTALLATION_BLOCKED);
123 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
124 ExtensionManagement::INSTALLATION_ALLOWED);
125 }
126
127 // Verify that preference controlled by legacy ExtensionInstallWhitelist policy
128 // is handled well.
129 TEST_F(ExtensionManagementTest, LegacyInstallWhitelist) {
130 base::ListValue denied_list_pref;
131 denied_list_pref.AppendString("*");
132 base::ListValue allowed_list_pref;
133 allowed_list_pref.AppendString(kTargetExtension);
134
135 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
136 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
137 Refresh();
138 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
139 ExtensionManagement::INSTALLATION_ALLOWED);
140 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
141 ExtensionManagement::INSTALLATION_BLOCKED);
142
143 // Verify that install whitelist preference set by user is ignored.
144 RemovePref(true, pref_names::kInstallAllowList);
145 SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
146 Refresh();
147 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
148 ExtensionManagement::INSTALLATION_BLOCKED);
149 }
150
151 // Verify that preference controlled by legacy ExtensionInstallForcelist policy
152 // is handled well.
153 TEST_F(ExtensionManagementTest, LegacyInstallForcelist) {
154 base::DictionaryValue forced_list_pref;
155 ExternalPolicyLoader::AddExtension(
156 &forced_list_pref, kTargetExtension, kExampleUpdateUrl);
157
158 SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
159 Refresh();
160 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
161 ExtensionManagement::INSTALLATION_FORCED);
162 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url,
163 kExampleUpdateUrl);
164 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
165 ExtensionManagement::INSTALLATION_ALLOWED);
166
167 // Verify that install forcelist preference set by user is ignored.
168 RemovePref(true, pref_names::kInstallForceList);
169 SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
170 Refresh();
171 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
172 ExtensionManagement::INSTALLATION_ALLOWED);
173 }
174
175 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698