summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/unit/test_streamupdater.js
blob: e5abc4e91a81dbbc35554bab742282dd2520a6d1 (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
286
287
288
function doTest(updates, assertions, expectError)
{
  if (expectError) {
    doUpdateTest(updates, assertions, updateError, runNextTest);
  } else {
    doUpdateTest(updates, assertions, runNextTest, updateError);
  }
}

// Never use the same URLs for multiple tests, because we aren't guaranteed
// to reset the database between tests.
function testFillDb() {
  var add1Urls = [ "zaz.com/a", "yxz.com/c" ];

  var update = "n:1000\n";
  update += "i:test-phish-simple\n";

  var update1 = buildBareUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update1) + "\n";

  var assertions = {
    "tableData" : "test-phish-simple;a:1",
    "urlsExist" : add1Urls
  };

  doTest([update], assertions, false);
}

function testSimpleForward() {
  var add1Urls = [ "foo-simple.com/a", "bar-simple.com/c" ];
  var add2Urls = [ "foo-simple.com/b" ];
  var add3Urls = [ "bar-simple.com/d" ];

  var update = "n:1000\n";
  update += "i:test-phish-simple\n";

  var update1 = buildBareUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update1) + "\n";

  var update2 = buildBareUpdate(
    [{ "chunkNum" : 2,
       "urls" : add2Urls }]);
  update += "u:data:," + encodeURIComponent(update2) + "\n";

  var update3 = buildBareUpdate(
    [{ "chunkNum" : 3,
       "urls" : add3Urls }]);
  update += "u:data:," + encodeURIComponent(update3) + "\n";

  var assertions = {
    "tableData" : "test-phish-simple;a:1-3",
    "urlsExist" : add1Urls.concat(add2Urls).concat(add3Urls)
  };

  doTest([update], assertions, false);
}

// Make sure that a nested forward (a forward within a forward) causes
// the update to fail.
function testNestedForward() {
  var add1Urls = [ "foo-nested.com/a", "bar-nested.com/c" ];
  var add2Urls = [ "foo-nested.com/b" ];

  var update = "n:1000\n";
  update += "i:test-phish-simple\n";

  var update1 = buildBareUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update1) + "\n";

  var update2 = buildBareUpdate(
    [{ "chunkNum" : 2 }]);
  var update3 = buildBareUpdate(
    [{ "chunkNum" : 3,
       "urls" : add1Urls }]);

  update2 += "u:data:," + encodeURIComponent(update3) + "\n";

  update += "u:data:," + encodeURIComponent(update2) + "\n";

  var assertions = {
    "tableData" : "",
    "urlsDontExist" : add1Urls.concat(add2Urls)
  };

  doTest([update], assertions, true);
}

// An invalid URL forward causes the update to fail.
function testInvalidUrlForward() {
  var add1Urls = [ "foo-invalid.com/a", "bar-invalid.com/c" ];

  var update = buildPhishingUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:asdf://blah/blah\n";  // invalid URL scheme

  // add1Urls is present, but that is an artifact of the way we do the test.
  var assertions = {
    "tableData" : "",
    "urlsExist" : add1Urls
  };

  doTest([update], assertions, true);
}

// A failed network request causes the update to fail.
function testErrorUrlForward() {
  var add1Urls = [ "foo-forward.com/a", "bar-forward.com/c" ];

  var update = buildPhishingUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:http://test.invalid/asdf/asdf\n";  // invalid URL scheme

  // add1Urls is present, but that is an artifact of the way we do the test.
  var assertions = {
    "tableData" : "",
    "urlsExist" : add1Urls
  };

  doTest([update], assertions, true);
}

