summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_IgnoreList.js
blob: 0f51995ac0488465f00536aa9a3cd96a29166c2e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  IgnoreLists: "resource://gre/modules/IgnoreLists.sys.mjs",
  PromiseUtils: "resource://gre/modules/PromiseUtils.sys.mjs",
  RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
  RemoteSettingsClient:
    "resource://services-settings/RemoteSettingsClient.sys.mjs",
  sinon: "resource://testing-common/Sinon.sys.mjs",
});

const IGNORELIST_KEY = "hijack-blocklists";

const IGNORELIST_TEST_DATA = [
  {
    id: "load-paths",
    matches: ["[other]addEngineWithDetails:searchignore@mozilla.com"],
  },
  {
    id: "submission-urls",
    matches: ["ignore=true"],
  },
];

let getStub;

add_task(async function setup() {
  const ignoreListSettings = RemoteSettings(IGNORELIST_KEY);
  getStub = sinon.stub(ignoreListSettings, "get");
});

add_task(async function test_ignoreList_basic_get() {
  getStub.onFirstCall().returns(IGNORELIST_TEST_DATA);

  const settings = await IgnoreLists.getAndSubscribe(() => {});

  Assert.deepEqual(
    settings,
    IGNORELIST_TEST_DATA,
    "Should have obtained the correct data from the database."
  );
});

add_task(async function test_ignoreList_reentry() {
  let promise = PromiseUtils.defer();
  getStub.resetHistory();
  getStub.onFirstCall().returns(promise.promise);

  let firstResult;
  let secondResult;

  const firstCallPromise = IgnoreLists.getAndSubscribe(() => {}).then(
    result => (firstResult = result)
  );
  const secondCallPromise = IgnoreLists.getAndSubscribe(() => {}).then(
    result => (secondResult = result)
  );

  Assert.strictEqual(
    firstResult,
    undefined,
    "Should not have returned the first result yet."
  );
  Assert.strictEqual(
    secondResult,
    undefined,
    "Should not have returned the second result yet."
  );

  promise.resolve(IGNORELIST_TEST_DATA);

  await Promise.all([firstCallPromise, secondCallPromise]);

  Assert.deepEqual(
    firstResult,
    IGNORELIST_TEST_DATA,
    "Should have returned the correct data to the first call."
  );
  Assert.deepEqual(
    secondResult,
    IGNORELIST_TEST_DATA,
    "Should have returned the correct data to the second call."
  );
});

add_task(async function test_ignoreList_updates() {
  getStub.onFirstCall().returns([]);

  let updateData;
  let listener = eventData => {
    updateData = eventData.data.current;
  };

  await IgnoreLists.getAndSubscribe(listener);

  Assert.ok(!updateData, "Should not have given an update yet");

  const NEW_DATA = [
    {
      id: "load-paths",
      schema: 1553857697843,
      last_modified: 1553859483588,
      matches: ["[other]addEngineWithDetails:searchignore@mozilla.com"],
    },
    {
      id: "submission-urls",
      schema: 1553857697843,
      last_modified: 1553859435500,
      matches: ["ignore=true"],
    },
  ];

  // Simulate an ignore list update.
  await RemoteSettings("hijack-blocklists").emit("sync", {
    data: {
      current: NEW_DATA,
    },
  });

  Assert.deepEqual(
    updateData,
    NEW_DATA,
    "Should have updated the listener with the correct data"
  );

  IgnoreLists.unsubscribe(listener);

  await RemoteSettings("hijack-blocklists").emit("sync", {
    data: {
      current: [
        {
          id: "load-paths",
          schema: 1553857697843,
          last_modified: 1553859483589,
          matches: [],
        },
        {
          id: "submission-urls",
          schema: 1553857697843,
          last_modified: 1553859435501,
          matches: [],
        },
      ],
    },
  });

  Assert.deepEqual(
    updateData,
    NEW_DATA,
    "Should not have updated the listener"
  );
});

add_task(async function test_ignoreList_db_modification() {
  // Fill the database with some values that we can use to test that it is cleared.
  const db = RemoteSettings(IGNORELIST_KEY).db;
  await db.importChanges({}, Date.now(), IGNORELIST_TEST_DATA, { clear: true });

  // Stub the get() so that the first call simulates a signature error, and
  // the second simulates success reading from the dump.
  getStub.resetHistory();
  getStub
    .onFirstCall()
    .rejects(new RemoteSettingsClient.InvalidSignatureError("abc"));
  getStub.onSecondCall().returns(IGNORELIST_TEST_DATA);

  let result = await IgnoreLists.getAndSubscribe(() => {});

  Assert.ok(
    getStub.calledTwice,
    "Should have called the get() function twice."
  );

  const databaseEntries = await db.list();
  Assert.equal(databaseEntries.length, 0, "Should have cleared the database.");

  Assert.deepEqual(
    result,
    IGNORELIST_TEST_DATA,
    "Should have returned the correct data."
  );
});

add_task(async function test_ignoreList_db_modification_never_succeeds() {
  // Fill the database with some values that we can use to test that it is cleared.
  const db = RemoteSettings(IGNORELIST_KEY).db;
  await db.importChanges({}, Date.now(), IGNORELIST_TEST_DATA, { clear: true });

  // Now simulate the condition where for some reason we never get a
  // valid result.
  getStub.reset();
  getStub.rejects(new RemoteSettingsClient.InvalidSignatureError("abc"));

  let result = await IgnoreLists.getAndSubscribe(() => {});

  Assert.ok(
    getStub.calledTwice,
    "Should have called the get() function twice."
  );

  const databaseEntries = await db.list();
  Assert.equal(databaseEntries.length, 0, "Should have cleared the database.");

  Assert.deepEqual(result, [], "Should have returned an empty result.");
});