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.
5 #include "chrome/browser/browsing_data/browsing_data_helper.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h"
9 #include "chrome/common/url_constants.h"
10 #include "content/public/common/url_constants.h"
11 #include "extensions/common/constants.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 const char kTestOrigin1
[] = "http://host1:1/";
18 const char kTestOrigin2
[] = "http://host2:1/";
19 const char kTestOrigin3
[] = "http://host3:1/";
20 const char kTestOriginExt
[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
21 const char kTestOriginDevTools
[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
23 const GURL
kOrigin1(kTestOrigin1
);
24 const GURL
kOrigin2(kTestOrigin2
);
25 const GURL
kOrigin3(kTestOrigin3
);
26 const GURL
kOriginExt(kTestOriginExt
);
27 const GURL
kOriginDevTools(kTestOriginDevTools
);
29 const int kExtension
= BrowsingDataHelper::EXTENSION
;
30 const int kProtected
= BrowsingDataHelper::PROTECTED_WEB
;
31 const int kUnprotected
= BrowsingDataHelper::UNPROTECTED_WEB
;
33 class BrowsingDataHelperTest
: public testing::Test
{
35 BrowsingDataHelperTest() {}
36 virtual ~BrowsingDataHelperTest() {}
38 bool IsWebScheme(const std::string
& scheme
) {
39 GURL
test(scheme
+ "://example.com");
40 return (BrowsingDataHelper::HasWebScheme(test
) &&
41 BrowsingDataHelper::IsWebScheme(scheme
));
44 bool IsExtensionScheme(const std::string
& scheme
) {
45 GURL
test(scheme
+ "://example.com");
46 return (BrowsingDataHelper::HasExtensionScheme(test
) &&
47 BrowsingDataHelper::IsExtensionScheme(scheme
));
50 bool Match(const GURL
& origin
,
52 ExtensionSpecialStoragePolicy
* policy
) {
53 return BrowsingDataHelper::DoesOriginMatchMask(origin
, mask
, policy
);
57 DISALLOW_COPY_AND_ASSIGN(BrowsingDataHelperTest
);
60 TEST_F(BrowsingDataHelperTest
, WebSafeSchemesAreWebSafe
) {
61 EXPECT_TRUE(IsWebScheme(content::kHttpScheme
));
62 EXPECT_TRUE(IsWebScheme(content::kHttpsScheme
));
63 EXPECT_TRUE(IsWebScheme(content::kFtpScheme
));
64 EXPECT_TRUE(IsWebScheme(chrome::kDataScheme
));
65 EXPECT_TRUE(IsWebScheme("feed"));
66 EXPECT_TRUE(IsWebScheme(chrome::kBlobScheme
));
67 EXPECT_TRUE(IsWebScheme(chrome::kFileSystemScheme
));
68 EXPECT_FALSE(IsWebScheme("invalid-scheme-i-just-made-up"));
71 TEST_F(BrowsingDataHelperTest
, ChromeSchemesAreNotWebSafe
) {
72 EXPECT_FALSE(IsWebScheme(extensions::kExtensionScheme
));
73 EXPECT_FALSE(IsWebScheme(chrome::kAboutScheme
));
74 EXPECT_FALSE(IsWebScheme(chrome::kChromeDevToolsScheme
));
75 EXPECT_FALSE(IsWebScheme(chrome::kChromeInternalScheme
));
76 EXPECT_FALSE(IsWebScheme(chrome::kChromeUIScheme
));
77 EXPECT_FALSE(IsWebScheme(content::kJavaScriptScheme
));
78 EXPECT_FALSE(IsWebScheme(content::kMailToScheme
));
79 EXPECT_FALSE(IsWebScheme(content::kMetadataScheme
));
80 EXPECT_FALSE(IsWebScheme(content::kSwappedOutScheme
));
81 EXPECT_FALSE(IsWebScheme(content::kViewSourceScheme
));
84 TEST_F(BrowsingDataHelperTest
, WebSafeSchemesAreNotExtensions
) {
85 EXPECT_FALSE(IsExtensionScheme(content::kHttpScheme
));
86 EXPECT_FALSE(IsExtensionScheme(content::kHttpsScheme
));
87 EXPECT_FALSE(IsExtensionScheme(content::kFtpScheme
));
88 EXPECT_FALSE(IsExtensionScheme(chrome::kDataScheme
));
89 EXPECT_FALSE(IsExtensionScheme("feed"));
90 EXPECT_FALSE(IsExtensionScheme(chrome::kBlobScheme
));
91 EXPECT_FALSE(IsExtensionScheme(chrome::kFileSystemScheme
));
92 EXPECT_FALSE(IsExtensionScheme("invalid-scheme-i-just-made-up"));
95 TEST_F(BrowsingDataHelperTest
, ChromeSchemesAreNotAllExtension
) {
96 EXPECT_TRUE(IsExtensionScheme(extensions::kExtensionScheme
));
98 EXPECT_FALSE(IsExtensionScheme(chrome::kAboutScheme
));
99 EXPECT_FALSE(IsExtensionScheme(chrome::kChromeDevToolsScheme
));
100 EXPECT_FALSE(IsExtensionScheme(chrome::kChromeInternalScheme
));
101 EXPECT_FALSE(IsExtensionScheme(chrome::kChromeUIScheme
));
102 EXPECT_FALSE(IsExtensionScheme(content::kJavaScriptScheme
));
103 EXPECT_FALSE(IsExtensionScheme(content::kMailToScheme
));
104 EXPECT_FALSE(IsExtensionScheme(content::kMetadataScheme
));
105 EXPECT_FALSE(IsExtensionScheme(content::kSwappedOutScheme
));
106 EXPECT_FALSE(IsExtensionScheme(content::kViewSourceScheme
));
109 TEST_F(BrowsingDataHelperTest
, TestMatches
) {
110 scoped_refptr
<MockExtensionSpecialStoragePolicy
> mock_policy
=
111 new MockExtensionSpecialStoragePolicy
;
113 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
115 EXPECT_FALSE(Match(kOrigin1
, kUnprotected
, mock_policy
.get()));
116 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
, mock_policy
.get()));
117 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
, mock_policy
.get()));
118 EXPECT_FALSE(Match(kOriginDevTools
, kUnprotected
, mock_policy
.get()));
120 EXPECT_TRUE(Match(kOrigin1
, kProtected
, mock_policy
.get()));
121 EXPECT_FALSE(Match(kOrigin2
, kProtected
, mock_policy
.get()));
122 EXPECT_FALSE(Match(kOriginExt
, kProtected
, mock_policy
.get()));
123 EXPECT_FALSE(Match(kOriginDevTools
, kProtected
, mock_policy
.get()));
125 EXPECT_FALSE(Match(kOrigin1
, kExtension
, mock_policy
.get()));
126 EXPECT_FALSE(Match(kOrigin2
, kExtension
, mock_policy
.get()));
127 EXPECT_TRUE(Match(kOriginExt
, kExtension
, mock_policy
.get()));
128 EXPECT_FALSE(Match(kOriginDevTools
, kExtension
, mock_policy
.get()));
130 EXPECT_TRUE(Match(kOrigin1
, kUnprotected
| kProtected
, mock_policy
.get()));
131 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
| kProtected
, mock_policy
.get()));
132 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
| kProtected
, mock_policy
.get()));
134 Match(kOriginDevTools
, kUnprotected
| kProtected
, mock_policy
.get()));
136 EXPECT_FALSE(Match(kOrigin1
, kUnprotected
| kExtension
, mock_policy
.get()));
137 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
| kExtension
, mock_policy
.get()));
138 EXPECT_TRUE(Match(kOriginExt
, kUnprotected
| kExtension
, mock_policy
.get()));
140 Match(kOriginDevTools
, kUnprotected
| kExtension
, mock_policy
.get()));
142 EXPECT_TRUE(Match(kOrigin1
, kProtected
| kExtension
, mock_policy
.get()));
143 EXPECT_FALSE(Match(kOrigin2
, kProtected
| kExtension
, mock_policy
.get()));
144 EXPECT_TRUE(Match(kOriginExt
, kProtected
| kExtension
, mock_policy
.get()));
146 Match(kOriginDevTools
, kProtected
| kExtension
, mock_policy
.get()));
149 kOrigin1
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
151 kOrigin2
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
153 kOriginExt
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
154 EXPECT_FALSE(Match(kOriginDevTools
,
155 kUnprotected
| kProtected
| kExtension
,