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/common/url_constants.h"
9 #include "extensions/common/constants.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #include "url/url_constants.h"
14 #if defined(ENABLE_EXTENSIONS)
15 #include "chrome/browser/extensions/mock_extension_special_storage_policy.h"
20 const char kTestOrigin1
[] = "http://host1:1/";
21 const char kTestOrigin2
[] = "http://host2:1/";
22 const char kTestOrigin3
[] = "http://host3:1/";
23 const char kTestOriginExt
[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
24 const char kTestOriginDevTools
[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
26 const GURL
kOrigin1(kTestOrigin1
);
27 const GURL
kOrigin2(kTestOrigin2
);
28 const GURL
kOrigin3(kTestOrigin3
);
29 const GURL
kOriginExt(kTestOriginExt
);
30 const GURL
kOriginDevTools(kTestOriginDevTools
);
32 const int kExtension
= BrowsingDataHelper::EXTENSION
;
33 const int kProtected
= BrowsingDataHelper::PROTECTED_WEB
;
34 const int kUnprotected
= BrowsingDataHelper::UNPROTECTED_WEB
;
36 class BrowsingDataHelperTest
: public testing::Test
{
38 BrowsingDataHelperTest() {}
39 ~BrowsingDataHelperTest() override
{}
41 bool IsWebScheme(const std::string
& scheme
) {
42 GURL
test(scheme
+ "://example.com");
43 return (BrowsingDataHelper::HasWebScheme(test
) &&
44 BrowsingDataHelper::IsWebScheme(scheme
));
47 bool IsExtensionScheme(const std::string
& scheme
) {
48 GURL
test(scheme
+ "://example.com");
49 return (BrowsingDataHelper::HasExtensionScheme(test
) &&
50 BrowsingDataHelper::IsExtensionScheme(scheme
));
53 bool Match(const GURL
& origin
,
55 storage::SpecialStoragePolicy
* policy
) {
56 return BrowsingDataHelper::DoesOriginMatchMask(origin
, mask
, policy
);
60 DISALLOW_COPY_AND_ASSIGN(BrowsingDataHelperTest
);
63 TEST_F(BrowsingDataHelperTest
, WebSafeSchemesAreWebSafe
) {
64 EXPECT_TRUE(IsWebScheme(url::kHttpScheme
));
65 EXPECT_TRUE(IsWebScheme(url::kHttpsScheme
));
66 EXPECT_TRUE(IsWebScheme(url::kFtpScheme
));
67 EXPECT_TRUE(IsWebScheme(url::kDataScheme
));
68 EXPECT_TRUE(IsWebScheme("feed"));
69 EXPECT_TRUE(IsWebScheme(url::kBlobScheme
));
70 EXPECT_TRUE(IsWebScheme(url::kFileSystemScheme
));
71 EXPECT_FALSE(IsWebScheme("invalid-scheme-i-just-made-up"));
74 TEST_F(BrowsingDataHelperTest
, ChromeSchemesAreNotWebSafe
) {
75 EXPECT_FALSE(IsWebScheme(extensions::kExtensionScheme
));
76 EXPECT_FALSE(IsWebScheme(url::kAboutScheme
));
77 EXPECT_FALSE(IsWebScheme(content::kChromeDevToolsScheme
));
78 EXPECT_FALSE(IsWebScheme(content::kChromeUIScheme
));
79 EXPECT_FALSE(IsWebScheme(url::kJavaScriptScheme
));
80 EXPECT_FALSE(IsWebScheme(url::kMailToScheme
));
81 EXPECT_FALSE(IsWebScheme(content::kMetadataScheme
));
82 EXPECT_FALSE(IsWebScheme(content::kSwappedOutScheme
));
83 EXPECT_FALSE(IsWebScheme(content::kViewSourceScheme
));
86 TEST_F(BrowsingDataHelperTest
, WebSafeSchemesAreNotExtensions
) {
87 EXPECT_FALSE(IsExtensionScheme(url::kHttpScheme
));
88 EXPECT_FALSE(IsExtensionScheme(url::kHttpsScheme
));
89 EXPECT_FALSE(IsExtensionScheme(url::kFtpScheme
));
90 EXPECT_FALSE(IsExtensionScheme(url::kDataScheme
));
91 EXPECT_FALSE(IsExtensionScheme("feed"));
92 EXPECT_FALSE(IsExtensionScheme(url::kBlobScheme
));
93 EXPECT_FALSE(IsExtensionScheme(url::kFileSystemScheme
));
94 EXPECT_FALSE(IsExtensionScheme("invalid-scheme-i-just-made-up"));
97 TEST_F(BrowsingDataHelperTest
, ChromeSchemesAreNotAllExtension
) {
98 EXPECT_TRUE(IsExtensionScheme(extensions::kExtensionScheme
));
100 EXPECT_FALSE(IsExtensionScheme(url::kAboutScheme
));
101 EXPECT_FALSE(IsExtensionScheme(content::kChromeDevToolsScheme
));
102 EXPECT_FALSE(IsExtensionScheme(content::kChromeUIScheme
));
103 EXPECT_FALSE(IsExtensionScheme(url::kJavaScriptScheme
));
104 EXPECT_FALSE(IsExtensionScheme(url::kMailToScheme
));
105 EXPECT_FALSE(IsExtensionScheme(content::kMetadataScheme
));
106 EXPECT_FALSE(IsExtensionScheme(content::kSwappedOutScheme
));
107 EXPECT_FALSE(IsExtensionScheme(content::kViewSourceScheme
));
110 #if defined(ENABLE_EXTENSIONS)
111 TEST_F(BrowsingDataHelperTest
, TestMatches
) {
112 scoped_refptr
<MockExtensionSpecialStoragePolicy
> mock_policy
=
113 new MockExtensionSpecialStoragePolicy
;
115 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
117 EXPECT_FALSE(Match(kOrigin1
, kUnprotected
, mock_policy
.get()));
118 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
, mock_policy
.get()));
119 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
, mock_policy
.get()));
120 EXPECT_FALSE(Match(kOriginDevTools
, kUnprotected
, mock_policy
.get()));
122 EXPECT_TRUE(Match(kOrigin1
, kProtected
, mock_policy
.get()));
123 EXPECT_FALSE(Match(kOrigin2
, kProtected
, mock_policy
.get()));
124 EXPECT_FALSE(Match(kOriginExt
, kProtected
, mock_policy
.get()));
125 EXPECT_FALSE(Match(kOriginDevTools
, kProtected
, mock_policy
.get()));
127 EXPECT_FALSE(Match(kOrigin1
, kExtension
, mock_policy
.get()));
128 EXPECT_FALSE(Match(kOrigin2
, kExtension
, mock_policy
.get()));
129 EXPECT_TRUE(Match(kOriginExt
, kExtension
, mock_policy
.get()));
130 EXPECT_FALSE(Match(kOriginDevTools
, kExtension
, mock_policy
.get()));
132 EXPECT_TRUE(Match(kOrigin1
, kUnprotected
| kProtected
, mock_policy
.get()));
133 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
| kProtected
, mock_policy
.get()));
134 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
| kProtected
, mock_policy
.get()));
136 Match(kOriginDevTools
, kUnprotected
| kProtected
, mock_policy
.get()));
138 EXPECT_FALSE(Match(kOrigin1
, kUnprotected
| kExtension
, mock_policy
.get()));
139 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
| kExtension
, mock_policy
.get()));
140 EXPECT_TRUE(Match(kOriginExt
, kUnprotected
| kExtension
, mock_policy
.get()));
142 Match(kOriginDevTools
, kUnprotected
| kExtension
, mock_policy
.get()));
144 EXPECT_TRUE(Match(kOrigin1
, kProtected
| kExtension
, mock_policy
.get()));
145 EXPECT_FALSE(Match(kOrigin2
, kProtected
| kExtension
, mock_policy
.get()));
146 EXPECT_TRUE(Match(kOriginExt
, kProtected
| kExtension
, mock_policy
.get()));
148 Match(kOriginDevTools
, kProtected
| kExtension
, mock_policy
.get()));
151 kOrigin1
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
153 kOrigin2
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
155 kOriginExt
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
156 EXPECT_FALSE(Match(kOriginDevTools
,
157 kUnprotected
| kProtected
| kExtension
,
162 // If extensions are disabled, there is no policy.
163 TEST_F(BrowsingDataHelperTest
, TestNoPolicyMatches
) {
164 EXPECT_FALSE(Match(kOrigin1
, kExtension
, nullptr));
165 EXPECT_TRUE(Match(kOrigin1
, kUnprotected
, nullptr));
166 EXPECT_FALSE(Match(kOrigin1
, kProtected
, nullptr));
168 EXPECT_TRUE(Match(kOriginExt
, kExtension
, nullptr));
169 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
, nullptr));
170 EXPECT_FALSE(Match(kOriginExt
, kProtected
, nullptr));
172 EXPECT_FALSE(Match(kOriginDevTools
, kExtension
, nullptr));
173 EXPECT_FALSE(Match(kOriginDevTools
, kUnprotected
, nullptr));
174 EXPECT_FALSE(Match(kOriginDevTools
, kProtected
, nullptr));