| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/json/json_file_value_serializer.h" | 5 #include "base/json/json_file_value_serializer.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "chrome/common/chrome_paths.h" | 8 #include "chrome/common/chrome_paths.h" |
| 9 #include "content/public/test/test_browser_thread.h" | 9 #include "content/public/test/test_browser_thread.h" |
| 10 #include "extensions/browser/info_map.h" | 10 #include "extensions/browser/info_map.h" |
| 11 #include "extensions/common/extension.h" | 11 #include "extensions/common/extension.h" |
| 12 #include "extensions/common/manifest_constants.h" | 12 #include "extensions/common/manifest_constants.h" |
| 13 #include "extensions/common/permissions/permissions_data.h" | 13 #include "extensions/common/permissions/permissions_data.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using content::BrowserThread; | 16 using content::BrowserThread; |
| 17 | 17 |
| 18 namespace keys = extensions::manifest_keys; | 18 namespace extensions { |
| 19 namespace { |
| 19 | 20 |
| 20 namespace extensions { | 21 scoped_refptr<Extension> LoadManifest(const std::string& dir, |
| 21 | 22 const std::string& test_file) { |
| 22 class InfoMapTest : public testing::Test { | |
| 23 public: | |
| 24 InfoMapTest() | |
| 25 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 26 io_thread_(BrowserThread::IO, &message_loop_) {} | |
| 27 | |
| 28 private: | |
| 29 base::MessageLoop message_loop_; | |
| 30 content::TestBrowserThread ui_thread_; | |
| 31 content::TestBrowserThread io_thread_; | |
| 32 }; | |
| 33 | |
| 34 // Returns a barebones test Extension object with the given name. | |
| 35 static scoped_refptr<Extension> CreateExtension(const std::string& name) { | |
| 36 base::FilePath path; | |
| 37 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
| 38 path = path.AppendASCII("extensions"); | |
| 39 | |
| 40 base::DictionaryValue manifest; | |
| 41 manifest.SetString(keys::kVersion, "1.0.0.0"); | |
| 42 manifest.SetString(keys::kName, name); | |
| 43 | |
| 44 std::string error; | |
| 45 scoped_refptr<Extension> extension = | |
| 46 Extension::Create(path.AppendASCII(name), | |
| 47 Manifest::INVALID_LOCATION, | |
| 48 manifest, | |
| 49 Extension::NO_FLAGS, | |
| 50 &error); | |
| 51 EXPECT_TRUE(extension.get()) << error; | |
| 52 | |
| 53 return extension; | |
| 54 } | |
| 55 | |
| 56 static scoped_refptr<Extension> LoadManifest(const std::string& dir, | |
| 57 const std::string& test_file) { | |
| 58 base::FilePath path; | 23 base::FilePath path; |
| 59 PathService::Get(chrome::DIR_TEST_DATA, &path); | 24 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 60 path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file); | 25 path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file); |
| 61 | 26 |
| 62 JSONFileValueSerializer serializer(path); | 27 JSONFileValueSerializer serializer(path); |
| 63 scoped_ptr<base::Value> result(serializer.Deserialize(NULL, NULL)); | 28 scoped_ptr<base::Value> result(serializer.Deserialize(NULL, NULL)); |
| 64 if (!result) | 29 if (!result) |
| 65 return NULL; | 30 return NULL; |
| 66 | 31 |
| 67 std::string error; | 32 std::string error; |
| 68 scoped_refptr<Extension> extension = | 33 scoped_refptr<Extension> extension = |
| 69 Extension::Create(path, | 34 Extension::Create(path, |
| 70 Manifest::INVALID_LOCATION, | 35 Manifest::INVALID_LOCATION, |
| 71 *static_cast<base::DictionaryValue*>(result.get()), | 36 *static_cast<base::DictionaryValue*>(result.get()), |
| 72 Extension::NO_FLAGS, | 37 Extension::NO_FLAGS, |
| 73 &error); | 38 &error); |
| 74 EXPECT_TRUE(extension.get()) << error; | 39 EXPECT_TRUE(extension.get()) << error; |
| 75 | 40 |
| 76 return extension; | 41 return extension; |
| 77 } | 42 } |
| 78 | 43 |
| 79 // Test that the InfoMap handles refcounting properly. | 44 } // namespace |
| 80 TEST_F(InfoMapTest, RefCounting) { | |
| 81 scoped_refptr<InfoMap> info_map(new InfoMap()); | |
| 82 | 45 |
| 83 // New extensions should have a single reference holding onto them. | 46 // This test lives in Chrome because it depends on hosted app permissions |
| 84 scoped_refptr<Extension> extension1(CreateExtension("extension1")); | 47 // (specifically, notifications) that do not exist in src/extensions. |
| 85 scoped_refptr<Extension> extension2(CreateExtension("extension2")); | 48 class ChromeInfoMapTest : public testing::Test { |
| 86 scoped_refptr<Extension> extension3(CreateExtension("extension3")); | 49 public: |
| 87 EXPECT_TRUE(extension1->HasOneRef()); | 50 ChromeInfoMapTest() |
| 88 EXPECT_TRUE(extension2->HasOneRef()); | 51 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 89 EXPECT_TRUE(extension3->HasOneRef()); | 52 io_thread_(BrowserThread::IO, &message_loop_) {} |
| 90 | 53 |
| 91 // Add a ref to each extension and give it to the info map. | 54 private: |
| 92 info_map->AddExtension(extension1.get(), base::Time(), false, false); | 55 base::MessageLoop message_loop_; |
| 93 info_map->AddExtension(extension2.get(), base::Time(), false, false); | 56 content::TestBrowserThread ui_thread_; |
| 94 info_map->AddExtension(extension3.get(), base::Time(), false, false); | 57 content::TestBrowserThread io_thread_; |
| 58 }; |
| 95 | 59 |
| 96 // Release extension1, and the info map should have the only ref. | 60 // Tests API access permissions given both extension and app URLs. |
| 97 const Extension* weak_extension1 = extension1.get(); | 61 TEST_F(ChromeInfoMapTest, CheckPermissions) { |
| 98 extension1 = NULL; | |
| 99 EXPECT_TRUE(weak_extension1->HasOneRef()); | |
| 100 | |
| 101 // Remove extension2, and the extension2 object should have the only ref. | |
| 102 info_map->RemoveExtension( | |
| 103 extension2->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); | |
| 104 EXPECT_TRUE(extension2->HasOneRef()); | |
| 105 | |
| 106 // Delete the info map, and the extension3 object should have the only ref. | |
| 107 info_map = NULL; | |
| 108 EXPECT_TRUE(extension3->HasOneRef()); | |
| 109 } | |
| 110 | |
| 111 // Tests that we can query a few extension properties from the InfoMap. | |
| 112 TEST_F(InfoMapTest, Properties) { | |
| 113 scoped_refptr<InfoMap> info_map(new InfoMap()); | |
| 114 | |
| 115 scoped_refptr<Extension> extension1(CreateExtension("extension1")); | |
| 116 scoped_refptr<Extension> extension2(CreateExtension("extension2")); | |
| 117 | |
| 118 info_map->AddExtension(extension1.get(), base::Time(), false, false); | |
| 119 info_map->AddExtension(extension2.get(), base::Time(), false, false); | |
| 120 | |
| 121 EXPECT_EQ(2u, info_map->extensions().size()); | |
| 122 EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id())); | |
| 123 EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id())); | |
| 124 } | |
| 125 | |
| 126 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. | |
| 127 TEST_F(InfoMapTest, CheckPermissions) { | |
| 128 scoped_refptr<InfoMap> info_map(new InfoMap()); | 62 scoped_refptr<InfoMap> info_map(new InfoMap()); |
| 129 | 63 |
| 130 scoped_refptr<Extension> app( | 64 scoped_refptr<Extension> app( |
| 131 LoadManifest("manifest_tests", "valid_app.json")); | 65 LoadManifest("manifest_tests", "valid_app.json")); |
| 132 scoped_refptr<Extension> extension( | 66 scoped_refptr<Extension> extension( |
| 133 LoadManifest("manifest_tests", "tabs_extension.json")); | 67 LoadManifest("manifest_tests", "tabs_extension.json")); |
| 134 | 68 |
| 135 GURL app_url("http://www.google.com/mail/foo.html"); | 69 GURL app_url("http://www.google.com/mail/foo.html"); |
| 136 ASSERT_TRUE(app->is_app()); | 70 ASSERT_TRUE(app->is_app()); |
| 137 ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); | 71 ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 162 EXPECT_FALSE(match && | 96 EXPECT_FALSE(match && |
| 163 match->permissions_data()->HasAPIPermission( | 97 match->permissions_data()->HasAPIPermission( |
| 164 APIPermission::kNotifications)); | 98 APIPermission::kNotifications)); |
| 165 | 99 |
| 166 // Random URL should not have any permissions. | 100 // Random URL should not have any permissions. |
| 167 GURL evil_url("http://evil.com/a.html"); | 101 GURL evil_url("http://evil.com/a.html"); |
| 168 match = info_map->extensions().GetExtensionOrAppByURL(evil_url); | 102 match = info_map->extensions().GetExtensionOrAppByURL(evil_url); |
| 169 EXPECT_FALSE(match); | 103 EXPECT_FALSE(match); |
| 170 } | 104 } |
| 171 | 105 |
| 172 TEST_F(InfoMapTest, TestNotificationsDisabled) { | 106 TEST_F(ChromeInfoMapTest, TestNotificationsDisabled) { |
| 173 scoped_refptr<InfoMap> info_map(new InfoMap()); | 107 scoped_refptr<InfoMap> info_map(new InfoMap()); |
| 174 scoped_refptr<Extension> app(LoadManifest("manifest_tests", | 108 scoped_refptr<Extension> app( |
| 175 "valid_app.json")); | 109 LoadManifest("manifest_tests", "valid_app.json")); |
| 176 info_map->AddExtension(app.get(), base::Time(), false, false); | 110 info_map->AddExtension(app.get(), base::Time(), false, false); |
| 177 | 111 |
| 178 EXPECT_FALSE(info_map->AreNotificationsDisabled(app->id())); | 112 EXPECT_FALSE(info_map->AreNotificationsDisabled(app->id())); |
| 179 info_map->SetNotificationsDisabled(app->id(), true); | 113 info_map->SetNotificationsDisabled(app->id(), true); |
| 180 EXPECT_TRUE(info_map->AreNotificationsDisabled(app->id())); | 114 EXPECT_TRUE(info_map->AreNotificationsDisabled(app->id())); |
| 181 info_map->SetNotificationsDisabled(app->id(), false); | 115 info_map->SetNotificationsDisabled(app->id(), false); |
| 182 } | 116 } |
| 183 | 117 |
| 184 // Tests that extension URLs are properly mapped to local file paths. | |
| 185 TEST_F(InfoMapTest, MapUrlToLocalFilePath) { | |
| 186 scoped_refptr<InfoMap> info_map(new InfoMap()); | |
| 187 scoped_refptr<Extension> app(CreateExtension("app")); | |
| 188 info_map->AddExtension(app.get(), base::Time(), false, false); | |
| 189 | |
| 190 // Non-extension URLs don't map to anything. | |
| 191 base::FilePath non_extension_path; | |
| 192 GURL non_extension_url("http://not-an-extension.com/"); | |
| 193 EXPECT_FALSE(info_map->MapUrlToLocalFilePath( | |
| 194 non_extension_url, false, &non_extension_path)); | |
| 195 EXPECT_TRUE(non_extension_path.empty()); | |
| 196 | |
| 197 // Valid resources return a valid path. | |
| 198 base::FilePath valid_path; | |
| 199 GURL valid_url = app->GetResourceURL("manifest.json"); | |
| 200 EXPECT_TRUE(info_map->MapUrlToLocalFilePath( | |
| 201 valid_url, true /* use_blocking_api */, &valid_path)); | |
| 202 EXPECT_FALSE(valid_path.empty()); | |
| 203 | |
| 204 // A file must exist to be mapped to a path using the blocking API. | |
| 205 base::FilePath does_not_exist_path; | |
| 206 GURL does_not_exist_url = app->GetResourceURL("does-not-exist.html"); | |
| 207 EXPECT_FALSE(info_map->MapUrlToLocalFilePath( | |
| 208 does_not_exist_url, true /* use_blocking_api */, &does_not_exist_path)); | |
| 209 EXPECT_TRUE(does_not_exist_path.empty()); | |
| 210 | |
| 211 // A file does not need to exist to be mapped to a path with the non-blocking | |
| 212 // API. This avoids hitting the disk to see if it exists. | |
| 213 EXPECT_TRUE(info_map->MapUrlToLocalFilePath( | |
| 214 does_not_exist_url, false /* use_blocking_api */, &does_not_exist_path)); | |
| 215 EXPECT_FALSE(does_not_exist_path.empty()); | |
| 216 } | |
| 217 | |
| 218 } // namespace extensions | 118 } // namespace extensions |
| OLD | NEW |