summaryrefslogtreecommitdiffstats
path: root/browser/components/resistfingerprinting/test/browser/browser_cross_origin_isolated_reduce_time_precision.js
blob: 1366604b4c5dd3ec144d25009c8f855f3cc103d9 (plain)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
 * Bug 1621677 - A test for making sure getting the correct (higher) precision
 *   when it's cross-origin-isolated.
 */

const TEST_SCENARIO_1 = 1;
const TEST_SCENARIO_2 = 2;
const TEST_SCENARIO_3 = 3;
const TEST_SCENARIO_4 = 4;
const TEST_SCENARIO_5 = 5;
const TEST_SCENARIO_6 = 6;
const TEST_SCENARIO_7 = 7;
const TEST_SCENARIO_8 = 8;
const TEST_SCENARIO_9 = 9;
const TEST_SCENARIO_10 = 10;
const TEST_SCENARIO_11 = 11;

const TEST_SCENARIO_101 = 101;
const TEST_SCENARIO_102 = 102;
const TEST_SCENARIO_103 = 103;
const TEST_SCENARIO_104 = 104;
const TEST_SCENARIO_105 = 105;
const TEST_SCENARIO_106 = 106;
const TEST_SCENARIO_107 = 107;
const TEST_SCENARIO_108 = 108;
const TEST_SCENARIO_109 = 109;
const TEST_SCENARIO_110 = 110;
const TEST_SCENARIO_111 = 111;

requestLongerTimeout(2);

let processResultsGlobal = (data, successes, failures) => {
  let expectedPrecision = data.precision;
  let scenario = data.options.scenario;
  let shouldBeRounded = data.options.shouldBeRounded;
  for (let success of successes) {
    ok(
      true,
      (shouldBeRounded ? "Should " : "Should not ") +
        `have rounded '${success[0]}' to nearest ${expectedPrecision} ms; saw ${success[1]}. ` +
        `scenario: TEST_SCENARIO_${scenario}`
    );
  }
  if (failures.length > 2) {
    for (let failure of failures) {
      ok(
        false,
        (shouldBeRounded ? "Should " : "Should not ") +
          `have rounded '${failure[0]}' to nearest ${expectedPrecision} ms; saw ${failure[1]}. ` +
          `scenario: TEST_SCENARIO_${scenario}`
      );
    }
  } else if (
    failures.length == 2 &&
    expectedPrecision < 10 &&
    failures[0][0].indexOf("Date().getTime()") > 0 &&
    failures[1][0].indexOf('File([], "").lastModified') > 0
  ) {
    /*
     * At high precisions, the epoch-based timestamps are large enough that their expected
     * rounding values lie directly between two integers; and floating point math is imprecise enough
     * that we need to accept these failures
     */
    ok(
      true,
      "Two Free Failures that " +
        (data.options.shouldBeRounded ? "ahould " : "should not ") +
        `be rounded on the epoch dates and precision: ${expectedPrecision}. ` +
        `scenario: TEST_SCENARIO_${data.options.scenario}`
    );
  } else if (failures.length == 1) {
    ok(
      true,
      "Free Failure: " +
        (data.options.shouldBeRounded ? "Should " : "Should not ") +
        `have rounded '${failures[0][0]}' to nearest ${expectedPrecision} ms; saw ${failures[0][1]}. ` +
        `scenario: TEST_SCENARIO_${data.options.scenario}`
    );
  }
};

