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