user_script_unittest.cc revision 513209b27ff55e2841eac0e4120199c23acce758
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
11static const int kAllSchemes =
12    URLPattern::SCHEME_HTTP |
13    URLPattern::SCHEME_HTTPS |
14    URLPattern::SCHEME_FILE |
15    URLPattern::SCHEME_FTP |
16    URLPattern::SCHEME_CHROMEUI;
17
18TEST(UserScriptTest, 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
37TEST(UserScriptTest, 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
46TEST(UserScriptTest, 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
55TEST(UserScriptTest, 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
64TEST(UserScriptTest, 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
72TEST(UserScriptTest, Match6) {
73  URLPattern pattern(kAllSchemes);
74  ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/foo*"));
75
76  UserScript script;
77  script.add_url_pattern(pattern);
78  EXPECT_TRUE(script.MatchesUrl(GURL("http://monkey.com/foobar")));
79  EXPECT_FALSE(script.MatchesUrl(GURL("http://monkey.com/hotdog")));
80
81  // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc.
82}
83
84TEST(UserScriptTest, UrlPatternGlobInteraction) {
85  // If there are both, match intersection(union(globs), union(urlpatterns)).
86  UserScript script;
87
88  URLPattern pattern(kAllSchemes);
89  ASSERT_EQ(URLPattern::PARSE_SUCCESS,
90            pattern.Parse("http://www.google.com/*"));
91  script.add_url_pattern(pattern);
92
93  script.add_glob("*bar*");
94
95  // No match, because it doesn't match the glob.
96  EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/foo")));
97
98  script.add_exclude_glob("*baz*");
99
100  // No match, because it matches the exclude glob.
101  EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/baz")));
102
103  // Match, because it matches the glob, doesn't match the exclude glob.
104  EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/bar")));
105
106  // Try with just a single exclude glob.
107  script.clear_globs();
108  EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo")));
109
110  // Try with no globs or exclude globs.
111  script.clear_exclude_globs();
112  EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo")));
113}
114
115TEST(UserScriptTest, Pickle) {
116  URLPattern pattern1(kAllSchemes);
117  URLPattern pattern2(kAllSchemes);
118  ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse("http://*/foo*"));
119  ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse("http://bar/baz*"));
120
121  UserScript script1;
122  script1.js_scripts().push_back(UserScript::File(
123      FilePath(FILE_PATH_LITERAL("c:\\foo\\")),
124      FilePath(FILE_PATH_LITERAL("foo.user.js")),
125      GURL("chrome-user-script:/foo.user.js")));
126  script1.css_scripts().push_back(UserScript::File(
127      FilePath(FILE_PATH_LITERAL("c:\\foo\\")),
128      FilePath(FILE_PATH_LITERAL("foo.user.css")),
129      GURL("chrome-user-script:/foo.user.css")));
130  script1.css_scripts().push_back(UserScript::File(
131      FilePath(FILE_PATH_LITERAL("c:\\foo\\")),
132      FilePath(FILE_PATH_LITERAL("foo2.user.css")),
133      GURL("chrome-user-script:/foo2.user.css")));
134  script1.set_run_location(UserScript::DOCUMENT_START);
135
136  script1.add_url_pattern(pattern1);
137  script1.add_url_pattern(pattern2);
138
139  Pickle pickle;
140  script1.Pickle(&pickle);
141
142  void* iter = NULL;
143  UserScript script2;
144  script2.Unpickle(pickle, &iter);
145
146  EXPECT_EQ(1U, script2.js_scripts().size());
147  EXPECT_EQ(script1.js_scripts()[0].url(), script2.js_scripts()[0].url());
148
149  EXPECT_EQ(2U, script2.css_scripts().size());
150  for (size_t i = 0; i < script2.js_scripts().size(); ++i) {
151    EXPECT_EQ(script1.css_scripts()[i].url(), script2.css_scripts()[i].url());
152  }
153
154  ASSERT_EQ(script1.globs().size(), script2.globs().size());
155  for (size_t i = 0; i < script1.globs().size(); ++i) {
156    EXPECT_EQ(script1.globs()[i], script2.globs()[i]);
157  }
158  ASSERT_EQ(script1.url_patterns().size(), script2.url_patterns().size());
159  for (size_t i = 0; i < script1.url_patterns().size(); ++i) {
160    EXPECT_EQ(script1.url_patterns()[i].GetAsString(),
161              script2.url_patterns()[i].GetAsString());
162  }
163}
164
165TEST(UserScriptTest, Defaults) {
166  UserScript script;
167  ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location());
168}
169