function testMultipleTables() {
  var add1Urls = [ "foo-multiple.com/a", "bar-multiple.com/c" ];
  var add2Urls = [ "foo-multiple.com/b" ];
  var add3Urls = [ "bar-multiple.com/d" ];
  var add4Urls = [ "bar-multiple.com/e" ];
  var add6Urls = [ "bar-multiple.com/g" ];

  var update = "n:1000\n";
  update += "i:test-phish-simple\n";

  var update1 = buildBareUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update1) + "\n";

  var update2 = buildBareUpdate(
    [{ "chunkNum" : 2,
       "urls" : add2Urls }]);
  update += "u:data:," + encodeURIComponent(update2) + "\n";

  update += "i:test-malware-simple\n";

  var update3 = buildBareUpdate(
    [{ "chunkNum" : 3,
       "urls" : add3Urls }]);
  update += "u:data:," + encodeURIComponent(update3) + "\n";

  update += "i:test-unwanted-simple\n";
  var update4 = buildBareUpdate(
    [{ "chunkNum" : 4,
       "urls" : add4Urls }]);
  update += "u:data:," + encodeURIComponent(update4) + "\n";

  update += "i:test-block-simple\n";
  var update6 = buildBareUpdate(
    [{ "chunkNum" : 6,
       "urls" : add6Urls }]);
  update += "u:data:," + encodeURIComponent(update6) + "\n";

  var assertions = {
    "tableData" : "test-block-simple;a:6\ntest-malware-simple;a:3\ntest-phish-simple;a:1-2\ntest-unwanted-simple;a:4",
    "urlsExist" : add1Urls.concat(add2Urls),
    "malwareUrlsExist" : add3Urls,
    "unwantedUrlsExist" : add4Urls,
    "blockedUrlsExist" : add6Urls
  };

  doTest([update], assertions, false);
}

function testUrlInMultipleTables() {
  var add1Urls = [ "foo-forward.com/a" ];

  var update = "n:1000\n";
  update += "i:test-phish-simple\n";

  var update1 = buildBareUpdate(
    [{ "chunkNum" : 1,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update1) + "\n";

  update += "i:test-malware-simple\n";
  var update2 = buildBareUpdate(
    [{ "chunkNum" : 2,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update2) + "\n";

  update += "i:test-unwanted-simple\n";
  var update3 = buildBareUpdate(
    [{ "chunkNum" : 3,
       "urls" : add1Urls }]);
  update += "u:data:," + encodeURIComponent(update3) + "\n";

  var assertions = {
    "tableData" : "test-malware-simple;a:2\ntest-phish-simple;a:1\ntest-unwanted-simple;a:3",
    "urlExistInMultipleTables" : { url: add1Urls,
                                   tables: "test-malware-simple,test-phish-simple,test-unwanted-simple" }
  };

  doTest([update], assertions, false);
}

function Observer(callback) {
  this.observe = callback;
}

Observer.prototype =
{
QueryInterface: function(iid)
{
  if (!iid.equals(Ci.nsISupports) &&
      !iid.equals(Ci.nsIObserver)) {
    throw Cr.NS_ERROR_NO_INTERFACE;
  }
  return this;
}
};

// Tests a database reset request.
function testReset() {
  // The moz-phish-simple table is populated separately from the other update in
  // a separate update request. Therefore it should not be reset when we run the
  // updates later in this function.
  var mozAddUrls = [ "moz-reset.com/a" ];
  var mozUpdate = buildMozPhishingUpdate(
    [
      { "chunkNum" : 1,
        "urls" : mozAddUrls
      }]);

  var dataUpdate = "data:," + encodeURIComponent(mozUpdate);

  streamUpdater.downloadUpdates(mozTables, "", true,
                                dataUpdate, () => {}, updateError, updateError);

  var addUrls1 = [ "foo-reset.com/a", "foo-reset.com/b" ];
  var update1 = buildPhishingUpdate(
    [
      { "chunkNum" : 1,
        "urls" : addUrls1
      }]);

  var update2 = "n:1000\nr:pleasereset\n";

  var addUrls3 = [ "bar-reset.com/a", "bar-reset.com/b" ];
  var update3 = buildPhishingUpdate(
    [
      { "chunkNum" : 3,
        "urls" : addUrls3
      }]);

  var assertions = {
    "tableData" : "moz-phish-simple;a:1\ntest-phish-simple;a:3", // tables that should still be there.
    "mozPhishingUrlsExist" : mozAddUrls,                         // mozAddUrls added prior to the reset
                                                                 // but it should still exist after reset.
    "urlsExist" : addUrls3,                                      // addUrls3 added after the reset.
    "urlsDontExist" : addUrls1                                   // addUrls1 added prior to the reset
  };

  // Use these update responses in order. The update request only
  // contains test-*-simple tables so the reset will only apply to these.
  doTest([update1, update2, update3], assertions, false);
}


function run_test()
{
  runTests([
    testSimpleForward,
    testNestedForward,
    testInvalidUrlForward,
    testErrorUrlForward,
    testMultipleTables,
    testUrlInMultipleTables,
    testReset
  ]);
}

do_test_pending();