summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/unit/test_exceptionListService.js
blob: 218aec4934246c0ce037995fc0e8db95311b01f4 (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
/* 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";

/* Unit tests for the nsIUrlClassifierExceptionListService implementation. */

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

const COLLECTION_NAME = "url-classifier-skip-urls";
const FEATURE_TRACKING_NAME = "tracking-annotation-test";
const FEATURE_TRACKING_PREF_NAME = "urlclassifier.tracking-annotation-test";
const FEATURE_SOCIAL_NAME = "socialtracking-annotation-test";
const FEATURE_SOCIAL_PREF_NAME = "urlclassifier.socialtracking-annotation-test";
const FEATURE_FINGERPRINTING_NAME = "fingerprinting-annotation-test";
const FEATURE_FINGERPRINTING_PREF_NAME =
  "urlclassifier.fingerprinting-annotation-test";

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 function test_list_changes() {
  let exceptionListService = Cc[
    "@mozilla.org/url-classifier/exception-list-service;1"
  ].getService(Ci.nsIUrlClassifierExceptionListService);

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

  let updateEvent = new UpdateEvent();
  let obs = data => {
    let event = new CustomEvent("update", { detail: data });
    updateEvent.dispatchEvent(event);
  };

  let records = [
    {
      id: "1",
      last_modified: 1000000000000001,
      feature: FEATURE_TRACKING_NAME,
      pattern: "example.com",
    },
  ];

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

  exceptionListService.registerAndRunExceptionListObserver(
    FEATURE_TRACKING_NAME,
    FEATURE_TRACKING_PREF_NAME,
    obs
  );

  Assert.equal(await promise, "", "No items in the list");

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

  records.push(
    {
      id: "2",
      last_modified: 1000000000000002,
      feature: FEATURE_TRACKING_NAME,
      pattern: "MOZILLA.ORG",
    },
    {
      id: "3",
      last_modified: 1000000000000003,
      feature: "some-other-feature",
      pattern: "noinclude.com",
    },
    {
      last_modified: 1000000000000004,
      feature: FEATURE_TRACKING_NAME,
      pattern: "*.example.org",
    }
  );

  promise = waitForEvent(updateEvent, "update");

  await RemoteSettings(COLLECTION_NAME).emit("sync", {
    data: { current: records },
  });

  list = await promise;

  Assert.equal(
    list,
    "example.com,mozilla.org,*.example.org",
    "Has several items in the list"
  );

  promise = waitForEvent(updateEvent, "update");

  Services.prefs.setStringPref(FEATURE_TRACKING_PREF_NAME, "test.com");

  list = await promise;

  Assert.equal(
    list,
    "test.com,example.com,mozilla.org,*.example.org",
    "Has several items in the list"
  );

  promise = waitForEvent(updateEvent, "update");

  Services.prefs.setStringPref(
    FEATURE_TRACKING_PREF_NAME,
    "test.com,whatever.com,*.abc.com"
  );

  list = await promise;

  Assert.equal(
    list,
    "test.com,whatever.com,*.abc.com,example.com,mozilla.org,*.example.org",
    "Has several items in the list"
  );

  exceptionListService.unregisterExceptionListObserver(
    FEATURE_TRACKING_NAME,
    obs
  );
  exceptionListService.clear();

  await db.clear();
});

/**
 * This test make sure when a feature registers itself to exceptionlist service,
 * it can get the correct initial data.
 */
add_task(async function test_list_init_data() {
  let exceptionListService = Cc[
    "@mozilla.org/url-classifier/exception-list-service;1"
  ].getService(Ci.nsIUrlClassifierExceptionListService);

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

  let updateEvent = new UpdateEvent();

  let records = [
    {
      id: "1",
      last_modified: 1000000000000001,
      feature: FEATURE_TRACKING_NAME,
      pattern: "tracking.example.com",
    },
    {
      id: "2",
      last_modified: 1000000000000002,
      feature: FEATURE_SOCIAL_NAME,
      pattern: "social.example.com",
    },
    {
      id: "3",
      last_modified: 1000000000000003,
      feature: FEATURE_TRACKING_NAME,
      pattern: "*.tracking.org",
    },
    {
      id: "4",
      last_modified: 1000000000000004,
      feature: FEATURE_SOCIAL_NAME,
      pattern: "MOZILLA.ORG",
    },
  ];

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

  // The first registered feature make ExceptionListService get the initial data
  // from remote setting.
  let promise = waitForEvent(updateEvent, "update");

  let obs = data => {
    let event = new CustomEvent("update", { detail: data });
    updateEvent.dispatchEvent(event);
  };
  exceptionListService.registerAndRunExceptionListObserver(
    FEATURE_TRACKING_NAME,
    FEATURE_TRACKING_PREF_NAME,
    obs
  );

  let list = await promise;
  Assert.equal(list, "", "Empty list initially");

  Assert.equal(
    await waitForEvent(updateEvent, "update"),
    "tracking.example.com,*.tracking.org",
    "Has several items in the list"
  );

  // Register another feature after ExceptionListService got the initial data.
  promise = waitForEvent(updateEvent, "update");

  exceptionListService.registerAndRunExceptionListObserver(
    FEATURE_SOCIAL_NAME,
    FEATURE_SOCIAL_PREF_NAME,
    obs
  );

  list = await promise;

  Assert.equal(
    list,
    "social.example.com,mozilla.org",
    "Has several items in the list"
  );

  // Test registering a feature after ExceptionListService recieved the synced data.
  records.push(
    {
      id: "5",
      last_modified: 1000000000000002,
      feature: FEATURE_FINGERPRINTING_NAME,
      pattern: "fingerprinting.example.com",
    },
    {
      id: "6",
      last_modified: 1000000000000002,
      feature: "other-fature",
      pattern: "not-a-fingerprinting.example.com",
    },
    {
      id: "7",
      last_modified: 1000000000000002,
      feature: FEATURE_FINGERPRINTING_NAME,
      pattern: "*.fingerprinting.org",
    }
  );

  await RemoteSettings(COLLECTION_NAME).emit("sync", {
    data: { current: records },
  });

  promise = waitForEvent(updateEvent, "update");

  exceptionListService.registerAndRunExceptionListObserver(
    FEATURE_FINGERPRINTING_NAME,
    FEATURE_FINGERPRINTING_PREF_NAME,
    obs
  );

  list = await promise;

  Assert.equal(
    list,
    "fingerprinting.example.com,*.fingerprinting.org",
    "Has several items in the list"
  );

  exceptionListService.unregisterExceptionListObserver(
    FEATURE_TRACKING_NAME,
    obs
  );
  exceptionListService.unregisterExceptionListObserver(
    FEATURE_SOCIAL_NAME,
    obs
  );
  exceptionListService.unregisterExceptionListObserver(
    FEATURE_FINGERPRINTING_NAME,
    obs
  );
  exceptionListService.clear();

  await db.clear();
});