| 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" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/browser/extensions/extension_apitest.h" | 14 #include "chrome/browser/extensions/extension_apitest.h" |
| 15 #include "chrome/browser/extensions/test_extension_dir.h" | 15 #include "chrome/browser/extensions/test_extension_dir.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "components/crx_file/id_util.h" | 20 #include "components/crx_file/id_util.h" |
| 20 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
| 21 #include "extensions/common/extension_builder.h" | 22 #include "extensions/common/extension_builder.h" |
| 22 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" | 23 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" |
| 23 #include "extensions/common/switches.h" | 24 #include "extensions/common/switches.h" |
| 24 #include "extensions/common/url_pattern.h" | 25 #include "extensions/common/url_pattern.h" |
| 25 #include "net/dns/mock_host_resolver.h" | 26 #include "net/dns/mock_host_resolver.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 27 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 28 #include "storage/browser/quota/special_storage_policy.h" |
| 27 | 29 |
| 28 using extensions::DictionaryBuilder; | 30 using extensions::DictionaryBuilder; |
| 29 using extensions::Extension; | 31 using extensions::Extension; |
| 30 using extensions::ExtensionBuilder; | 32 using extensions::ExtensionBuilder; |
| 31 using extensions::ListBuilder; | 33 using extensions::ListBuilder; |
| 32 | 34 |
| 33 class ContentCapabilitiesTest : public ExtensionApiTest { | 35 class ContentCapabilitiesTest : public ExtensionApiTest { |
| 34 protected: | 36 protected: |
| 35 void SetUpCommandLine(base::CommandLine* command_line) override { | 37 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 36 ExtensionApiTest::SetUpCommandLine(command_line); | 38 ExtensionApiTest::SetUpCommandLine(command_line); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 embedded_test_server()->ServeFilesFromDirectory( | 98 embedded_test_server()->ServeFilesFromDirectory( |
| 97 test_data.AppendASCII("extensions/content_capabilities")); | 99 test_data.AppendASCII("extensions/content_capabilities")); |
| 98 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 100 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 99 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); | 101 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); |
| 100 } | 102 } |
| 101 | 103 |
| 102 // Run some script in the context of the given origin and in the presence of | 104 // Run some script in the context of the given origin and in the presence of |
| 103 // the given extension. This is used to wrap calls into the JS test functions | 105 // the given extension. This is used to wrap calls into the JS test functions |
| 104 // defined by | 106 // defined by |
| 105 // $(DIR_TEST_DATA)/extensions/content_capabilities/capability_tests.js. | 107 // $(DIR_TEST_DATA)/extensions/content_capabilities/capability_tests.js. |
| 106 bool TestContentCapability(const Extension* extension, | 108 testing::AssertionResult TestScriptResult(const Extension* extension, |
| 107 const char* origin, | 109 const char* origin, |
| 108 const char* code) { | 110 const char* code) { |
| 109 ui_test_utils::NavigateToURL(browser(), GetTestURLFor(origin)); | 111 ui_test_utils::NavigateToURL(browser(), GetTestURLFor(origin)); |
| 110 bool result = false; | 112 bool result = false; |
| 111 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), code, &result)); | 113 if (!content::ExecuteScriptAndExtractBool(web_contents(), code, &result)) |
| 112 return result; | 114 return testing::AssertionFailure() << "Could not execute test script."; |
| 115 if (!result) |
| 116 return testing::AssertionFailure(); |
| 117 return testing::AssertionSuccess(); |
| 113 } | 118 } |
| 114 | 119 |
| 115 bool CanReadClipboard(const Extension* extension, const char* origin) { | 120 testing::AssertionResult CanReadClipboard(const Extension* extension, |
| 116 return TestContentCapability(extension, origin, "tests.canReadClipboard()"); | 121 const char* origin) { |
| 122 return TestScriptResult(extension, origin, "tests.canReadClipboard()"); |
| 117 } | 123 } |
| 118 | 124 |
| 119 bool CanWriteClipboard(const Extension* extension, const char* origin) { | 125 testing::AssertionResult CanWriteClipboard(const Extension* extension, |
| 120 return TestContentCapability(extension, origin, | 126 const char* origin) { |
| 121 "tests.canWriteClipboard()"); | 127 return TestScriptResult(extension, origin, "tests.canWriteClipboard()"); |
| 128 } |
| 129 |
| 130 testing::AssertionResult HasUnlimitedStorage(const Extension* extension, |
| 131 const char* origin) { |
| 132 if (profile()->GetSpecialStoragePolicy()->IsStorageUnlimited( |
| 133 GetTestURLFor(origin))) |
| 134 return testing::AssertionSuccess(); |
| 135 return testing::AssertionFailure(); |
| 122 } | 136 } |
| 123 | 137 |
| 124 private: | 138 private: |
| 125 extensions::TestExtensionDir test_extension_dir_; | 139 extensions::TestExtensionDir test_extension_dir_; |
| 126 }; | 140 }; |
| 127 | 141 |
| 128 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, NoCapabilities) { | 142 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, NoCapabilities) { |
| 129 InitializeTestServer(); | 143 InitializeTestServer(); |
| 130 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 144 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 131 MakeJSONList("http://foo.example.com/*"), MakeJSONList()); | 145 MakeJSONList("http://foo.example.com/*"), MakeJSONList()); |
| 132 EXPECT_FALSE(CanReadClipboard(extension.get(), "foo.example.com")); | 146 EXPECT_FALSE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 133 EXPECT_FALSE(CanWriteClipboard(extension.get(), "foo.example.com")); | 147 EXPECT_FALSE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 148 EXPECT_FALSE(HasUnlimitedStorage(extension.get(), "foo.example.com")); |
| 134 } | 149 } |
| 135 | 150 |
| 136 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) { | 151 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) { |
| 137 InitializeTestServer(); | 152 InitializeTestServer(); |
| 138 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 153 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 139 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardRead")); | 154 MakeJSONList("http://foo.example.com/*"), MakeJSONList("clipboardRead")); |
| 140 EXPECT_TRUE(CanReadClipboard(extension.get(), "foo.example.com")); | 155 EXPECT_TRUE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 141 EXPECT_FALSE(CanReadClipboard(extension.get(), "bar.example.com")); | 156 EXPECT_FALSE(CanReadClipboard(extension.get(), "bar.example.com")); |
| 142 EXPECT_FALSE(CanWriteClipboard(extension.get(), "foo.example.com")); | 157 EXPECT_FALSE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 143 } | 158 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 154 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) { | 169 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) { |
| 155 InitializeTestServer(); | 170 InitializeTestServer(); |
| 156 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | 171 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 157 MakeJSONList("http://foo.example.com/*"), | 172 MakeJSONList("http://foo.example.com/*"), |
| 158 MakeJSONList("clipboardRead", "clipboardWrite")); | 173 MakeJSONList("clipboardRead", "clipboardWrite")); |
| 159 EXPECT_TRUE(CanReadClipboard(extension.get(), "foo.example.com")); | 174 EXPECT_TRUE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 160 EXPECT_TRUE(CanWriteClipboard(extension.get(), "foo.example.com")); | 175 EXPECT_TRUE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 161 EXPECT_FALSE(CanReadClipboard(extension.get(), "bar.example.com")); | 176 EXPECT_FALSE(CanReadClipboard(extension.get(), "bar.example.com")); |
| 162 EXPECT_FALSE(CanWriteClipboard(extension.get(), "bar.example.com")); | 177 EXPECT_FALSE(CanWriteClipboard(extension.get(), "bar.example.com")); |
| 163 } | 178 } |
| 179 |
| 180 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, UnlimitedStorage) { |
| 181 InitializeTestServer(); |
| 182 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 183 MakeJSONList("http://foo.example.com/*"), |
| 184 MakeJSONList("unlimitedStorage")); |
| 185 EXPECT_TRUE(HasUnlimitedStorage(extension.get(), "foo.example.com")); |
| 186 EXPECT_FALSE(HasUnlimitedStorage(extension.get(), "bar.example.com")); |
| 187 } |
| OLD | NEW |