[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" |
| 7 | #include "chrome/browser/chrome_thread.h" |
| 8 | #include "chrome/browser/extensions/extension_info_map.h" |
| 9 | #include "chrome/common/chrome_paths.h" |
| 10 | #include "chrome/common/json_value_serializer.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace keys = extension_manifest_keys; |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | class ExtensionInfoMapTest : public testing::Test { |
| 18 | public: |
| 19 | ExtensionInfoMapTest() |
| 20 | : ui_thread_(ChromeThread::UI, &message_loop_), |
| 21 | io_thread_(ChromeThread::IO, &message_loop_) { |
| 22 | } |
| 23 | |
| 24 | private: |
| 25 | MessageLoop message_loop_; |
| 26 | ChromeThread ui_thread_; |
| 27 | ChromeThread io_thread_; |
| 28 | }; |
| 29 | |
| 30 | // Returns a barebones test Extension object with the given name. |
| 31 | static Extension* CreateExtension(const std::string& name) { |
| 32 | #if defined(OS_WIN) |
| 33 | FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 34 | #elif defined(OS_POSIX) |
| 35 | FilePath path(FILE_PATH_LITERAL("/foo")); |
| 36 | #endif |
| 37 | |
| 38 | scoped_ptr<Extension> extension(new Extension(path.AppendASCII(name))); |
| 39 | |
| 40 | DictionaryValue manifest; |
| 41 | manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 42 | manifest.SetString(keys::kName, name); |
| 43 | |
| 44 | std::string error; |
| 45 | EXPECT_TRUE(extension->InitFromValue(manifest, false, &error)) << error; |
| 46 | |
| 47 | return extension.release(); |
| 48 | } |
| 49 | |
| 50 | static Extension* LoadManifest(const std::string& dir, |
| 51 | const std::string& test_file) { |
| 52 | FilePath path; |
| 53 | PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 54 | path = path.AppendASCII("extensions") |
| 55 | .AppendASCII(dir) |
| 56 | .AppendASCII(test_file); |
| 57 | |
| 58 | JSONFileValueSerializer serializer(path); |
| 59 | scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL)); |
| 60 | if (!result.get()) |
| 61 | return NULL; |
| 62 | |
| 63 | std::string error; |
| 64 | scoped_ptr<Extension> extension(new Extension(path)); |
| 65 | EXPECT_TRUE(extension->InitFromValue( |
| 66 | *static_cast<DictionaryValue*>(result.get()), false, &error)) << error; |
| 67 | |
| 68 | return extension.release(); |
| 69 | } |
| 70 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 71 | // Test that the ExtensionInfoMap handles refcounting properly. |
| 72 | TEST_F(ExtensionInfoMapTest, RefCounting) { |
| 73 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 74 | |
| 75 | // New extensions should have a single reference holding onto their static |
| 76 | // data. |
| 77 | scoped_ptr<Extension> extension1(CreateExtension("extension1")); |
| 78 | scoped_ptr<Extension> extension2(CreateExtension("extension2")); |
| 79 | scoped_ptr<Extension> extension3(CreateExtension("extension3")); |
| 80 | EXPECT_TRUE(extension1->static_data()->HasOneRef()); |
| 81 | EXPECT_TRUE(extension2->static_data()->HasOneRef()); |
| 82 | EXPECT_TRUE(extension3->static_data()->HasOneRef()); |
| 83 | |
| 84 | // Add a ref to each extension and give it to the info map. The info map |
| 85 | // expects the caller to add a ref for it, but then assumes ownership of that |
| 86 | // reference. |
| 87 | extension1->static_data()->AddRef(); |
| 88 | info_map->AddExtension(extension1->static_data()); |
| 89 | extension2->static_data()->AddRef(); |
| 90 | info_map->AddExtension(extension2->static_data()); |
| 91 | extension3->static_data()->AddRef(); |
| 92 | info_map->AddExtension(extension3->static_data()); |
| 93 | |
| 94 | // Delete extension1, and the info map should have the only ref. |
| 95 | const Extension::StaticData* data1 = extension1->static_data(); |
| 96 | extension1.reset(); |
| 97 | EXPECT_TRUE(data1->HasOneRef()); |
| 98 | |
| 99 | // Remove extension2, and the extension2 object should have the only ref. |
| 100 | info_map->RemoveExtension(extension2->id()); |
| 101 | EXPECT_TRUE(extension2->static_data()->HasOneRef()); |
| 102 | |
| 103 | // Delete the info map, and the extension3 object should have the only ref. |
| 104 | info_map = NULL; |
| 105 | EXPECT_TRUE(extension3->static_data()->HasOneRef()); |
| 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 | |
| 112 | scoped_ptr<Extension> extension1(CreateExtension("extension1")); |
| 113 | scoped_ptr<Extension> extension2(CreateExtension("extension2")); |
| 114 | |
| 115 | extension1->static_data()->AddRef(); |
| 116 | info_map->AddExtension(extension1->static_data()); |
| 117 | extension2->static_data()->AddRef(); |
| 118 | info_map->AddExtension(extension2->static_data()); |
| 119 | |
| 120 | EXPECT_EQ(extension1->name(), |
| 121 | info_map->GetNameForExtension(extension1->id())); |
| 122 | EXPECT_EQ(extension2->name(), |
| 123 | info_map->GetNameForExtension(extension2->id())); |
| 124 | |
| 125 | EXPECT_EQ(extension1->path().value(), |
| 126 | info_map->GetPathForExtension(extension1->id()).value()); |
| 127 | EXPECT_EQ(extension2->path().value(), |
| 128 | info_map->GetPathForExtension(extension2->id()).value()); |
| 129 | } |
| 130 | |
| 131 | // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. |
| 132 | TEST_F(ExtensionInfoMapTest, CheckPermissions) { |
| 133 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 134 | |
| 135 | scoped_ptr<Extension> app(LoadManifest("manifest_tests", |
| 136 | "valid_app.json")); |
| 137 | scoped_ptr<Extension> extension(LoadManifest("manifest_tests", |
| 138 | "tabs_extension.json")); |
| 139 | |
| 140 | GURL app_url("http://www.google.com/mail/foo.html"); |
| 141 | ASSERT_TRUE(app->is_app()); |
| 142 | ASSERT_TRUE(app->web_extent().ContainsURL(app_url)); |
| 143 | |
| 144 | app->static_data()->AddRef(); |
| 145 | info_map->AddExtension(app->static_data()); |
| 146 | extension->static_data()->AddRef(); |
| 147 | info_map->AddExtension(extension->static_data()); |
| 148 | |
| 149 | // The app should have the notifications permission, either from a |
| 150 | // chrome-extension URL or from its web extent. |
| 151 | EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 152 | app->GetResourceURL("a.html"), Extension::kNotificationPermission)); |
| 153 | EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 154 | app_url, Extension::kNotificationPermission)); |
| 155 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 156 | app_url, Extension::kTabPermission)); |
| 157 | |
| 158 | // The extension should have the tabs permission. |
| 159 | EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 160 | extension->GetResourceURL("a.html"), Extension::kTabPermission)); |
| 161 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 162 | extension->GetResourceURL("a.html"), Extension::kNotificationPermission)); |
| 163 | |
| 164 | // Random URL should not have any permissions. |
| 165 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 166 | GURL("http://evil.com/a.html"), Extension::kNotificationPermission)); |
| 167 | EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 168 | GURL("http://evil.com/a.html"), Extension::kTabPermission)); |
| 169 | } |
[email protected] | 5eaba82a | 2010-10-01 20:55:53 | [diff] [blame^] | 170 | |
| 171 | } // namespace |