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

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: rebase minor robustness improvement 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 std::string kTargetExtension = "abcdefghijklmnopabcdefghijklmnop";
Joao da Silva 2014/09/02 19:34:02 Make this const char kTargetExtension[] = "..."; (
binjin 2014/09/02 20:02:56 Done.
21 const std::string kOtherExtension = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
22 const std::string kExampleUpdateUrl = "http://example.com/update_url";
23 };
Joao da Silva 2014/09/02 19:34:02 nit: no ";" when closing a namespace. Add a comme
binjin 2014/09/02 20:02:56 Done.
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());
Joao da Silva 2014/09/02 19:34:02 Why is this in a scoped_ptr and not just directly
binjin 2014/09/02 20:02:56 See below.
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()));
Joao da Silva 2014/09/02 19:34:02 Same for this one, can't it be directly used as a
binjin 2014/09/02 20:02:56 See below.
41 }
42
43 virtual void TearDown() OVERRIDE {
44 extension_management_.reset();
45 pref_service_.reset();
Joao da Silva 2014/09/02 19:34:02 Is this TearDown needed?
binjin 2014/09/02 20:02:56 Because extension_management_ doesn't own pref_ser
Joao da Silva 2014/09/03 09:20:40 They will be destroyed in the reverse order of dec
binjin 2014/09/03 11:28:21 I see. But in the following CL I need to reset bot
46 }
47
48 void SetPref(bool managed, const char* path, base::Value* value) {
49 if (managed)
50 pref_service_->SetManagedPref(path, value);
51 else
52 pref_service_->SetUserPref(path, value);
53 }
54
55 void RemovePref(bool managed, const char* path) {
56 if (managed)
57 pref_service_->RemoveManagedPref(path);
58 else
59 pref_service_->RemoveUserPref(path);
60 }
61
62 void Refresh() {
63 extension_management_->Refresh();
64 }
65
66 protected:
67 scoped_ptr<TestingPrefServiceSimple> pref_service_;
68 scoped_ptr<ExtensionManagement> extension_management_;
69 };
70
71 // Verify that preference controlled by legacy ExtensionInstallSources policy is
72 // handled well.
73 TEST_F(ExtensionManagementTest, LegacyInstallSources) {
74 base::ListValue allowed_sites_pref;
75 allowed_sites_pref.AppendString("https://www.example.com/foo");
76 allowed_sites_pref.AppendString("https://corp.mycompany.com/*");
77 SetPref(
78 true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy());
79 Refresh();
80 const URLPatternSet* allowed_sites =
81 extension_management_->ReadGlobalSettings().install_sources.get();
82 ASSERT_TRUE(allowed_sites);
83 EXPECT_FALSE(allowed_sites->is_empty());
84 EXPECT_TRUE(allowed_sites->MatchesURL(GURL("https://www.example.com/foo")));
85 EXPECT_FALSE(allowed_sites->MatchesURL(GURL("https://www.example.com/bar")));
86 EXPECT_TRUE(
87 allowed_sites->MatchesURL(GURL("https://corp.mycompany.com/entry")));
88 EXPECT_FALSE(
89 allowed_sites->MatchesURL(GURL("https://www.mycompany.com/entry")));
90 }
91
92 // Verify that preference controlled by legacy ExtensionAllowedTypes policy is
93 // handled well.
94 TEST_F(ExtensionManagementTest, LegacyAllowedTypes) {
95 base::ListValue allowed_types_pref;
96 allowed_types_pref.AppendInteger(Manifest::TYPE_THEME);
97 allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT);
98
99 SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy());
100 Refresh();
101 const std::vector<Manifest::Type>* allowed_types =
102 extension_management_->ReadGlobalSettings().allowed_types.get();
103 ASSERT_TRUE(allowed_types);
104 EXPECT_TRUE(allowed_types->size() == 2);
105 EXPECT_FALSE(find(allowed_types->begin(),
Joao da Silva 2014/09/02 19:34:02 Use Contains from base/stl_util.h
binjin 2014/09/02 20:02:56 stl_util.h only provide ContainsKey for containers
106 allowed_types->end(),
107 Manifest::TYPE_EXTENSION) != allowed_types->end());
108 EXPECT_TRUE(find(allowed_types->begin(),
109 allowed_types->end(),
110 Manifest::TYPE_THEME) != allowed_types->end());
111 EXPECT_TRUE(find(allowed_types->begin(),
112 allowed_types->end(),
113 Manifest::TYPE_USER_SCRIPT) != allowed_types->end());
114 }
115
116 // Verify that preference controlled by legacy ExtensionInstallBlacklist policy
117 // is handled well.
118 TEST_F(ExtensionManagementTest, LegacyInstallBlacklist) {
119 base::ListValue denied_list_pref;
120 denied_list_pref.AppendString(kTargetExtension);
121
122 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
123 Refresh();
124 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
125 ExtensionManagement::INSTALLATION_BLOCKED);
126 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
127 ExtensionManagement::INSTALLATION_ALLOWED);
128 }
129
130 // Verify that preference controlled by legacy ExtensionInstallWhitelist policy
131 // is handled well.
132 TEST_F(ExtensionManagementTest, LegacyInstallWhitelist) {
133 base::ListValue denied_list_pref;
134 denied_list_pref.AppendString("*");
135 base::ListValue allowed_list_pref;
136 allowed_list_pref.AppendString(kTargetExtension);
137
138 SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
139 SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
140 Refresh();
141 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
142 ExtensionManagement::INSTALLATION_ALLOWED);
143 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
144 ExtensionManagement::INSTALLATION_BLOCKED);
145
146 // Verify that install whitelist preference set by user is ignored.
147 RemovePref(true, pref_names::kInstallAllowList);
148 SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
149 Refresh();
150 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
151 ExtensionManagement::INSTALLATION_BLOCKED);
152 }
153
154 // Verify that preference controlled by legacy ExtensionInstallForcelist policy
155 // is handled well.
156 TEST_F(ExtensionManagementTest, LegacyInstallForcelist) {
157 base::DictionaryValue forced_list_pref;
158 ExternalPolicyLoader::AddExtension(
159 &forced_list_pref, kTargetExtension, kExampleUpdateUrl);
160
161 SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
162 Refresh();
163 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
164 ExtensionManagement::INSTALLATION_FORCED);
165 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url,
166 kExampleUpdateUrl);
167 EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
168 ExtensionManagement::INSTALLATION_ALLOWED);
169
170 // Verify that install forcelist preference set by user is ignored.
171 RemovePref(true, pref_names::kInstallForceList);
172 SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
173 Refresh();
174 EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
175 ExtensionManagement::INSTALLATION_ALLOWED);
176 }
177
178 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698