| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/browser/extensions/extension_info_map.h" |
| 10 #include "chrome/browser/extensions/extension_protocols.h" |
| 11 #include "chrome/common/url_constants.h" |
| 12 #include "content/public/browser/resource_request_info.h" |
| 13 #include "content/test/mock_resource_context.h" |
| 14 #include "content/test/test_browser_thread.h" |
| 15 #include "net/url_request/url_request.h" |
| 16 #include "net/url_request/url_request_status.h" |
| 17 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using content::BrowserThread; |
| 21 |
| 22 namespace { |
| 23 |
| 24 scoped_refptr<Extension> CreateTestExtension(const std::string& name, |
| 25 bool incognito_split_mode) { |
| 26 DictionaryValue manifest; |
| 27 manifest.SetString("name", name); |
| 28 manifest.SetString("version", "1"); |
| 29 manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); |
| 30 |
| 31 FilePath path; |
| 32 EXPECT_TRUE(file_util::GetCurrentDirectory(&path)); |
| 33 |
| 34 std::string error; |
| 35 scoped_refptr<Extension> extension( |
| 36 Extension::Create(path, Extension::INTERNAL, manifest, |
| 37 Extension::STRICT_ERROR_CHECKS, &error)); |
| 38 EXPECT_TRUE(extension.get()) << error; |
| 39 return extension; |
| 40 } |
| 41 |
| 42 class ExtensionProtocolTest : public testing::Test { |
| 43 public: |
| 44 ExtensionProtocolTest() |
| 45 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 46 file_thread_(BrowserThread::FILE, &message_loop_), |
| 47 io_thread_(BrowserThread::IO, &message_loop_) {} |
| 48 |
| 49 virtual void SetUp() { |
| 50 extension_info_map_ = new ExtensionInfoMap(); |
| 51 net::URLRequestContext* request_context = |
| 52 resource_context_.GetRequestContext(); |
| 53 old_factory_ = request_context->job_factory(); |
| 54 // Register an incognito extension protocol handler. |
| 55 job_factory_.SetProtocolHandler( |
| 56 chrome::kExtensionScheme, |
| 57 CreateExtensionProtocolHandler(true, extension_info_map_)); |
| 58 request_context->set_job_factory(&job_factory_); |
| 59 } |
| 60 |
| 61 virtual void TearDown() { |
| 62 net::URLRequestContext* request_context = |
| 63 resource_context_.GetRequestContext(); |
| 64 request_context->set_job_factory(old_factory_); |
| 65 } |
| 66 |
| 67 void StartRequest(net::URLRequest* request, |
| 68 ResourceType::Type resource_type) { |
| 69 content::ResourceRequestInfo::AllocateForTesting(request, |
| 70 resource_type, |
| 71 &resource_context_); |
| 72 request->set_context(resource_context_.GetRequestContext()); |
| 73 request->Start(); |
| 74 MessageLoop::current()->Run(); |
| 75 } |
| 76 |
| 77 protected: |
| 78 MessageLoopForIO message_loop_; |
| 79 content::TestBrowserThread ui_thread_; |
| 80 content::TestBrowserThread file_thread_; |
| 81 content::TestBrowserThread io_thread_; |
| 82 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
| 83 net::URLRequestJobFactory job_factory_; |
| 84 const net::URLRequestJobFactory* old_factory_; |
| 85 TestDelegate test_delegate_; |
| 86 content::MockResourceContext resource_context_; |
| 87 }; |
| 88 |
| 89 // Tests that making a chrome-extension request in an incognito context is |
| 90 // only allowed under the right circumstances (if the extension is allowed |
| 91 // in incognito, and it's either a non-main-frame request or a split-mode |
| 92 // extension). |
| 93 TEST_F(ExtensionProtocolTest, IncognitoRequest) { |
| 94 struct TestCase { |
| 95 // Inputs. |
| 96 std::string name; |
| 97 bool incognito_split_mode; |
| 98 bool incognito_enabled; |
| 99 |
| 100 // Expected results. |
| 101 bool should_allow_main_frame_load; |
| 102 bool should_allow_sub_frame_load; |
| 103 } cases[] = { |
| 104 {"spanning disabled", false, false, false, false}, |
| 105 {"split disabled", true, false, false, false}, |
| 106 {"spanning enabled", false, true, false, true}, |
| 107 {"split enabled", true, true, true, true}, |
| 108 }; |
| 109 |
| 110 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 111 scoped_refptr<Extension> extension = |
| 112 CreateTestExtension(cases[i].name, cases[i].incognito_split_mode); |
| 113 extension_info_map_->AddExtension( |
| 114 extension, base::Time::Now(), cases[i].incognito_enabled); |
| 115 |
| 116 // First test a main frame request. |
| 117 { |
| 118 // It doesn't matter that the resource doesn't exist. If the resource |
| 119 // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request |
| 120 // should just fail because the file doesn't exist. |
| 121 net::URLRequest request(extension->GetResourceURL("404.html"), |
| 122 &test_delegate_); |
| 123 StartRequest(&request, ResourceType::MAIN_FRAME); |
| 124 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 125 |
| 126 if (cases[i].should_allow_main_frame_load) { |
| 127 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()) << |
| 128 cases[i].name; |
| 129 } else { |
| 130 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request.status().error()) << |
| 131 cases[i].name; |
| 132 } |
| 133 } |
| 134 |
| 135 // Now do a subframe request. |
| 136 { |
| 137 net::URLRequest request(extension->GetResourceURL("404.html"), |
| 138 &test_delegate_); |
| 139 StartRequest(&request, ResourceType::SUB_FRAME); |
| 140 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 141 |
| 142 if (cases[i].should_allow_sub_frame_load) { |
| 143 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()) << |
| 144 cases[i].name; |
| 145 } else { |
| 146 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request.status().error()) << |
| 147 cases[i].name; |
| 148 } |
| 149 } |
| 150 } |
| 151 } |
| 152 |
| 153 } // namespace |
| OLD | NEW |