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

Side by Side Diff: chrome/common/extensions/api/extension_api_unittest.cc

Issue 14494013: Allow API functions and events to have entries in _api_features.json (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix browser_tests, Stubs now inspects _api_features.json Created 7 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/common/extensions/api/extension_api.h" 5 #include "chrome/common/extensions/api/extension_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/common/chrome_paths.h" 19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/extensions/extension.h" 20 #include "chrome/common/extensions/extension.h"
21 #include "chrome/common/extensions/extension_manifest_constants.h"
21 #include "chrome/common/extensions/extension_unittest.h" 22 #include "chrome/common/extensions/extension_unittest.h"
22 #include "chrome/common/extensions/features/api_feature.h" 23 #include "chrome/common/extensions/features/api_feature.h"
23 #include "chrome/common/extensions/features/base_feature_provider.h" 24 #include "chrome/common/extensions/features/base_feature_provider.h"
24 #include "chrome/common/extensions/features/simple_feature.h" 25 #include "chrome/common/extensions/features/simple_feature.h"
25 #include "chrome/common/extensions/manifest.h" 26 #include "chrome/common/extensions/manifest.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 namespace extensions { 29 namespace extensions {
29 namespace { 30 namespace {
30 31
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i; 86 EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i;
86 } 87 }
87 } 88 }
88 89
89 TEST_F(ExtensionAPITest, IsPrivileged) { 90 TEST_F(ExtensionAPITest, IsPrivileged) {
90 scoped_ptr<ExtensionAPI> extension_api( 91 scoped_ptr<ExtensionAPI> extension_api(
91 ExtensionAPI::CreateWithDefaultConfiguration()); 92 ExtensionAPI::CreateWithDefaultConfiguration());
92 93
93 EXPECT_FALSE(extension_api->IsPrivileged("runtime.connect")); 94 EXPECT_FALSE(extension_api->IsPrivileged("runtime.connect"));
94 EXPECT_FALSE(extension_api->IsPrivileged("runtime.onConnect")); 95 EXPECT_FALSE(extension_api->IsPrivileged("runtime.onConnect"));
95 96 EXPECT_FALSE(extension_api->IsPrivileged("runtime.lastError"));
96 // Properties are not supported yet.
97 EXPECT_TRUE(extension_api->IsPrivileged("runtime.lastError"));
98 97
99 // Default unknown names to privileged for paranoia's sake. 98 // Default unknown names to privileged for paranoia's sake.
100 EXPECT_TRUE(extension_api->IsPrivileged(std::string())); 99 EXPECT_TRUE(extension_api->IsPrivileged(std::string()));
101 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); 100 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>"));
102 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); 101 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>"));
103 102
104 // Exists, but privileged. 103 // Exists, but privileged.
105 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); 104 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews"));
106 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); 105 EXPECT_TRUE(extension_api->IsPrivileged("history.search"));
107 106
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 217 }
219 218
220 EXPECT_EQ(test_data[i].expect_is_available, 219 EXPECT_EQ(test_data[i].expect_is_available,
221 api.IsAvailable(test_data[i].api_full_name, 220 api.IsAvailable(test_data[i].api_full_name,
222 NULL, 221 NULL,
223 test_data[i].context, 222 test_data[i].context,
224 test_data[i].url).is_available()) << i; 223 test_data[i].url).is_available()) << i;
225 } 224 }
226 } 225 }
227 226
227 TEST(ExtensionAPI, IsAnyFeatureAvailableToContext) {
228 struct {
229 std::string api_full_name;
230 bool expect_is_available;
231 Feature::Context context;
232 GURL url;
233 } test_data[] = {
234 { "test1", false, Feature::WEB_PAGE_CONTEXT, GURL() },
235 { "test1", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() },
236 { "test2", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
237 { "test2", true, Feature::WEB_PAGE_CONTEXT, GURL("http://google.com") },
238 { "test2.foo", false, Feature::WEB_PAGE_CONTEXT,
239 GURL("http://google.com") },
240 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
241 { "test3", true, Feature::WEB_PAGE_CONTEXT, GURL("http://foo.com") },
242 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
243 { "test7", false, Feature::WEB_PAGE_CONTEXT, GURL("http://google.com") },
244 { "test7", true, Feature::WEB_PAGE_CONTEXT, GURL("http://foo.com") },
245 { "test7", false, Feature::WEB_PAGE_CONTEXT, GURL("http://bar.com") }
246 };
247
248 base::FilePath api_features_path;
249 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
250 api_features_path = api_features_path.AppendASCII("extensions")
251 .AppendASCII("extension_api_unittest")
252 .AppendASCII("api_features.json");
253
254 std::string api_features_str;
255 ASSERT_TRUE(file_util::ReadFileToString(
256 api_features_path, &api_features_str)) << "api_features.json";
257
258 scoped_ptr<base::DictionaryValue> value(static_cast<DictionaryValue*>(
259 base::JSONReader::Read(api_features_str)));
260 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature);
261
262 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
263 ExtensionAPI api;
264 api.RegisterDependencyProvider("api", &api_feature_provider);
265 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
266 iter.Advance()) {
267 if (iter.key().find(".") == std::string::npos)
268 api.RegisterSchema(iter.key(), "");
269 }
270
271 EXPECT_EQ(test_data[i].expect_is_available,
272 api.IsAnyFeatureAvailableToContext(test_data[i].api_full_name,
273 test_data[i].context,
274 test_data[i].url)) << i;
275 }
276 }
277
228 TEST_F(ExtensionAPITest, LazyGetSchema) { 278 TEST_F(ExtensionAPITest, LazyGetSchema) {
229 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration()); 279 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration());
230 280
231 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 281 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
232 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 282 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
233 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 283 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
234 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 284 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
235 EXPECT_EQ(NULL, apis->GetSchema("foo")); 285 EXPECT_EQ(NULL, apis->GetSchema("foo"));
236 EXPECT_EQ(NULL, apis->GetSchema("foo")); 286 EXPECT_EQ(NULL, apis->GetSchema("foo"));
237 287
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 { 334 {
285 std::set<std::string> permissions; 335 std::set<std::string> permissions;
286 permissions.insert("storage"); 336 permissions.insert("storage");
287 permissions.insert("history"); 337 permissions.insert("history");
288 extension = CreateExtensionWithPermissions(permissions); 338 extension = CreateExtensionWithPermissions(permissions);
289 } 339 }
290 340
291 scoped_ptr<ExtensionAPI> extension_api( 341 scoped_ptr<ExtensionAPI> extension_api(
292 ExtensionAPI::CreateWithDefaultConfiguration()); 342 ExtensionAPI::CreateWithDefaultConfiguration());
293 343
344 // "runtime" has privileged parts that should not be accessed by content
345 // scripts.
346 EXPECT_FALSE(extension_api->IsAvailable("runtime",
347 extension.get(),
348 Feature::CONTENT_SCRIPT_CONTEXT,
349 GURL()).is_available());
350 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendNativeMessage",
351 extension.get(),
352 Feature::CONTENT_SCRIPT_CONTEXT,
353 GURL()).is_available());
354 // "runtime" also has unprivileged parts.
355 EXPECT_TRUE(extension_api->IsAvailable("runtime.sendMessage",
356 extension.get(),
357 Feature::CONTENT_SCRIPT_CONTEXT,
358 GURL()).is_available());
359 EXPECT_TRUE(extension_api->IsAvailable("runtime.id",
360 extension.get(),
361 Feature::CONTENT_SCRIPT_CONTEXT,
362 GURL()).is_available());
363
294 // "storage" is completely unprivileged. 364 // "storage" is completely unprivileged.
295 EXPECT_TRUE(extension_api->IsAvailable("storage", 365 EXPECT_TRUE(extension_api->IsAvailable("storage",
296 extension.get(), 366 extension.get(),
297 Feature::BLESSED_EXTENSION_CONTEXT, 367 Feature::BLESSED_EXTENSION_CONTEXT,
298 GURL()).is_available()); 368 GURL()).is_available());
299 EXPECT_TRUE(extension_api->IsAvailable("storage", 369 EXPECT_TRUE(extension_api->IsAvailable("storage",
300 extension.get(), 370 extension.get(),
301 Feature::UNBLESSED_EXTENSION_CONTEXT, 371 Feature::UNBLESSED_EXTENSION_CONTEXT,
302 GURL()).is_available()); 372 GURL()).is_available());
303 EXPECT_TRUE(extension_api->IsAvailable("storage", 373 EXPECT_TRUE(extension_api->IsAvailable("storage",
(...skipping 23 matching lines...) Expand all
327 EXPECT_FALSE(extension_api->IsAvailable("history", 397 EXPECT_FALSE(extension_api->IsAvailable("history",
328 extension.get(), 398 extension.get(),
329 Feature::UNBLESSED_EXTENSION_CONTEXT, 399 Feature::UNBLESSED_EXTENSION_CONTEXT,
330 GURL()).is_available()); 400 GURL()).is_available());
331 EXPECT_FALSE(extension_api->IsAvailable("history", 401 EXPECT_FALSE(extension_api->IsAvailable("history",
332 extension.get(), 402 extension.get(),
333 Feature::CONTENT_SCRIPT_CONTEXT, 403 Feature::CONTENT_SCRIPT_CONTEXT,
334 GURL()).is_available()); 404 GURL()).is_available());
335 } 405 }
336 406
407 scoped_refptr<Extension> CreateHostedApp() {
408 base::DictionaryValue values;
409 values.SetString(extension_manifest_keys::kName, "test");
410 values.SetString(extension_manifest_keys::kVersion, "0.1");
411 values.Set(extension_manifest_keys::kWebURLs, new base::ListValue());
412 values.SetString(extension_manifest_keys::kLaunchWebURL,
413 "http://www.example.com");
414
415 std::string error;
416 scoped_refptr<Extension> extension(Extension::Create(
417 base::FilePath(), Manifest::INTERNAL, values, Extension::NO_FLAGS,
418 &error));
419 CHECK(extension.get());
420 return extension;
421 }
422
423 TEST_F(ExtensionAPITest, HostedAppPermissions) {
424 scoped_refptr<Extension> extension = CreateHostedApp();
425
426 scoped_ptr<ExtensionAPI> extension_api(
427 ExtensionAPI::CreateWithDefaultConfiguration());
428
429 // "runtime" should not be available in hosted apps.
430 EXPECT_FALSE(extension_api->IsAvailable("runtime",
431 extension.get(),
432 Feature::BLESSED_EXTENSION_CONTEXT,
433 GURL()).is_available());
434 EXPECT_FALSE(extension_api->IsAvailable("runtime.id",
435 extension.get(),
436 Feature::BLESSED_EXTENSION_CONTEXT,
437 GURL()).is_available());
438 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendMessage",
439 extension.get(),
440 Feature::BLESSED_EXTENSION_CONTEXT,
441 GURL()).is_available());
442 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendNativeMessage",
443 extension.get(),
444 Feature::BLESSED_EXTENSION_CONTEXT,
445 GURL()).is_available());
446 }
447
337 TEST_F(ExtensionAPITest, ExtensionWithDependencies) { 448 TEST_F(ExtensionAPITest, ExtensionWithDependencies) {
338 // Extension with the "ttsEngine" permission but not the "tts" permission; it 449 // Extension with the "ttsEngine" permission but not the "tts" permission; it
339 // should not automatically get "tts" permission. 450 // should not automatically get "tts" permission.
340 { 451 {
341 scoped_refptr<Extension> extension = 452 scoped_refptr<Extension> extension =
342 CreateExtensionWithPermission("ttsEngine"); 453 CreateExtensionWithPermission("ttsEngine");
343 scoped_ptr<ExtensionAPI> api( 454 scoped_ptr<ExtensionAPI> api(
344 ExtensionAPI::CreateWithDefaultConfiguration()); 455 ExtensionAPI::CreateWithDefaultConfiguration());
345 EXPECT_TRUE(api->IsAvailable("ttsEngine", 456 EXPECT_TRUE(api->IsAvailable("ttsEngine",
346 extension.get(), 457 extension.get(),
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); 668 GetDictionaryFromList(dict, "parameters", 0, &sub_dict);
558 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); 669 EXPECT_TRUE(sub_dict->GetString("$ref", &type));
559 EXPECT_EQ("test.foo.TestType", type); 670 EXPECT_EQ("test.foo.TestType", type);
560 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); 671 GetDictionaryFromList(dict, "parameters", 1, &sub_dict);
561 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); 672 EXPECT_TRUE(sub_dict->GetString("$ref", &type));
562 EXPECT_EQ("fully.qualified.Type", type); 673 EXPECT_EQ("fully.qualified.Type", type);
563 } 674 }
564 675
565 } // namespace 676 } // namespace
566 } // namespace extensions 677 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698