Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 2eb7622

Browse files
committed
Bug 1669731 - P4. Add a testcase r=timhuang
Differential Revision: https://phabricator.services.mozilla.com/D93272
1 parent 7a61308 commit 2eb7622

1 file changed

Lines changed: 69 additions & 9 deletions

File tree

toolkit/components/url-classifier/tests/unit/test_channelClassifierService.js

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function setupChannel(uri, topUri = TOP_LEVEL_DOMAIN) {
4242
return channel;
4343
}
4444

45-
function waitForBeforeBlockEvent(expected, unblock = false) {
45+
function waitForBeforeBlockEvent(expected, callback) {
4646
return new Promise(function(resolve) {
4747
let observer = function observe(aSubject, aTopic, aData) {
4848
switch (aTopic) {
@@ -61,8 +61,8 @@ function waitForBeforeBlockEvent(expected, unblock = false) {
6161
"verify url of blocked channel"
6262
);
6363

64-
if (unblock) {
65-
channel.unblock();
64+
if (callback) {
65+
callback(channel);
6666
}
6767

6868
service.removeListener(observer);
@@ -84,10 +84,13 @@ add_task(async function test_block_channel() {
8484

8585
let channel = setupChannel(TRACKER_DOMAIN);
8686

87-
let blockPromise = waitForBeforeBlockEvent({
88-
reason: Ci.nsIUrlClassifierBlockedChannel.SOCIAL_TRACKING_PROTECTION,
89-
url: channel.URI.spec,
90-
});
87+
let blockPromise = waitForBeforeBlockEvent(
88+
{
89+
reason: Ci.nsIUrlClassifierBlockedChannel.SOCIAL_TRACKING_PROTECTION,
90+
url: channel.URI.spec,
91+
},
92+
null
93+
);
9194

9295
let openPromise = new Promise((resolve, reject) => {
9396
channel.asyncOpen({
@@ -134,7 +137,9 @@ add_task(async function test_unblock_channel() {
134137
reason: Ci.nsIUrlClassifierBlockedChannel.SOCIAL_TRACKING_PROTECTION,
135138
url: channel.URI.spec,
136139
},
137-
true //unblock
140+
ch => {
141+
ch.unblock();
142+
}
138143
);
139144

140145
let openPromise = new Promise((resolve, reject) => {
@@ -147,7 +152,62 @@ add_task(async function test_unblock_channel() {
147152
} else {
148153
// This request is supposed to fail, but we need to ensure it
149154
// is not canceled by url-classifier
150-
Assert.ok(true, "Not cancel by classifier");
155+
Assert.equal(
156+
status,
157+
Cr.NS_ERROR_UNKNOWN_HOST,
158+
"Not cancel by classifier"
159+
);
160+
}
161+
resolve();
162+
},
163+
});
164+
});
165+
166+
// wait for block event from url-classifier
167+
await blockPromise;
168+
169+
// wait for onStopRequest callback from AsyncOpen
170+
await openPromise;
171+
172+
// clean up
173+
UrlClassifierTestUtils.cleanupTestTrackers();
174+
Services.prefs.clearUserPref(FEATURE_STP_PREF);
175+
httpserver.stop();
176+
});
177+
178+
add_task(async function test_allow_channel() {
179+
Services.prefs.setBoolPref(FEATURE_STP_PREF, true);
180+
//Services.prefs.setBoolPref("network.dns.native-is-localhost", true);
181+
182+
await UrlClassifierTestUtils.addTestTrackers();
183+
184+
let channel = setupChannel(TRACKER_DOMAIN);
185+
186+
let blockPromise = waitForBeforeBlockEvent(
187+
{
188+
reason: Ci.nsIUrlClassifierBlockedChannel.SOCIAL_TRACKING_PROTECTION,
189+
url: channel.URI.spec,
190+
},
191+
ch => {
192+
ch.allow();
193+
}
194+
);
195+
196+
let openPromise = new Promise((resolve, reject) => {
197+
channel.asyncOpen({
198+
onStartRequest: (request, context) => {},
199+
onDataAvailable: (request, context, stream, offset, count) => {},
200+
onStopRequest: (request, status) => {
201+
if (status == Cr.NS_ERROR_SOCIALTRACKING_URI) {
202+
Assert.ok(false, "Classifier should not cancel this channel");
203+
} else {
204+
// This request is supposed to fail, but we need to ensure it
205+
// is not canceled by url-classifier
206+
Assert.equal(
207+
status,
208+
Cr.NS_ERROR_UNKNOWN_HOST,
209+
"Not cancel by classifier"
210+
);
151211
}
152212
resolve();
153213
},

0 commit comments

Comments
 (0)