// ================================================================================================
// ================================================================================================
// This test case is mostly copy-and-paste from the test case for window in
// test_reduce_time_precision.html. The main difference is this test case
// verifies DOM API has more precsion when it's in cross-origin-isolated and
// cross-origin-isolated doesn't affect RFP.
add_task(async function runRTPTestDOM() {
  let runTests = async function (data) {
    let expectedPrecision = data.precision;
    // eslint beleives that isrounded is available in this scope, but if you
    // remove the assignment, you will see it is not
    // eslint-disable-next-line
    let isRounded = eval(data.isRoundedFunc);
    // eslint-disable-next-line
    let processResults = eval(data.options.processResultsFunc);

    // Prepare for test of AudioContext.currentTime
    // eslint-disable-next-line
    let audioContext = new content.AudioContext();

    // Known ways to generate time stamps, in milliseconds
    const timeStampCodes = [
      "new content.Date().getTime()",
      'new content.File([], "").lastModified',
      "content.performance.now()",
      'new content.Event("").timeStamp',
    ];
    // These are measured in seconds, so we need to scale them up
    var timeStampCodesDOM = timeStampCodes.concat([
      "audioContext.currentTime * 1000",
    ]);

    // If we are not rounding values, this function will invert the return value
    let resultSwitchisRounded = function (timeStamp) {
      if (timeStamp == 0) {
        return true;
      }
      let result = isRounded(timeStamp, expectedPrecision, content.console);
      return data.options.shouldBeRounded ? result : !result;
    };

    // It's possible that even if we shouldn't be rounding something, we get a timestamp that is rounded.
    // If we have only one of these outliers, we are okay with that.  This should significantly
    // reduce intermittents, although it's still likely there will be some intermittents.  We can increase
    // this if we need to
    let successes = [],
      failures = [];

    // Loop through each timeStampCode, evaluate it,
    // and check if it is rounded
    for (let timeStampCode of timeStampCodesDOM) {
      // eslint-disable-next-line
      let timeStamp = eval(timeStampCode);

      // Audio Contexts increment in intervals of (minimum) 5.4ms, so we don't
      // clamp/jitter if the timer precision is les than that.
      // (Technically on MBPs they increment in intervals of 2.6 but this is
      // non-standard and will eventually be changed. We don't cover this situation
      // because we don't really support arbitrary Timer Precision, especially in
      // the 2.6 - 5.4ms interval.)
      if (timeStampCode.includes("audioContext") && expectedPrecision < 5.4) {
        continue;
      }

      if (timeStamp != 0 && resultSwitchisRounded(timeStamp)) {
        successes = successes.concat([[timeStampCode, timeStamp]]);
      } else if (timeStamp != 0) {
        failures = failures.concat([[timeStampCode, timeStamp]]);
      }
    }

    processResults(data, successes, failures);
  };

  // RFP
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_1,
      processResultsFunc: processResultsGlobal.toString(),
    },
    100,
    runTests
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      shouldBeRounded: false,
      scenario: TEST_SCENARIO_2,
      processResultsFunc: processResultsGlobal.toString(),
    },
    100,
    runTests
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      openPrivateWindow: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_3,
      processResultsFunc: processResultsGlobal.toString(),
    },
    100,
    runTests
  );

  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_4,
      processResultsFunc: processResultsGlobal.toString(),
    },
    13,
    runTests
  );
  /*
  Disabled because it causes too many intermittents
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      crossOriginIsolated: true,
      shouldBeRounded: false,
      scenario: TEST_SCENARIO_5,
      processResultsFunc: processResultsGlobal.toString(),
    },
    13,
    runTests
  );
  */
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      openPrivateWindow: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_6,
      processResultsFunc: processResultsGlobal.toString(),
    },
    13,
    runTests
  );

  // We cannot run the tests with too fine a precision, or it becomes very likely
  // to get false results that a number 'should not been rounded', when it really
  // wasn't, we had just gotten an accidental match
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_7,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runTests
  );
  /*
  Disabled because it causes too many intermittents
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      crossOriginIsolated: true,
      shouldBeRounded: false,
      scenario: TEST_SCENARIO_8,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runTests
  );
  */
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      openPrivateWindow: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_9,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runTests
  );

  // RTP
  await setupAndRunCrossOriginIsolatedTest(
    {
      reduceTimerPrecision: true,
      scenario: TEST_SCENARIO_10,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runTests
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_11,
      processResultsFunc: processResultsGlobal.toString(),
    },
    0.005,
    runTests
  );
});

