| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/test/test_file_util.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/extensions/chrome_content_verifier_delegate.h" |
| 17 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/test/base/testing_profile.h" |
| 22 #include "components/crx_file/id_util.h" |
| 18 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
| 19 #include "content/public/common/browser_side_navigation_policy.h" | 24 #include "content/public/common/browser_side_navigation_policy.h" |
| 20 #include "content/public/common/previews_state.h" | 25 #include "content/public/common/previews_state.h" |
| 21 #include "content/public/test/mock_resource_context.h" | 26 #include "content/public/test/mock_resource_context.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "content/public/test/test_utils.h" |
| 29 #include "extensions/browser/content_verifier.h" |
| 23 #include "extensions/browser/extension_protocols.h" | 30 #include "extensions/browser/extension_protocols.h" |
| 24 #include "extensions/browser/info_map.h" | 31 #include "extensions/browser/info_map.h" |
| 25 #include "extensions/common/constants.h" | 32 #include "extensions/common/constants.h" |
| 26 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
| 34 #include "extensions/common/extension_builder.h" |
| 27 #include "extensions/common/file_util.h" | 35 #include "extensions/common/file_util.h" |
| 28 #include "net/base/request_priority.h" | 36 #include "net/base/request_priority.h" |
| 29 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
| 30 #include "net/url_request/url_request_job_factory_impl.h" | 38 #include "net/url_request/url_request_job_factory_impl.h" |
| 31 #include "net/url_request/url_request_status.h" | 39 #include "net/url_request/url_request_status.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 40 #include "net/url_request/url_request_test_util.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 42 |
| 35 using content::ResourceType; | 43 using content::ResourceType; |
| 36 | 44 |
| 37 namespace extensions { | 45 namespace extensions { |
| 38 namespace { | 46 namespace { |
| 39 | 47 |
| 40 base::FilePath GetTestPath(const std::string& name) { | 48 base::FilePath GetTestPath(const std::string& name) { |
| 41 base::FilePath path; | 49 base::FilePath path; |
| 42 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 50 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 43 return path.AppendASCII("extensions").AppendASCII(name); | 51 return path.AppendASCII("extensions").AppendASCII(name); |
| 44 } | 52 } |
| 45 | 53 |
| 54 // Helper function that creates a file at |relative_path| within |directory| |
| 55 // and fills it with |content|. |
| 56 bool AddFileToDirectory(const base::FilePath& directory, |
| 57 const base::FilePath& relative_path, |
| 58 const std::string& content) { |
| 59 base::FilePath full_path = directory.Append(relative_path); |
| 60 int result = base::WriteFile(full_path, content.data(), content.size()); |
| 61 return static_cast<size_t>(result) == content.size(); |
| 62 } |
| 63 |
| 46 scoped_refptr<Extension> CreateTestExtension(const std::string& name, | 64 scoped_refptr<Extension> CreateTestExtension(const std::string& name, |
| 47 bool incognito_split_mode) { | 65 bool incognito_split_mode) { |
| 48 base::DictionaryValue manifest; | 66 base::DictionaryValue manifest; |
| 49 manifest.SetString("name", name); | 67 manifest.SetString("name", name); |
| 50 manifest.SetString("version", "1"); | 68 manifest.SetString("version", "1"); |
| 51 manifest.SetInteger("manifest_version", 2); | 69 manifest.SetInteger("manifest_version", 2); |
| 52 manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); | 70 manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); |
| 53 | 71 |
| 54 base::FilePath path = GetTestPath("response_headers"); | 72 base::FilePath path = GetTestPath("response_headers"); |
| 55 | 73 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 base::FilePath path = GetTestPath("response_headers"); | 109 base::FilePath path = GetTestPath("response_headers"); |
| 92 | 110 |
| 93 std::string error; | 111 std::string error; |
| 94 scoped_refptr<Extension> extension( | 112 scoped_refptr<Extension> extension( |
| 95 Extension::Create(path, Manifest::UNPACKED, manifest, | 113 Extension::Create(path, Manifest::UNPACKED, manifest, |
| 96 Extension::NO_FLAGS, &error)); | 114 Extension::NO_FLAGS, &error)); |
| 97 EXPECT_TRUE(extension.get()) << error; | 115 EXPECT_TRUE(extension.get()) << error; |
| 98 return extension; | 116 return extension; |
| 99 } | 117 } |
| 100 | 118 |
| 119 // A ContentVerifyJob::TestDelegate that observes DoneReading(). |
| 120 class JobDelegate : public ContentVerifyJob::TestDelegate { |
| 121 public: |
| 122 explicit JobDelegate(const std::string& expected_contents) |
| 123 : expected_contents_(expected_contents), run_loop_(new base::RunLoop()) { |
| 124 ContentVerifyJob::SetDelegateForTests(this); |
| 125 } |
| 126 ~JobDelegate() override { ContentVerifyJob::SetDelegateForTests(nullptr); } |
| 127 |
| 128 ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id, |
| 129 int count, |
| 130 const char* data) override { |
| 131 read_contents_.append(data, count); |
| 132 return ContentVerifyJob::NONE; |
| 133 } |
| 134 |
| 135 ContentVerifyJob::FailureReason DoneReading(const ExtensionId& id) override { |
| 136 seen_done_reading_extension_ids_.insert(id); |
| 137 if (waiting_for_extension_id_ == id) |
| 138 run_loop_->Quit(); |
| 139 |
| 140 if (!base::StartsWith(expected_contents_, read_contents_, |
| 141 base::CompareCase::SENSITIVE)) { |
| 142 ADD_FAILURE() << "Unexpected read, expected: " << expected_contents_ |
| 143 << ", but found: " << read_contents_; |
| 144 } |
| 145 return ContentVerifyJob::NONE; |
| 146 } |
| 147 |
| 148 void WaitForDoneReading(const ExtensionId& id) { |
| 149 ASSERT_FALSE(waiting_for_extension_id_); |
| 150 if (seen_done_reading_extension_ids_.count(id)) |
| 151 return; |
| 152 waiting_for_extension_id_ = id; |
| 153 run_loop_->Run(); |
| 154 } |
| 155 |
| 156 void Reset() { |
| 157 read_contents_.clear(); |
| 158 waiting_for_extension_id_.reset(); |
| 159 seen_done_reading_extension_ids_.clear(); |
| 160 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 161 } |
| 162 |
| 163 private: |
| 164 std::string expected_contents_; |
| 165 std::string read_contents_; |
| 166 std::set<ExtensionId> seen_done_reading_extension_ids_; |
| 167 base::Optional<ExtensionId> waiting_for_extension_id_; |
| 168 std::unique_ptr<base::RunLoop> run_loop_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(JobDelegate); |
| 171 }; |
| 172 |
| 101 } // namespace | 173 } // namespace |
| 102 | 174 |
| 103 // This test lives in src/chrome instead of src/extensions because it tests | 175 // This test lives in src/chrome instead of src/extensions because it tests |
| 104 // functionality delegated back to Chrome via ChromeExtensionsBrowserClient. | 176 // functionality delegated back to Chrome via ChromeExtensionsBrowserClient. |
| 105 // See chrome/browser/extensions/chrome_url_request_util.cc. | 177 // See chrome/browser/extensions/chrome_url_request_util.cc. |
| 106 class ExtensionProtocolTest : public testing::Test { | 178 class ExtensionProtocolsTest : public testing::Test { |
| 107 public: | 179 public: |
| 108 ExtensionProtocolTest() | 180 ExtensionProtocolsTest() |
| 109 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 181 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 110 old_factory_(NULL), | 182 old_factory_(NULL), |
| 111 resource_context_(&test_url_request_context_) {} | 183 resource_context_(&test_url_request_context_) {} |
| 112 | 184 |
| 113 void SetUp() override { | 185 void SetUp() override { |
| 114 testing::Test::SetUp(); | 186 testing::Test::SetUp(); |
| 187 testing_profile_ = TestingProfile::Builder().Build(); |
| 115 extension_info_map_ = new InfoMap(); | 188 extension_info_map_ = new InfoMap(); |
| 116 net::URLRequestContext* request_context = | 189 net::URLRequestContext* request_context = |
| 117 resource_context_.GetRequestContext(); | 190 resource_context_.GetRequestContext(); |
| 118 old_factory_ = request_context->job_factory(); | 191 old_factory_ = request_context->job_factory(); |
| 192 |
| 193 // Set up content verification. |
| 194 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 195 command_line->AppendSwitchASCII( |
| 196 switches::kExtensionContentVerification, |
| 197 switches::kExtensionContentVerificationEnforce); |
| 198 content_verifier_ = new ContentVerifier( |
| 199 testing_profile_.get(), |
| 200 new ChromeContentVerifierDelegate(testing_profile_.get())); |
| 201 extension_info_map_->SetContentVerifier(content_verifier_.get()); |
| 119 } | 202 } |
| 120 | 203 |
| 121 void TearDown() override { | 204 void TearDown() override { |
| 122 net::URLRequestContext* request_context = | 205 net::URLRequestContext* request_context = |
| 123 resource_context_.GetRequestContext(); | 206 resource_context_.GetRequestContext(); |
| 124 request_context->set_job_factory(old_factory_); | 207 request_context->set_job_factory(old_factory_); |
| 208 content_verifier_->Shutdown(); |
| 125 } | 209 } |
| 126 | 210 |
| 127 void SetProtocolHandler(bool is_incognito) { | 211 void SetProtocolHandler(bool is_incognito) { |
| 128 net::URLRequestContext* request_context = | 212 net::URLRequestContext* request_context = |
| 129 resource_context_.GetRequestContext(); | 213 resource_context_.GetRequestContext(); |
| 130 job_factory_.SetProtocolHandler( | 214 job_factory_.SetProtocolHandler( |
| 131 kExtensionScheme, | 215 kExtensionScheme, |
| 132 CreateExtensionProtocolHandler(is_incognito, | 216 CreateExtensionProtocolHandler(is_incognito, |
| 133 extension_info_map_.get())); | 217 extension_info_map_.get())); |
| 134 request_context->set_job_factory(&job_factory_); | 218 request_context->set_job_factory(&job_factory_); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 252 } |
| 169 | 253 |
| 170 protected: | 254 protected: |
| 171 content::TestBrowserThreadBundle thread_bundle_; | 255 content::TestBrowserThreadBundle thread_bundle_; |
| 172 scoped_refptr<InfoMap> extension_info_map_; | 256 scoped_refptr<InfoMap> extension_info_map_; |
| 173 net::URLRequestJobFactoryImpl job_factory_; | 257 net::URLRequestJobFactoryImpl job_factory_; |
| 174 const net::URLRequestJobFactory* old_factory_; | 258 const net::URLRequestJobFactory* old_factory_; |
| 175 net::TestDelegate test_delegate_; | 259 net::TestDelegate test_delegate_; |
| 176 net::TestURLRequestContext test_url_request_context_; | 260 net::TestURLRequestContext test_url_request_context_; |
| 177 content::MockResourceContext resource_context_; | 261 content::MockResourceContext resource_context_; |
| 262 scoped_refptr<ContentVerifier> content_verifier_; |
| 263 std::unique_ptr<TestingProfile> testing_profile_; |
| 178 }; | 264 }; |
| 179 | 265 |
| 180 // Tests that making a chrome-extension request in an incognito context is | 266 // Tests that making a chrome-extension request in an incognito context is |
| 181 // only allowed under the right circumstances (if the extension is allowed | 267 // only allowed under the right circumstances (if the extension is allowed |
| 182 // in incognito, and it's either a non-main-frame request or a split-mode | 268 // in incognito, and it's either a non-main-frame request or a split-mode |
| 183 // extension). | 269 // extension). |
| 184 TEST_F(ExtensionProtocolTest, IncognitoRequest) { | 270 TEST_F(ExtensionProtocolsTest, IncognitoRequest) { |
| 185 // Register an incognito extension protocol handler. | 271 // Register an incognito extension protocol handler. |
| 186 SetProtocolHandler(true); | 272 SetProtocolHandler(true); |
| 187 | 273 |
| 188 struct TestCase { | 274 struct TestCase { |
| 189 // Inputs. | 275 // Inputs. |
| 190 std::string name; | 276 std::string name; |
| 191 bool incognito_split_mode; | 277 bool incognito_split_mode; |
| 192 bool incognito_enabled; | 278 bool incognito_enabled; |
| 193 | 279 |
| 194 // Expected results. | 280 // Expected results. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, | 343 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, |
| 258 &content_length); | 344 &content_length); |
| 259 EXPECT_FALSE(content_length.empty()); | 345 EXPECT_FALSE(content_length.empty()); |
| 260 int length_value = 0; | 346 int length_value = 0; |
| 261 EXPECT_TRUE(base::StringToInt(content_length, &length_value)); | 347 EXPECT_TRUE(base::StringToInt(content_length, &length_value)); |
| 262 EXPECT_GT(length_value, 0); | 348 EXPECT_GT(length_value, 0); |
| 263 } | 349 } |
| 264 | 350 |
| 265 // Tests getting a resource for a component extension works correctly, both when | 351 // Tests getting a resource for a component extension works correctly, both when |
| 266 // the extension is enabled and when it is disabled. | 352 // the extension is enabled and when it is disabled. |
| 267 TEST_F(ExtensionProtocolTest, ComponentResourceRequest) { | 353 TEST_F(ExtensionProtocolsTest, ComponentResourceRequest) { |
| 268 // Register a non-incognito extension protocol handler. | 354 // Register a non-incognito extension protocol handler. |
| 269 SetProtocolHandler(false); | 355 SetProtocolHandler(false); |
| 270 | 356 |
| 271 scoped_refptr<Extension> extension = CreateWebStoreExtension(); | 357 scoped_refptr<Extension> extension = CreateWebStoreExtension(); |
| 272 extension_info_map_->AddExtension(extension.get(), | 358 extension_info_map_->AddExtension(extension.get(), |
| 273 base::Time::Now(), | 359 base::Time::Now(), |
| 274 false, | 360 false, |
| 275 false); | 361 false); |
| 276 | 362 |
| 277 // First test it with the extension enabled. | 363 // First test it with the extension enabled. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 294 extension->GetResourceURL("webstore_icon_16.png"), | 380 extension->GetResourceURL("webstore_icon_16.png"), |
| 295 net::DEFAULT_PRIORITY, &test_delegate_)); | 381 net::DEFAULT_PRIORITY, &test_delegate_)); |
| 296 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | 382 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 297 EXPECT_EQ(net::OK, test_delegate_.request_status()); | 383 EXPECT_EQ(net::OK, test_delegate_.request_status()); |
| 298 CheckForContentLengthHeader(request.get()); | 384 CheckForContentLengthHeader(request.get()); |
| 299 } | 385 } |
| 300 } | 386 } |
| 301 | 387 |
| 302 // Tests that a URL request for resource from an extension returns a few | 388 // Tests that a URL request for resource from an extension returns a few |
| 303 // expected response headers. | 389 // expected response headers. |
| 304 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) { | 390 TEST_F(ExtensionProtocolsTest, ResourceRequestResponseHeaders) { |
| 305 // Register a non-incognito extension protocol handler. | 391 // Register a non-incognito extension protocol handler. |
| 306 SetProtocolHandler(false); | 392 SetProtocolHandler(false); |
| 307 | 393 |
| 308 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); | 394 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); |
| 309 extension_info_map_->AddExtension(extension.get(), | 395 extension_info_map_->AddExtension(extension.get(), |
| 310 base::Time::Now(), | 396 base::Time::Now(), |
| 311 false, | 397 false, |
| 312 false); | 398 false); |
| 313 | 399 |
| 314 { | 400 { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 332 // We set test.dat as web-accessible, so it should have a CORS header. | 418 // We set test.dat as web-accessible, so it should have a CORS header. |
| 333 std::string access_control; | 419 std::string access_control; |
| 334 request->GetResponseHeaderByName("Access-Control-Allow-Origin", | 420 request->GetResponseHeaderByName("Access-Control-Allow-Origin", |
| 335 &access_control); | 421 &access_control); |
| 336 EXPECT_EQ("*", access_control); | 422 EXPECT_EQ("*", access_control); |
| 337 } | 423 } |
| 338 } | 424 } |
| 339 | 425 |
| 340 // Tests that a URL request for main frame or subframe from an extension | 426 // Tests that a URL request for main frame or subframe from an extension |
| 341 // succeeds, but subresources fail. See http://crbug.com/312269. | 427 // succeeds, but subresources fail. See http://crbug.com/312269. |
| 342 TEST_F(ExtensionProtocolTest, AllowFrameRequests) { | 428 TEST_F(ExtensionProtocolsTest, AllowFrameRequests) { |
| 343 // Register a non-incognito extension protocol handler. | 429 // Register a non-incognito extension protocol handler. |
| 344 SetProtocolHandler(false); | 430 SetProtocolHandler(false); |
| 345 | 431 |
| 346 scoped_refptr<Extension> extension = CreateTestExtension("foo", false); | 432 scoped_refptr<Extension> extension = CreateTestExtension("foo", false); |
| 347 extension_info_map_->AddExtension(extension.get(), | 433 extension_info_map_->AddExtension(extension.get(), |
| 348 base::Time::Now(), | 434 base::Time::Now(), |
| 349 false, | 435 false, |
| 350 false); | 436 false); |
| 351 | 437 |
| 352 // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not | 438 // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not |
| (...skipping 26 matching lines...) Expand all Loading... |
| 379 { | 465 { |
| 380 std::unique_ptr<net::URLRequest> request( | 466 std::unique_ptr<net::URLRequest> request( |
| 381 resource_context_.GetRequestContext()->CreateRequest( | 467 resource_context_.GetRequestContext()->CreateRequest( |
| 382 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, | 468 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
| 383 &test_delegate_)); | 469 &test_delegate_)); |
| 384 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | 470 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 385 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()); | 471 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()); |
| 386 } | 472 } |
| 387 } | 473 } |
| 388 | 474 |
| 389 | 475 TEST_F(ExtensionProtocolsTest, MetadataFolder) { |
| 390 TEST_F(ExtensionProtocolTest, MetadataFolder) { | |
| 391 SetProtocolHandler(false); | 476 SetProtocolHandler(false); |
| 392 | 477 |
| 393 base::FilePath extension_dir = GetTestPath("metadata_folder"); | 478 base::FilePath extension_dir = GetTestPath("metadata_folder"); |
| 394 std::string error; | 479 std::string error; |
| 395 scoped_refptr<Extension> extension = | 480 scoped_refptr<Extension> extension = |
| 396 file_util::LoadExtension(extension_dir, Manifest::INTERNAL, | 481 file_util::LoadExtension(extension_dir, Manifest::INTERNAL, |
| 397 Extension::NO_FLAGS, &error); | 482 Extension::NO_FLAGS, &error); |
| 398 ASSERT_NE(extension.get(), nullptr) << "error: " << error; | 483 ASSERT_NE(extension.get(), nullptr) << "error: " << error; |
| 399 | 484 |
| 400 // Loading "/test.html" should succeed. | 485 // Loading "/test.html" should succeed. |
| 401 EXPECT_EQ(net::OK, DoRequest(*extension, "test.html")); | 486 EXPECT_EQ(net::OK, DoRequest(*extension, "test.html")); |
| 402 | 487 |
| 403 // Loading "/_metadata/verified_contents.json" should fail. | 488 // Loading "/_metadata/verified_contents.json" should fail. |
| 404 base::FilePath relative_path = | 489 base::FilePath relative_path = |
| 405 base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); | 490 base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); |
| 406 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); | 491 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
| 407 EXPECT_EQ(net::ERR_FAILED, | 492 EXPECT_EQ(net::ERR_FAILED, |
| 408 DoRequest(*extension, relative_path.AsUTF8Unsafe())); | 493 DoRequest(*extension, relative_path.AsUTF8Unsafe())); |
| 409 | 494 |
| 410 // Loading "/_metadata/a.txt" should also fail. | 495 // Loading "/_metadata/a.txt" should also fail. |
| 411 relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); | 496 relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); |
| 412 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); | 497 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
| 413 EXPECT_EQ(net::ERR_FAILED, | 498 EXPECT_EQ(net::ERR_FAILED, |
| 414 DoRequest(*extension, relative_path.AsUTF8Unsafe())); | 499 DoRequest(*extension, relative_path.AsUTF8Unsafe())); |
| 415 } | 500 } |
| 416 | 501 |
| 502 // Tests that unreadable files and deleted files correctly go through |
| 503 // ContentVerifyJob. |
| 504 TEST_F(ExtensionProtocolsTest, VerificationSeenForFileAccessErrors) { |
| 505 const char kFooJsContents[] = "hello world."; |
| 506 JobDelegate test_job_delegate(kFooJsContents); |
| 507 SetProtocolHandler(false); |
| 508 |
| 509 const std::string kFooJs("foo.js"); |
| 510 // Create a temporary directory that a fake extension will live in and fill |
| 511 // it with some test files. |
| 512 base::ScopedTempDir temp_dir; |
| 513 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 514 base::FilePath foo_js(FILE_PATH_LITERAL("foo.js")); |
| 515 ASSERT_TRUE(AddFileToDirectory(temp_dir.GetPath(), foo_js, kFooJsContents)) |
| 516 << "Failed to write " << temp_dir.GetPath().value() << "/" |
| 517 << foo_js.value(); |
| 518 |
| 519 ExtensionBuilder builder; |
| 520 builder |
| 521 .SetManifest(DictionaryBuilder() |
| 522 .Set("name", "Foo") |
| 523 .Set("version", "1.0") |
| 524 .Set("manifest_version", 2) |
| 525 .Set("update_url", |
| 526 "https://clients2.google.com/service/update2/crx") |
| 527 .Build()) |
| 528 .SetID(crx_file::id_util::GenerateId("whatever")) |
| 529 .SetPath(temp_dir.GetPath()) |
| 530 .SetLocation(Manifest::INTERNAL); |
| 531 scoped_refptr<Extension> extension(builder.Build()); |
| 532 |
| 533 ASSERT_TRUE(extension.get()); |
| 534 content_verifier_->OnExtensionLoaded(testing_profile_.get(), extension.get()); |
| 535 // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 536 content::RunAllPendingInMessageLoop(); |
| 537 |
| 538 // Valid and readable foo.js. |
| 539 EXPECT_EQ(net::OK, DoRequest(*extension, kFooJs)); |
| 540 test_job_delegate.WaitForDoneReading(extension->id()); |
| 541 |
| 542 // chmod -r foo.js. |
| 543 base::FilePath foo_path = temp_dir.GetPath().AppendASCII(kFooJs); |
| 544 ASSERT_TRUE(base::MakeFileUnreadable(foo_path)); |
| 545 test_job_delegate.Reset(); |
| 546 EXPECT_EQ(net::ERR_ACCESS_DENIED, DoRequest(*extension, kFooJs)); |
| 547 test_job_delegate.WaitForDoneReading(extension->id()); |
| 548 |
| 549 // Delete foo.js. |
| 550 ASSERT_TRUE(base::DieFileDie(foo_path, false)); |
| 551 test_job_delegate.Reset(); |
| 552 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, DoRequest(*extension, kFooJs)); |
| 553 test_job_delegate.WaitForDoneReading(extension->id()); |
| 554 } |
| 555 |
| 417 } // namespace extensions | 556 } // namespace extensions |
| OLD | NEW |