Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / webstore_inline_installer_unittest.cc
bloba9d28043a9d77ac934b4d1e237e6acf914f0dd92
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 <vector>
7 #include "chrome/browser/extensions/webstore_inline_installer.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
15 namespace extensions {
17 namespace {
19 // Wraps WebstoreInlineInstaller to provide access to domain verification
20 // methods for testing.
21 class TestWebstoreInlineInstaller : public WebstoreInlineInstaller {
22 public:
23 explicit TestWebstoreInlineInstaller(content::WebContents* contents,
24 const std::string& requestor_url);
26 bool TestCheckRequestorPermitted(const base::DictionaryValue& webstore_data) {
27 std::string error;
28 return CheckRequestorPermitted(webstore_data, &error);
31 protected:
32 virtual ~TestWebstoreInlineInstaller();
35 void TestInstallerCallback(bool success, const std::string& error) {}
37 TestWebstoreInlineInstaller::TestWebstoreInlineInstaller(
38 content::WebContents* contents,
39 const std::string& requestor_url)
40 : WebstoreInlineInstaller(contents,
41 "",
42 GURL(requestor_url),
43 base::Bind(&TestInstallerCallback)) {
46 TestWebstoreInlineInstaller::~TestWebstoreInlineInstaller() {}
48 // We inherit from ChromeRenderViewHostTestHarness only for
49 // CreateTestWebContents, because we need a mock WebContents to support the
50 // underlying WebstoreInlineInstaller in each test case.
51 class WebstoreInlineInstallerTest : public ChromeRenderViewHostTestHarness {
52 public:
53 // testing::Test
54 virtual void SetUp() OVERRIDE;
55 virtual void TearDown() OVERRIDE;
57 bool TestSingleVerifiedSite(const std::string& requestor_url,
58 const std::string& verified_site);
60 bool TestMultipleVerifiedSites(
61 const std::string& requestor_url,
62 const std::vector<std::string>& verified_sites);
64 protected:
65 scoped_ptr<content::WebContents> web_contents_;
68 void WebstoreInlineInstallerTest::SetUp() {
69 ChromeRenderViewHostTestHarness::SetUp();
70 web_contents_.reset(CreateTestWebContents());
73 void WebstoreInlineInstallerTest::TearDown() {
74 web_contents_.reset(NULL);
75 ChromeRenderViewHostTestHarness::TearDown();
78 // Simulates a test against the verified site string from a Webstore item's
79 // "verified_site" manifest entry.
80 bool WebstoreInlineInstallerTest::TestSingleVerifiedSite(
81 const std::string& requestor_url,
82 const std::string& verified_site) {
83 base::DictionaryValue webstore_data;
84 webstore_data.SetString("verified_site", verified_site);
86 scoped_refptr<TestWebstoreInlineInstaller> installer =
87 new TestWebstoreInlineInstaller(web_contents_.get(), requestor_url);
88 return installer->TestCheckRequestorPermitted(webstore_data);
91 // Simulates a test against a list of verified site strings from a Webstore
92 // item's "verified_sites" manifest entry.
93 bool WebstoreInlineInstallerTest::TestMultipleVerifiedSites(
94 const std::string& requestor_url,
95 const std::vector<std::string>& verified_sites) {
96 base::ListValue* sites = new base::ListValue();
97 for (std::vector<std::string>::const_iterator it = verified_sites.begin();
98 it != verified_sites.end(); ++it) {
99 sites->Append(new base::StringValue(*it));
101 base::DictionaryValue webstore_data;
102 webstore_data.Set("verified_sites", sites);
104 scoped_refptr<TestWebstoreInlineInstaller> installer =
105 new TestWebstoreInlineInstaller(web_contents_.get(), requestor_url);
106 return installer->TestCheckRequestorPermitted(webstore_data);
109 } // namespace
111 TEST_F(WebstoreInlineInstallerTest, DomainVerification) {
112 // Exact domain match.
113 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com", "example.com"));
115 // The HTTPS scheme is allowed.
116 EXPECT_TRUE(TestSingleVerifiedSite("https://example.com", "example.com"));
118 // The file: scheme is not allowed.
119 EXPECT_FALSE(TestSingleVerifiedSite("file:///example.com", "example.com"));
121 // Trailing slash in URL.
122 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com/", "example.com"));
124 // Page on the domain.
125 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com/page.html",
126 "example.com"));
128 // Page on a subdomain.
129 EXPECT_TRUE(TestSingleVerifiedSite("http://sub.example.com/page.html",
130 "example.com"));
132 // Root domain when only a subdomain is verified.
133 EXPECT_FALSE(TestSingleVerifiedSite("http://example.com/",
134 "sub.example.com"));
136 // Different subdomain when only a subdomain is verified.
137 EXPECT_FALSE(TestSingleVerifiedSite("http://www.example.com/",
138 "sub.example.com"));
140 // Port matches.
141 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com:123/",
142 "example.com:123"));
144 // Port doesn't match.
145 EXPECT_FALSE(TestSingleVerifiedSite("http://example.com:456/",
146 "example.com:123"));
148 // Port is missing in the requestor URL.
149 EXPECT_FALSE(TestSingleVerifiedSite("http://example.com/",
150 "example.com:123"));
152 // Port is missing in the verified site (any port matches).
153 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com:123/", "example.com"));
155 // Path matches.
156 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com/path",
157 "example.com/path"));
159 // Path doesn't match.
160 EXPECT_FALSE(TestSingleVerifiedSite("http://example.com/foo",
161 "example.com/path"));
163 // Path is missing.
164 EXPECT_FALSE(TestSingleVerifiedSite("http://example.com",
165 "example.com/path"));
167 // Path matches (with trailing slash).
168 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com/path/",
169 "example.com/path"));
171 // Path matches (is a file under the path).
172 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com/path/page.html",
173 "example.com/path"));
175 // Path and port match.
176 EXPECT_TRUE(TestSingleVerifiedSite(
177 "http://example.com:123/path/page.html", "example.com:123/path"));
179 // Match specific valid schemes
180 EXPECT_TRUE(TestSingleVerifiedSite("http://example.com",
181 "http://example.com"));
182 EXPECT_TRUE(TestSingleVerifiedSite("https://example.com",
183 "https://example.com"));
185 // Mismatch specific vaild schemes
186 EXPECT_FALSE(TestSingleVerifiedSite("https://example.com",
187 "http://example.com"));
188 EXPECT_FALSE(TestSingleVerifiedSite("http://example.com",
189 "https://example.com"));
191 // Invalid scheme spec
192 EXPECT_FALSE(TestSingleVerifiedSite("file://example.com",
193 "file://example.com"));
195 std::vector<std::string> verified_sites;
196 verified_sites.push_back("foo.example.com");
197 verified_sites.push_back("bar.example.com:123");
198 verified_sites.push_back("example.com/unicorns");
200 // Test valid examples against the site list.
202 EXPECT_TRUE(TestMultipleVerifiedSites("http://foo.example.com",
203 verified_sites));
205 EXPECT_TRUE(TestMultipleVerifiedSites("http://bar.example.com:123",
206 verified_sites));
208 EXPECT_TRUE(TestMultipleVerifiedSites(
209 "http://cooking.example.com/unicorns/bacon.html", verified_sites));
211 // Test invalid examples against the site list.
213 EXPECT_FALSE(TestMultipleVerifiedSites("http://example.com",
214 verified_sites));
216 EXPECT_FALSE(TestMultipleVerifiedSites("file://foo.example.com",
217 verified_sites));
219 EXPECT_FALSE(TestMultipleVerifiedSites("http://baz.example.com",
220 verified_sites));
222 EXPECT_FALSE(TestMultipleVerifiedSites("http://bar.example.com:456",
223 verified_sites));
226 } // namespace extensions