Chromium Code Reviews| 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 + "\""; | |
|
not at google - send to devlin
2014/12/10 17:33:54
This is borderline too nitty for me, but I'll say
Ken Rockot(use gerrit already)
2014/12/10 18:31:41
Actually an empty string will lead to the 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 private: | |
| 100 extensions::TestExtensionDir test_extension_dir_; | |
| 101 }; | |
| 102 | |
| 103 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) { | |
| 104 InitializeTestServer(); | |
| 105 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | |
| 106 MakeJSONList("http://foo.example.com/*"), | |
| 107 MakeJSONList("clipboardRead")); | |
|
not at google - send to devlin
2014/12/10 17:33:54
Is it worth testing an empty set of permissions?
Ken Rockot(use gerrit already)
2014/12/10 18:31:41
Sure.
| |
| 108 ui_test_utils::NavigateToURL(browser(), GetTestURLFor("foo.example.com")); | |
| 109 bool result = false; | |
| 110 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 111 "tests.canReadClipboard()", &result)); | |
| 112 EXPECT_TRUE(result); | |
| 113 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 114 "tests.canWriteClipboard()", &result)); | |
| 115 EXPECT_FALSE(result); | |
| 116 | |
| 117 ui_test_utils::NavigateToURL(browser(), GetTestURLFor("bar.example.com")); | |
| 118 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 119 "tests.canReadClipboard()", &result)); | |
| 120 EXPECT_FALSE(result); | |
| 121 } | |
| 122 | |
| 123 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardWrite) { | |
| 124 InitializeTestServer(); | |
| 125 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | |
| 126 MakeJSONList("http://foo.example.com/*"), | |
| 127 MakeJSONList("clipboardWrite")); | |
| 128 ui_test_utils::NavigateToURL(browser(), GetTestURLFor("foo.example.com")); | |
| 129 bool result = false; | |
| 130 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 131 "tests.canWriteClipboard()", &result)); | |
| 132 EXPECT_TRUE(result); | |
| 133 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 134 "tests.canReadClipboard()", &result)); | |
| 135 EXPECT_FALSE(result); | |
| 136 | |
| 137 ui_test_utils::NavigateToURL(browser(), GetTestURLFor("bar.example.com")); | |
| 138 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 139 "tests.canWriteClipboard()", &result)); | |
| 140 EXPECT_FALSE(result); | |
| 141 } | |
| 142 | |
| 143 IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) { | |
| 144 InitializeTestServer(); | |
| 145 scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities( | |
| 146 MakeJSONList("http://foo.example.com/*"), | |
| 147 MakeJSONList("clipboardRead", "clipboardWrite")); | |
| 148 ui_test_utils::NavigateToURL(browser(), GetTestURLFor("foo.example.com")); | |
| 149 bool result = false; | |
| 150 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 151 "tests.canWriteClipboard()", &result)); | |
| 152 EXPECT_TRUE(result); | |
| 153 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 154 "tests.canReadClipboard()", &result)); | |
| 155 EXPECT_TRUE(result); | |
|
not at google - send to devlin
2014/12/10 17:33:54
You should be able to cut out a bunch of this boil
Ken Rockot(use gerrit already)
2014/12/10 18:31:41
Good call, though I don't really see the value in
not at google - send to devlin
2014/12/10 19:30:07
The value in testing::AssertionResult is that it m
| |
| 156 | |
| 157 ui_test_utils::NavigateToURL(browser(), GetTestURLFor("bar.example.com")); | |
| 158 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 159 "tests.canWriteClipboard()", &result)); | |
| 160 EXPECT_FALSE(result); | |
| 161 CHECK(content::ExecuteScriptAndExtractBool(web_contents(), | |
| 162 "tests.canReadClipboard()", &result)); | |
| 163 EXPECT_FALSE(result); | |
| 164 } | |
| OLD | NEW |