1 // Copyright (c) 2012 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.
5 #include "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/path_service.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/crx_installer.h"
11 #include "chrome/browser/extensions/extension_browsertest.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/plugin_service.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/webplugininfo.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "extensions/browser/extension_system.h"
24 #include "net/dns/mock_host_resolver.h"
26 using content::PluginService
;
27 using content::WebContents
;
28 using extensions::Extension
;
29 using extensions::Manifest
;
33 const char kExtensionId
[] = "bjjcibdiodkkeanflmiijlcfieiemced";
35 // This class tests that the Native Client plugin is blocked unless the
36 // .nexe is part of an extension from the Chrome Webstore.
37 class NaClExtensionTest
: public ExtensionBrowserTest
{
39 NaClExtensionTest() {}
43 INSTALL_TYPE_COMPONENT
,
44 INSTALL_TYPE_UNPACKED
,
45 INSTALL_TYPE_FROM_WEBSTORE
,
46 INSTALL_TYPE_NON_WEBSTORE
,
50 PLUGIN_TYPE_EMBED
= 1,
51 PLUGIN_TYPE_CONTENT_HANDLER
= 2,
52 PLUGIN_TYPE_ALL
= PLUGIN_TYPE_EMBED
|
53 PLUGIN_TYPE_CONTENT_HANDLER
,
57 const Extension
* InstallExtension(const base::FilePath
& file_path
,
58 InstallType install_type
) {
59 ExtensionService
* service
= extensions::ExtensionSystem::Get(
60 browser()->profile())->extension_service();
61 const Extension
* extension
= NULL
;
62 switch (install_type
) {
63 case INSTALL_TYPE_COMPONENT
:
64 if (LoadExtensionAsComponent(file_path
)) {
65 extension
= service
->GetExtensionById(kExtensionId
, false);
69 case INSTALL_TYPE_UNPACKED
:
70 // Install the extension from a folder so it's unpacked.
71 if (LoadExtension(file_path
)) {
72 extension
= service
->GetExtensionById(kExtensionId
, false);
76 case INSTALL_TYPE_FROM_WEBSTORE
:
77 // Install native_client.crx from the webstore.
78 if (InstallExtensionFromWebstore(file_path
, 1)) {
79 extension
= service
->GetExtensionById(last_loaded_extension_id(),
84 case INSTALL_TYPE_NON_WEBSTORE
:
85 // Install native_client.crx but not from the webstore.
86 if (ExtensionBrowserTest::InstallExtension(file_path
, 1)) {
87 extension
= service
->GetExtensionById(last_loaded_extension_id(),
95 const Extension
* InstallExtension(InstallType install_type
) {
96 base::FilePath file_path
= test_data_dir_
.AppendASCII("native_client");
97 return InstallExtension(file_path
, install_type
);
100 const Extension
* InstallHostedApp() {
101 base::FilePath file_path
= test_data_dir_
.AppendASCII(
102 "native_client_hosted_app");
103 return InstallExtension(file_path
, INSTALL_TYPE_FROM_WEBSTORE
);
106 bool IsNaClPluginLoaded() {
108 if (PathService::Get(chrome::FILE_NACL_PLUGIN
, &path
)) {
109 content::WebPluginInfo info
;
110 return PluginService::GetInstance()->GetPluginInfoByPath(path
, &info
);
115 void CheckPluginsCreated(const GURL
& url
, PluginType expected_to_succeed
) {
116 ui_test_utils::NavigateToURL(browser(), url
);
117 // Don't run tests if the NaCl plugin isn't loaded.
118 if (!IsNaClPluginLoaded())
121 bool embedded_plugin_created
= false;
122 bool content_handler_plugin_created
= false;
123 WebContents
* web_contents
=
124 browser()->tab_strip_model()->GetActiveWebContents();
125 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
127 "window.domAutomationController.send(EmbeddedPluginCreated());",
128 &embedded_plugin_created
));
129 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
131 "window.domAutomationController.send(ContentHandlerPluginCreated());",
132 &content_handler_plugin_created
));
134 EXPECT_EQ(embedded_plugin_created
,
135 (expected_to_succeed
& PLUGIN_TYPE_EMBED
) != 0);
136 EXPECT_EQ(content_handler_plugin_created
,
137 (expected_to_succeed
& PLUGIN_TYPE_CONTENT_HANDLER
) != 0);
140 void CheckPluginsCreated(const Extension
* extension
,
141 PluginType expected_to_succeed
) {
142 CheckPluginsCreated(extension
->GetResourceURL("test.html"),
143 expected_to_succeed
);
147 // Test that the NaCl plugin isn't blocked for Webstore extensions.
148 // Disabled: http://crbug.com/319892
149 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, DISABLED_WebStoreExtension
) {
150 ASSERT_TRUE(test_server()->Start());
152 const Extension
* extension
= InstallExtension(INSTALL_TYPE_FROM_WEBSTORE
);
153 ASSERT_TRUE(extension
);
154 CheckPluginsCreated(extension
, PLUGIN_TYPE_ALL
);
157 // Test that the NaCl plugin is blocked for non-Webstore extensions.
158 // Disabled: http://crbug.com/319892
159 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, DISABLED_NonWebStoreExtension
) {
160 ASSERT_TRUE(test_server()->Start());
162 const Extension
* extension
= InstallExtension(INSTALL_TYPE_NON_WEBSTORE
);
163 ASSERT_TRUE(extension
);
164 CheckPluginsCreated(extension
, PLUGIN_TYPE_NONE
);
167 // Test that the NaCl plugin isn't blocked for component extensions.
168 // Disabled: http://crbug.com/319892
169 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, DISABLED_ComponentExtension
) {
170 ASSERT_TRUE(test_server()->Start());
172 const Extension
* extension
= InstallExtension(INSTALL_TYPE_COMPONENT
);
173 ASSERT_TRUE(extension
);
174 ASSERT_EQ(extension
->location(), Manifest::COMPONENT
);
175 CheckPluginsCreated(extension
, PLUGIN_TYPE_ALL
);
178 // Test that the NaCl plugin isn't blocked for unpacked extensions.
179 // Disabled: http://crbug.com/319892
180 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, DISABLED_UnpackedExtension
) {
181 ASSERT_TRUE(test_server()->Start());
183 const Extension
* extension
= InstallExtension(INSTALL_TYPE_UNPACKED
);
184 ASSERT_TRUE(extension
);
185 ASSERT_EQ(extension
->location(), Manifest::UNPACKED
);
186 CheckPluginsCreated(extension
, PLUGIN_TYPE_ALL
);
189 // Test that the NaCl plugin is blocked for non chrome-extension urls, except
190 // if it's a content (MIME type) handler.
191 // Disabled: http://crbug.com/319892
192 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, DISABLED_NonExtensionScheme
) {
193 ASSERT_TRUE(test_server()->Start());
195 const Extension
* extension
= InstallExtension(INSTALL_TYPE_FROM_WEBSTORE
);
196 ASSERT_TRUE(extension
);
198 test_server()->GetURL("files/extensions/native_client/test.html"),
199 PLUGIN_TYPE_CONTENT_HANDLER
);
202 // Test that NaCl plugin isn't blocked for hosted app URLs.
203 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, HostedApp
) {
204 host_resolver()->AddRule("*", "127.0.0.1");
205 ASSERT_TRUE(test_server()->Start());
207 GURL url
= test_server()->GetURL("files/extensions/native_client/test.html");
208 GURL::Replacements replace_host
;
209 replace_host
.SetHostStr("localhost");
210 replace_host
.ClearPort();
211 url
= url
.ReplaceComponents(replace_host
);
213 const Extension
* extension
= InstallHostedApp();
214 ASSERT_TRUE(extension
);
215 CheckPluginsCreated(url
, PLUGIN_TYPE_ALL
);