summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/test/browser/browser_cookiebanner_gdpr_telemetry.js
blob: 2d6f3343b89ba47be5194ea9c794f3c8b3f83a8e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { SiteDataTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SiteDataTestUtils.sys.mjs"
);

const TEST_CASES = [
  {
    value: "CAISHAgBEhJnd3NfMjAyNDAzMjUtMF9SQzEaAmRlIAEaBgiAw42wBg",
    expected: "Accept",
    region: "de",
    host: ".google.de",
  },
  {
    value: "CAESHAgBEhJnd3NfMjAyNDAzMjUtMF9SQzEaAmRlIAEaBgiAw42wBg",
    expected: "Reject",
    region: "de",
    host: ".google.de",
  },
  {
    value:
      "CAISNQgBEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjQwMzI0LjA4X3AwGgJkZSADGgYIgMONsAY",
    expected: "Custom",
    region: "de",
    host: ".google.de",
  },
  {
    value: "CAISHAgBEhJnd3NfMjAyNDAzMjUtMF9SQzEaAmVuIAEaBgiAw42wBg",
    expected: "Accept",
    region: "en",
    host: ".google.com",
  },
];

add_setup(async function () {
  registerCleanupFunction(async () => {
    // Clear cookies that have been set during testing.
    await SiteDataTestUtils.clear();
  });
});

add_task(async function test_google_gdpr_telemetry() {
  // Clear telemetry before starting telemetry test.
  Services.fog.testResetFOG();

  for (let test of TEST_CASES) {
    // Add a Google SOCS cookie to trigger recording telemetry.
    SiteDataTestUtils.addToCookies({
      name: "SOCS",
      value: test.value,
      host: test.host,
      path: "/",
    });

    // Verify if the google GDPR choice cookie telemetry is recorded properly.
    is(
      Glean.cookieBanners.googleGdprChoiceCookie[test.host].testGetValue(),
      test.expected,
      "The Google GDPR telemetry is recorded properly."
    );

    // Verify the the event telemetry is recorded properly.
    let events = Glean.cookieBanners.googleGdprChoiceCookieEvent.testGetValue();
    let event = events[events.length - 1];

    is(
      event.extra.search_domain,
      test.host,
      "The Google GDPR event telemetry records the search domain properly."
    );

    is(
      event.extra.choice,
      test.expected,
      "The Google GDPR event telemetry records the choice properly."
    );

    is(
      event.extra.region,
      test.region,
      "The Google GDPR event telemetry records the region properly."
    );
  }

  is(
    Glean.cookieBanners.googleGdprChoiceCookieEvent.testGetValue().length,
    TEST_CASES.length,
    "The number of events is expected."
  );
});

add_task(async function test_invalid_google_socs_cookies() {
  // Clear telemetry before starting telemetry test.
  Services.fog.testResetFOG();

  // Add an invalid Google SOCS cookie which is not Base64 encoding.
  SiteDataTestUtils.addToCookies({
    name: "SOCS",
    value: "invalid",
    host: ".google.com",
    path: "/",
  });

  // Ensure no GDPR telemetry is recorded.
  is(
    Glean.cookieBanners.googleGdprChoiceCookie[".google.com"].testGetValue(),
    null,
    "No Google GDPR telemetry is recorded."
  );

  is(
    Glean.cookieBanners.googleGdprChoiceCookieEvent.testGetValue(),
    null,
    "No event telemetry is recorded."
  );

  // Add an invalid Google SOCS cookie which is Base64 encoding but in wrong
  // protobuf format.
  SiteDataTestUtils.addToCookies({
    name: "SOCS",
    value: "CAISAggBGgYIgMONsAY",
    host: ".google.com",
    path: "/",
  });

  // Ensure no GDPR telemetry is recorded.
  is(
    Glean.cookieBanners.googleGdprChoiceCookie[".google.com"].testGetValue(),
    null,
    "No Google GDPR telemetry is recorded."
  );

  is(
    Glean.cookieBanners.googleGdprChoiceCookieEvent.testGetValue(),
    null,
    "No event telemetry is recorded."
  );
});

add_task(async function test_google_gdpr_telemetry_pbm() {
  // Clear telemetry before starting telemetry test.
  Services.fog.testResetFOG();

  for (let test of TEST_CASES) {
    // Add a private Google SOCS cookie to trigger recording telemetry.
    SiteDataTestUtils.addToCookies({
      name: "SOCS",
      value: test.value,
      host: test.host,
      path: "/",
      originAttributes: { privateBrowsingId: 1 },
    });

    // Ensure we don't record google GDPR choice cookie telemetry.
    is(
      Glean.cookieBanners.googleGdprChoiceCookie[test.host].testGetValue(),
      null,
      "No Google GDPR telemetry is recorded."
    );

    // Verify the the event telemetry for PBM is recorded properly.
    let events =
      Glean.cookieBanners.googleGdprChoiceCookieEventPbm.testGetValue();
    let event = events[events.length - 1];

    is(
      event.extra.choice,
      test.expected,
      "The Google GDPR event telemetry records the choice properly."
    );

    is(event.extra.search_domain, undefined, "No search domain is recorded.");
    is(event.extra.region, undefined, "No region is recorded.");
  }

  is(
    Glean.cookieBanners.googleGdprChoiceCookieEventPbm.testGetValue().length,
    TEST_CASES.length,
    "The number of events is expected."
  );

  // We need to notify the "last-pb-context-exited" tp explicitly remove private
  // cookies. Otherwise, the remaining private cookies could affect following
  // tests. The SiteDataTestUtils.clear() doesn't clear for private cookies.
  Services.obs.notifyObservers(null, "last-pb-context-exited");
});