[email protected] | 83048a2 | 2011-03-29 00:14:13 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 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 "base/message_loop.h" |
| 6 | #include "base/path_service.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_info_map.h" |
| 8 | #include "chrome/common/chrome_paths.h" |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 9 | #include "chrome/common/extensions/extension.h" |
[email protected] | 1bda9755 | 2011-03-01 20:11:52 | [diff] [blame] | 10 | #include "content/browser/browser_thread.h" |
[email protected] | c311302 | 2011-04-16 03:26:30 | [diff] [blame] | 11 | #include "content/common/json_value_serializer.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace keys = extension_manifest_keys; |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class ExtensionInfoMapTest : public testing::Test { |
| 19 | public: |
| 20 | ExtensionInfoMapTest() |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 21 | : ui_thread_(BrowserThread::UI, &message_loop_), |
| 22 | io_thread_(BrowserThread::IO, &message_loop_) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | private: |
| 26 | MessageLoop message_loop_; |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 27 | BrowserThread ui_thread_; |
| 28 | BrowserThread io_thread_; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | // Returns a barebones test Extension object with the given name. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 32 | static scoped_refptr<Extension> CreateExtension(const std::string& name) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 33 | #if defined(OS_WIN) |
| 34 | FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 35 | #elif defined(OS_POSIX) |
| 36 | FilePath path(FILE_PATH_LITERAL("/foo")); |
| 37 | #endif |
| 38 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 39 | DictionaryValue manifest; |
| 40 | manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 41 | manifest.SetString(keys::kName, name); |
| 42 | |
| 43 | std::string error; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 44 | scoped_refptr<Extension> extension = Extension::Create( |
[email protected] | 83048a2 | 2011-03-29 00:14:13 | [diff] [blame] | 45 | path.AppendASCII(name), Extension::INVALID, manifest, |
| 46 | Extension::STRICT_ERROR_CHECKS, &error); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 47 | EXPECT_TRUE(extension) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 48 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 49 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 52 | static scoped_refptr<Extension> LoadManifest(const std::string& dir, |
| 53 | const std::string& test_file) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 54 | FilePath path; |
| 55 | PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 56 | path = path.AppendASCII("extensions") |
| 57 | .AppendASCII(dir) |
| 58 | .AppendASCII(test_file); |
| 59 | |
| 60 | JSONFileValueSerializer serializer(path); |
| 61 | scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL)); |
| 62 | if (!result.get()) |
| 63 | return NULL; |
| 64 | |
| 65 | std::string error; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 66 | scoped_refptr<Extension> extension = Extension::Create( |
| 67 | path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()), |
[email protected] | 83048a2 | 2011-03-29 00:14:13 | [diff] [blame] | 68 | Extension::STRICT_ERROR_CHECKS, &error); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 69 | EXPECT_TRUE(extension) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 70 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 71 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 74 | // Test that the ExtensionInfoMap handles refcounting properly. |
| 75 | TEST_F(ExtensionInfoMapTest, RefCounting) { |
| 76 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 77 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 78 | // New extensions should have a single reference holding onto them. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 79 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 80 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
| 81 | scoped_refptr<Extension> extension3(CreateExtension("extension3")); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 82 | EXPECT_TRUE(extension1->HasOneRef()); |
| 83 | EXPECT_TRUE(extension2->HasOneRef()); |
| 84 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 85 | |
[email protected] | 05cc4e7 | 2011-03-08 21:29:48 | [diff] [blame] | 86 | // Add a ref to each extension and give it to the info map. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame^] | 87 | info_map->AddExtension(extension1, base::Time(), false); |
| 88 | info_map->AddExtension(extension2, base::Time(), false); |
| 89 | info_map->AddExtension(extension3, base::Time(), false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 90 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 91 | // Release extension1, and the info map should have the only ref. |
| 92 | const Extension* weak_extension1 = extension1; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 93 | extension1 = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 94 | EXPECT_TRUE(weak_extension1->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 95 | |
| 96 | // Remove extension2, and the extension2 object should have the only ref. |
[email protected] | dd163fb0 | 2011-05-04 22:22:17 | [diff] [blame] | 97 | info_map->RemoveExtension(extension2->id(), UnloadedExtensionInfo::UNINSTALL); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 98 | EXPECT_TRUE(extension2->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 99 | |
| 100 | // Delete the info map, and the extension3 object should have the only ref. |
| 101 | info_map = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 102 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Tests that we can query a few extension properties from the ExtensionInfoMap. |
| 106 | TEST_F(ExtensionInfoMapTest, Properties) { |
| 107 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 108 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 109 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 110 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 111 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame^] | 112 | info_map->AddExtension(extension1, base::Time(), false); |
| 113 | info_map->AddExtension(extension2, base::Time(), false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 114 | |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 115 | EXPECT_EQ(2u, info_map->extensions().size()); |
| 116 | EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id())); |
| 117 | EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id())); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. |
| 121 | TEST_F(ExtensionInfoMapTest, CheckPermissions) { |
| 122 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 123 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 124 | scoped_refptr<Extension> app(LoadManifest("manifest_tests", |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 125 | "valid_app.json")); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 126 | scoped_refptr<Extension> extension(LoadManifest("manifest_tests", |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 127 | "tabs_extension.json")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 128 | |
| 129 | GURL app_url("http://www.google.com/mail/foo.html"); |
| 130 | ASSERT_TRUE(app->is_app()); |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 131 | ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 132 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame^] | 133 | info_map->AddExtension(app, base::Time(), false); |
| 134 | info_map->AddExtension(extension, base::Time(), false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 135 | |
| 136 | // The app should have the notifications permission, either from a |
| 137 | // chrome-extension URL or from its web extent. |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 138 | const Extension* match = info_map->extensions().GetByURL( |
| 139 | app->GetResourceURL("a.html")); |
| 140 | EXPECT_TRUE(match && |
| 141 | match->HasApiPermission(Extension::kNotificationPermission)); |
| 142 | match = info_map->extensions().GetByURL(app_url); |
| 143 | EXPECT_TRUE(match && |
| 144 | match->HasApiPermission(Extension::kNotificationPermission)); |
| 145 | EXPECT_FALSE(match && |
| 146 | match->HasApiPermission(Extension::kTabPermission)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 147 | |
| 148 | // The extension should have the tabs permission. |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 149 | match = info_map->extensions().GetByURL(extension->GetResourceURL("a.html")); |
| 150 | EXPECT_TRUE(match && |
| 151 | match->HasApiPermission(Extension::kTabPermission)); |
| 152 | EXPECT_FALSE(match && |
| 153 | match->HasApiPermission(Extension::kNotificationPermission)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 154 | |
| 155 | // Random URL should not have any permissions. |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 156 | match = info_map->extensions().GetByURL(GURL("http://evil.com/a.html")); |
| 157 | EXPECT_FALSE(match); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 158 | } |
[email protected] | 5eaba82a | 2010-10-01 20:55:53 | [diff] [blame] | 159 | |
| 160 | } // namespace |