| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "chrome/common/chrome_paths.h" | 9 #include "chrome/common/chrome_paths.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/extension_constants.h" | 11 #include "chrome/common/extensions/extension_constants.h" |
| 12 #include "chrome/common/extensions/extension_error_reporter.h" | 12 #include "chrome/common/extensions/extension_error_reporter.h" |
| 13 #include "chrome/common/json_value_serializer.h" | 13 #include "chrome/common/json_value_serializer.h" |
| 14 #include "net/base/mime_sniffer.h" | 14 #include "net/base/mime_sniffer.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace keys = extension_manifest_keys; | 17 namespace keys = extension_manifest_keys; |
| 18 namespace values = extension_manifest_values; | 18 namespace values = extension_manifest_values; |
| 19 namespace errors = extension_manifest_errors; | 19 namespace errors = extension_manifest_errors; |
| 20 | 20 |
| 21 class ExtensionTest : public testing::Test { | 21 class ExtensionTest : public testing::Test { |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 static Value* ValueFromJSON(const std::string& json_string) { |
| 25 std::string error; |
| 26 JSONStringValueSerializer content_scripts(json_string); |
| 27 Value* result = content_scripts.Deserialize(&error); |
| 28 DCHECK(result) << error; |
| 29 return result; |
| 30 } |
| 31 |
| 24 TEST(ExtensionTest, InitFromValueInvalid) { | 32 TEST(ExtensionTest, InitFromValueInvalid) { |
| 25 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
| 26 FilePath path(FILE_PATH_LITERAL("c:\\foo")); | 34 FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 27 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
| 28 FilePath path(FILE_PATH_LITERAL("/foo")); | 36 FilePath path(FILE_PATH_LITERAL("/foo")); |
| 29 #endif | 37 #endif |
| 30 Extension extension(path); | 38 Extension extension(path); |
| 31 std::string error; | 39 std::string error; |
| 32 ExtensionErrorReporter::Init(false); | 40 ExtensionErrorReporter::Init(false); |
| 33 | 41 |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 EXPECT_EQ(std::string(Extension::kMimeType), result); | 456 EXPECT_EQ(std::string(Extension::kMimeType), result); |
| 449 | 457 |
| 450 data.clear(); | 458 data.clear(); |
| 451 result.clear(); | 459 result.clear(); |
| 452 path = path.DirName().AppendASCII("bad_magic.crx"); | 460 path = path.DirName().AppendASCII("bad_magic.crx"); |
| 453 ASSERT_TRUE(file_util::ReadFileToString(path, &data)); | 461 ASSERT_TRUE(file_util::ReadFileToString(path, &data)); |
| 454 EXPECT_TRUE(net::SniffMimeType(data.c_str(), data.size(), | 462 EXPECT_TRUE(net::SniffMimeType(data.c_str(), data.size(), |
| 455 GURL("http://www.example.com/foo.crx"), "", &result)); | 463 GURL("http://www.example.com/foo.crx"), "", &result)); |
| 456 EXPECT_EQ("application/octet-stream", result); | 464 EXPECT_EQ("application/octet-stream", result); |
| 457 } | 465 } |
| 466 |
| 467 TEST(ExtensionTest, PermissionClass) { |
| 468 #if defined(OS_WIN) |
| 469 FilePath path(FILE_PATH_LITERAL("C:\\foo")); |
| 470 #elif defined(OS_POSIX) |
| 471 FilePath path(FILE_PATH_LITERAL("/foo")); |
| 472 #endif |
| 473 Extension::ResetGeneratedIdCounter(); |
| 474 |
| 475 Extension extension(path); |
| 476 std::string error; |
| 477 DictionaryValue bare_manifest; |
| 478 scoped_ptr<DictionaryValue> manifest; |
| 479 |
| 480 // Start with a minimalist extension. |
| 481 bare_manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 482 bare_manifest.SetString(keys::kName, "my extension"); |
| 483 EXPECT_TRUE(extension.InitFromValue(bare_manifest, false, &error)); |
| 484 EXPECT_EQ(Extension::PERMISSION_CLASS_LOW, extension.GetPermissionClass()); |
| 485 |
| 486 // Toolstrips don't affect the permission class. |
| 487 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); |
| 488 manifest->Set(keys::kToolstrips, ValueFromJSON( |
| 489 "[\"toolstrip.html\", \"toolstrip2.html\"]")); |
| 490 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); |
| 491 EXPECT_EQ(Extension::PERMISSION_CLASS_LOW, extension.GetPermissionClass()); |
| 492 |
| 493 // Requesting API permissions bumps you to medium. |
| 494 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); |
| 495 manifest->Set(keys::kPermissions, ValueFromJSON( |
| 496 "[\"tabs\", \"bookmarks\"]")); |
| 497 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); |
| 498 EXPECT_EQ(Extension::PERMISSION_CLASS_MEDIUM, extension.GetPermissionClass()); |
| 499 |
| 500 // Adding a content script bumps you to high. |
| 501 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); |
| 502 manifest->Set(keys::kContentScripts, ValueFromJSON( |
| 503 "[{" |
| 504 " \"matches\": [\"http://*.google.com/*\"]," |
| 505 " \"js\": [\"script.js\"]" |
| 506 "}]")); |
| 507 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); |
| 508 EXPECT_EQ(Extension::PERMISSION_CLASS_HIGH, extension.GetPermissionClass()); |
| 509 |
| 510 // ... or asking for a host permission. |
| 511 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); |
| 512 manifest->Set(keys::kPermissions, ValueFromJSON( |
| 513 "[\"tabs\", \"http://google.com/*\"]")); |
| 514 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); |
| 515 EXPECT_EQ(Extension::PERMISSION_CLASS_HIGH, extension.GetPermissionClass()); |
| 516 |
| 517 // Using native code (NPAPI) is automatically the max class. |
| 518 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); |
| 519 manifest->Set(keys::kPlugins, ValueFromJSON( |
| 520 "[{\"path\": \"harddrive_exploder.dll\"}]")); |
| 521 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); |
| 522 EXPECT_EQ(Extension::PERMISSION_CLASS_FULL, extension.GetPermissionClass()); |
| 523 |
| 524 // Using everything at once should obviously be the max class as well. |
| 525 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); |
| 526 manifest->Set(keys::kPlugins, ValueFromJSON( |
| 527 "[{\"path\": \"harddrive_exploder.dll\"}]")); |
| 528 manifest->Set(keys::kPermissions, ValueFromJSON( |
| 529 "[\"tabs\", \"http://google.com/*\"]")); |
| 530 manifest->Set(keys::kContentScripts, ValueFromJSON( |
| 531 "[{" |
| 532 " \"matches\": [\"http://*.google.com/*\"]," |
| 533 " \"js\": [\"script.js\"]" |
| 534 "}]")); |
| 535 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); |
| 536 EXPECT_EQ(Extension::PERMISSION_CLASS_FULL, extension.GetPermissionClass()); |
| 537 } |
| OLD | NEW |