OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
11 #include "content/browser/child_process_security_policy_impl.h" | 11 #include "content/browser/child_process_security_policy_impl.h" |
12 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
13 #include "content/test/test_content_browser_client.h" | 13 #include "content/test/test_content_browser_client.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
| 17 namespace content { |
17 namespace { | 18 namespace { |
18 | 19 |
19 const int kRendererID = 42; | 20 const int kRendererID = 42; |
20 const int kWorkerRendererID = kRendererID + 1; | 21 const int kWorkerRendererID = kRendererID + 1; |
21 | 22 |
22 class ChildProcessSecurityPolicyTestBrowserClient | 23 class ChildProcessSecurityPolicyTestBrowserClient |
23 : public content::TestContentBrowserClient { | 24 : public TestContentBrowserClient { |
24 public: | 25 public: |
25 ChildProcessSecurityPolicyTestBrowserClient() {} | 26 ChildProcessSecurityPolicyTestBrowserClient() {} |
26 | 27 |
27 virtual bool IsHandledURL(const GURL& url) { | 28 virtual bool IsHandledURL(const GURL& url) { |
28 return schemes_.find(url.scheme()) != schemes_.end(); | 29 return schemes_.find(url.scheme()) != schemes_.end(); |
29 } | 30 } |
30 | 31 |
31 void ClearSchemes() { | 32 void ClearSchemes() { |
32 schemes_.clear(); | 33 schemes_.clear(); |
33 } | 34 } |
34 | 35 |
35 void AddScheme(const std::string& scheme) { | 36 void AddScheme(const std::string& scheme) { |
36 schemes_.insert(scheme); | 37 schemes_.insert(scheme); |
37 } | 38 } |
38 | 39 |
39 private: | 40 private: |
40 std::set<std::string> schemes_; | 41 std::set<std::string> schemes_; |
41 }; | 42 }; |
42 | 43 |
43 } // namespace | 44 } // namespace |
44 | 45 |
45 class ChildProcessSecurityPolicyTest : public testing::Test { | 46 class ChildProcessSecurityPolicyTest : public testing::Test { |
46 public: | 47 public: |
47 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) { | 48 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) { |
48 } | 49 } |
49 | 50 |
50 virtual void SetUp() { | 51 virtual void SetUp() { |
51 old_browser_client_ = content::GetContentClient()->browser(); | 52 old_browser_client_ = GetContentClient()->browser(); |
52 content::GetContentClient()->set_browser_for_testing(&test_browser_client_); | 53 GetContentClient()->set_browser_for_testing(&test_browser_client_); |
53 | 54 |
54 // Claim to always handle chrome:// URLs because the CPSP's notion of | 55 // Claim to always handle chrome:// URLs because the CPSP's notion of |
55 // allowing WebUI bindings is hard-wired to this particular scheme. | 56 // allowing WebUI bindings is hard-wired to this particular scheme. |
56 test_browser_client_.AddScheme("chrome"); | 57 test_browser_client_.AddScheme("chrome"); |
57 } | 58 } |
58 | 59 |
59 virtual void TearDown() { | 60 virtual void TearDown() { |
60 test_browser_client_.ClearSchemes(); | 61 test_browser_client_.ClearSchemes(); |
61 content::GetContentClient()->set_browser_for_testing(old_browser_client_); | 62 GetContentClient()->set_browser_for_testing(old_browser_client_); |
62 } | 63 } |
63 | 64 |
64 protected: | 65 protected: |
65 void RegisterTestScheme(const std::string& scheme) { | 66 void RegisterTestScheme(const std::string& scheme) { |
66 test_browser_client_.AddScheme(scheme); | 67 test_browser_client_.AddScheme(scheme); |
67 } | 68 } |
68 | 69 |
69 private: | 70 private: |
70 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_; | 71 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_; |
71 content::ContentBrowserClient* old_browser_client_; | 72 ContentBrowserClient* old_browser_client_; |
72 }; | 73 }; |
73 | 74 |
74 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { | 75 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { |
75 ChildProcessSecurityPolicyImpl* p = | 76 ChildProcessSecurityPolicyImpl* p = |
76 ChildProcessSecurityPolicyImpl::GetInstance(); | 77 ChildProcessSecurityPolicyImpl::GetInstance(); |
77 | 78 |
78 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); | 79 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); |
79 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme)); | 80 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme)); |
80 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme)); | 81 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme)); |
81 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme)); | 82 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme)); |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 484 |
484 // Renderers are added and removed on the UI thread, but the policy can be | 485 // Renderers are added and removed on the UI thread, but the policy can be |
485 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be | 486 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be |
486 // prepared to answer policy questions about renderers who no longer exist. | 487 // prepared to answer policy questions about renderers who no longer exist. |
487 | 488 |
488 // In this case, we default to secure behavior. | 489 // In this case, we default to secure behavior. |
489 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); | 490 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); |
490 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); | 491 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); |
491 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); | 492 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); |
492 } | 493 } |
| 494 |
| 495 } // namespace content |
OLD | NEW |