summaryrefslogtreecommitdiffstats
path: root/netwerk/cookie/test/browser/browser_partitioned_telemetry.js
blob: f89bcdd1899e68253f440ca19ee77a9445d9e383 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

const TEST_URL =
  "https://example.com/browser/netwerk/cookie/test/browser/file_empty.html";

async function validateTelemetryValues(
  { setCookies, setForeigns, setPartitioneds, setForeignPartitioneds },
  message
) {
  await Services.fog.testFlushAllChildren();
  let setCookieTelemetry = Glean.networking.setCookie.testGetValue();
  is(
    setCookieTelemetry ?? undefined,
    setCookies,
    message + " - all set cookies"
  );
  let foreignTelemetry = Glean.networking.setCookieForeign.testGetValue();
  is(
    foreignTelemetry?.numerator,
    setForeigns,
    message + " - foreign set cookies"
  );
  is(
    foreignTelemetry?.denominator,
    setCookies,
    message + " - foreign set cookies denominator"
  );
  let partitonedTelemetry =
    Glean.networking.setCookiePartitioned.testGetValue();
  is(
    partitonedTelemetry?.numerator,
    setPartitioneds,
    message + " - partitioned set cookies"
  );
  is(
    partitonedTelemetry?.denominator,
    setCookies,
    message + " - partitioned set cookies denominator"
  );
  let foreignPartitonedTelemetry =
    Glean.networking.setCookieForeignPartitioned.testGetValue();
  is(
    foreignPartitonedTelemetry?.numerator,
    setForeignPartitioneds,
    message + " - foreign partitioned set cookies"
  );
  is(
    foreignPartitonedTelemetry?.denominator,
    setCookies,
    message + " - foreign partitioned set cookies denominator"
  );
}

add_task(async () => {
  await Services.fog.testFlushAllChildren();
  Services.fog.testResetFOG();
  await validateTelemetryValues({}, "initially empty");

  // open a browser window for the test
  let tab = BrowserTestUtils.addTab(gBrowser, TEST_URL);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  // Set cookies with Javascript
  await SpecialPowers.spawn(browser, [], function () {
    content.document.cookie = "a=1; Partitioned; SameSite=None; Secure";
    content.document.cookie = "b=2; SameSite=None; Secure";
  });
  await validateTelemetryValues(
    {
      setCookies: 2,
      setForeigns: 0,
      setPartitioneds: 1,
      setForeignPartitioneds: 0,
    },
    "javascript cookie"
  );

  // Set cookies with HTTP
  await SpecialPowers.spawn(browser, [], async function () {
    await content.fetch("partitioned.sjs");
  });
  await validateTelemetryValues(
    {
      setCookies: 4,
      setForeigns: 0,
      setPartitioneds: 2,
      setForeignPartitioneds: 0,
    },
    "same site fetch"
  );

  // Set cookies with cross-site HTTP
  await SpecialPowers.spawn(browser, [], async function () {
    await content.fetch(
      "https://example.org/browser/netwerk/cookie/test/browser/partitioned.sjs",
      { credentials: "include" }
    );
  });
  await validateTelemetryValues(
    {
      setCookies: 6,
      setForeigns: 2,
      setPartitioneds: 3,
      setForeignPartitioneds: 1,
    },
    "foreign fetch"
  );

  // Set cookies with cross-site HTTP redirect
  await SpecialPowers.spawn(browser, [], async function () {
    await content.fetch(
      encodeURI(
        "https://example.org/browser/netwerk/cookie/test/browser/partitioned.sjs?redirect=https://example.com/browser/netwerk/cookie/test/browser/partitioned.sjs?nocookie"
      ),
      { credentials: "include" }
    );
  });

  await validateTelemetryValues(
    {
      setCookies: 8,
      setForeigns: 4,
      setPartitioneds: 4,
      setForeignPartitioneds: 2,
    },
    "foreign fetch redirect"
  );

  // remove the tab
  gBrowser.removeTab(tab);
});