// ================================================================================================
// ================================================================================================
// This test case is mostly copy-and-paste from the test case for worker in
// test_reduce_time_precision.html. The main difference is this test case
// verifies DOM API has more precsion when it's in cross-origin-isolated and
// cross-origin-isolated doesn't affect RFP.
let runWorkerTest = async function (data) {
  let expectedPrecision = data.precision;
  await new Promise(resolve => {
    // eslint beleives that isrounded is available in this scope, but if you
    // remove the assignment, you will see it is not
    // eslint-disable-next-line
    let isRounded = eval(data.isRoundedFunc);
    // eslint-disable-next-line
    let processResults = eval(data.options.processResultsFunc);

    let worker = new content.Worker(
      "coop_header.sjs?crossOriginIsolated=true&worker=true"
    );

    // Known ways to generate time stamps, in milliseconds
    const timeStampCodes = [
      "new Date().getTime()",
      'new File([], "").lastModified',
      "performance.now()",
      'new Event("").timeStamp',
    ];

    let promises = [],
      successes = [],
      failures = [];
    for (let timeStampCode of timeStampCodes) {
      promises.push(
        new Promise(res => {
          worker.postMessage({
            type: "runCmdAndGetResult",
            cmd: timeStampCode,
          });

          worker.addEventListener("message", function (e) {
            // If we are not rounding values, this function will invert the return value
            let resultSwitchisRounded = function (timeStamp) {
              if (timeStamp == 0) {
                return true;
              }
              let result = isRounded(timeStamp, expectedPrecision);
              return data.options.shouldBeRounded ? result : !result;
            };

            if (e.data.type == "result") {
              if (e.data.resultOf == timeStampCode) {
                if (resultSwitchisRounded(e.data.result)) {
                  successes = successes.concat([
                    [timeStampCode, e.data.result],
                  ]);
                } else {
                  failures = failures.concat([[timeStampCode, e.data.result]]);
                }
                worker.removeEventListener("message", this);
                res();
              }

              return;
            }

            ok(false, `Unknown message type. Got ${e.data.type}`);
            res();
          });
        })
      );
    }

    Promise.all(promises).then(_ => {
      worker.terminate();
      processResults(data, successes, failures);
      resolve();
    });
  });
};

add_task(async function runRTPTestsForWorker() {
  // RFP
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_101,
      processResultsFunc: processResultsGlobal.toString(),
    },
    100,
    runWorkerTest
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      shouldBeRounded: false,
      scenario: TEST_SCENARIO_102,
      processResultsFunc: processResultsGlobal.toString(),
    },
    100,
    runWorkerTest
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      openPrivateWindow: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_103,
      processResultsFunc: processResultsGlobal.toString(),
    },
    100,
    runWorkerTest
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_104,
      processResultsFunc: processResultsGlobal.toString(),
    },
    13,
    runWorkerTest
  );
  /*
  Disabled because it causes too many intermittents
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      crossOriginIsolated: true,
      shouldBeRounded: false,
      scenario: TEST_SCENARIO_105,
      processResultsFunc: processResultsGlobal.toString(),
    },
    13,
    runWorkerTest
  );
  */
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      openPrivateWindow: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_106,
      processResultsFunc: processResultsGlobal.toString(),
    },
    13,
    runWorkerTest
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_107,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runWorkerTest
  );
  /* Disabled because it causes too many intermittents
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      crossOriginIsolated: true,
      shouldBeRounded: false,
      scenario: TEST_SCENARIO_108,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runWorkerTest
  );
  */
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprintingPBMOnly: true,
      openPrivateWindow: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_109,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runWorkerTest
  );

  // RTP
  await setupAndRunCrossOriginIsolatedTest(
    {
      reduceTimerPrecision: true,
      scenario: TEST_SCENARIO_110,
      processResultsFunc: processResultsGlobal.toString(),
    },
    7.97,
    runWorkerTest
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
      scenario: TEST_SCENARIO_111,
      processResultsFunc: processResultsGlobal.toString(),
    },
    0.005,
    runWorkerTest
  );
});