OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/command_line.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/extensions/extension_apitest.h" |
| 14 #include "chrome/browser/extensions/test_extension_dir.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "components/crx_file/id_util.h" |
| 19 #include "content/public/test/browser_test_utils.h" |
| 20 #include "extensions/common/extension_builder.h" |
| 21 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" |
| 22 #include "extensions/common/switches.h" |
| 23 #include "extensions/common/url_pattern.h" |
| 24 #include "net/dns/mock_host_resolver.h" |
| 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 26 |
| 27 using extensions::DictionaryBuilder; |
| 28 using extensions::Extension; |
| 29 using extensions::ExtensionBuilder; |
| 30 using extensions::ListBuilder; |
| 31 |
| 32 class ContentCapabilitiesTest : public ExtensionApiTest { |
| 33 protected: |
| 34 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 35 ExtensionApiTest::SetUpCommandLine(command_line); |
| 36 command_line->AppendSwitchASCII( |
| 37 extensions::switches::kWhitelistedExtensionID, |
| 38 crx_file::id_util::GenerateIdForPath( |
| 39 test_extension_dir_.unpacked_path())); |
| 40 } |
| 41 |
| 42 // Builds an extension manifest with the given content_capabilities matches |
| 43 // and permissions. The extension always has the same (whitelisted) ID. |
| 44 scoped_refptr<const Extension> LoadExtensionWithCapabilities( |
| 45 const std::string& matches, |
| 46 const std::string& permissions) { |
| 47 std::string manifest = base::StringPrintf( |
| 48 "{\n" |
| 49 " \"name\": \"content_capabilities test extensions\",\n" |
| 50 " \"version\": \"1\",\n" |
| 51 " \"manifest_version\": 2,\n" |
| 52 " \"content_capabilities\": {\n" |
| 53 " \"matches\": %s,\n" |
| 54 " \"permissions\": %s\n" |
| 55 " }\n" |
| 56 "}\n", |
| 57 matches.c_str(), |
| 58 permissions.c_str()); |
| 59 test_extension_dir_.WriteManifest(manifest); |
| 60 return LoadExtension(test_extension_dir_.unpacked_path()); |
| 61 } |
| 62 |
| 63 std::string MakeJSONList( |
| 64 const std::string& s0 = "", |
| 65 const std::string& s1 = "", |
| 66 const std::string& s2 = "") { |
| 67 std::vector<std::string> v; |
| 68 if (!s0.empty()) v.push_back(s0); |
| 69 if (!s1.empty()) v.push_back(s1); |
| 70 if (!s2.empty()) v.push_back(s2); |
| 71 std::string list = JoinString(v, "\",\""); |
| 72 if (!list.empty()) |
| 73 list = "\"" + list + "\""; |
| 74 return "[" + list + "]"; |
| 75 } |
| 76 |
| 77 content::WebContents* web_contents() { |
| 78 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 79 } |
| 80 |
| 81 GURL GetTestURLFor(const std::string& host) { |
| 82 std::string port = base::IntToString(embedded_test_server()->port()); |
| 83 GURL::Replacements replacements; |
| 84 replacements.SetHostStr(host); |
| 85 replacements.SetPortStr(port); |
| 86 return embedded_test_server()->GetURL("/" + host + ".html") |
| 87 .ReplaceComponents(replacements); |
| 88 } |
| 89 |
| 90 void InitializeTestServer() { |
| 91 base::FilePath test_data; |
| 92 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data)); |
| 93 embedded_test_server()->ServeFilesFromDirectory(test_data.AppendASCII( |
| 94 "extensions/content_capabilities")); |
| 95 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 96 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); |
| 97 } |
| 98 |
| 99 // Run some script in the context of the given origin and in the presence of |
| 100 // the given extension. This is used to wrap calls into the JS test functions |
| 101 // defined by |
| 102 // $(DIR_TEST_DATA)/extensions/content_capabilities/capability_tests.js. |
| 103 bool TestContentCapability(const Extension* extension, |
| 104 const char* origin, |
| 105 const char* code) { |
| 106 ui_test_utils::NavigateToURL(browser(), GetTestURLFor(origin)); |
| 107 bool result = false; |
| 108 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), code, &result)); |
| 109 return result; |
| 110 } |
| 111 |
| 112 bool CanReadClipboard(const Extension* extension, const char *origin) { |
| 113 return TestContentCapability(extension, origin, "tests.canReadClipboard()"); |
| 114 } |
| 115 |
| 116 bool CanWriteClipboard(const Extension* extension, const char *origin) { |
| 117 return TestContentCapability(extension, origin, |
| 118 "tests.canWriteClipboard()"); |
| 119 } |
| 120 |
| 121 private: |
| 122 extensions::TestExtensionDir test_extension_dir_; |
| 123 }; |
| 124 |
| 125 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, NoCapabilities) { |
| 126 InitializeTestServer(); |
| 127 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 128 MakeJSONList("http://foo.example.com/*"), |
| 129 MakeJSONList()); |
| 130 EXPECT_FALSE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 131 EXPECT_FALSE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 132 } |
| 133 |
| 134 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) { |
| 135 InitializeTestServer(); |
| 136 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 137 MakeJSONList("http://foo.example.com/*"), |
| 138 MakeJSONList("clipboardRead")); |
| 139 EXPECT_TRUE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 140 EXPECT_FALSE(CanReadClipboard(extension.get(), "bar.example.com")); |
| 141 EXPECT_FALSE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 142 } |
| 143 |
| 144 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardWrite) { |
| 145 InitializeTestServer(); |
| 146 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 147 MakeJSONList("http://foo.example.com/*"), |
| 148 MakeJSONList("clipboardWrite")); |
| 149 EXPECT_TRUE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 150 EXPECT_FALSE(CanWriteClipboard(extension.get(), "bar.example.com")); |
| 151 EXPECT_FALSE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 152 } |
| 153 |
| 154 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) { |
| 155 InitializeTestServer(); |
| 156 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( |
| 157 MakeJSONList("http://foo.example.com/*"), |
| 158 MakeJSONList("clipboardRead", "clipboardWrite")); |
| 159 EXPECT_TRUE(CanReadClipboard(extension.get(), "foo.example.com")); |
| 160 EXPECT_TRUE(CanWriteClipboard(extension.get(), "foo.example.com")); |
| 161 EXPECT_FALSE(CanReadClipboard(extension.get(), "bar.example.com")); |
| 162 EXPECT_FALSE(CanWriteClipboard(extension.get(), "bar.example.com")); |
| 163 } |
OLD | NEW |