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

Side by Side Diff: chrome/common/extensions/manifest_unittest.cc

Issue 8654001: Reland restrict extension features based on the extension type. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/common/extensions/manifest.h"
6
7 #include <algorithm>
8 #include <set>
9 #include <string>
10
11 #include "base/memory/scoped_ptr.h"
12 #include "base/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "chrome/common/extensions/extension_error_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace keys = extension_manifest_keys;
19 namespace errors = extension_manifest_errors;
20
21 namespace extensions {
22
23 namespace {
24
25 // Keys that define types.
26 const char* kTypeKeys[] = {
27 keys::kApp,
28 keys::kTheme,
29 keys::kPlatformApp
30 };
31
32 // Keys that are not accesible by themes.
33 const char* kNotThemeKeys[] = {
34 keys::kBrowserAction,
35 keys::kPageAction,
36 keys::kPageActions,
37 keys::kChromeURLOverrides,
38 keys::kPermissions,
39 keys::kOptionalPermissions,
40 keys::kOptionsPage,
41 keys::kBackground,
42 keys::kOfflineEnabled,
43 keys::kMinimumChromeVersion,
44 keys::kRequirements,
45 keys::kConvertedFromUserScript,
46 keys::kNaClModules,
47 keys::kPlugins,
48 keys::kContentScripts,
49 keys::kOmnibox,
50 keys::kDevToolsPage,
51 keys::kSidebar,
52 keys::kHomepageURL,
53 keys::kContentSecurityPolicy,
54 keys::kFileBrowserHandlers,
55 keys::kIncognito,
56 keys::kInputComponents,
57 keys::kTtsEngine,
58 keys::kIntents
59 };
60
61 // Keys that are not accessible by hosted apps.
62 const char* kNotHostedAppKeys[] = {
63 keys::kBrowserAction,
64 keys::kPageAction,
65 keys::kPageActions,
66 keys::kChromeURLOverrides,
67 keys::kContentScripts,
68 keys::kOmnibox,
69 keys::kDevToolsPage,
70 keys::kSidebar,
71 keys::kHomepageURL,
72 keys::kContentSecurityPolicy,
73 keys::kFileBrowserHandlers,
74 keys::kIncognito,
75 keys::kInputComponents,
76 keys::kTtsEngine,
77 keys::kIntents
78 };
79
80 // Keys not accessible by packaged aps.
81 const char* kNotPackagedAppKeys[] = {
82 keys::kBrowserAction,
83 keys::kPageAction,
84 keys::kPageActions
85 };
86
87 // Keys not accessible by platform apps.
88 const char* kNotPlatformAppKeys[] = {
89 keys::kBrowserAction,
90 keys::kPageAction,
91 keys::kPageActions,
92 keys::kChromeURLOverrides,
93 keys::kContentScripts,
94 keys::kOmnibox,
95 keys::kDevToolsPage,
96 keys::kSidebar,
97 keys::kHomepageURL,
98 };
99
100 // Returns all the manifest keys not including those in |filtered| or kTypeKeys.
101 std::set<std::string> GetAccessibleKeys(const char* filtered[], size_t length) {
102 std::set<std::string> all_keys = Manifest::GetAllKnownKeys();
103 std::set<std::string> filtered_keys(filtered, filtered + length);
104
105 // Starting with all possible manfiest keys, remove the keys that aren't
106 // accessible for the given type.
107 std::set<std::string> intermediate;
108 std::set_difference(all_keys.begin(), all_keys.end(),
109 filtered_keys.begin(), filtered_keys.end(),
110 std::insert_iterator<std::set<std::string> >(
111 intermediate, intermediate.begin()));
112
113 // Then remove the keys that specify types (app, platform_app, etc.).
114 std::set<std::string> result;
115 std::set<std::string> type_keys(
116 kTypeKeys, kTypeKeys + ARRAYSIZE_UNSAFE(kTypeKeys));
117 std::set_difference(intermediate.begin(), intermediate.end(),
118 type_keys.begin(), type_keys.end(),
119 std::insert_iterator<std::set<std::string> >(
120 result, result.begin()));
121
122 return result;
123 }
124
125 } // namespace
126
127 class ManifestTest : public testing::Test {
128 public:
129 ManifestTest() : default_value_("test") {}
130
131 protected:
132 void AssertType(Manifest* manifest, Manifest::Type type) {
133 EXPECT_EQ(type, manifest->GetType());
134 EXPECT_EQ(type == Manifest::kTypeTheme, manifest->IsTheme());
135 EXPECT_EQ(type == Manifest::kTypePlatformApp, manifest->IsPlatformApp());
136 EXPECT_EQ(type == Manifest::kTypePackagedApp, manifest->IsPackagedApp());
137 EXPECT_EQ(type == Manifest::kTypeHostedApp, manifest->IsHostedApp());
138 }
139
140 void TestRestrictedKeys(Manifest* manifest,
141 const char* restricted_keys[],
142 size_t restricted_keys_length) {
143 // Verify that the keys on the restricted key list for the given manifest
144 // fail validation and are filtered out.
145 DictionaryValue* value = manifest->value();
146 for (size_t i = 0; i < restricted_keys_length; ++i) {
147 std::string error, str;
148 value->Set(restricted_keys[i], Value::CreateStringValue(default_value_));
149 EXPECT_FALSE(manifest->ValidateManifest(&error));
150 EXPECT_EQ(error, ExtensionErrorUtils::FormatErrorMessage(
151 errors::kFeatureNotAllowed, restricted_keys[i]));
152 EXPECT_FALSE(manifest->GetString(restricted_keys[i], &str));
153 EXPECT_TRUE(value->Remove(restricted_keys[i], NULL));
154 }
155 }
156
157 std::string default_value_;
158 };
159
160 // Verifies that extensions can access the correct keys.
161 TEST_F(ManifestTest, Extension) {
162 // Generate the list of keys accessible by extensions.
163 std::set<std::string> extension_keys = GetAccessibleKeys(NULL, 0u);
164
165 // Construct the underlying value using every single key other than those
166 // on the restricted list.. We can use the same value for every key because we
167 // validate only by checking the presence of the keys.
168 DictionaryValue* value = new DictionaryValue();
169 for (std::set<std::string>::iterator i = extension_keys.begin();
170 i != extension_keys.end(); ++i)
171 value->Set(*i, Value::CreateStringValue(default_value_));
172
173 scoped_ptr<Manifest> manifest(new Manifest(value));
174 std::string error;
175 EXPECT_TRUE(manifest->ValidateManifest(&error));
176 EXPECT_EQ("", error);
177 AssertType(manifest.get(), Manifest::kTypeExtension);
178
179 // Verify that all the extension keys are accessible.
180 for (std::set<std::string>::iterator i = extension_keys.begin();
181 i != extension_keys.end(); ++i) {
182 std::string value;
183 manifest->GetString(*i, &value);
184 EXPECT_EQ(default_value_, value) << *i;
185 }
186
187 // Test DeepCopy and Equals.
188 scoped_ptr<Manifest> manifest2(manifest->DeepCopy());
189 EXPECT_TRUE(manifest->Equals(manifest2.get()));
190 EXPECT_TRUE(manifest2->Equals(manifest.get()));
191 value->Set("foo", Value::CreateStringValue("blah"));
192 EXPECT_FALSE(manifest->Equals(manifest2.get()));
193 }
194
195 // Verifies that themes can access the right keys.
196 TEST_F(ManifestTest, Theme) {
197 std::set<std::string> theme_keys =
198 GetAccessibleKeys(kNotThemeKeys, ARRAYSIZE_UNSAFE(kNotThemeKeys));
199
200 DictionaryValue* value = new DictionaryValue();
201 for (std::set<std::string>::iterator i = theme_keys.begin();
202 i != theme_keys.end(); ++i)
203 value->Set(*i, Value::CreateStringValue(default_value_));
204
205 std::string theme_key = keys::kTheme + std::string(".test");
206 value->Set(theme_key, Value::CreateStringValue(default_value_));
207
208 scoped_ptr<Manifest> manifest(new Manifest(value));
209 std::string error;
210 EXPECT_TRUE(manifest->ValidateManifest(&error));
211 EXPECT_EQ("", error);
212 AssertType(manifest.get(), Manifest::kTypeTheme);
213
214 // Verify that all the theme keys are accessible.
215 std::string str;
216 for (std::set<std::string>::iterator i = theme_keys.begin();
217 i != theme_keys.end(); ++i) {
218 EXPECT_TRUE(manifest->GetString(*i, &str));
219 EXPECT_EQ(default_value_, str) << *i;
220 }
221 EXPECT_TRUE(manifest->GetString(theme_key, &str));
222 EXPECT_EQ(default_value_, str) << theme_key;
223
224 // And that all the other keys fail validation and are filtered out
225 TestRestrictedKeys(manifest.get(), kNotThemeKeys,
226 ARRAYSIZE_UNSAFE(kNotThemeKeys));
227 };
228
229 // Verifies that platform apps can access the right keys.
230 TEST_F(ManifestTest, PlatformApp) {
231 std::set<std::string> platform_keys = GetAccessibleKeys(
232 kNotPlatformAppKeys,
233 ARRAYSIZE_UNSAFE(kNotPlatformAppKeys));
234
235 DictionaryValue* value = new DictionaryValue();
236 for (std::set<std::string>::iterator i = platform_keys.begin();
237 i != platform_keys.end(); ++i)
238 value->Set(*i, Value::CreateStringValue(default_value_));
239
240 value->Set(keys::kPlatformApp, Value::CreateBooleanValue(true));
241
242 scoped_ptr<Manifest> manifest(new Manifest(value));
243 std::string error;
244 EXPECT_TRUE(manifest->ValidateManifest(&error));
245 EXPECT_EQ("", error);
246 AssertType(manifest.get(), Manifest::kTypePlatformApp);
247
248 // Verify that all the platform app keys are accessible.
249 std::string str;
250 for (std::set<std::string>::iterator i = platform_keys.begin();
251 i != platform_keys.end(); ++i) {
252 EXPECT_TRUE(manifest->GetString(*i, &str));
253 EXPECT_EQ(default_value_, str) << *i;
254 }
255 bool is_platform_app = false;
256 EXPECT_TRUE(manifest->GetBoolean(keys::kPlatformApp, &is_platform_app));
257 EXPECT_TRUE(is_platform_app) << keys::kPlatformApp;
258
259 // And that all the other keys fail validation and are filtered out.
260 TestRestrictedKeys(manifest.get(), kNotPlatformAppKeys,
261 ARRAYSIZE_UNSAFE(kNotPlatformAppKeys));
262 };
263
264 // Verifies that hosted apps can access the right keys.
265 TEST_F(ManifestTest, HostedApp) {
266 std::set<std::string> keys = GetAccessibleKeys(
267 kNotHostedAppKeys,
268 ARRAYSIZE_UNSAFE(kNotHostedAppKeys));
269
270 DictionaryValue* value = new DictionaryValue();
271 for (std::set<std::string>::iterator i = keys.begin();
272 i != keys.end(); ++i)
273 value->Set(*i, Value::CreateStringValue(default_value_));
274
275 value->Set(keys::kWebURLs, Value::CreateStringValue(default_value_));
276
277 scoped_ptr<Manifest> manifest(new Manifest(value));
278 std::string error;
279 EXPECT_TRUE(manifest->ValidateManifest(&error));
280 EXPECT_EQ("", error);
281 AssertType(manifest.get(), Manifest::kTypeHostedApp);
282
283 // Verify that all the hosted app keys are accessible.
284 std::string str;
285 for (std::set<std::string>::iterator i = keys.begin();
286 i != keys.end(); ++i) {
287 EXPECT_TRUE(manifest->GetString(*i, &str));
288 EXPECT_EQ(default_value_, str) << *i;
289 }
290 EXPECT_TRUE(manifest->GetString(keys::kWebURLs, &str));
291 EXPECT_EQ(default_value_, str) << keys::kWebURLs;
292
293 // And that all the other keys fail validation and are filtered out.
294 TestRestrictedKeys(manifest.get(), kNotHostedAppKeys,
295 ARRAYSIZE_UNSAFE(kNotHostedAppKeys));
296 };
297
298 // Verifies that packaged apps can access the right keys.
299 TEST_F(ManifestTest, PackagedApp) {
300 std::set<std::string> keys = GetAccessibleKeys(
301 kNotPackagedAppKeys,
302 ARRAYSIZE_UNSAFE(kNotPackagedAppKeys));
303
304 DictionaryValue* value = new DictionaryValue();
305 for (std::set<std::string>::iterator i = keys.begin();
306 i != keys.end(); ++i)
307 value->Set(*i, Value::CreateStringValue(default_value_));
308 value->Set(keys::kApp, Value::CreateStringValue(default_value_));
309
310 scoped_ptr<Manifest> manifest(new Manifest(value));
311 std::string error;
312 EXPECT_TRUE(manifest->ValidateManifest(&error));
313 EXPECT_EQ("", error);
314 AssertType(manifest.get(), Manifest::kTypePackagedApp);
315
316 // Verify that all the packaged app keys are accessible.
317 std::string str;
318 for (std::set<std::string>::iterator i = keys.begin();
319 i != keys.end(); ++i) {
320 EXPECT_TRUE(manifest->GetString(*i, &str));
321 EXPECT_EQ(default_value_, str) << *i;
322 }
323 EXPECT_TRUE(manifest->GetString(keys::kApp, &str));
324 EXPECT_EQ(default_value_, str) << keys::kApp;
325
326 // And that all the other keys fail validation and are filtered out.
327 TestRestrictedKeys(manifest.get(), kNotPackagedAppKeys,
328 ARRAYSIZE_UNSAFE(kNotPackagedAppKeys));
329 };
330
331 // Verifies that the various getters filter unknown and restricted keys.
332 TEST_F(ManifestTest, Getters) {
333 DictionaryValue* value = new DictionaryValue();
334 scoped_ptr<Manifest> manifest(new Manifest(value));
335 std::string unknown_key = "asdfaskldjf";
336
337 // Verify that the key filtering works for each of the getters.
338 // Get and GetBoolean
339 bool expected_bool = true, actual_bool = false;
340 value->Set(unknown_key, Value::CreateBooleanValue(expected_bool));
341 EXPECT_FALSE(manifest->HasKey(unknown_key));
342 EXPECT_FALSE(manifest->GetBoolean(unknown_key, &actual_bool));
343 EXPECT_FALSE(actual_bool);
344 Value* actual_value = NULL;
345 EXPECT_FALSE(manifest->Get(unknown_key, &actual_value));
346 EXPECT_TRUE(value->Remove(unknown_key, NULL));
347
348 // GetInteger
349 int expected_int = 5, actual_int = 0;
350 value->Set(unknown_key, Value::CreateIntegerValue(expected_int));
351 EXPECT_FALSE(manifest->GetInteger(unknown_key, &actual_int));
352 EXPECT_NE(expected_int, actual_int);
353 EXPECT_TRUE(value->Remove(unknown_key, NULL));
354
355 // GetString
356 std::string expected_str = "hello", actual_str;
357 value->Set(unknown_key, Value::CreateStringValue(expected_str));
358 EXPECT_FALSE(manifest->GetString(unknown_key, &actual_str));
359 EXPECT_NE(expected_str, actual_str);
360 EXPECT_TRUE(value->Remove(unknown_key, NULL));
361
362 // GetString (string16)
363 string16 expected_str16(UTF8ToUTF16("hello")), actual_str16;
364 value->Set(unknown_key, Value::CreateStringValue(expected_str16));
365 EXPECT_FALSE(manifest->GetString(unknown_key, &actual_str16));
366 EXPECT_NE(expected_str16, actual_str16);
367 EXPECT_TRUE(value->Remove(unknown_key, NULL));
368
369 // GetDictionary
370 DictionaryValue* expected_dict = new DictionaryValue();
371 DictionaryValue* actual_dict = NULL;
372 expected_dict->Set("foo", Value::CreateStringValue("bar"));
373 value->Set(unknown_key, expected_dict);
374 EXPECT_FALSE(manifest->GetDictionary(unknown_key, &actual_dict));
375 EXPECT_EQ(NULL, actual_dict);
376 std::string path = unknown_key + ".foo";
377 EXPECT_FALSE(manifest->GetString(path, &actual_str));
378 EXPECT_NE("bar", actual_str);
379 EXPECT_TRUE(value->Remove(unknown_key, NULL));
380
381 // GetList
382 ListValue* expected_list = new ListValue();
383 ListValue* actual_list = NULL;
384 expected_list->Append(Value::CreateStringValue("blah"));
385 value->Set(unknown_key, expected_list);
386 EXPECT_FALSE(manifest->GetList(unknown_key, &actual_list));
387 EXPECT_EQ(NULL, actual_list);
388 EXPECT_TRUE(value->Remove(unknown_key, NULL));
389 }
390
391 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698