Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5854)

Unified Diff: chrome/browser/extensions/content_capabilities_browsertest.cc

Issue 789063002: Implement clipboardRead/Write content capabilities (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/content_capabilities_browsertest.cc
diff --git a/chrome/browser/extensions/content_capabilities_browsertest.cc b/chrome/browser/extensions/content_capabilities_browsertest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d1461be3071ea09b389aa1f4345ebe02255024ae
--- /dev/null
+++ b/chrome/browser/extensions/content_capabilities_browsertest.cc
@@ -0,0 +1,164 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+#include <vector>
+
+#include "base/command_line.h"
+#include "base/path_service.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "chrome/browser/extensions/extension_apitest.h"
+#include "chrome/browser/extensions/test_extension_dir.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/test/base/ui_test_utils.h"
+#include "components/crx_file/id_util.h"
+#include "content/public/test/browser_test_utils.h"
+#include "extensions/common/extension_builder.h"
+#include "extensions/common/manifest_handlers/content_capabilities_handler.h"
+#include "extensions/common/switches.h"
+#include "extensions/common/url_pattern.h"
+#include "net/dns/mock_host_resolver.h"
+#include "net/test/embedded_test_server/embedded_test_server.h"
+
+using extensions::DictionaryBuilder;
+using extensions::Extension;
+using extensions::ExtensionBuilder;
+using extensions::ListBuilder;
+
+class ContentCapabilitiesTest : public ExtensionApiTest {
+ protected:
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ ExtensionApiTest::SetUpCommandLine(command_line);
+ command_line->AppendSwitchASCII(
+ extensions::switches::kWhitelistedExtensionID,
+ crx_file::id_util::GenerateIdForPath(
+ test_extension_dir_.unpacked_path()));
+ }
+
+ // Builds an extension manifest with the given content_capabilities matches
+ // and permissions. The extension always has the same (whitelisted) ID.
+ scoped_refptr<const Extension> LoadExtensionWithCapabilities(
+ const std::string& matches,
+ const std::string& permissions) {
+ std::string manifest = base::StringPrintf(
+ "{\n"
+ " \"name\": \"content_capabilities test extensions\",\n"
+ " \"version\": \"1\",\n"
+ " \"manifest_version\": 2,\n"
+ " \"content_capabilities\": {\n"
+ " \"matches\": [%s],\n"
+ " \"permissions\": [%s]\n"
+ " }\n"
+ "}\n",
+ matches.c_str(),
+ permissions.c_str());
+ test_extension_dir_.WriteManifest(manifest);
+ return LoadExtension(test_extension_dir_.unpacked_path());
+ }
+
+ std::string MakeJSONList(
+ const std::string& s0 = "",
+ const std::string& s1 = "",
+ const std::string& s2 = "") {
+ std::vector<std::string> v;
+ if (!s0.empty()) v.push_back(s0);
+ if (!s1.empty()) v.push_back(s1);
+ if (!s2.empty()) v.push_back(s2);
+ std::string list = JoinString(v, "\",\"");
+ if (!list.empty())
+ 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 []
+ return list;
+ }
+
+ content::WebContents* web_contents() {
+ return browser()->tab_strip_model()->GetActiveWebContents();
+ }
+
+ GURL GetTestURLFor(const std::string& host) {
+ std::string port = base::IntToString(embedded_test_server()->port());
+ GURL::Replacements replacements;
+ replacements.SetHostStr(host);
+ replacements.SetPortStr(port);
+ return embedded_test_server()->GetURL("/" + host + ".html")
+ .ReplaceComponents(replacements);
+ }
+
+ void InitializeTestServer() {
+ base::FilePath test_data;
+ EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data));
+ embedded_test_server()->ServeFilesFromDirectory(test_data.AppendASCII(
+ "extensions/content_capabilities"));
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
+ host_resolver()->AddRule("*", embedded_test_server()->base_url().host());
+ }
+
+ private:
+ extensions::TestExtensionDir test_extension_dir_;
+};
+
+IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardRead) {
+ InitializeTestServer();
+ scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities(
+ MakeJSONList("http://foo.example.com/*"),
+ 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.
+ ui_test_utils::NavigateToURL(browser(), GetTestURLFor("foo.example.com"));
+ bool result = false;
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canReadClipboard()", &result));
+ EXPECT_TRUE(result);
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canWriteClipboard()", &result));
+ EXPECT_FALSE(result);
+
+ ui_test_utils::NavigateToURL(browser(), GetTestURLFor("bar.example.com"));
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canReadClipboard()", &result));
+ EXPECT_FALSE(result);
+}
+
+IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardWrite) {
+ InitializeTestServer();
+ scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities(
+ MakeJSONList("http://foo.example.com/*"),
+ MakeJSONList("clipboardWrite"));
+ ui_test_utils::NavigateToURL(browser(), GetTestURLFor("foo.example.com"));
+ bool result = false;
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canWriteClipboard()", &result));
+ EXPECT_TRUE(result);
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canReadClipboard()", &result));
+ EXPECT_FALSE(result);
+
+ ui_test_utils::NavigateToURL(browser(), GetTestURLFor("bar.example.com"));
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canWriteClipboard()", &result));
+ EXPECT_FALSE(result);
+}
+
+IN_PROC_BROWSER_TEST_F(ContentCapabilitiesTest, ClipboardReadWrite) {
+ InitializeTestServer();
+ scoped_refptr<const Extension> extension = LoadExtensionWithCapabilities(
+ MakeJSONList("http://foo.example.com/*"),
+ MakeJSONList("clipboardRead", "clipboardWrite"));
+ ui_test_utils::NavigateToURL(browser(), GetTestURLFor("foo.example.com"));
+ bool result = false;
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canWriteClipboard()", &result));
+ EXPECT_TRUE(result);
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canReadClipboard()", &result));
+ 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
+
+ ui_test_utils::NavigateToURL(browser(), GetTestURLFor("bar.example.com"));
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canWriteClipboard()", &result));
+ EXPECT_FALSE(result);
+ CHECK(content::ExecuteScriptAndExtractBool(web_contents(),
+ "tests.canReadClipboard()", &result));
+ EXPECT_FALSE(result);
+}
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | chrome/test/data/extensions/content_capabilities/bar.example.com.html » ('J')

Powered by Google App Engine
This is Rietveld 408576698