| OLD | NEW |
| 1 // Copyright 2014 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 class ContentCapabilitiesTest : public ExtensionApiTest { | 36 class ContentCapabilitiesTest : public ExtensionApiTest { |
| 37 protected: | 37 protected: |
| 38 void SetUpCommandLine(base::CommandLine* command_line) override { | 38 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 39 ExtensionApiTest::SetUpCommandLine(command_line); | 39 ExtensionApiTest::SetUpCommandLine(command_line); |
| 40 command_line->AppendSwitchASCII( | 40 command_line->AppendSwitchASCII( |
| 41 extensions::switches::kWhitelistedExtensionID, | 41 extensions::switches::kWhitelistedExtensionID, |
| 42 crx_file::id_util::GenerateIdForPath( | 42 crx_file::id_util::GenerateIdForPath( |
| 43 base::MakeAbsoluteFilePath(test_extension_dir_.UnpackedPath()))); | 43 base::MakeAbsoluteFilePath(test_extension_dir_.UnpackedPath()))); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void SetUpOnMainThread() override { |
| 47 ExtensionApiTest::SetUpOnMainThread(); |
| 48 base::FilePath test_data; |
| 49 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data)); |
| 50 embedded_test_server()->ServeFilesFromDirectory( |
| 51 test_data.AppendASCII("extensions/content_capabilities")); |
| 52 ASSERT_TRUE(embedded_test_server()->Start()); |
| 53 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); |
| 54 } |
| 55 |
| 46 // Builds an extension manifest with the given content_capabilities matches | 56 // Builds an extension manifest with the given content_capabilities matches |
| 47 // and permissions. The extension always has the same (whitelisted) ID. | 57 // and permissions. The extension always has the same (whitelisted) ID. |
| 48 scoped_refptr<const Extension> LoadExtensionWithCapabilities( | 58 scoped_refptr<const Extension> LoadExtensionWithCapabilities( |
| 49 const std::string& matches, | 59 const std::string& matches, |
| 50 const std::string& permissions, | 60 const std::string& permissions, |
| 51 const std::string& extension_permissions = "[]") { | 61 const std::string& extension_permissions = "[]") { |
| 52 std::string manifest = base::StringPrintf( | 62 std::string manifest = base::StringPrintf( |
| 53 "{\n" | 63 "{\n" |
| 54 " \"name\": \"content_capabilities test extensions\",\n" | 64 " \"name\": \"content_capabilities test extensions\",\n" |
| 55 " \"version\": \"1\",\n" | 65 " \"version\": \"1\",\n" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 GURL GetTestURLFor(const std::string& host) { | 98 GURL GetTestURLFor(const std::string& host) { |
| 89 std::string port = base::UintToString(embedded_test_server()->port()); | 99 std::string port = base::UintToString(embedded_test_server()->port()); |
| 90 GURL::Replacements replacements; | 100 GURL::Replacements replacements; |
| 91 replacements.SetHostStr(host); | 101 replacements.SetHostStr(host); |
| 92 replacements.SetPortStr(port); | 102 replacements.SetPortStr(port); |
| 93 return embedded_test_server() | 103 return embedded_test_server() |
| 94 ->GetURL("/" + host + ".html") | 104 ->GetURL("/" + host + ".html") |
| 95 .ReplaceComponents(replacements); | 105 .ReplaceComponents(replacements); |
| 96 } | 106 } |
| 97 | 107 |
| 98 void InitializeTestServer() { | |
| 99 base::FilePath test_data; | |
| 100 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data)); | |
| 101 embedded_test_server()->ServeFilesFromDirectory( | |
| 102 test_data.AppendASCII("extensions/content_capabilities")); | |
| 103 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 104 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); | |
| 105 } | |
| 106 | |
| 107 // Run some script in the context of the given origin and in the presence of | 108 // Run some script in the context of the given origin and in the presence of |
| 108 // the given extension. This is used to wrap calls into the JS test functions | 109 // the given extension. This is used to wrap calls into the JS test functions |
| 109 // defined by | 110 // defined by |
| 110 // $(DIR_TEST_DATA)/extensions/content_capabilities/capability_tests.js. | 111 // $(DIR_TEST_DATA)/extensions/content_capabilities/capability_tests.js. |
| 111 testing::AssertionResult TestScriptResult(const Extension* extension, | 112 testing::AssertionResult TestScriptResult(const Extension* extension, |
| 112 const GURL& url, | 113 const GURL& url, |
| 113 const char* code) { | 114 const char* code) { |
| 114 ui_test_utils::NavigateToURL(browser(), url); | 115 ui_test_utils::NavigateToURL(browser(), url); |
| 115 bool result = false; | 116 bool result = false; |
| 116 if (!content::ExecuteScriptAndExtractBool(web_contents(), code, &result)) | 117 if (!content::ExecuteScriptAndExtractBool(web_contents(), code, &result)) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 if (profile()->GetSpecialStoragePolicy()->IsStorageUnlimited(url)) | 150 if (profile()->GetSpecialStoragePolicy()->IsStorageUnlimited(url)) |
| 150 return testing::AssertionSuccess(); | 151 return testing::AssertionSuccess(); |
| 151 return testing::AssertionFailure(); | 152 return testing::AssertionFailure(); |
| 152 } | 153 } |
| 153 | 154 |
| 154 private: | 155 private: |
| 155 extensions::TestExtensionDir test_extension_dir_; | 156 extensions::TestExtensionDir test_extension_dir_; |
| 156 }; | 157 }; |
| 157 | 158 |
| 158 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, NoCapabilities) { | 159 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, NoCapabilities) { |
| 159 InitializeTestServer(); | |
| 160 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 160 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 161 MakeJSONList("http://foo.example.com/*"), MakeJSONList()); | 161 MakeJSONList("http://foo.example.com/*"), MakeJSONList()); |
| 162 EXPECT_FALSE( | 162 EXPECT_FALSE( |
| 163 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 163 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 164 // TODO(dcheng): This should be false, but we cannot currently execute testing | 164 // TODO(dcheng): This should be false, but we cannot currently execute testing |
| 165 // script without a user gesture. | 165 // script without a user gesture. |
| 166 EXPECT_TRUE( | 166 EXPECT_TRUE( |
| 167 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 167 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 168 EXPECT_FALSE( | 168 EXPECT_FALSE( |
| 169 HasUnlimitedStorage(extension.get(), GetTestURLFor("foo.example.com"))); | 169 HasUnlimitedStorage(extension.get(), GetTestURLFor("foo.example.com"))); |
| 170 } | 170 } |
| 171 | 171 |
| 172 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) { | 172 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) { |
| 173 InitializeTestServer(); | |
| 174 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 173 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 175 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardRead")); | 174 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardRead")); |
| 176 EXPECT_TRUE( | 175 EXPECT_TRUE( |
| 177 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 176 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 178 EXPECT_FALSE( | 177 EXPECT_FALSE( |
| 179 CanReadClipboard(extension.get(), GetTestURLFor("bar.example.com"))); | 178 CanReadClipboard(extension.get(), GetTestURLFor("bar.example.com"))); |
| 180 EXPECT_TRUE( | 179 EXPECT_TRUE( |
| 181 CanReadClipboardInAboutBlankFrame(extension.get(), | 180 CanReadClipboardInAboutBlankFrame(extension.get(), |
| 182 GetTestURLFor("foo.example.com"))); | 181 GetTestURLFor("foo.example.com"))); |
| 183 EXPECT_FALSE( | 182 EXPECT_FALSE( |
| 184 CanReadClipboardInAboutBlankFrame(extension.get(), | 183 CanReadClipboardInAboutBlankFrame(extension.get(), |
| 185 GetTestURLFor("bar.example.com"))); | 184 GetTestURLFor("bar.example.com"))); |
| 186 // TODO(dcheng): This should be false, but we cannot currently execute testing | 185 // TODO(dcheng): This should be false, but we cannot currently execute testing |
| 187 // script without a user gesture. | 186 // script without a user gesture. |
| 188 EXPECT_TRUE( | 187 EXPECT_TRUE( |
| 189 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 188 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 190 } | 189 } |
| 191 | 190 |
| 192 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardWrite) { | 191 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardWrite) { |
| 193 InitializeTestServer(); | |
| 194 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 192 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 195 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardWrite")); | 193 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardWrite")); |
| 196 EXPECT_TRUE( | 194 EXPECT_TRUE( |
| 197 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 195 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 198 EXPECT_TRUE( | 196 EXPECT_TRUE( |
| 199 CanWriteClipboardInAboutBlankFrame(extension.get(), | 197 CanWriteClipboardInAboutBlankFrame(extension.get(), |
| 200 GetTestURLFor("foo.example.com"))); | 198 GetTestURLFor("foo.example.com"))); |
| 201 // TODO(dcheng): This should be false, but we cannot currently execute testing | 199 // TODO(dcheng): This should be false, but we cannot currently execute testing |
| 202 // script without a user gesture. | 200 // script without a user gesture. |
| 203 EXPECT_TRUE( | 201 EXPECT_TRUE( |
| 204 CanWriteClipboard(extension.get(), GetTestURLFor("bar.example.com"))); | 202 CanWriteClipboard(extension.get(), GetTestURLFor("bar.example.com"))); |
| 205 EXPECT_TRUE( | 203 EXPECT_TRUE( |
| 206 CanWriteClipboardInAboutBlankFrame(extension.get(), | 204 CanWriteClipboardInAboutBlankFrame(extension.get(), |
| 207 GetTestURLFor("bar.example.com"))); | 205 GetTestURLFor("bar.example.com"))); |
| 208 | 206 |
| 209 EXPECT_FALSE( | 207 EXPECT_FALSE( |
| 210 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 208 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 211 } | 209 } |
| 212 | 210 |
| 213 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) { | 211 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) { |
| 214 InitializeTestServer(); | |
| 215 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 212 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 216 MakeJSONList("http://foo.example.com/*"), | 213 MakeJSONList("http://foo.example.com/*"), |
| 217 MakeJSONList("clipboardRead", "clipboardWrite")); | 214 MakeJSONList("clipboardRead", "clipboardWrite")); |
| 218 EXPECT_TRUE( | 215 EXPECT_TRUE( |
| 219 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 216 CanReadClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 220 EXPECT_TRUE( | 217 EXPECT_TRUE( |
| 221 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); | 218 CanWriteClipboard(extension.get(), GetTestURLFor("foo.example.com"))); |
| 222 EXPECT_FALSE( | 219 EXPECT_FALSE( |
| 223 CanReadClipboard(extension.get(), GetTestURLFor("bar.example.com"))); | 220 CanReadClipboard(extension.get(), GetTestURLFor("bar.example.com"))); |
| 224 // TODO(dcheng): This should be false, but we cannot currently execute testing | 221 // TODO(dcheng): This should be false, but we cannot currently execute testing |
| 225 // script without a user gesture. | 222 // script without a user gesture. |
| 226 EXPECT_TRUE( | 223 EXPECT_TRUE( |
| 227 CanWriteClipboard(extension.get(), GetTestURLFor("bar.example.com"))); | 224 CanWriteClipboard(extension.get(), GetTestURLFor("bar.example.com"))); |
| 228 } | 225 } |
| 229 | 226 |
| 230 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, UnlimitedStorage) { | 227 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, UnlimitedStorage) { |
| 231 InitializeTestServer(); | |
| 232 scoped_refptr<const Extension> extension = | 228 scoped_refptr<const Extension> extension = |
| 233 LoadExtensionWithCapabilities(MakeJSONList("http://foo.example.com/*"), | 229 LoadExtensionWithCapabilities(MakeJSONList("http://foo.example.com/*"), |
| 234 MakeJSONList("unlimitedStorage")); | 230 MakeJSONList("unlimitedStorage")); |
| 235 EXPECT_TRUE( | 231 EXPECT_TRUE( |
| 236 HasUnlimitedStorage(extension.get(), GetTestURLFor("foo.example.com"))); | 232 HasUnlimitedStorage(extension.get(), GetTestURLFor("foo.example.com"))); |
| 237 EXPECT_FALSE( | 233 EXPECT_FALSE( |
| 238 HasUnlimitedStorage(extension.get(), GetTestURLFor("bar.example.com"))); | 234 HasUnlimitedStorage(extension.get(), GetTestURLFor("bar.example.com"))); |
| 239 } | 235 } |
| 240 | 236 |
| 241 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, WebUnlimitedStorageIsIsolated) { | 237 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, WebUnlimitedStorageIsIsolated) { |
| 242 InitializeTestServer(); | |
| 243 // This extension grants unlimited storage to bar.example.com but does not | 238 // This extension grants unlimited storage to bar.example.com but does not |
| 244 // have unlimitedStorage itself. | 239 // have unlimitedStorage itself. |
| 245 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 240 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 246 MakeJSONList("http://bar.example.com/*"), | 241 MakeJSONList("http://bar.example.com/*"), |
| 247 MakeJSONList("unlimitedStorage"), MakeJSONList("storage")); | 242 MakeJSONList("unlimitedStorage"), MakeJSONList("storage")); |
| 248 EXPECT_FALSE( | 243 EXPECT_FALSE( |
| 249 HasUnlimitedStorage(extension.get(), extension->GetResourceURL(""))); | 244 HasUnlimitedStorage(extension.get(), extension->GetResourceURL(""))); |
| 250 EXPECT_TRUE( | 245 EXPECT_TRUE( |
| 251 HasUnlimitedStorage(extension.get(), GetTestURLFor("bar.example.com"))); | 246 HasUnlimitedStorage(extension.get(), GetTestURLFor("bar.example.com"))); |
| 252 } | 247 } |
| 253 | 248 |
| 254 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, | 249 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, |
| 255 ExtensionUnlimitedStorageIsIsolated) { | 250 ExtensionUnlimitedStorageIsIsolated) { |
| 256 InitializeTestServer(); | |
| 257 // This extension has unlimitedStorage but doesn't grant it to foo.example.com | 251 // This extension has unlimitedStorage but doesn't grant it to foo.example.com |
| 258 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 252 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 259 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardRead"), | 253 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardRead"), |
| 260 MakeJSONList("unlimitedStorage")); | 254 MakeJSONList("unlimitedStorage")); |
| 261 | 255 |
| 262 EXPECT_TRUE( | 256 EXPECT_TRUE( |
| 263 HasUnlimitedStorage(extension.get(), extension->GetResourceURL(""))); | 257 HasUnlimitedStorage(extension.get(), extension->GetResourceURL(""))); |
| 264 EXPECT_FALSE( | 258 EXPECT_FALSE( |
| 265 HasUnlimitedStorage(extension.get(), GetTestURLFor("foo.example.com"))); | 259 HasUnlimitedStorage(extension.get(), GetTestURLFor("foo.example.com"))); |
| 266 } | 260 } |
| OLD | NEW |