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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let { UrlClassifierTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/UrlClassifierTestUtils.sys.mjs"
);
const TEST_DOMAIN = "https://example.com/";
const TEST_EMAIL_WEBAPP_DOMAIN = "https://test1.example.com/";
const EMAIL_TRACKER_DOMAIN = "https://email-tracking.example.org/";
const TEST_PATH = "browser/toolkit/components/url-classifier/tests/browser/";
const TEST_PAGE = TEST_DOMAIN + TEST_PATH + "page.html";
const TEST_EMAIL_WEBAPP_PAGE =
TEST_EMAIL_WEBAPP_DOMAIN + TEST_PATH + "page.html";
const EMAIL_TRACKER_PAGE = EMAIL_TRACKER_DOMAIN + TEST_PATH + "page.html";
const EMAIL_TRACKER_IMAGE = EMAIL_TRACKER_DOMAIN + TEST_PATH + "raptor.jpg";
const TELEMETRY_EMAIL_TRACKER_COUNT = "EMAIL_TRACKER_COUNT";
const TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB =
"EMAIL_TRACKER_EMBEDDED_PER_TAB";
const LABEL_BASE_NORMAL = 0;
const LABEL_CONTENT_NORMAL = 1;
const LABEL_BASE_EMAIL_WEBAPP = 2;
const LABEL_CONTENT_EMAIL_WEBAPP = 3;
const KEY_BASE_NORMAL = "base_normal";
const KEY_CONTENT_NORMAL = "content_normal";
const KEY_ALL_NORMAL = "all_normal";
const KEY_BASE_EMAILAPP = "base_emailapp";
const KEY_CONTENT_EMAILAPP = "content_emailapp";
const KEY_ALL_EMAILAPP = "all_emailapp";
async function clearTelemetry() {
Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
Services.telemetry.getHistogramById(TELEMETRY_EMAIL_TRACKER_COUNT).clear();
Services.telemetry
.getKeyedHistogramById(TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB)
.clear();
}
async function loadImage(browser, url) {
return SpecialPowers.spawn(browser, [url], page => {
return new Promise(resolve => {
let image = new content.Image();
image.src = page + "?" + Math.random();
image.onload = _ => resolve(true);
image.onerror = _ => resolve(false);
});
});
}
async function getTelemetryProbe(key, label, checkCntFn) {
let histogram;
// Wait until the telemetry probe appears.
await TestUtils.waitForCondition(() => {
let histograms = Services.telemetry.getSnapshotForHistograms(
"main",
false /* clear */
).parent;
histogram = histograms[key];
let checkRes = false;
if (histogram) {
checkRes = checkCntFn ? checkCntFn(histogram.values[label]) : true;
}
return checkRes;
});
return histogram.values[label] || 0;
}
async function getKeyedHistogram(histogram_id, key, bucket, checkCntFn) {
let histogram;
// Wait until the telemetry probe appears.
await TestUtils.waitForCondition(() => {
let histograms = Services.telemetry.getSnapshotForKeyedHistograms(
"main",
false /* clear */
).parent;
histogram = histograms[histogram_id];
let checkRes = false;
if (histogram && histogram[key]) {
checkRes = checkCntFn ? checkCntFn(histogram[key].values[bucket]) : true;
}
return checkRes;
});
return histogram[key].values[bucket] || 0;
}
async function checkTelemetryProbe(key, label, expectedCnt) {
let cnt = await getTelemetryProbe(key, label, cnt => {
if (cnt === undefined) {
cnt = 0;
}
return cnt == expectedCnt;
});
is(cnt, expectedCnt, "There should be expected count in telemetry.");
}
async function checkKeyedHistogram(histogram_id, key, bucket, expectedCnt) {
let cnt = await getKeyedHistogram(histogram_id, key, bucket, cnt => {
if (cnt === undefined) {
cnt = 0;
}
return cnt == expectedCnt;
});
is(cnt, expectedCnt, "There should be expected count in keyed telemetry.");
}
function checkNoTelemetryProbe(key) {
let histograms = Services.telemetry.getSnapshotForHistograms(
"main",
false /* clear */
).parent;
let histogram = histograms[key];
ok(!histogram, `No Telemetry has been recorded for ${key}`);
}
add_setup(async function () {
await SpecialPowers.pushPrefEnv({
set: [
[
"urlclassifier.features.emailtracking.datacollection.blocklistTables",
"mochitest5-track-simple",
],
[
"urlclassifier.features.emailtracking.datacollection.allowlistTables",
"",
],
[
"urlclassifier.features.emailtracking.blocklistTables",
"mochitest5-track-simple",
],
["urlclassifier.features.emailtracking.allowlistTables", ""],
["privacy.trackingprotection.enabled", false],
["privacy.trackingprotection.annotate_channels", false],
["privacy.trackingprotection.cryptomining.enabled", false],
["privacy.trackingprotection.emailtracking.enabled", true],
[
"privacy.trackingprotection.emailtracking.data_collection.enabled",
true,
],
["privacy.trackingprotection.fingerprinting.enabled", false],
["privacy.trackingprotection.socialtracking.enabled", false],
[
"privacy.trackingprotection.emailtracking.webapp.domains",
"test1.example.com",
],
],
});
await UrlClassifierTestUtils.addTestTrackers();
registerCleanupFunction(function () {
UrlClassifierTestUtils.cleanupTestTrackers();
});
await clearTelemetry();
});
add_task(async function test_email_tracking_telemetry() {
// Open a non email webapp tab.
await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
// Load a image from the email tracker
let res = await loadImage(browser, EMAIL_TRACKER_IMAGE);
is(res, false, "The image is blocked.");
// Verify the telemetry of the email tracker count.
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_BASE_NORMAL,
1
);
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_CONTENT_NORMAL,
0
);
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_BASE_EMAIL_WEBAPP,
0
);
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_CONTENT_EMAIL_WEBAPP,
0
);
});
// Open an email webapp tab.
await BrowserTestUtils.withNewTab(TEST_EMAIL_WEBAPP_PAGE, async browser => {
// Load a image from the email tracker
let res = await loadImage(browser, EMAIL_TRACKER_IMAGE);
is(res, false, "The image is blocked.");
// Verify the telemetry of the email tracker count.
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_BASE_NORMAL,
1
);
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_CONTENT_NORMAL,
0
);
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_BASE_EMAIL_WEBAPP,
1
);
await checkTelemetryProbe(
TELEMETRY_EMAIL_TRACKER_COUNT,
LABEL_CONTENT_EMAIL_WEBAPP,
0
);
});
// Make sure the tab was closed properly before clearing Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
await clearTelemetry();
});
add_task(async function test_no_telemetry_for_first_party_email_tracker() {
// Open a email tracker tab.
await BrowserTestUtils.withNewTab(EMAIL_TRACKER_PAGE, async browser => {
// Load a image from the first-party email tracker
let res = await loadImage(browser, EMAIL_TRACKER_IMAGE);
is(res, true, "The image is loaded.");
// Verify that there was no telemetry recorded.
checkNoTelemetryProbe(TELEMETRY_EMAIL_TRACKER_COUNT);
});
// Make sure the tab was closed properly before clearing Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
await clearTelemetry();
});
add_task(async function test_disable_email_data_collection() {
// Disable Email Tracking Data Collection.
await SpecialPowers.pushPrefEnv({
set: [
[
"privacy.trackingprotection.emailtracking.data_collection.enabled",
false,
],
],
});
// Open an email webapp tab.
await BrowserTestUtils.withNewTab(TEST_EMAIL_WEBAPP_PAGE, async browser => {
// Load a image from the email tracker
let res = await loadImage(browser, EMAIL_TRACKER_IMAGE);
is(res, false, "The image is blocked.");
// Verify that there was no telemetry recorded.
checkNoTelemetryProbe(TELEMETRY_EMAIL_TRACKER_COUNT);
});
// Make sure the tab was closed properly before clearing Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
await SpecialPowers.popPrefEnv();
await clearTelemetry();
});
add_task(async function test_email_tracker_embedded_telemetry() {
// First, we open a page without loading any email trackers.
await BrowserTestUtils.withNewTab(TEST_PAGE, async _ => {});
// Make sure the tab was closed properly before checking Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
// Check that the telemetry has been record properly for normal page. The
// telemetry should show there was no email tracker loaded.
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_BASE_NORMAL,
0,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_CONTENT_NORMAL,
0,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_ALL_NORMAL,
0,
1
);
// Second, Open a email webapp tab that doesn't a load email tracker.
await BrowserTestUtils.withNewTab(TEST_EMAIL_WEBAPP_PAGE, async _ => {});
// Make sure the tab was closed properly before checking Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
// Check that the telemetry has been record properly for the email webapp. The
// telemetry should show there was no email tracker loaded.
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_BASE_EMAILAPP,
0,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_CONTENT_EMAILAPP,
0,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_ALL_EMAILAPP,
0,
1
);
// Third, open a page with one email tracker loaded.
await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
// Load a image from the email tracker
let res = await loadImage(browser, EMAIL_TRACKER_IMAGE);
is(res, false, "The image is blocked.");
});
// Make sure the tab was closed properly before checking Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
// Verify that the telemetry has been record properly, The telemetry should
// show there was one base email tracker loaded.
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_BASE_NORMAL,
1,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_CONTENT_NORMAL,
0,
2
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_ALL_NORMAL,
0,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_ALL_NORMAL,
1,
1
);
// Open a page and load the same email tracker multiple times. There
// should be only one count for the same tracker.
await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
// Load a image from the email tracker two times.
await loadImage(browser, EMAIL_TRACKER_IMAGE);
await loadImage(browser, EMAIL_TRACKER_IMAGE);
});
// Make sure the tab was closed properly before checking Telemetry.
await BrowserUtils.promiseObserved("window-global-destroyed");
// Verify that there is still only one count when loading the same tracker
// multiple times.
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_BASE_NORMAL,
1,
2
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_CONTENT_NORMAL,
0,
3
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_ALL_NORMAL,
0,
1
);
await checkKeyedHistogram(
TELEMETRY_EMAIL_TRACKER_EMBEDDED_PER_TAB,
KEY_ALL_NORMAL,
1,
2
);
await clearTelemetry();
});
|