1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
const { newURI } = Services.io;
const server = createHttpServer({ hosts: ["example.com"] });
server.registerDirectory("/data/", do_get_file("data"));
let policy = new WebExtensionPolicy({
id: "foo@bar.baz",
mozExtensionHostname: "88fb51cd-159f-4859-83db-7065485bc9b2",
baseURL: "file:///foo",
allowedOrigins: new MatchPatternSet([]),
localizeCallback() {},
});
add_task(async function test_WebExtensinonContentScript_url_matching() {
let contentScript = new WebExtensionContentScript(policy, {
matches: new MatchPatternSet(["http://foo.com/bar", "*://bar.com/baz/*"]),
excludeMatches: new MatchPatternSet(["*://bar.com/baz/quux"]),
includeGlobs: ["*flerg*", "*.com/bar", "*/quux"].map(
glob => new MatchGlob(glob)
),
excludeGlobs: ["*glorg*"].map(glob => new MatchGlob(glob)),
});
ok(
contentScript.matchesURI(newURI("http://foo.com/bar")),
"Simple matches include should match"
);
ok(
contentScript.matchesURI(newURI("https://bar.com/baz/xflergx")),
"Simple matches include should match"
);
ok(
!contentScript.matchesURI(newURI("https://bar.com/baz/xx")),
"Failed includeGlobs match pattern should not match"
);
ok(
!contentScript.matchesURI(newURI("https://bar.com/baz/quux")),
"Excluded match pattern should not match"
);
ok(
!contentScript.matchesURI(newURI("https://bar.com/baz/xflergxglorgx")),
"Excluded match glob should not match"
);
});
async function loadURL(url) {
let requests = new Map();
function requestObserver(request) {
request.QueryInterface(Ci.nsIChannel);
if (request.isDocument) {
requests.set(request.name, request);
}
}
Services.obs.addObserver(requestObserver, "http-on-examine-response");
let contentPage = await ExtensionTestUtils.loadContentPage(url);
Services.obs.removeObserver(requestObserver, "http-on-examine-response");
return { contentPage, requests };
}
add_task(async function test_WebExtensinonContentScript_frame_matching() {
if (AppConstants.platform == "linux") {
// The windowless browser currently does not load correctly on Linux on
// infra.
return;
}
let baseURL = `http://example.com/data`;
let urls = {
topLevel: `${baseURL}/file_toplevel.html`,
iframe: `${baseURL}/file_iframe.html`,
srcdoc: "about:srcdoc",
aboutBlank: "about:blank",
};
let { contentPage, requests } = await loadURL(urls.topLevel);
let tests = [
{
matches: ["http://example.com/data/*"],
contentScript: {},
topLevel: true,
iframe: false,
aboutBlank: false,
srcdoc: false,
},
{
matches: ["http://example.com/data/*"],
contentScript: {
frameID: 0,
},
topLevel: true,
iframe: false,
aboutBlank: false,
srcdoc: false,
},
{
matches: ["http://example.com/data/*"],
contentScript: {
allFrames: true,
},
topLevel: true,
iframe: true,
aboutBlank: false,
srcdoc: false,
},
{
matches: ["http://example.com/data/*"],
contentScript: {
allFrames: true,
matchAboutBlank: true,
},
topLevel: true,
iframe: true,
aboutBlank: true,
srcdoc: true,
},
{
matches: ["http://foo.com/data/*"],
contentScript: {
allFrames: true,
matchAboutBlank: true,
},
topLevel: false,
iframe: false,
aboutBlank: false,
srcdoc: false,
},
];
// matchesWindowGlobal tests against content frames
await contentPage.spawn({ tests, urls }, args => {
this.windows = new Map();
this.windows.set(this.content.location.href, this.content);
for (let c of Array.from(this.content.frames)) {
this.windows.set(c.location.href, c);
}
this.policy = new WebExtensionPolicy({
id: "foo@bar.baz",
mozExtensionHostname: "88fb51cd-159f-4859-83db-7065485bc9b2",
baseURL: "file:///foo",
allowedOrigins: new MatchPatternSet([]),
localizeCallback() {},
});
let tests = args.tests.map(t => {
t.contentScript.matches = new MatchPatternSet(t.matches);
t.script = new WebExtensionContentScript(this.policy, t.contentScript);
return t;
});
for (let [i, test] of tests.entries()) {
for (let [frame, url] of Object.entries(args.urls)) {
let should = test[frame] ? "should" : "should not";
let wgc = this.windows.get(url).windowGlobalChild;
Assert.equal(
test.script.matchesWindowGlobal(wgc),
test[frame],
`Script ${i} ${should} match the ${frame} frame`
);
}
}
});
// Parent tests against loadInfo
tests = tests.map(t => {
t.contentScript.matches = new MatchPatternSet(t.matches);
t.script = new WebExtensionContentScript(policy, t.contentScript);
return t;
});
for (let [i, test] of tests.entries()) {
for (let [frame, url] of Object.entries(urls)) {
let should = test[frame] ? "should" : "should not";
if (url.startsWith("http")) {
let request = requests.get(url);
equal(
test.script.matchesLoadInfo(request.URI, request.loadInfo),
test[frame],
`Script ${i} ${should} match the request LoadInfo for ${frame} frame`
);
}
}
}
await contentPage.close();
});
|