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