| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/platform_file.h" | 9 #include "base/platform_file.h" |
| 10 #include "content/browser/child_process_security_policy.h" | 10 #include "content/browser/child_process_security_policy.h" |
| 11 #include "content/common/test_url_constants.h" | 11 #include "content/common/test_url_constants.h" |
| 12 #include "content/common/url_constants.h" | 12 #include "content/common/url_constants.h" |
| 13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 14 #include "net/url_request/url_request_test_job.h" | 14 #include "net/url_request/url_request_test_job.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 class ChildProcessSecurityPolicyTest : public testing::Test { | 17 class ChildProcessSecurityPolicyTest : public testing::Test { |
| 18 protected: | 18 protected: |
| 19 // testing::Test | 19 // testing::Test |
| 20 virtual void SetUp() { | 20 virtual void SetUp() { |
| 21 // In the real world, "chrome:" is a handled scheme. | 21 // In the real world, "chrome:" is a handled scheme. |
| 22 net::URLRequest::RegisterProtocolFactory(chrome::kChromeUIScheme, | 22 RegisterProtocolFactory(chrome::kChromeUIScheme, |
| 23 &net::URLRequestTestJob::Factory); | 23 &net::URLRequestTestJob::Factory); |
| 24 } | 24 } |
| 25 virtual void TearDown() { | 25 virtual void TearDown() { |
| 26 net::URLRequest::RegisterProtocolFactory(chrome::kChromeUIScheme, NULL); | 26 RegisterProtocolFactory(chrome::kChromeUIScheme, NULL); |
| 27 } |
| 28 |
| 29 static net::URLRequest::ProtocolFactory* RegisterProtocolFactory( |
| 30 const std::string& scheme, |
| 31 net::URLRequest::ProtocolFactory* factory) { |
| 32 return net::URLRequest::Deprecated::RegisterProtocolFactory( |
| 33 scheme, factory); |
| 27 } | 34 } |
| 28 }; | 35 }; |
| 29 | 36 |
| 30 static int kRendererID = 42; | 37 static int kRendererID = 42; |
| 31 static int kWorkerRendererID = kRendererID + 1; | 38 static int kWorkerRendererID = kRendererID + 1; |
| 32 | 39 |
| 33 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { | 40 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { |
| 34 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); | 41 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); |
| 35 | 42 |
| 36 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); | 43 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 170 |
| 164 TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) { | 171 TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) { |
| 165 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); | 172 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); |
| 166 | 173 |
| 167 p->Add(kRendererID); | 174 p->Add(kRendererID); |
| 168 | 175 |
| 169 // Currently, "asdf" is destined for ShellExecute, so it is allowed. | 176 // Currently, "asdf" is destined for ShellExecute, so it is allowed. |
| 170 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 177 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
| 171 | 178 |
| 172 // Once we register a ProtocolFactory for "asdf", we default to deny. | 179 // Once we register a ProtocolFactory for "asdf", we default to deny. |
| 173 net::URLRequest::RegisterProtocolFactory("asdf", | 180 RegisterProtocolFactory("asdf", &net::URLRequestTestJob::Factory); |
| 174 &net::URLRequestTestJob::Factory); | |
| 175 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 181 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
| 176 | 182 |
| 177 // We can allow new schemes by adding them to the whitelist. | 183 // We can allow new schemes by adding them to the whitelist. |
| 178 p->RegisterWebSafeScheme("asdf"); | 184 p->RegisterWebSafeScheme("asdf"); |
| 179 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 185 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
| 180 | 186 |
| 181 // Cleanup. | 187 // Cleanup. |
| 182 net::URLRequest::RegisterProtocolFactory("asdf", NULL); | 188 RegisterProtocolFactory("asdf", NULL); |
| 183 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 189 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
| 184 | 190 |
| 185 p->Remove(kRendererID); | 191 p->Remove(kRendererID); |
| 186 } | 192 } |
| 187 | 193 |
| 188 TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) { | 194 TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) { |
| 189 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); | 195 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); |
| 190 | 196 |
| 191 p->Add(kRendererID); | 197 p->Add(kRendererID); |
| 192 | 198 |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 432 |
| 427 // Renderers are added and removed on the UI thread, but the policy can be | 433 // Renderers are added and removed on the UI thread, but the policy can be |
| 428 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be | 434 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be |
| 429 // prepared to answer policy questions about renderers who no longer exist. | 435 // prepared to answer policy questions about renderers who no longer exist. |
| 430 | 436 |
| 431 // In this case, we default to secure behavior. | 437 // In this case, we default to secure behavior. |
| 432 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); | 438 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); |
| 433 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); | 439 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); |
| 434 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); | 440 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); |
| 435 } | 441 } |
| OLD | NEW |