[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 1 | // Copyright (c) 2010 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 "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] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 10 | #include "chrome/common/json_value_serializer.h" |
[email protected] | 1bda9755 | 2011-03-01 20:11:52 | [diff] [blame^] | 11 | #include "content/browser/browser_thread.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( |
| 45 | path.AppendASCII(name), Extension::INVALID, manifest, false, &error); |
| 46 | EXPECT_TRUE(extension) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 47 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 48 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 51 | static scoped_refptr<Extension> LoadManifest(const std::string& dir, |
| 52 | const std::string& test_file) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 53 | FilePath path; |
| 54 | PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 55 | path = path.AppendASCII("extensions") |
| 56 | .AppendASCII(dir) |
| 57 | .AppendASCII(test_file); |
| 58 | |
| 59 | JSONFileValueSerializer serializer(path); |
| 60 | scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL)); |
| 61 | if (!result.get()) |
| 62 | return NULL; |
| 63 | |
| 64 | std::string error; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 65 | scoped_refptr<Extension> extension = Extension::Create( |
| 66 | path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()), |
| 67 | false, &error); |
| 68 | EXPECT_TRUE(extension) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 69 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 70 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 73 | // Test that the ExtensionInfoMap handles refcounting properly. |
| 74 | TEST_F(ExtensionInfoMapTest, RefCounting) { |
| 75 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 76 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 77 | // New extensions should have a single reference holding onto them. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 78 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 79 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
| 80 | scoped_refptr<Extension> extension3(CreateExtension("extension3")); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 81 | EXPECT_TRUE(extension1->HasOneRef()); |
| 82 | EXPECT_TRUE(extension2->HasOneRef()); |
| 83 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 84 | |
| 85 | // Add a ref to each extension and give it to the info map. The info map |
| 86 | // expects the caller to add a ref for it, but then assumes ownership of that |
| 87 | // reference. |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 88 | extension1->AddRef(); |
| 89 | info_map->AddExtension(extension1); |
| 90 | extension2->AddRef(); |
| 91 | info_map->AddExtension(extension2); |
| 92 | extension3->AddRef(); |
| 93 | info_map->AddExtension(extension3); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 94 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 95 | // Release extension1, and the info map should have the only ref. |
| 96 | const Extension* weak_extension1 = extension1; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 97 | extension1 = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 98 | EXPECT_TRUE(weak_extension1->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 99 | |
| 100 | // Remove extension2, and the extension2 object should have the only ref. |
| 101 | info_map->RemoveExtension(extension2->id()); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 102 | EXPECT_TRUE(extension2->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 103 | |
| 104 | // Delete the info map, and the extension3 object should have the only ref. |
| 105 | info_map = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 106 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Tests that we can query a few extension properties from the ExtensionInfoMap. |
| 110 | TEST_F(ExtensionInfoMapTest, Properties) { |
| 111 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 112 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 113 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 114 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 115 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 116 | extension1->AddRef(); |
| 117 | info_map->AddExtension(extension1); |
| 118 | extension2->AddRef(); |
| 119 | info_map->AddExtension(extension2); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 120 | |
| 121 | EXPECT_EQ(extension1->name(), |
| 122 | info_map->GetNameForExtension(extension1->id())); |
| 123 | EXPECT_EQ(extension2->name(), |
| 124 | info_map->GetNameForExtension(extension2->id())); |
| 125 | |
| 126 | EXPECT_EQ(extension1->path().value(), |
| 127 | info_map->GetPathForExtension(extension1->id()).value()); |
| 128 | EXPECT_EQ(extension2->path().value(), |
| 129 | info_map->GetPathForExtension(extension2->id()).value()); |
| 130 | } |
| 131 | |
| 132 | // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. |
| 133 | TEST_F(ExtensionInfoMapTest, CheckPermissions) { |
| 134 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 135 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 136 | scoped_refptr<Extension> app(LoadManifest("manifest_tests", |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 137 | "valid_app.json")); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 138 | scoped_refptr<Extension> extension(LoadManifest("manifest_tests", |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 139 | "tabs_extension.json")); |
| 140 | |
| 141 | GURL app_url("http://www.google.com/mail/foo.html"); |
| 142 | ASSERT_TRUE(app->is_app()); |
| 143 | ASSERT_TRUE(app->web_extent().ContainsURL(app_url)); |
| 144 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 145 | app->AddRef(); |
| 146 | info_map->AddExtension(app); |
| 147 | extension->AddRef(); |
| 148 | info_map->AddExtension(extension); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 149 | |
| 150 | // The app should have the notifications permission, either from a |
| 151 | // chrome-extension URL or from its web extent. |
| 152 | EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 153 | app->GetResourceURL("a.html"), Extension::kNotificationPermission)); |
| 154 | EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 155 | app_url, Extension::kNotificationPermission)); |
| 156 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 157 | app_url, Extension::kTabPermission)); |
| 158 | |
| 159 | // The extension should have the tabs permission. |
| 160 | EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 161 | extension->GetResourceURL("a.html"), Extension::kTabPermission)); |
| 162 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 163 | extension->GetResourceURL("a.html"), Extension::kNotificationPermission)); |
| 164 | |
| 165 | // Random URL should not have any permissions. |
| 166 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 167 | GURL("http://evil.com/a.html"), Extension::kNotificationPermission)); |
| 168 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 169 | GURL("http://evil.com/a.html"), Extension::kTabPermission)); |
| 170 | } |
[email protected] | 5eaba82a | 2010-10-01 20:55:53 | [diff] [blame] | 171 | |
| 172 | } // namespace |