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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
if (AppConstants.MOZ_GLEAN) {
Cu.importGlobalProperties(["Glean"]);
}
const { ClientID } = ChromeUtils.import("resource://gre/modules/ClientID.jsm");
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const PREF_CACHED_CLIENTID = "toolkit.telemetry.cachedClientID";
const SCALAR_DELETION_REQUEST_ECOSYSTEM_CLIENT_ID =
"deletion.request.ecosystem_client_id";
var drsPath;
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
function run_test() {
do_get_profile();
drsPath = OS.Path.join(
OS.Constants.Path.profileDir,
"datareporting",
"state.json"
);
if (AppConstants.MOZ_GLEAN) {
// We need to ensure FOG is initialized, otherwise operations will be stuck in the pre-init queue.
let FOG = Cc["@mozilla.org/toolkit/glean;1"].createInstance(Ci.nsIFOG);
FOG.initializeFOG();
}
run_next_test();
}
add_task(async function test_ecosystemClientID() {
await ClientID._reset();
Assert.ok(!ClientID.getCachedEcosystemClientID());
let ecosystemClientID = await ClientID.getEcosystemClientID();
Assert.equal(typeof ecosystemClientID, "string");
Assert.equal(ClientID.getCachedEcosystemClientID(), ecosystemClientID);
let clientID = await ClientID.getClientID();
await ClientID._reset();
await OS.File.writeAtomic(
drsPath,
JSON.stringify({
clientID,
}),
{
encoding: "utf-8",
tmpPath: drsPath + ".tmp",
}
);
let newClientID = await ClientID.getClientID();
Assert.equal(newClientID, clientID);
let newEcosystemClientID = await ClientID.getEcosystemClientID();
Assert.notEqual(newEcosystemClientID, ecosystemClientID);
});
add_task(async function test_client_id() {
const invalidIDs = [
[-1, "setIntPref"],
[0.5, "setIntPref"],
["INVALID-UUID", "setStringPref"],
[true, "setBoolPref"],
["", "setStringPref"],
["3d1e1560-682a-4043-8cf2-aaaaaaaaaaaZ", "setStringPref"],
];
// If there is no DRS file, we should get a new client ID.
await ClientID._reset();
let clientID = await ClientID.getClientID();
Assert.equal(typeof clientID, "string");
Assert.ok(uuidRegex.test(clientID));
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
clientID
);
}
// We should be guarded against invalid DRS json.
await ClientID._reset();
await OS.File.writeAtomic(drsPath, "abcd", {
encoding: "utf-8",
tmpPath: drsPath + ".tmp",
});
clientID = await ClientID.getClientID();
Assert.equal(typeof clientID, "string");
Assert.ok(uuidRegex.test(clientID));
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
clientID
);
}
// If the DRS data is broken, we should end up with a new client ID.
for (let [invalidID] of invalidIDs) {
await ClientID._reset();
await CommonUtils.writeJSON({ clientID: invalidID }, drsPath);
clientID = await ClientID.getClientID();
Assert.equal(typeof clientID, "string");
Assert.ok(uuidRegex.test(clientID));
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
clientID
);
}
}
// Assure that cached IDs are being checked for validity.
for (let [invalidID, prefFunc] of invalidIDs) {
await ClientID._reset();
Services.prefs[prefFunc](PREF_CACHED_CLIENTID, invalidID);
let cachedID = ClientID.getCachedClientID();
Assert.strictEqual(
cachedID,
null,
"ClientID should ignore invalid cached IDs"
);
Assert.ok(
!Services.prefs.prefHasUserValue(PREF_CACHED_CLIENTID),
"ClientID should reset invalid cached IDs"
);
Assert.ok(
Services.prefs.getPrefType(PREF_CACHED_CLIENTID) ==
Ci.nsIPrefBranch.PREF_INVALID,
"ClientID should reset invalid cached IDs"
);
}
});
add_task(async function test_setCanaryClientIDs() {
const KNOWN_UUID = "c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0";
await ClientID._reset();
// We should be able to set a valid UUID
await ClientID.setCanaryClientIDs();
let clientID = await ClientID.getClientID();
Assert.equal(KNOWN_UUID, clientID);
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
clientID
);
}
});
add_task(async function test_resetEcosystemClientID() {
await ClientID._reset();
let firstClientID = await ClientID.getClientID();
let firstEcosystemClientID = await ClientID.getEcosystemClientID();
Assert.ok(firstClientID);
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
firstClientID
);
}
Assert.ok(firstEcosystemClientID);
// We should reset the ecosystem client id, but not the main client id.
await ClientID.resetEcosystemClientID();
let secondClientID = await ClientID.getClientID();
let secondEcosystemClientID = await ClientID.getEcosystemClientID();
Assert.equal(firstClientID, secondClientID);
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
firstClientID
);
}
Assert.notEqual(firstEcosystemClientID, secondEcosystemClientID);
// The new id should have been persisted to disk.
await ClientID._reset();
let thirdClientID = await ClientID.getClientID();
let thirdEcosystemClientID = await ClientID.getEcosystemClientID();
Assert.equal(thirdClientID, secondClientID);
Assert.equal(thirdEcosystemClientID, secondEcosystemClientID);
});
add_task(async function test_removeClientIDs() {
// We should get a valid UUID after reset
await ClientID._reset();
let firstClientID = await ClientID.getClientID();
let firstEcosystemClientID = await ClientID.getEcosystemClientID();
Assert.equal(typeof firstClientID, "string");
Assert.equal(typeof firstEcosystemClientID, "string");
Assert.ok(uuidRegex.test(firstClientID));
Assert.ok(uuidRegex.test(firstEcosystemClientID));
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
firstClientID
);
}
await ClientID.removeClientIDs();
if (
AppConstants.platform != "android" &&
AppConstants.MOZ_APP_NAME != "thunderbird"
) {
// We don't record the old ecosystem client ID on Android or Thunderbird,
// since the FxA and telemetry infrastructure is different there.
let prefClientID = Services.prefs.getStringPref(PREF_CACHED_CLIENTID, null);
let scalarsDeletionRequest = Services.telemetry.getSnapshotForScalars(
"deletion-request"
);
Assert.ok(!prefClientID);
Assert.ok(
!scalarsDeletionRequest.parent?.[
SCALAR_DELETION_REQUEST_ECOSYSTEM_CLIENT_ID
]
);
}
// When resetting again we should get a new ID
let nextClientID = await ClientID.getClientID();
let nextEcosystemClientID = await ClientID.getEcosystemClientID();
Assert.equal(typeof nextClientID, "string");
Assert.equal(typeof nextEcosystemClientID, "string");
Assert.ok(uuidRegex.test(nextClientID));
Assert.ok(uuidRegex.test(nextEcosystemClientID));
Assert.notEqual(
firstClientID,
nextClientID,
"After reset client ID should be different."
);
Assert.notEqual(
firstEcosystemClientID,
nextEcosystemClientID,
"After reset ecosystem client ID should be different."
);
let cachedID = ClientID.getCachedClientID();
Assert.equal(nextClientID, cachedID);
let cachedEcosystemID = ClientID.getCachedEcosystemClientID();
Assert.equal(nextEcosystemClientID, cachedEcosystemID);
let prefClientID = Services.prefs.getStringPref(PREF_CACHED_CLIENTID, null);
Assert.equal(nextClientID, prefClientID);
if (
AppConstants.platform != "android" &&
AppConstants.MOZ_APP_NAME != "thunderbird"
) {
let scalarsDeletionRequest = Services.telemetry.getSnapshotForScalars(
"deletion-request"
);
Assert.equal(
nextEcosystemClientID,
scalarsDeletionRequest.parent[SCALAR_DELETION_REQUEST_ECOSYSTEM_CLIENT_ID]
);
}
});
add_task(async function test_removeParallelGet() {
// We should get a valid UUID after reset
await ClientID.removeClientIDs();
let firstClientID = await ClientID.getClientID();
// We should get the same ID twice when requesting it in parallel to a reset.
let promiseRemoveClientIDs = ClientID.removeClientIDs();
let p = ClientID.getClientID();
let newClientID = await ClientID.getClientID();
await promiseRemoveClientIDs;
let otherClientID = await p;
Assert.notEqual(
firstClientID,
newClientID,
"After reset client ID should be different."
);
Assert.equal(
newClientID,
otherClientID,
"Getting the client ID in parallel to a reset should give the same id."
);
if (AppConstants.MOZ_GLEAN) {
Assert.equal(
Glean.fogValidation.legacyTelemetryClientId.testGetValue(
"fog-validation"
),
newClientID
);
}
});
add_task(
{
skip_if: () => AppConstants.platform != "android",
},
async function test_FennecCanaryDetect() {
const KNOWN_UUID = "c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0";
// We should get a valid UUID after reset
await ClientID.removeClientIDs();
let firstClientID = await ClientID.getClientID();
Assert.notEqual(KNOWN_UUID, firstClientID, "Client ID should be random.");
// Set the canary client ID.
await ClientID.setCanaryClientIDs();
Assert.equal(
KNOWN_UUID,
await ClientID.getClientID(),
"Client ID should be known canary."
);
await ClientID.removeClientIDs();
let newClientID = await ClientID.getClientID();
Assert.notEqual(
KNOWN_UUID,
newClientID,
"After reset Client ID should be random."
);
Assert.notEqual(
firstClientID,
newClientID,
"After reset Client ID should be new."
);
Assert.ok(
ClientID.wasCanaryClientID(),
"After reset we should have detected a canary client ID"
);
await ClientID.removeClientIDs();
let clientID = await ClientID.getClientID();
Assert.notEqual(
KNOWN_UUID,
clientID,
"After reset Client ID should be random."
);
Assert.notEqual(
newClientID,
clientID,
"After reset Client ID should be new."
);
Assert.ok(
!ClientID.wasCanaryClientID(),
"After reset we should not have detected a canary client ID"
);
}
);
|