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(content::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::kChromeUIScheme
));
76 EXPECT_FALSE(IsWebScheme(content::kJavaScriptScheme
));
77 EXPECT_FALSE(IsWebScheme(content::kMailToScheme
));
78 EXPECT_FALSE(IsWebScheme(content::kMetadataScheme
));
79 EXPECT_FALSE(IsWebScheme(content::kSwappedOutScheme
));
80 EXPECT_FALSE(IsWebScheme(content::kViewSourceScheme
));
83 TEST_F(BrowsingDataHelperTest
, WebSafeSchemesAreNotExtensions
) {
84 EXPECT_FALSE(IsExtensionScheme(content::kHttpScheme
));
85 EXPECT_FALSE(IsExtensionScheme(content::kHttpsScheme
));
86 EXPECT_FALSE(IsExtensionScheme(content::kFtpScheme
));
87 EXPECT_FALSE(IsExtensionScheme(chrome::kDataScheme
));
88 EXPECT_FALSE(IsExtensionScheme("feed"));
89 EXPECT_FALSE(IsExtensionScheme(chrome::kBlobScheme
));
90 EXPECT_FALSE(IsExtensionScheme(content::kFileSystemScheme
));
91 EXPECT_FALSE(IsExtensionScheme("invalid-scheme-i-just-made-up"));
94 TEST_F(BrowsingDataHelperTest
, ChromeSchemesAreNotAllExtension
) {
95 EXPECT_TRUE(IsExtensionScheme(extensions::kExtensionScheme
));
97 EXPECT_FALSE(IsExtensionScheme(chrome::kAboutScheme
));
98 EXPECT_FALSE(IsExtensionScheme(chrome::kChromeDevToolsScheme
));
99 EXPECT_FALSE(IsExtensionScheme(chrome::kChromeUIScheme
));
100 EXPECT_FALSE(IsExtensionScheme(content::kJavaScriptScheme
));
101 EXPECT_FALSE(IsExtensionScheme(content::kMailToScheme
));
102 EXPECT_FALSE(IsExtensionScheme(content::kMetadataScheme
));
103 EXPECT_FALSE(IsExtensionScheme(content::kSwappedOutScheme
));
104 EXPECT_FALSE(IsExtensionScheme(content::kViewSourceScheme
));
107 TEST_F(BrowsingDataHelperTest
, TestMatches
) {
108 scoped_refptr
<MockExtensionSpecialStoragePolicy
> mock_policy
=
109 new MockExtensionSpecialStoragePolicy
;
111 mock_policy
->AddProtected(kOrigin1
.GetOrigin());
113 EXPECT_FALSE(Match(kOrigin1
, kUnprotected
, mock_policy
.get()));
114 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
, mock_policy
.get()));
115 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
, mock_policy
.get()));
116 EXPECT_FALSE(Match(kOriginDevTools
, kUnprotected
, mock_policy
.get()));
118 EXPECT_TRUE(Match(kOrigin1
, kProtected
, mock_policy
.get()));
119 EXPECT_FALSE(Match(kOrigin2
, kProtected
, mock_policy
.get()));
120 EXPECT_FALSE(Match(kOriginExt
, kProtected
, mock_policy
.get()));
121 EXPECT_FALSE(Match(kOriginDevTools
, kProtected
, mock_policy
.get()));
123 EXPECT_FALSE(Match(kOrigin1
, kExtension
, mock_policy
.get()));
124 EXPECT_FALSE(Match(kOrigin2
, kExtension
, mock_policy
.get()));
125 EXPECT_TRUE(Match(kOriginExt
, kExtension
, mock_policy
.get()));
126 EXPECT_FALSE(Match(kOriginDevTools
, kExtension
, mock_policy
.get()));
128 EXPECT_TRUE(Match(kOrigin1
, kUnprotected
| kProtected
, mock_policy
.get()));
129 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
| kProtected
, mock_policy
.get()));
130 EXPECT_FALSE(Match(kOriginExt
, kUnprotected
| kProtected
, mock_policy
.get()));
132 Match(kOriginDevTools
, kUnprotected
| kProtected
, mock_policy
.get()));
134 EXPECT_FALSE(Match(kOrigin1
, kUnprotected
| kExtension
, mock_policy
.get()));
135 EXPECT_TRUE(Match(kOrigin2
, kUnprotected
| kExtension
, mock_policy
.get()));
136 EXPECT_TRUE(Match(kOriginExt
, kUnprotected
| kExtension
, mock_policy
.get()));
138 Match(kOriginDevTools
, kUnprotected
| kExtension
, mock_policy
.get()));
140 EXPECT_TRUE(Match(kOrigin1
, kProtected
| kExtension
, mock_policy
.get()));
141 EXPECT_FALSE(Match(kOrigin2
, kProtected
| kExtension
, mock_policy
.get()));
142 EXPECT_TRUE(Match(kOriginExt
, kProtected
| kExtension
, mock_policy
.get()));
144 Match(kOriginDevTools
, kProtected
| kExtension
, mock_policy
.get()));
147 kOrigin1
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
149 kOrigin2
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
151 kOriginExt
, kUnprotected
| kProtected
| kExtension
, mock_policy
.get()));
152 EXPECT_FALSE(Match(kOriginDevTools
,
153 kUnprotected
| kProtected
| kExtension
,