| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_protocols.h" | |
| 13 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/url_constants.h" | 13 //#include "chrome/common/url_constants.h" |
| 15 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 16 #include "content/public/test/mock_resource_context.h" | 15 #include "content/public/test/mock_resource_context.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "extensions/browser/extension_protocols.h" |
| 18 #include "extensions/browser/info_map.h" | 18 #include "extensions/browser/info_map.h" |
| 19 #include "extensions/common/constants.h" | 19 #include "extensions/common/constants.h" |
| 20 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 21 #include "net/base/request_priority.h" | 21 #include "net/base/request_priority.h" |
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_job_factory_impl.h" | 23 #include "net/url_request/url_request_job_factory_impl.h" |
| 24 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 scoped_refptr<Extension> extension( | 82 scoped_refptr<Extension> extension( |
| 83 Extension::Create(path, Manifest::UNPACKED, manifest, | 83 Extension::Create(path, Manifest::UNPACKED, manifest, |
| 84 Extension::NO_FLAGS, &error)); | 84 Extension::NO_FLAGS, &error)); |
| 85 EXPECT_TRUE(extension.get()) << error; | 85 EXPECT_TRUE(extension.get()) << error; |
| 86 return extension; | 86 return extension; |
| 87 } | 87 } |
| 88 | 88 |
| 89 class ExtensionProtocolTest : public testing::Test { | 89 class ExtensionProtocolTest : public testing::Test { |
| 90 public: | 90 public: |
| 91 ExtensionProtocolTest() | 91 ExtensionProtocolTest() |
| 92 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 92 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 93 resource_context_(&test_url_request_context_) {} | 93 old_factory_(NULL), |
| 94 resource_context_(&test_url_request_context_) {} |
| 94 | 95 |
| 95 virtual void SetUp() OVERRIDE { | 96 virtual void SetUp() OVERRIDE { |
| 96 testing::Test::SetUp(); | 97 testing::Test::SetUp(); |
| 97 extension_info_map_ = new InfoMap(); | 98 extension_info_map_ = new InfoMap(); |
| 98 net::URLRequestContext* request_context = | 99 net::URLRequestContext* request_context = |
| 99 resource_context_.GetRequestContext(); | 100 resource_context_.GetRequestContext(); |
| 100 old_factory_ = request_context->job_factory(); | 101 old_factory_ = request_context->job_factory(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 virtual void TearDown() { | 104 virtual void TearDown() { |
| 104 net::URLRequestContext* request_context = | 105 net::URLRequestContext* request_context = |
| 105 resource_context_.GetRequestContext(); | 106 resource_context_.GetRequestContext(); |
| 106 request_context->set_job_factory(old_factory_); | 107 request_context->set_job_factory(old_factory_); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void SetProtocolHandler(Profile::ProfileType profile_type) { | 110 void SetProtocolHandler(bool is_incognito) { |
| 110 net::URLRequestContext* request_context = | 111 net::URLRequestContext* request_context = |
| 111 resource_context_.GetRequestContext(); | 112 resource_context_.GetRequestContext(); |
| 112 job_factory_.SetProtocolHandler( | 113 job_factory_.SetProtocolHandler( |
| 113 kExtensionScheme, | 114 kExtensionScheme, |
| 114 CreateExtensionProtocolHandler(profile_type, | 115 CreateExtensionProtocolHandler(is_incognito, |
| 115 extension_info_map_.get())); | 116 extension_info_map_.get())); |
| 116 request_context->set_job_factory(&job_factory_); | 117 request_context->set_job_factory(&job_factory_); |
| 117 } | 118 } |
| 118 | 119 |
| 119 void StartRequest(net::URLRequest* request, | 120 void StartRequest(net::URLRequest* request, |
| 120 ResourceType::Type resource_type) { | 121 ResourceType::Type resource_type) { |
| 121 content::ResourceRequestInfo::AllocateForTesting(request, | 122 content::ResourceRequestInfo::AllocateForTesting(request, |
| 122 resource_type, | 123 resource_type, |
| 123 &resource_context_, | 124 &resource_context_, |
| 124 -1, | 125 -1, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 138 net::TestURLRequestContext test_url_request_context_; | 139 net::TestURLRequestContext test_url_request_context_; |
| 139 content::MockResourceContext resource_context_; | 140 content::MockResourceContext resource_context_; |
| 140 }; | 141 }; |
| 141 | 142 |
| 142 // Tests that making a chrome-extension request in an incognito context is | 143 // Tests that making a chrome-extension request in an incognito context is |
| 143 // only allowed under the right circumstances (if the extension is allowed | 144 // only allowed under the right circumstances (if the extension is allowed |
| 144 // in incognito, and it's either a non-main-frame request or a split-mode | 145 // in incognito, and it's either a non-main-frame request or a split-mode |
| 145 // extension). | 146 // extension). |
| 146 TEST_F(ExtensionProtocolTest, IncognitoRequest) { | 147 TEST_F(ExtensionProtocolTest, IncognitoRequest) { |
| 147 // Register an incognito extension protocol handler. | 148 // Register an incognito extension protocol handler. |
| 148 SetProtocolHandler(Profile::INCOGNITO_PROFILE); | 149 SetProtocolHandler(true); |
| 149 | 150 |
| 150 struct TestCase { | 151 struct TestCase { |
| 151 // Inputs. | 152 // Inputs. |
| 152 std::string name; | 153 std::string name; |
| 153 bool incognito_split_mode; | 154 bool incognito_split_mode; |
| 154 bool incognito_enabled; | 155 bool incognito_enabled; |
| 155 | 156 |
| 156 // Expected results. | 157 // Expected results. |
| 157 bool should_allow_main_frame_load; | 158 bool should_allow_main_frame_load; |
| 158 bool should_allow_sub_frame_load; | 159 bool should_allow_sub_frame_load; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 EXPECT_FALSE(content_length.empty()); | 218 EXPECT_FALSE(content_length.empty()); |
| 218 int length_value = 0; | 219 int length_value = 0; |
| 219 EXPECT_TRUE(base::StringToInt(content_length, &length_value)); | 220 EXPECT_TRUE(base::StringToInt(content_length, &length_value)); |
| 220 EXPECT_GT(length_value, 0); | 221 EXPECT_GT(length_value, 0); |
| 221 } | 222 } |
| 222 | 223 |
| 223 // Tests getting a resource for a component extension works correctly, both when | 224 // Tests getting a resource for a component extension works correctly, both when |
| 224 // the extension is enabled and when it is disabled. | 225 // the extension is enabled and when it is disabled. |
| 225 TEST_F(ExtensionProtocolTest, ComponentResourceRequest) { | 226 TEST_F(ExtensionProtocolTest, ComponentResourceRequest) { |
| 226 // Register a non-incognito extension protocol handler. | 227 // Register a non-incognito extension protocol handler. |
| 227 SetProtocolHandler(Profile::REGULAR_PROFILE); | 228 SetProtocolHandler(false); |
| 228 | 229 |
| 229 scoped_refptr<Extension> extension = CreateWebStoreExtension(); | 230 scoped_refptr<Extension> extension = CreateWebStoreExtension(); |
| 230 extension_info_map_->AddExtension(extension.get(), | 231 extension_info_map_->AddExtension(extension.get(), |
| 231 base::Time::Now(), | 232 base::Time::Now(), |
| 232 false, | 233 false, |
| 233 false); | 234 false); |
| 234 | 235 |
| 235 // First test it with the extension enabled. | 236 // First test it with the extension enabled. |
| 236 { | 237 { |
| 237 net::URLRequest request(extension->GetResourceURL("webstore_icon_16.png"), | 238 net::URLRequest request(extension->GetResourceURL("webstore_icon_16.png"), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 254 StartRequest(&request, ResourceType::MEDIA); | 255 StartRequest(&request, ResourceType::MEDIA); |
| 255 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 256 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
| 256 CheckForContentLengthHeader(&request); | 257 CheckForContentLengthHeader(&request); |
| 257 } | 258 } |
| 258 } | 259 } |
| 259 | 260 |
| 260 // Tests that a URL request for resource from an extension returns a few | 261 // Tests that a URL request for resource from an extension returns a few |
| 261 // expected response headers. | 262 // expected response headers. |
| 262 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) { | 263 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) { |
| 263 // Register a non-incognito extension protocol handler. | 264 // Register a non-incognito extension protocol handler. |
| 264 SetProtocolHandler(Profile::REGULAR_PROFILE); | 265 SetProtocolHandler(false); |
| 265 | 266 |
| 266 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); | 267 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); |
| 267 extension_info_map_->AddExtension(extension.get(), | 268 extension_info_map_->AddExtension(extension.get(), |
| 268 base::Time::Now(), | 269 base::Time::Now(), |
| 269 false, | 270 false, |
| 270 false); | 271 false); |
| 271 | 272 |
| 272 { | 273 { |
| 273 net::URLRequest request(extension->GetResourceURL("test.dat"), | 274 net::URLRequest request(extension->GetResourceURL("test.dat"), |
| 274 net::DEFAULT_PRIORITY, | 275 net::DEFAULT_PRIORITY, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 292 request.GetResponseHeaderByName("Access-Control-Allow-Origin", | 293 request.GetResponseHeaderByName("Access-Control-Allow-Origin", |
| 293 &access_control); | 294 &access_control); |
| 294 EXPECT_EQ("*", access_control); | 295 EXPECT_EQ("*", access_control); |
| 295 } | 296 } |
| 296 } | 297 } |
| 297 | 298 |
| 298 // Tests that a URL request for main frame or subframe from an extension | 299 // Tests that a URL request for main frame or subframe from an extension |
| 299 // succeeds, but subresources fail. See http://crbug.com/312269. | 300 // succeeds, but subresources fail. See http://crbug.com/312269. |
| 300 TEST_F(ExtensionProtocolTest, AllowFrameRequests) { | 301 TEST_F(ExtensionProtocolTest, AllowFrameRequests) { |
| 301 // Register a non-incognito extension protocol handler. | 302 // Register a non-incognito extension protocol handler. |
| 302 SetProtocolHandler(Profile::REGULAR_PROFILE); | 303 SetProtocolHandler(false); |
| 303 | 304 |
| 304 scoped_refptr<Extension> extension = CreateTestExtension("foo", false); | 305 scoped_refptr<Extension> extension = CreateTestExtension("foo", false); |
| 305 extension_info_map_->AddExtension(extension.get(), | 306 extension_info_map_->AddExtension(extension.get(), |
| 306 base::Time::Now(), | 307 base::Time::Now(), |
| 307 false, | 308 false, |
| 308 false); | 309 false); |
| 309 | 310 |
| 310 // All MAIN_FRAME and SUB_FRAME requests should succeed. | 311 // All MAIN_FRAME and SUB_FRAME requests should succeed. |
| 311 { | 312 { |
| 312 net::URLRequest request(extension->GetResourceURL("test.dat"), | 313 net::URLRequest request(extension->GetResourceURL("test.dat"), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 330 net::URLRequest request(extension->GetResourceURL("test.dat"), | 331 net::URLRequest request(extension->GetResourceURL("test.dat"), |
| 331 net::DEFAULT_PRIORITY, | 332 net::DEFAULT_PRIORITY, |
| 332 &test_delegate_, | 333 &test_delegate_, |
| 333 resource_context_.GetRequestContext()); | 334 resource_context_.GetRequestContext()); |
| 334 StartRequest(&request, ResourceType::MEDIA); | 335 StartRequest(&request, ResourceType::MEDIA); |
| 335 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 336 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 336 } | 337 } |
| 337 } | 338 } |
| 338 | 339 |
| 339 } // namespace extensions | 340 } // namespace extensions |
| OLD | NEW |