summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/xpcshell/test_ExceptionListService.js
blob: 4063d067f59e82fc78a44fa1ec46196d9a0ff663 (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
// This test ensures that the URL decoration annotations service works as
// expected, and also we successfully downgrade document.referrer to the
// eTLD+1 URL when tracking identifiers controlled by this service are
// present in the referrer URI.

"use strict";

/* Unit tests for the nsIPartitioningExceptionListService implementation. */

const { RemoteSettings } = ChromeUtils.importESModule(
  "resource://services-settings/remote-settings.sys.mjs"
);

const COLLECTION_NAME = "partitioning-exempt-urls";
const PREF_NAME = "privacy.restrict3rdpartystorage.skip_list";

do_get_profile();

class UpdateEvent extends EventTarget {}
function waitForEvent(element, eventName) {
  return new Promise(function (resolve) {
    element.addEventListener(eventName, e => resolve(e.detail), { once: true });
  });
}

add_task(async _ => {
  let peuService = Cc[
    "@mozilla.org/partitioning/exception-list-service;1"
  ].getService(Ci.nsIPartitioningExceptionListService);

  // Make sure we have a pref initially, since the exception list service
  // requires it.
  Services.prefs.setStringPref(PREF_NAME, "");

  let updateEvent = new UpdateEvent();
  let records = [
    {
      id: "1",
      last_modified: 1000000000000001,
      firstPartyOrigin: "https://example.org",
      thirdPartyOrigin: "https://tracking.example.com",
    },
  ];

  // Add some initial data
  let db = RemoteSettings(COLLECTION_NAME).db;
  await db.importChanges({}, Date.now(), records);

  let promise = waitForEvent(updateEvent, "update");
  let obs = data => {
    let event = new CustomEvent("update", { detail: data });
    updateEvent.dispatchEvent(event);
  };
  peuService.registerAndRunExceptionListObserver(obs);
  let list = await promise;
  Assert.equal(list, "", "No items in the list");

  // Second event is from the RemoteSettings record.
  list = await waitForEvent(updateEvent, "update");
  Assert.equal(
    list,
    "https://example.org,https://tracking.example.com",
    "Has one item in the list"
  );

  records.push({
    id: "2",
    last_modified: 1000000000000002,
    firstPartyOrigin: "https://foo.org",
    thirdPartyOrigin: "https://bar.com",
  });

  promise = waitForEvent(updateEvent, "update");
  await RemoteSettings(COLLECTION_NAME).emit("sync", {
    data: { current: records },
  });
  list = await promise;
  Assert.equal(
    list,
    "https://example.org,https://tracking.example.com;https://foo.org,https://bar.com",
    "Has several items in the list"
  );

  promise = waitForEvent(updateEvent, "update");
  Services.prefs.setStringPref(PREF_NAME, "https://test.com,https://test3.com");
  list = await promise;
  Assert.equal(
    list,
    "https://test.com,https://test3.com;https://example.org,https://tracking.example.com;https://foo.org,https://bar.com",
    "Has several items in the list"
  );

  promise = waitForEvent(updateEvent, "update");
  Services.prefs.setStringPref(
    PREF_NAME,
    "https://test.com,https://test3.com;https://abc.com,https://def.com"
  );
  list = await promise;
  Assert.equal(
    list,
    "https://test.com,https://test3.com;https://abc.com,https://def.com;https://example.org,https://tracking.example.com;https://foo.org,https://bar.com",
    "Has several items in the list"
  );

  peuService.unregisterExceptionListObserver(obs);
  await db.clear();
});