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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
// ClientID fails without...
do_get_profile();
const { TestUtils } = ChromeUtils.importESModule(
"resource://testing-common/TestUtils.sys.mjs"
);
const { ClientID } = ChromeUtils.importESModule(
"resource://gre/modules/ClientID.sys.mjs"
);
const { Discovery } = ChromeUtils.importESModule(
"resource:///modules/Discovery.sys.mjs"
);
const { ContextualIdentityService } = ChromeUtils.importESModule(
"resource://gre/modules/ContextualIdentityService.sys.mjs"
);
const TAAR_COOKIE_NAME = "taarId";
add_task(async function test_discovery() {
let uri = Services.io.newURI("https://example.com/foobar");
// Ensure the prefs we need
Services.prefs.setBoolPref("browser.discovery.enabled", true);
Services.prefs.setBoolPref("browser.discovery.containers.enabled", true);
Services.prefs.setBoolPref("datareporting.healthreport.uploadEnabled", true);
Services.prefs.setCharPref("browser.discovery.sites", uri.host);
registerCleanupFunction(() => {
Services.prefs.clearUserPref("browser.discovery.enabled");
Services.prefs.clearUserPref("browser.discovery.containers.enabled");
Services.prefs.clearUserPref("browser.discovery.sites");
Services.prefs.clearUserPref("datareporting.healthreport.uploadEnabled");
});
// This is normally initialized by telemetry, force id creation. This results
// in Discovery setting the cookie.
await ClientID.getClientID();
await Discovery.update();
ok(
Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {}),
"cookie exists"
);
ok(
!Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
privateBrowsingId: 1,
}),
"no private cookie exists"
);
ContextualIdentityService.getPublicIdentities().forEach(identity => {
let { userContextId } = identity;
equal(
Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
userContextId,
}),
identity.public,
"cookie exists"
);
});
// Test the addition of a new container.
let changed = TestUtils.topicObserved("cookie-changed", subject => {
let cookie = subject
.QueryInterface(Ci.nsICookieNotification)
.cookie.QueryInterface(Ci.nsICookie);
equal(cookie.name, TAAR_COOKIE_NAME, "taar cookie exists");
equal(cookie.host, uri.host, "cookie exists for host");
equal(
cookie.originAttributes.userContextId,
container.userContextId,
"cookie userContextId is correct"
);
return true;
});
let container = ContextualIdentityService.create(
"New Container",
"Icon",
"Color"
);
await changed;
// Test disabling
Discovery.enabled = false;
// Wait for the update to remove the cookie.
await TestUtils.waitForCondition(() => {
return !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {});
});
ContextualIdentityService.getPublicIdentities().forEach(identity => {
let { userContextId } = identity;
ok(
!Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
userContextId,
}),
"no cookie exists"
);
});
// turn off containers
Services.prefs.setBoolPref("browser.discovery.containers.enabled", false);
Discovery.enabled = true;
await TestUtils.waitForCondition(() => {
return Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {});
});
// make sure we did not set cookies on containers
ContextualIdentityService.getPublicIdentities().forEach(identity => {
let { userContextId } = identity;
ok(
!Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
userContextId,
}),
"no cookie exists"
);
});
// Make sure clientId changes update discovery
changed = TestUtils.topicObserved("cookie-changed", subject => {
let notification = subject.QueryInterface(Ci.nsICookieNotification);
if (notification.action != Ci.nsICookieNotification.COOKIE_ADDED) {
return false;
}
let cookie = notification.cookie.QueryInterface(Ci.nsICookie);
equal(cookie.name, TAAR_COOKIE_NAME, "taar cookie exists");
equal(cookie.host, uri.host, "cookie exists for host");
return true;
});
await ClientID.removeClientID();
await ClientID.getClientID();
await changed;
// Make sure disabling telemetry disables discovery.
Services.prefs.setBoolPref("datareporting.healthreport.uploadEnabled", false);
await TestUtils.waitForCondition(() => {
return !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {});
});
});
|