1 // Copyright (c) 2011 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 "base/logging.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/test/ui_test_utils.h"
9 #include "content/browser/tab_contents/tab_contents.h"
10 #include "googleurl/src/gurl.h"
11 #include "net/base/mock_host_resolver.h"
12
13 class ExtensionResourceRequestPolicyTest : public ExtensionApiTest {
14 };
15
16 // Note, this mostly tests the logic of chrome/renderer/extensions/
17 // extension_resource_request_policy.*, but we have it as a browser test so that
18 // can make sure it works end-to-end.
IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest,OriginPrivileges)19 IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest, OriginPrivileges) {
20 host_resolver()->AddRule("*", "127.0.0.1");
21 ASSERT_TRUE(test_server()->Start());
22 ASSERT_TRUE(LoadExtension(test_data_dir_
23 .AppendASCII("extension_resource_request_policy")
24 .AppendASCII("extension")));
25
26 GURL web_resource(
27 test_server()->GetURL(
28 "files/extensions/api_test/extension_resource_request_policy/"
29 "index.html"));
30
31 std::string host_a("a.com");
32 GURL::Replacements make_host_a_com;
33 make_host_a_com.SetHostStr(host_a);
34
35 std::string host_b("b.com");
36 GURL::Replacements make_host_b_com;
37 make_host_b_com.SetHostStr(host_b);
38
39 // A web host that has permission.
40 ui_test_utils::NavigateToURL(
41 browser(), web_resource.ReplaceComponents(make_host_a_com));
42 std::string result;
43 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
44 browser()->GetSelectedTabContents()->render_view_host(), L"",
45 L"window.domAutomationController.send(document.title)",
46 &result));
47 EXPECT_EQ(result, "Loaded");
48
49 // A web host that loads a non-existent extension.
50 GURL non_existent_extension(
51 test_server()->GetURL(
52 "files/extensions/api_test/extension_resource_request_policy/"
53 "non_existent_extension.html"));
54 ui_test_utils::NavigateToURL(browser(), non_existent_extension);
55 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
56 browser()->GetSelectedTabContents()->render_view_host(), L"",
57 L"window.domAutomationController.send(document.title)",
58 &result));
59 EXPECT_EQ(result, "Image failed to load");
60
61 // A data URL. Data URLs should always be able to load chrome-extension://
62 // resources.
63 std::string file_source;
64 ASSERT_TRUE(file_util::ReadFileToString(
65 test_data_dir_.AppendASCII("extension_resource_request_policy")
66 .AppendASCII("index.html"), &file_source));
67 ui_test_utils::NavigateToURL(browser(),
68 GURL(std::string("data:text/html;charset=utf-8,") + file_source));
69 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
70 browser()->GetSelectedTabContents()->render_view_host(), L"",
71 L"window.domAutomationController.send(document.title)",
72 &result));
73 EXPECT_EQ(result, "Loaded");
74
75 // A different extension. Extensions should always be able to load each
76 // other's resources.
77 ASSERT_TRUE(LoadExtension(test_data_dir_
78 .AppendASCII("extension_resource_request_policy")
79 .AppendASCII("extension2")));
80 ui_test_utils::NavigateToURL(
81 browser(),
82 GURL("chrome-extension://pbkkcbgdkliohhfaeefcijaghglkahja/index.html"));
83 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
84 browser()->GetSelectedTabContents()->render_view_host(), L"",
85 L"window.domAutomationController.send(document.title)",
86 &result));
87 EXPECT_EQ(result, "Loaded");
88 }
89
IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest,ExtensionCanLoadHostedAppIcons)90 IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest,
91 ExtensionCanLoadHostedAppIcons) {
92 ASSERT_TRUE(LoadExtension(test_data_dir_
93 .AppendASCII("extension_resource_request_policy")
94 .AppendASCII("extension")));
95
96 ASSERT_TRUE(RunExtensionSubtest(
97 "extension_resource_request_policy/extension2/",
98 "can_load_icons_from_hosted_apps.html"));
99 }
100
IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest,Audio)101 IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest, Audio) {
102 EXPECT_TRUE(RunExtensionSubtest(
103 "extension_resource_request_policy/extension2",
104 "audio.html"));
105 }
106
IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest,Video)107 IN_PROC_BROWSER_TEST_F(ExtensionResourceRequestPolicyTest, Video) {
108 EXPECT_TRUE(RunExtensionSubtest(
109 "extension_resource_request_policy/extension2",
110 "video.html"));
111 }
112