| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/extensions/extension_constants.h" | 14 #include "chrome/common/extensions/extension_constants.h" |
| 15 #include "chrome/common/extensions/extension_error_utils.h" | 15 #include "chrome/common/extensions/extension_error_utils.h" |
| 16 #include "chrome/common/json_value_serializer.h" | 16 #include "chrome/common/json_value_serializer.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace errors = extension_manifest_errors; | 19 namespace errors = extension_manifest_errors; |
| 20 | 20 |
| 21 class ManifestTest : public testing::Test { | 21 class ExtensionManifestTest : public testing::Test { |
| 22 public: | 22 public: |
| 23 ManifestTest() : enable_apps_(true) {} | 23 ExtensionManifestTest() : enable_apps_(true) {} |
| 24 | 24 |
| 25 protected: | 25 protected: |
| 26 Extension* LoadExtension(const std::string& name, | 26 Extension* LoadExtension(const std::string& name, |
| 27 std::string* error) { | 27 std::string* error) { |
| 28 return LoadExtensionWithLocation(name, Extension::INTERNAL, error); | 28 return LoadExtensionWithLocation(name, Extension::INTERNAL, error); |
| 29 } | 29 } |
| 30 | 30 |
| 31 Extension* LoadExtensionWithLocation(const std::string& name, | 31 Extension* LoadExtensionWithLocation(const std::string& name, |
| 32 Extension::Location location, | 32 Extension::Location location, |
| 33 std::string* error) { | 33 std::string* error) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 EXPECT_FALSE(extension.get()) << | 69 EXPECT_FALSE(extension.get()) << |
| 70 "Expected failure loading extension '" << name << | 70 "Expected failure loading extension '" << name << |
| 71 "', but didn't get one."; | 71 "', but didn't get one."; |
| 72 EXPECT_TRUE(MatchPatternASCII(error, expected_error)) << name << | 72 EXPECT_TRUE(MatchPatternASCII(error, expected_error)) << name << |
| 73 " expected '" << expected_error << "' but got '" << error << "'"; | 73 " expected '" << expected_error << "' but got '" << error << "'"; |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool enable_apps_; | 76 bool enable_apps_; |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 TEST_F(ManifestTest, AppsDisabledByDefault) { | 79 TEST_F(ExtensionManifestTest, AppsDisabledByDefault) { |
| 80 #if defined(OS_CHROMEOS) | 80 #if defined(OS_CHROMEOS) |
| 81 // On ChromeOS, apps are enabled by default. | 81 // On ChromeOS, apps are enabled by default. |
| 82 if (Extension::AppsAreEnabled()) | 82 if (Extension::AppsAreEnabled()) |
| 83 return; | 83 return; |
| 84 #endif | 84 #endif |
| 85 | 85 |
| 86 enable_apps_ = false; | 86 enable_apps_ = false; |
| 87 LoadAndExpectError("launch_local_path.json", errors::kAppsNotEnabled); | 87 LoadAndExpectError("launch_local_path.json", errors::kAppsNotEnabled); |
| 88 } | 88 } |
| 89 | 89 |
| 90 TEST_F(ManifestTest, ValidApp) { | 90 TEST_F(ExtensionManifestTest, ValidApp) { |
| 91 scoped_ptr<Extension> extension(LoadAndExpectSuccess("valid_app.json")); | 91 scoped_ptr<Extension> extension(LoadAndExpectSuccess("valid_app.json")); |
| 92 ASSERT_EQ(2u, extension->web_extent().patterns().size()); | 92 ASSERT_EQ(2u, extension->web_extent().patterns().size()); |
| 93 EXPECT_EQ("http://www.google.com/mail/*", | 93 EXPECT_EQ("http://www.google.com/mail/*", |
| 94 extension->web_extent().patterns()[0].GetAsString()); | 94 extension->web_extent().patterns()[0].GetAsString()); |
| 95 EXPECT_EQ("http://www.google.com/foobar/*", | 95 EXPECT_EQ("http://www.google.com/foobar/*", |
| 96 extension->web_extent().patterns()[1].GetAsString()); | 96 extension->web_extent().patterns()[1].GetAsString()); |
| 97 EXPECT_EQ(Extension::LAUNCH_WINDOW, extension->launch_container()); | 97 EXPECT_EQ(Extension::LAUNCH_TAB, extension->launch_container()); |
| 98 EXPECT_EQ(false, extension->launch_fullscreen()); | 98 EXPECT_EQ(false, extension->launch_fullscreen()); |
| 99 EXPECT_EQ("http://www.google.com/mail/", extension->launch_web_url()); | 99 EXPECT_EQ("http://www.google.com/mail/", extension->launch_web_url()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 TEST_F(ManifestTest, AppWebUrls) { | 102 TEST_F(ExtensionManifestTest, AppWebUrls) { |
| 103 LoadAndExpectError("web_urls_wrong_type.json", | 103 LoadAndExpectError("web_urls_wrong_type.json", |
| 104 errors::kInvalidWebURLs); | 104 errors::kInvalidWebURLs); |
| 105 LoadAndExpectError("web_urls_invalid_1.json", | 105 LoadAndExpectError("web_urls_invalid_1.json", |
| 106 ExtensionErrorUtils::FormatErrorMessage( | 106 ExtensionErrorUtils::FormatErrorMessage( |
| 107 errors::kInvalidWebURL, "0")); | 107 errors::kInvalidWebURL, "0")); |
| 108 LoadAndExpectError("web_urls_invalid_2.json", | 108 LoadAndExpectError("web_urls_invalid_2.json", |
| 109 ExtensionErrorUtils::FormatErrorMessage( | 109 ExtensionErrorUtils::FormatErrorMessage( |
| 110 errors::kInvalidWebURL, "0")); | 110 errors::kInvalidWebURL, "0")); |
| 111 LoadAndExpectError("web_urls_invalid_3.json", | 111 LoadAndExpectError("web_urls_invalid_3.json", |
| 112 ExtensionErrorUtils::FormatErrorMessage( | 112 ExtensionErrorUtils::FormatErrorMessage( |
| 113 errors::kInvalidWebURL, "0")); | 113 errors::kInvalidWebURL, "0")); |
| 114 | 114 |
| 115 scoped_ptr<Extension> extension( | 115 scoped_ptr<Extension> extension( |
| 116 LoadAndExpectSuccess("web_urls_default.json")); | 116 LoadAndExpectSuccess("web_urls_default.json")); |
| 117 ASSERT_EQ(1u, extension->web_extent().patterns().size()); | 117 ASSERT_EQ(1u, extension->web_extent().patterns().size()); |
| 118 EXPECT_EQ("*://www.google.com/*", | 118 EXPECT_EQ("*://www.google.com/*", |
| 119 extension->web_extent().patterns()[0].GetAsString()); | 119 extension->web_extent().patterns()[0].GetAsString()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(ManifestTest, AppBrowseUrls) { | 122 TEST_F(ExtensionManifestTest, AppBrowseUrls) { |
| 123 LoadAndExpectError("browse_urls_wrong_type.json", | 123 LoadAndExpectError("browse_urls_wrong_type.json", |
| 124 errors::kInvalidBrowseURLs); | 124 errors::kInvalidBrowseURLs); |
| 125 LoadAndExpectError("browse_urls_invalid_1.json", | 125 LoadAndExpectError("browse_urls_invalid_1.json", |
| 126 ExtensionErrorUtils::FormatErrorMessage( | 126 ExtensionErrorUtils::FormatErrorMessage( |
| 127 errors::kInvalidBrowseURL, "0")); | 127 errors::kInvalidBrowseURL, "0")); |
| 128 LoadAndExpectError("browse_urls_invalid_2.json", | 128 LoadAndExpectError("browse_urls_invalid_2.json", |
| 129 ExtensionErrorUtils::FormatErrorMessage( | 129 ExtensionErrorUtils::FormatErrorMessage( |
| 130 errors::kInvalidBrowseURL, "0")); | 130 errors::kInvalidBrowseURL, "0")); |
| 131 LoadAndExpectError("browse_urls_invalid_3.json", | 131 LoadAndExpectError("browse_urls_invalid_3.json", |
| 132 ExtensionErrorUtils::FormatErrorMessage( | 132 ExtensionErrorUtils::FormatErrorMessage( |
| 133 errors::kInvalidBrowseURL, "0")); | 133 errors::kInvalidBrowseURL, "0")); |
| 134 | 134 |
| 135 scoped_ptr<Extension> extension( | 135 scoped_ptr<Extension> extension( |
| 136 LoadAndExpectSuccess("browse_urls_default.json")); | 136 LoadAndExpectSuccess("browse_urls_default.json")); |
| 137 EXPECT_EQ(0u, extension->browse_extent().patterns().size()); | 137 EXPECT_EQ(0u, extension->browse_extent().patterns().size()); |
| 138 | 138 |
| 139 extension.reset( | 139 extension.reset( |
| 140 LoadAndExpectSuccess("browse_urls_valid.json")); | 140 LoadAndExpectSuccess("browse_urls_valid.json")); |
| 141 ASSERT_EQ(1u, extension->browse_extent().patterns().size()); | 141 ASSERT_EQ(1u, extension->browse_extent().patterns().size()); |
| 142 EXPECT_EQ("https://www.google.com/accounts/*", | 142 EXPECT_EQ("https://www.google.com/accounts/*", |
| 143 extension->browse_extent().patterns()[0].GetAsString()); | 143 extension->browse_extent().patterns()[0].GetAsString()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 TEST_F(ManifestTest, AppLaunchContainer) { | 146 TEST_F(ExtensionManifestTest, AppLaunchContainer) { |
| 147 scoped_ptr<Extension> extension; | 147 scoped_ptr<Extension> extension; |
| 148 | 148 |
| 149 extension.reset(LoadAndExpectSuccess("launch_tab.json")); | 149 extension.reset(LoadAndExpectSuccess("launch_tab.json")); |
| 150 EXPECT_EQ(Extension::LAUNCH_TAB, extension->launch_container()); | 150 EXPECT_EQ(Extension::LAUNCH_TAB, extension->launch_container()); |
| 151 | 151 |
| 152 extension.reset(LoadAndExpectSuccess("launch_window.json")); | |
| 153 EXPECT_EQ(Extension::LAUNCH_WINDOW, extension->launch_container()); | |
| 154 | |
| 155 extension.reset(LoadAndExpectSuccess("launch_panel.json")); | 152 extension.reset(LoadAndExpectSuccess("launch_panel.json")); |
| 156 EXPECT_EQ(Extension::LAUNCH_PANEL, extension->launch_container()); | 153 EXPECT_EQ(Extension::LAUNCH_PANEL, extension->launch_container()); |
| 157 | 154 |
| 158 extension.reset(LoadAndExpectSuccess("launch_default.json")); | 155 extension.reset(LoadAndExpectSuccess("launch_default.json")); |
| 159 EXPECT_EQ(Extension::LAUNCH_TAB, extension->launch_container()); | 156 EXPECT_EQ(Extension::LAUNCH_TAB, extension->launch_container()); |
| 160 | 157 |
| 161 extension.reset(LoadAndExpectSuccess("launch_fullscreen.json")); | 158 extension.reset(LoadAndExpectSuccess("launch_fullscreen.json")); |
| 162 EXPECT_EQ(true, extension->launch_fullscreen()); | 159 EXPECT_EQ(true, extension->launch_fullscreen()); |
| 163 | 160 |
| 164 extension.reset(LoadAndExpectSuccess("launch_width.json")); | 161 extension.reset(LoadAndExpectSuccess("launch_width.json")); |
| 165 EXPECT_EQ(640, extension->launch_width()); | 162 EXPECT_EQ(640, extension->launch_width()); |
| 166 | 163 |
| 167 extension.reset(LoadAndExpectSuccess("launch_height.json")); | 164 extension.reset(LoadAndExpectSuccess("launch_height.json")); |
| 168 EXPECT_EQ(480, extension->launch_height()); | 165 EXPECT_EQ(480, extension->launch_height()); |
| 169 | 166 |
| 167 LoadAndExpectError("launch_window.json", |
| 168 errors::kInvalidLaunchContainer); |
| 170 LoadAndExpectError("launch_container_invalid_type.json", | 169 LoadAndExpectError("launch_container_invalid_type.json", |
| 171 errors::kInvalidLaunchContainer); | 170 errors::kInvalidLaunchContainer); |
| 172 LoadAndExpectError("launch_container_invalid_value.json", | 171 LoadAndExpectError("launch_container_invalid_value.json", |
| 173 errors::kInvalidLaunchContainer); | 172 errors::kInvalidLaunchContainer); |
| 174 LoadAndExpectError("launch_container_without_launch_url.json", | 173 LoadAndExpectError("launch_container_without_launch_url.json", |
| 175 errors::kLaunchURLRequired); | 174 errors::kLaunchURLRequired); |
| 176 LoadAndExpectError("launch_fullscreen_invalid.json", | 175 LoadAndExpectError("launch_fullscreen_invalid.json", |
| 177 errors::kInvalidLaunchFullscreen); | 176 errors::kInvalidLaunchFullscreen); |
| 178 LoadAndExpectError("launch_width_invalid.json", | 177 LoadAndExpectError("launch_width_invalid.json", |
| 179 errors::kInvalidLaunchWidthContainer); | 178 errors::kInvalidLaunchWidthContainer); |
| 180 LoadAndExpectError("launch_width_negative.json", | 179 LoadAndExpectError("launch_width_negative.json", |
| 181 errors::kInvalidLaunchWidth); | 180 errors::kInvalidLaunchWidth); |
| 182 LoadAndExpectError("launch_height_invalid.json", | 181 LoadAndExpectError("launch_height_invalid.json", |
| 183 errors::kInvalidLaunchHeightContainer); | 182 errors::kInvalidLaunchHeightContainer); |
| 184 LoadAndExpectError("launch_height_negative.json", | 183 LoadAndExpectError("launch_height_negative.json", |
| 185 errors::kInvalidLaunchHeight); | 184 errors::kInvalidLaunchHeight); |
| 186 } | 185 } |
| 187 | 186 |
| 188 TEST_F(ManifestTest, AppLaunchURL) { | 187 TEST_F(ExtensionManifestTest, AppLaunchURL) { |
| 189 LoadAndExpectError("launch_path_and_url.json", | 188 LoadAndExpectError("launch_path_and_url.json", |
| 190 errors::kLaunchPathAndURLAreExclusive); | 189 errors::kLaunchPathAndURLAreExclusive); |
| 191 LoadAndExpectError("launch_path_invalid_type.json", | 190 LoadAndExpectError("launch_path_invalid_type.json", |
| 192 errors::kInvalidLaunchLocalPath); | 191 errors::kInvalidLaunchLocalPath); |
| 193 LoadAndExpectError("launch_path_invalid_value.json", | 192 LoadAndExpectError("launch_path_invalid_value.json", |
| 194 errors::kInvalidLaunchLocalPath); | 193 errors::kInvalidLaunchLocalPath); |
| 195 LoadAndExpectError("launch_url_invalid_type.json", | 194 LoadAndExpectError("launch_url_invalid_type.json", |
| 196 errors::kInvalidLaunchWebURL); | 195 errors::kInvalidLaunchWebURL); |
| 197 | 196 |
| 198 scoped_ptr<Extension> extension; | 197 scoped_ptr<Extension> extension; |
| 199 extension.reset(LoadAndExpectSuccess("launch_local_path.json")); | 198 extension.reset(LoadAndExpectSuccess("launch_local_path.json")); |
| 200 EXPECT_EQ(extension->url().spec() + "launch.html", | 199 EXPECT_EQ(extension->url().spec() + "launch.html", |
| 201 extension->GetFullLaunchURL().spec()); | 200 extension->GetFullLaunchURL().spec()); |
| 202 | 201 |
| 203 LoadAndExpectError("launch_web_url_relative.json", | 202 LoadAndExpectError("launch_web_url_relative.json", |
| 204 errors::kInvalidLaunchWebURL); | 203 errors::kInvalidLaunchWebURL); |
| 205 | 204 |
| 206 extension.reset(LoadAndExpectSuccess("launch_web_url_absolute.json")); | 205 extension.reset(LoadAndExpectSuccess("launch_web_url_absolute.json")); |
| 207 EXPECT_EQ(GURL("http://www.google.com/launch.html"), | 206 EXPECT_EQ(GURL("http://www.google.com/launch.html"), |
| 208 extension->GetFullLaunchURL()); | 207 extension->GetFullLaunchURL()); |
| 209 } | 208 } |
| 210 | 209 |
| 211 TEST_F(ManifestTest, Override) { | 210 TEST_F(ExtensionManifestTest, Override) { |
| 212 LoadAndExpectError("override_newtab_and_history.json", | 211 LoadAndExpectError("override_newtab_and_history.json", |
| 213 errors::kMultipleOverrides); | 212 errors::kMultipleOverrides); |
| 214 LoadAndExpectError("override_invalid_page.json", | 213 LoadAndExpectError("override_invalid_page.json", |
| 215 errors::kInvalidChromeURLOverrides); | 214 errors::kInvalidChromeURLOverrides); |
| 216 | 215 |
| 217 scoped_ptr<Extension> extension; | 216 scoped_ptr<Extension> extension; |
| 218 | 217 |
| 219 extension.reset(LoadAndExpectSuccess("override_new_tab.json")); | 218 extension.reset(LoadAndExpectSuccess("override_new_tab.json")); |
| 220 EXPECT_EQ(extension->url().spec() + "newtab.html", | 219 EXPECT_EQ(extension->url().spec() + "newtab.html", |
| 221 extension->GetChromeURLOverrides().find("newtab")->second.spec()); | 220 extension->GetChromeURLOverrides().find("newtab")->second.spec()); |
| 222 | 221 |
| 223 extension.reset(LoadAndExpectSuccess("override_history.json")); | 222 extension.reset(LoadAndExpectSuccess("override_history.json")); |
| 224 EXPECT_EQ(extension->url().spec() + "history.html", | 223 EXPECT_EQ(extension->url().spec() + "history.html", |
| 225 extension->GetChromeURLOverrides().find("history")->second.spec()); | 224 extension->GetChromeURLOverrides().find("history")->second.spec()); |
| 226 } | 225 } |
| 227 | 226 |
| 228 TEST_F(ManifestTest, ChromeURLPermissionInvalid) { | 227 TEST_F(ExtensionManifestTest, ChromeURLPermissionInvalid) { |
| 229 LoadAndExpectError("permission_chrome_url_invalid.json", | 228 LoadAndExpectError("permission_chrome_url_invalid.json", |
| 230 errors::kInvalidPermissionScheme); | 229 errors::kInvalidPermissionScheme); |
| 231 } | 230 } |
| 232 | 231 |
| 233 TEST_F(ManifestTest, ChromeResourcesPermissionValidOnlyForComponents) { | 232 TEST_F(ExtensionManifestTest, ChromeResourcesPermissionValidOnlyForComponents) { |
| 234 LoadAndExpectError("permission_chrome_resources_url.json", | 233 LoadAndExpectError("permission_chrome_resources_url.json", |
| 235 errors::kInvalidPermissionScheme); | 234 errors::kInvalidPermissionScheme); |
| 236 std::string error; | 235 std::string error; |
| 237 scoped_ptr<Extension> extension; | 236 scoped_ptr<Extension> extension; |
| 238 extension.reset(LoadExtensionWithLocation( | 237 extension.reset(LoadExtensionWithLocation( |
| 239 "permission_chrome_resources_url.json", | 238 "permission_chrome_resources_url.json", |
| 240 Extension::COMPONENT, | 239 Extension::COMPONENT, |
| 241 &error)); | 240 &error)); |
| 242 EXPECT_EQ("", error); | 241 EXPECT_EQ("", error); |
| 243 } | 242 } |
| 244 | 243 |
| 245 TEST_F(ManifestTest, ChromeURLContentScriptInvalid) { | 244 TEST_F(ExtensionManifestTest, ChromeURLContentScriptInvalid) { |
| 246 LoadAndExpectError("content_script_chrome_url_invalid.json", | 245 LoadAndExpectError("content_script_chrome_url_invalid.json", |
| 247 errors::kInvalidMatch); | 246 errors::kInvalidMatch); |
| 248 } | 247 } |
| 249 | 248 |
| 250 TEST_F(ManifestTest, DevToolsExtensions) { | 249 TEST_F(ExtensionManifestTest, DevToolsExtensions) { |
| 251 LoadAndExpectError("devtools_extension_no_permissions.json", | 250 LoadAndExpectError("devtools_extension_no_permissions.json", |
| 252 errors::kDevToolsExperimental); | 251 errors::kDevToolsExperimental); |
| 253 LoadAndExpectError("devtools_extension_url_invalid_type.json", | 252 LoadAndExpectError("devtools_extension_url_invalid_type.json", |
| 254 errors::kInvalidDevToolsPage); | 253 errors::kInvalidDevToolsPage); |
| 255 | 254 |
| 256 CommandLine old_command_line = *CommandLine::ForCurrentProcess(); | 255 CommandLine old_command_line = *CommandLine::ForCurrentProcess(); |
| 257 CommandLine::ForCurrentProcess()->AppendSwitch( | 256 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 258 switches::kEnableExperimentalExtensionApis); | 257 switches::kEnableExperimentalExtensionApis); |
| 259 | 258 |
| 260 scoped_ptr<Extension> extension; | 259 scoped_ptr<Extension> extension; |
| 261 extension.reset(LoadAndExpectSuccess("devtools_extension.json")); | 260 extension.reset(LoadAndExpectSuccess("devtools_extension.json")); |
| 262 EXPECT_EQ(extension->url().spec() + "devtools.html", | 261 EXPECT_EQ(extension->url().spec() + "devtools.html", |
| 263 extension->devtools_url().spec()); | 262 extension->devtools_url().spec()); |
| 264 *CommandLine::ForCurrentProcess() = old_command_line; | 263 *CommandLine::ForCurrentProcess() = old_command_line; |
| 265 } | 264 } |
| 266 | 265 |
| 267 TEST_F(ManifestTest, DisallowHybridApps) { | 266 TEST_F(ExtensionManifestTest, DisallowHybridApps) { |
| 268 LoadAndExpectError("disallow_hybrid_1.json", | 267 LoadAndExpectError("disallow_hybrid_1.json", |
| 269 errors::kHostedAppsCannotIncludeExtensionFeatures); | 268 errors::kHostedAppsCannotIncludeExtensionFeatures); |
| 270 LoadAndExpectError("disallow_hybrid_2.json", | 269 LoadAndExpectError("disallow_hybrid_2.json", |
| 271 errors::kHostedAppsCannotIncludeExtensionFeatures); | 270 errors::kHostedAppsCannotIncludeExtensionFeatures); |
| 272 } | 271 } |
| OLD | NEW |