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