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.
4
5#include "chrome/browser/browsing_data/browsing_data_helper.h"
6
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"
11#include "url/gurl.h"
12#include "url/url_constants.h"
13
14#if defined(ENABLE_EXTENSIONS)
15#include "chrome/browser/extensions/mock_extension_special_storage_policy.h"
16#endif
17
18namespace {
19
20const char kTestOrigin1[] = "http://host1:1/";
21const char kTestOrigin2[] = "http://host2:1/";
22const char kTestOrigin3[] = "http://host3:1/";
23const char kTestOriginExt[] = "chrome-extension://abcdefghijklmnopqrstuvwxyz/";
24const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
25
26const GURL kOrigin1(kTestOrigin1);
27const GURL kOrigin2(kTestOrigin2);
28const GURL kOrigin3(kTestOrigin3);
29const GURL kOriginExt(kTestOriginExt);
30const GURL kOriginDevTools(kTestOriginDevTools);
31
32const int kExtension = BrowsingDataHelper::EXTENSION;
33const int kProtected = BrowsingDataHelper::PROTECTED_WEB;
34const int kUnprotected = BrowsingDataHelper::UNPROTECTED_WEB;
35
36class BrowsingDataHelperTest : public testing::Test {
37 public:
38  BrowsingDataHelperTest() {}
39  virtual ~BrowsingDataHelperTest() {}
40
41  bool IsWebScheme(const std::string& scheme) {
42    GURL test(scheme + "://example.com");
43    return (BrowsingDataHelper::HasWebScheme(test) &&
44            BrowsingDataHelper::IsWebScheme(scheme));
45  }
46
47  bool IsExtensionScheme(const std::string& scheme) {
48    GURL test(scheme + "://example.com");
49    return (BrowsingDataHelper::HasExtensionScheme(test) &&
50            BrowsingDataHelper::IsExtensionScheme(scheme));
51  }
52
53  bool Match(const GURL& origin,
54             int mask,
55             storage::SpecialStoragePolicy* policy) {
56    return BrowsingDataHelper::DoesOriginMatchMask(origin, mask, policy);
57  }
58
59 private:
60  DISALLOW_COPY_AND_ASSIGN(BrowsingDataHelperTest);
61};
62
63TEST_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"));
72}
73
74TEST_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));
84}
85
86TEST_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"));
95}
96
97TEST_F(BrowsingDataHelperTest, ChromeSchemesAreNotAllExtension) {
98  EXPECT_TRUE(IsExtensionScheme(extensions::kExtensionScheme));
99
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));
108}
109
110#if defined(ENABLE_EXTENSIONS)
111TEST_F(BrowsingDataHelperTest, TestMatches) {
112  scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy =
113      new MockExtensionSpecialStoragePolicy;
114  // Protect kOrigin1.
115  mock_policy->AddProtected(kOrigin1.GetOrigin());
116
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()));
121
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()));
126
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()));
131
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()));
135  EXPECT_FALSE(
136      Match(kOriginDevTools, kUnprotected | kProtected, mock_policy.get()));
137
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()));
141  EXPECT_FALSE(
142      Match(kOriginDevTools, kUnprotected | kExtension, mock_policy.get()));
143
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()));
147  EXPECT_FALSE(
148      Match(kOriginDevTools, kProtected | kExtension, mock_policy.get()));
149
150  EXPECT_TRUE(Match(
151      kOrigin1, kUnprotected | kProtected | kExtension, mock_policy.get()));
152  EXPECT_TRUE(Match(
153      kOrigin2, kUnprotected | kProtected | kExtension, mock_policy.get()));
154  EXPECT_TRUE(Match(
155      kOriginExt, kUnprotected | kProtected | kExtension, mock_policy.get()));
156  EXPECT_FALSE(Match(kOriginDevTools,
157                     kUnprotected | kProtected | kExtension,
158                     mock_policy.get()));
159}
160#endif
161
162// If extensions are disabled, there is no policy.
163TEST_F(BrowsingDataHelperTest, TestNoPolicyMatches) {
164  EXPECT_FALSE(Match(kOrigin1, kExtension, NULL));
165  EXPECT_TRUE(Match(kOrigin1, kUnprotected, NULL));
166  EXPECT_FALSE(Match(kOrigin1, kProtected, NULL));
167
168  EXPECT_TRUE(Match(kOriginExt, kExtension, NULL));
169  EXPECT_FALSE(Match(kOriginExt, kUnprotected, NULL));
170  EXPECT_FALSE(Match(kOriginExt, kProtected, NULL));
171
172  EXPECT_FALSE(Match(kOriginDevTools, kExtension, NULL));
173  EXPECT_FALSE(Match(kOriginDevTools, kUnprotected, NULL));
174  EXPECT_FALSE(Match(kOriginDevTools, kProtected, NULL));
175}
176
177}  // namespace
178