summaryrefslogtreecommitdiffstats
path: root/toolkit/components/resistfingerprinting/tests/browser/browser_canvas_fingerprinter_telemetry.js
blob: d2a33a4347872957cd6eebb94f2c4b471a78f9c0 (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
/* 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";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);
const TEST_PAGE_NORMAL = TEST_PATH + "empty.html";
const TEST_PAGE_FINGERPRINTER = TEST_PATH + "canvas-fingerprinter.html";

const TELEMETRY_CANVAS_FINGERPRINTING_PER_TAB = "CANVAS_FINGERPRINTING_PER_TAB";

const KEY_UNKNOWN = "unknown";
const KEY_KNOWN_TEXT = "known_text";

async function clearTelemetry() {
  Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
  Services.telemetry
    .getKeyedHistogramById(TELEMETRY_CANVAS_FINGERPRINTING_PER_TAB)
    .clear();
}

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 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.");
}

add_setup(async function () {
  await clearTelemetry();
});

add_task(async function test_canvas_fingerprinting_telemetry() {
  let promiseWindowDestroyed = BrowserUtils.promiseObserved(
    "window-global-destroyed"
  );

  // First, we open a page without any canvas fingerprinters
  await BrowserTestUtils.withNewTab(TEST_PAGE_NORMAL, async _ => {});

  // Make sure the tab was closed properly before checking Telemetry.
  await promiseWindowDestroyed;

  // Check that the telemetry has been record properly for normal page. The
  // telemetry should show there was no known fingerprinting attempt.
  await checkKeyedHistogram(
    TELEMETRY_CANVAS_FINGERPRINTING_PER_TAB,
    KEY_UNKNOWN,
    0,
    1
  );

  await clearTelemetry();
});

add_task(async function test_canvas_fingerprinting_telemetry() {
  let promiseWindowDestroyed = BrowserUtils.promiseObserved(
    "window-global-destroyed"
  );

  // Now open a page with a canvas fingerprinter
  await BrowserTestUtils.withNewTab(TEST_PAGE_FINGERPRINTER, async _ => {});

  // Make sure the tab was closed properly before checking Telemetry.
  await promiseWindowDestroyed;

  // The telemetry should show a a known fingerprinting text and a known
  // canvas fingerprinter.
  await checkKeyedHistogram(
    TELEMETRY_CANVAS_FINGERPRINTING_PER_TAB,
    KEY_KNOWN_TEXT,
    6, // CanvasFingerprinter::eVariant4
    1
  );

  await clearTelemetry();
});