• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 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 "base/file_path.h"
6 #include "base/pickle.h"
7 #include "chrome/common/extensions/user_script.h"
8 #include "googleurl/src/gurl.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 static const int kAllSchemes =
12     URLPattern::SCHEME_HTTP |
13     URLPattern::SCHEME_HTTPS |
14     URLPattern::SCHEME_FILE |
15     URLPattern::SCHEME_FTP |
16     URLPattern::SCHEME_CHROMEUI;
17 
TEST(ExtensionUserScriptTest,Match1)18 TEST(ExtensionUserScriptTest, Match1) {
19   UserScript script;
20   script.add_glob("*mail.google.com*");
21   script.add_glob("*mail.yahoo.com*");
22   script.add_glob("*mail.msn.com*");
23   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com")));
24   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo")));
25   EXPECT_TRUE(script.MatchesUrl(GURL("https://mail.google.com/foo")));
26   EXPECT_TRUE(script.MatchesUrl(GURL("ftp://mail.google.com/foo")));
27   EXPECT_TRUE(script.MatchesUrl(GURL("http://woo.mail.google.com/foo")));
28   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.yahoo.com/bar")));
29   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.msn.com/baz")));
30   EXPECT_FALSE(script.MatchesUrl(GURL("http://www.hotmail.com")));
31 
32   script.add_exclude_glob("*foo*");
33   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com")));
34   EXPECT_FALSE(script.MatchesUrl(GURL("http://mail.google.com/foo")));
35 }
36 
TEST(ExtensionUserScriptTest,Match2)37 TEST(ExtensionUserScriptTest, Match2) {
38   UserScript script;
39   script.add_glob("*mail.google.com/");
40   // GURL normalizes the URL to have a trailing "/"
41   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com")));
42   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/")));
43   EXPECT_FALSE(script.MatchesUrl(GURL("http://mail.google.com/foo")));
44 }
45 
TEST(ExtensionUserScriptTest,Match3)46 TEST(ExtensionUserScriptTest, Match3) {
47   UserScript script;
48   script.add_glob("http://mail.google.com/*");
49   // GURL normalizes the URL to have a trailing "/"
50   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com")));
51   EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo")));
52   EXPECT_FALSE(script.MatchesUrl(GURL("https://mail.google.com/foo")));
53 }
54 
TEST(ExtensionUserScriptTest,Match4)55 TEST(ExtensionUserScriptTest, Match4) {
56   UserScript script;
57   script.add_glob("*");
58   EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar")));
59   EXPECT_TRUE(script.MatchesUrl(GURL("http://hot.com/dog")));
60   EXPECT_TRUE(script.MatchesUrl(GURL("https://hot.com/dog")));
61   EXPECT_TRUE(script.MatchesUrl(GURL("file:///foo/bar")));
62 }
63 
TEST(ExtensionUserScriptTest,Match5)64 TEST(ExtensionUserScriptTest, Match5) {
65   UserScript script;
66   script.add_glob("*foo*");
67   EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar")));
68   EXPECT_TRUE(script.MatchesUrl(GURL("http://baz.org/foo/bar")));
69   EXPECT_FALSE(script.MatchesUrl(GURL("http://baz.org")));
70 }
71 
TEST(ExtensionUserScriptTest,Match6)72 TEST(ExtensionUserScriptTest, Match6) {
73   URLPattern pattern(kAllSchemes);
74   ASSERT_EQ(URLPattern::PARSE_SUCCESS,
75             pattern.Parse("http://*/foo*", URLPattern::PARSE_STRICT));
76 
77   UserScript script;
78   script.add_url_pattern(pattern);
79   EXPECT_TRUE(script.MatchesUrl(GURL("http://monkey.com/foobar")));
80   EXPECT_FALSE(script.MatchesUrl(GURL("http://monkey.com/hotdog")));
81 
82   // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc.
83 }
84 
TEST(ExtensionUserScriptTest,UrlPatternGlobInteraction)85 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) {
86   // If there are both, match intersection(union(globs), union(urlpatterns)).
87   UserScript script;
88 
89   URLPattern pattern(kAllSchemes);
90   ASSERT_EQ(URLPattern::PARSE_SUCCESS,
91             pattern.Parse("http://www.google.com/*",
92                           URLPattern::PARSE_STRICT));
93   script.add_url_pattern(pattern);
94 
95   script.add_glob("*bar*");
96 
97   // No match, because it doesn't match the glob.
98   EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/foo")));
99 
100   script.add_exclude_glob("*baz*");
101 
102   // No match, because it matches the exclude glob.
103   EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/baz")));
104 
105   // Match, because it matches the glob, doesn't match the exclude glob.
106   EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/bar")));
107 
108   // Try with just a single exclude glob.
109   script.clear_globs();
110   EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo")));
111 
112   // Try with no globs or exclude globs.
113   script.clear_exclude_globs();
114   EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo")));
115 }
116 
TEST(ExtensionUserScriptTest,Pickle)117 TEST(ExtensionUserScriptTest, Pickle) {
118   URLPattern pattern1(kAllSchemes);
119   URLPattern pattern2(kAllSchemes);
120   ASSERT_EQ(URLPattern::PARSE_SUCCESS,
121             pattern1.Parse("http://*/foo*", URLPattern::PARSE_STRICT));
122   ASSERT_EQ(URLPattern::PARSE_SUCCESS,
123             pattern2.Parse("http://bar/baz*", URLPattern::PARSE_STRICT));
124 
125   UserScript script1;
126   script1.js_scripts().push_back(UserScript::File(
127       FilePath(FILE_PATH_LITERAL("c:\\foo\\")),
128       FilePath(FILE_PATH_LITERAL("foo.user.js")),
129       GURL("chrome-user-script:/foo.user.js")));
130   script1.css_scripts().push_back(UserScript::File(
131       FilePath(FILE_PATH_LITERAL("c:\\foo\\")),
132       FilePath(FILE_PATH_LITERAL("foo.user.css")),
133       GURL("chrome-user-script:/foo.user.css")));
134   script1.css_scripts().push_back(UserScript::File(
135       FilePath(FILE_PATH_LITERAL("c:\\foo\\")),
136       FilePath(FILE_PATH_LITERAL("foo2.user.css")),
137       GURL("chrome-user-script:/foo2.user.css")));
138   script1.set_run_location(UserScript::DOCUMENT_START);
139 
140   script1.add_url_pattern(pattern1);
141   script1.add_url_pattern(pattern2);
142 
143   Pickle pickle;
144   script1.Pickle(&pickle);
145 
146   void* iter = NULL;
147   UserScript script2;
148   script2.Unpickle(pickle, &iter);
149 
150   EXPECT_EQ(1U, script2.js_scripts().size());
151   EXPECT_EQ(script1.js_scripts()[0].url(), script2.js_scripts()[0].url());
152 
153   EXPECT_EQ(2U, script2.css_scripts().size());
154   for (size_t i = 0; i < script2.js_scripts().size(); ++i) {
155     EXPECT_EQ(script1.css_scripts()[i].url(), script2.css_scripts()[i].url());
156   }
157 
158   ASSERT_EQ(script1.globs().size(), script2.globs().size());
159   for (size_t i = 0; i < script1.globs().size(); ++i) {
160     EXPECT_EQ(script1.globs()[i], script2.globs()[i]);
161   }
162   ASSERT_EQ(script1.url_patterns().size(), script2.url_patterns().size());
163   for (size_t i = 0; i < script1.url_patterns().size(); ++i) {
164     EXPECT_EQ(script1.url_patterns()[i].GetAsString(),
165               script2.url_patterns()[i].GetAsString());
166   }
167 }
168 
TEST(ExtensionUserScriptTest,Defaults)169 TEST(ExtensionUserScriptTest, Defaults) {
170   UserScript script;
171   ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location());
172 }
173