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