toolkit/components/url-classifier/tests/unit/test_partial.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1
michael@0 2 /**
michael@0 3 * DummyCompleter() lets tests easily specify the results of a partial
michael@0 4 * hash completion request.
michael@0 5 */
michael@0 6 function DummyCompleter() {
michael@0 7 this.fragments = {};
michael@0 8 this.queries = [];
michael@0 9 this.tableName = "test-phish-simple";
michael@0 10 }
michael@0 11
michael@0 12 DummyCompleter.prototype =
michael@0 13 {
michael@0 14 QueryInterface: function(iid)
michael@0 15 {
michael@0 16 if (!iid.equals(Ci.nsISupports) &&
michael@0 17 !iid.equals(Ci.nsIUrlClassifierHashCompleter)) {
michael@0 18 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 19 }
michael@0 20 return this;
michael@0 21 },
michael@0 22
michael@0 23 complete: function(partialHash, cb)
michael@0 24 {
michael@0 25 this.queries.push(partialHash);
michael@0 26 var fragments = this.fragments;
michael@0 27 var self = this;
michael@0 28 var doCallback = function() {
michael@0 29 if (self.alwaysFail) {
michael@0 30 cb.completionFinished(1);
michael@0 31 return;
michael@0 32 }
michael@0 33 var results;
michael@0 34 if (fragments[partialHash]) {
michael@0 35 for (var i = 0; i < fragments[partialHash].length; i++) {
michael@0 36 var chunkId = fragments[partialHash][i][0];
michael@0 37 var hash = fragments[partialHash][i][1];
michael@0 38 cb.completion(hash, self.tableName, chunkId);
michael@0 39 }
michael@0 40 }
michael@0 41 cb.completionFinished(0);
michael@0 42 }
michael@0 43 var timer = new Timer(0, doCallback);
michael@0 44 },
michael@0 45
michael@0 46 getHash: function(fragment)
michael@0 47 {
michael@0 48 var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
michael@0 49 createInstance(Ci.nsIScriptableUnicodeConverter);
michael@0 50 converter.charset = "UTF-8";
michael@0 51 var data = converter.convertToByteArray(fragment);
michael@0 52 var ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
michael@0 53 ch.init(ch.SHA256);
michael@0 54 ch.update(data, data.length);
michael@0 55 var hash = ch.finish(false);
michael@0 56 return hash.slice(0, 32);
michael@0 57 },
michael@0 58
michael@0 59 addFragment: function(chunkId, fragment)
michael@0 60 {
michael@0 61 this.addHash(chunkId, this.getHash(fragment));
michael@0 62 },
michael@0 63
michael@0 64 // This method allows the caller to generate complete hashes that match the
michael@0 65 // prefix of a real fragment, but have different complete hashes.
michael@0 66 addConflict: function(chunkId, fragment)
michael@0 67 {
michael@0 68 var realHash = this.getHash(fragment);
michael@0 69 var invalidHash = this.getHash("blah blah blah blah blah");
michael@0 70 this.addHash(chunkId, realHash.slice(0, 4) + invalidHash.slice(4, 32));
michael@0 71 },
michael@0 72
michael@0 73 addHash: function(chunkId, hash)
michael@0 74 {
michael@0 75 var partial = hash.slice(0, 4);
michael@0 76 if (this.fragments[partial]) {
michael@0 77 this.fragments[partial].push([chunkId, hash]);
michael@0 78 } else {
michael@0 79 this.fragments[partial] = [[chunkId, hash]];
michael@0 80 }
michael@0 81 },
michael@0 82
michael@0 83 compareQueries: function(fragments)
michael@0 84 {
michael@0 85 var expectedQueries = [];
michael@0 86 for (var i = 0; i < fragments.length; i++) {
michael@0 87 expectedQueries.push(this.getHash(fragments[i]).slice(0, 4));
michael@0 88 }
michael@0 89 do_check_eq(this.queries.length, expectedQueries.length);
michael@0 90 expectedQueries.sort();
michael@0 91 this.queries.sort();
michael@0 92 for (var i = 0; i < this.queries.length; i++) {
michael@0 93 do_check_eq(this.queries[i], expectedQueries[i]);
michael@0 94 }
michael@0 95 }
michael@0 96 };
michael@0 97
michael@0 98 function setupCompleter(table, hits, conflicts)
michael@0 99 {
michael@0 100 var completer = new DummyCompleter();
michael@0 101 completer.tableName = table;
michael@0 102 for (var i = 0; i < hits.length; i++) {
michael@0 103 var chunkId = hits[i][0];
michael@0 104 var fragments = hits[i][1];
michael@0 105 for (var j = 0; j < fragments.length; j++) {
michael@0 106 completer.addFragment(chunkId, fragments[j]);
michael@0 107 }
michael@0 108 }
michael@0 109 for (var i = 0; i < conflicts.length; i++) {
michael@0 110 var chunkId = conflicts[i][0];
michael@0 111 var fragments = conflicts[i][1];
michael@0 112 for (var j = 0; j < fragments.length; j++) {
michael@0 113 completer.addConflict(chunkId, fragments[j]);
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 dbservice.setHashCompleter(table, completer);
michael@0 118
michael@0 119 return completer;
michael@0 120 }
michael@0 121
michael@0 122 function installCompleter(table, fragments, conflictFragments)
michael@0 123 {
michael@0 124 return setupCompleter(table, fragments, conflictFragments);
michael@0 125 }
michael@0 126
michael@0 127 function installFailingCompleter(table) {
michael@0 128 var completer = setupCompleter(table, [], []);
michael@0 129 completer.alwaysFail = true;
michael@0 130 return completer;
michael@0 131 }
michael@0 132
michael@0 133 // Helper assertion for checking dummy completer queries
michael@0 134 gAssertions.completerQueried = function(data, cb)
michael@0 135 {
michael@0 136 var completer = data[0];
michael@0 137 completer.compareQueries(data[1]);
michael@0 138 cb();
michael@0 139 }
michael@0 140
michael@0 141 function doTest(updates, assertions)
michael@0 142 {
michael@0 143 doUpdateTest(updates, assertions, runNextTest, updateError);
michael@0 144 }
michael@0 145
michael@0 146 // Test an add of two partial urls to a fresh database
michael@0 147 function testPartialAdds() {
michael@0 148 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 149 var update = buildPhishingUpdate(
michael@0 150 [
michael@0 151 { "chunkNum" : 1,
michael@0 152 "urls" : addUrls
michael@0 153 }],
michael@0 154 4);
michael@0 155
michael@0 156
michael@0 157 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 158
michael@0 159 var assertions = {
michael@0 160 "tableData" : "test-phish-simple;a:1",
michael@0 161 "urlsExist" : addUrls,
michael@0 162 "completerQueried" : [completer, addUrls]
michael@0 163 };
michael@0 164
michael@0 165
michael@0 166 doTest([update], assertions);
michael@0 167 }
michael@0 168
michael@0 169 function testPartialAddsWithConflicts() {
michael@0 170 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 171 var update = buildPhishingUpdate(
michael@0 172 [
michael@0 173 { "chunkNum" : 1,
michael@0 174 "urls" : addUrls
michael@0 175 }],
michael@0 176 4);
michael@0 177
michael@0 178 // Each result will have both a real match and a conflict
michael@0 179 var completer = installCompleter('test-phish-simple',
michael@0 180 [[1, addUrls]],
michael@0 181 [[1, addUrls]]);
michael@0 182
michael@0 183 var assertions = {
michael@0 184 "tableData" : "test-phish-simple;a:1",
michael@0 185 "urlsExist" : addUrls,
michael@0 186 "completerQueried" : [completer, addUrls]
michael@0 187 };
michael@0 188
michael@0 189 doTest([update], assertions);
michael@0 190 }
michael@0 191
michael@0 192 // Test whether the fragmenting code does not cause duplicated completions
michael@0 193 function testFragments() {
michael@0 194 var addUrls = [ "foo.com/a/b/c", "foo.net/", "foo.com/c/" ];
michael@0 195 var update = buildPhishingUpdate(
michael@0 196 [
michael@0 197 { "chunkNum" : 1,
michael@0 198 "urls" : addUrls
michael@0 199 }],
michael@0 200 4);
michael@0 201
michael@0 202
michael@0 203 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 204
michael@0 205 var assertions = {
michael@0 206 "tableData" : "test-phish-simple;a:1",
michael@0 207 "urlsExist" : addUrls,
michael@0 208 "completerQueried" : [completer, addUrls]
michael@0 209 };
michael@0 210
michael@0 211
michael@0 212 doTest([update], assertions);
michael@0 213 }
michael@0 214
michael@0 215 // Test http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec
michael@0 216 // section 6.2 example 1
michael@0 217 function testSpecFragments() {
michael@0 218 var probeUrls = [ "a.b.c/1/2.html?param=1" ];
michael@0 219
michael@0 220 var addUrls = [ "a.b.c/1/2.html",
michael@0 221 "a.b.c/",
michael@0 222 "a.b.c/1/",
michael@0 223 "b.c/1/2.html?param=1",
michael@0 224 "b.c/1/2.html",
michael@0 225 "b.c/",
michael@0 226 "b.c/1/",
michael@0 227 "a.b.c/1/2.html?param=1" ];
michael@0 228
michael@0 229 var update = buildPhishingUpdate(
michael@0 230 [
michael@0 231 { "chunkNum" : 1,
michael@0 232 "urls" : addUrls
michael@0 233 }],
michael@0 234 4);
michael@0 235
michael@0 236
michael@0 237 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 238
michael@0 239 var assertions = {
michael@0 240 "tableData" : "test-phish-simple;a:1",
michael@0 241 "urlsExist" : probeUrls,
michael@0 242 "completerQueried" : [completer, addUrls]
michael@0 243 };
michael@0 244
michael@0 245 doTest([update], assertions);
michael@0 246
michael@0 247 }
michael@0 248
michael@0 249 // Test http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec
michael@0 250 // section 6.2 example 2
michael@0 251 function testMoreSpecFragments() {
michael@0 252 var probeUrls = [ "a.b.c.d.e.f.g/1.html" ];
michael@0 253
michael@0 254 var addUrls = [ "a.b.c.d.e.f.g/1.html",
michael@0 255 "a.b.c.d.e.f.g/",
michael@0 256 "c.d.e.f.g/1.html",
michael@0 257 "c.d.e.f.g/",
michael@0 258 "d.e.f.g/1.html",
michael@0 259 "d.e.f.g/",
michael@0 260 "e.f.g/1.html",
michael@0 261 "e.f.g/",
michael@0 262 "f.g/1.html",
michael@0 263 "f.g/" ];
michael@0 264
michael@0 265 var update = buildPhishingUpdate(
michael@0 266 [
michael@0 267 { "chunkNum" : 1,
michael@0 268 "urls" : addUrls
michael@0 269 }],
michael@0 270 4);
michael@0 271
michael@0 272 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 273
michael@0 274 var assertions = {
michael@0 275 "tableData" : "test-phish-simple;a:1",
michael@0 276 "urlsExist" : probeUrls,
michael@0 277 "completerQueried" : [completer, addUrls]
michael@0 278 };
michael@0 279
michael@0 280 doTest([update], assertions);
michael@0 281
michael@0 282 }
michael@0 283
michael@0 284 function testFalsePositives() {
michael@0 285 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 286 var update = buildPhishingUpdate(
michael@0 287 [
michael@0 288 { "chunkNum" : 1,
michael@0 289 "urls" : addUrls
michael@0 290 }],
michael@0 291 4);
michael@0 292
michael@0 293 // Each result will have no matching complete hashes and a non-matching
michael@0 294 // conflict
michael@0 295 var completer = installCompleter('test-phish-simple', [], [[1, addUrls]]);
michael@0 296
michael@0 297 var assertions = {
michael@0 298 "tableData" : "test-phish-simple;a:1",
michael@0 299 "urlsDontExist" : addUrls,
michael@0 300 "completerQueried" : [completer, addUrls]
michael@0 301 };
michael@0 302
michael@0 303 doTest([update], assertions);
michael@0 304 }
michael@0 305
michael@0 306 function testEmptyCompleter() {
michael@0 307 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 308 var update = buildPhishingUpdate(
michael@0 309 [
michael@0 310 { "chunkNum" : 1,
michael@0 311 "urls" : addUrls
michael@0 312 }],
michael@0 313 4);
michael@0 314
michael@0 315 // Completer will never return full hashes
michael@0 316 var completer = installCompleter('test-phish-simple', [], []);
michael@0 317
michael@0 318 var assertions = {
michael@0 319 "tableData" : "test-phish-simple;a:1",
michael@0 320 "urlsDontExist" : addUrls,
michael@0 321 "completerQueried" : [completer, addUrls]
michael@0 322 };
michael@0 323
michael@0 324 doTest([update], assertions);
michael@0 325 }
michael@0 326
michael@0 327 function testCompleterFailure() {
michael@0 328 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 329 var update = buildPhishingUpdate(
michael@0 330 [
michael@0 331 { "chunkNum" : 1,
michael@0 332 "urls" : addUrls
michael@0 333 }],
michael@0 334 4);
michael@0 335
michael@0 336 // Completer will never return full hashes
michael@0 337 var completer = installFailingCompleter('test-phish-simple');
michael@0 338
michael@0 339 var assertions = {
michael@0 340 "tableData" : "test-phish-simple;a:1",
michael@0 341 "urlsDontExist" : addUrls,
michael@0 342 "completerQueried" : [completer, addUrls]
michael@0 343 };
michael@0 344
michael@0 345 doTest([update], assertions);
michael@0 346 }
michael@0 347
michael@0 348 function testMixedSizesSameDomain() {
michael@0 349 var add1Urls = [ "foo.com/a" ];
michael@0 350 var add2Urls = [ "foo.com/b" ];
michael@0 351
michael@0 352 var update1 = buildPhishingUpdate(
michael@0 353 [
michael@0 354 { "chunkNum" : 1,
michael@0 355 "urls" : add1Urls }],
michael@0 356 4);
michael@0 357 var update2 = buildPhishingUpdate(
michael@0 358 [
michael@0 359 { "chunkNum" : 2,
michael@0 360 "urls" : add2Urls }],
michael@0 361 32);
michael@0 362
michael@0 363 // We should only need to complete the partial hashes
michael@0 364 var completer = installCompleter('test-phish-simple', [[1, add1Urls]], []);
michael@0 365
michael@0 366 var assertions = {
michael@0 367 "tableData" : "test-phish-simple;a:1-2",
michael@0 368 // both urls should match...
michael@0 369 "urlsExist" : add1Urls.concat(add2Urls),
michael@0 370 // ... but the completer should only be queried for the partial entry
michael@0 371 "completerQueried" : [completer, add1Urls]
michael@0 372 };
michael@0 373
michael@0 374 doTest([update1, update2], assertions);
michael@0 375 }
michael@0 376
michael@0 377 function testMixedSizesDifferentDomains() {
michael@0 378 var add1Urls = [ "foo.com/a" ];
michael@0 379 var add2Urls = [ "bar.com/b" ];
michael@0 380
michael@0 381 var update1 = buildPhishingUpdate(
michael@0 382 [
michael@0 383 { "chunkNum" : 1,
michael@0 384 "urls" : add1Urls }],
michael@0 385 4);
michael@0 386 var update2 = buildPhishingUpdate(
michael@0 387 [
michael@0 388 { "chunkNum" : 2,
michael@0 389 "urls" : add2Urls }],
michael@0 390 32);
michael@0 391
michael@0 392 // We should only need to complete the partial hashes
michael@0 393 var completer = installCompleter('test-phish-simple', [[1, add1Urls]], []);
michael@0 394
michael@0 395 var assertions = {
michael@0 396 "tableData" : "test-phish-simple;a:1-2",
michael@0 397 // both urls should match...
michael@0 398 "urlsExist" : add1Urls.concat(add2Urls),
michael@0 399 // ... but the completer should only be queried for the partial entry
michael@0 400 "completerQueried" : [completer, add1Urls]
michael@0 401 };
michael@0 402
michael@0 403 doTest([update1, update2], assertions);
michael@0 404 }
michael@0 405
michael@0 406 function testInvalidHashSize()
michael@0 407 {
michael@0 408 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 409 var update = buildPhishingUpdate(
michael@0 410 [
michael@0 411 { "chunkNum" : 1,
michael@0 412 "urls" : addUrls
michael@0 413 }],
michael@0 414 12); // only 4 and 32 are legal hash sizes
michael@0 415
michael@0 416 var addUrls2 = [ "zaz.com/a", "xyz.com/b" ];
michael@0 417 var update2 = buildPhishingUpdate(
michael@0 418 [
michael@0 419 { "chunkNum" : 2,
michael@0 420 "urls" : addUrls2
michael@0 421 }],
michael@0 422 4);
michael@0 423
michael@0 424 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 425
michael@0 426 var assertions = {
michael@0 427 "tableData" : "test-phish-simple;a:2",
michael@0 428 "urlsDontExist" : addUrls
michael@0 429 };
michael@0 430
michael@0 431 // A successful update will trigger an error
michael@0 432 doUpdateTest([update2, update], assertions, updateError, runNextTest);
michael@0 433 }
michael@0 434
michael@0 435 function testWrongTable()
michael@0 436 {
michael@0 437 var addUrls = [ "foo.com/a" ];
michael@0 438 var update = buildPhishingUpdate(
michael@0 439 [
michael@0 440 { "chunkNum" : 1,
michael@0 441 "urls" : addUrls
michael@0 442 }],
michael@0 443 4);
michael@0 444 var completer = installCompleter('test-malware-simple', // wrong table
michael@0 445 [[1, addUrls]], []);
michael@0 446
michael@0 447 // The above installCompleter installs the completer for test-malware-simple,
michael@0 448 // we want it to be used for test-phish-simple too.
michael@0 449 dbservice.setHashCompleter("test-phish-simple", completer);
michael@0 450
michael@0 451
michael@0 452 var assertions = {
michael@0 453 "tableData" : "test-phish-simple;a:1",
michael@0 454 // The urls were added as phishing urls, but the completer is claiming
michael@0 455 // that they are malware urls, and we trust the completer in this case.
michael@0 456 // The result will be discarded, so we can only check for non-existence.
michael@0 457 "urlsDontExist" : addUrls,
michael@0 458 // Make sure the completer was actually queried.
michael@0 459 "completerQueried" : [completer, addUrls]
michael@0 460 };
michael@0 461
michael@0 462 doUpdateTest([update], assertions,
michael@0 463 function() {
michael@0 464 // Give the dbservice a chance to (not) cache the result.
michael@0 465 var timer = new Timer(3000, function() {
michael@0 466 // The miss earlier will have caused a miss to be cached.
michael@0 467 // Resetting the completer does not count as an update,
michael@0 468 // so we will not be probed again.
michael@0 469 var newCompleter = installCompleter('test-malware-simple', [[1, addUrls]], []); dbservice.setHashCompleter("test-phish-simple",
michael@0 470 newCompleter);
michael@0 471
michael@0 472 var assertions = {
michael@0 473 "urlsDontExist" : addUrls
michael@0 474 };
michael@0 475 checkAssertions(assertions, runNextTest);
michael@0 476 });
michael@0 477 }, updateError);
michael@0 478 }
michael@0 479
michael@0 480 function setupCachedResults(addUrls, part2)
michael@0 481 {
michael@0 482 var update = buildPhishingUpdate(
michael@0 483 [
michael@0 484 { "chunkNum" : 1,
michael@0 485 "urls" : addUrls
michael@0 486 }],
michael@0 487 4);
michael@0 488
michael@0 489 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 490
michael@0 491 var assertions = {
michael@0 492 "tableData" : "test-phish-simple;a:1",
michael@0 493 // Request the add url. This should cause the completion to be cached.
michael@0 494 "urlsExist" : addUrls,
michael@0 495 // Make sure the completer was actually queried.
michael@0 496 "completerQueried" : [completer, addUrls]
michael@0 497 };
michael@0 498
michael@0 499 doUpdateTest([update], assertions,
michael@0 500 function() {
michael@0 501 // Give the dbservice a chance to cache the result.
michael@0 502 var timer = new Timer(3000, part2);
michael@0 503 }, updateError);
michael@0 504 }
michael@0 505
michael@0 506 function testCachedResults()
michael@0 507 {
michael@0 508 setupCachedResults(["foo.com/a"], function(add) {
michael@0 509 // This is called after setupCachedResults(). Verify that
michael@0 510 // checking the url again does not cause a completer request.
michael@0 511
michael@0 512 // install a new completer, this one should never be queried.
michael@0 513 var newCompleter = installCompleter('test-phish-simple', [[1, []]], []);
michael@0 514
michael@0 515 var assertions = {
michael@0 516 "urlsExist" : ["foo.com/a"],
michael@0 517 "completerQueried" : [newCompleter, []]
michael@0 518 };
michael@0 519 checkAssertions(assertions, runNextTest);
michael@0 520 });
michael@0 521 }
michael@0 522
michael@0 523 function testCachedResultsWithSub() {
michael@0 524 setupCachedResults(["foo.com/a"], function() {
michael@0 525 // install a new completer, this one should never be queried.
michael@0 526 var newCompleter = installCompleter('test-phish-simple', [[1, []]], []);
michael@0 527
michael@0 528 var removeUpdate = buildPhishingUpdate(
michael@0 529 [ { "chunkNum" : 2,
michael@0 530 "chunkType" : "s",
michael@0 531 "urls": ["1:foo.com/a"] }],
michael@0 532 4);
michael@0 533
michael@0 534 var assertions = {
michael@0 535 "urlsDontExist" : ["foo.com/a"],
michael@0 536 "completerQueried" : [newCompleter, []]
michael@0 537 }
michael@0 538
michael@0 539 doTest([removeUpdate], assertions);
michael@0 540 });
michael@0 541 }
michael@0 542
michael@0 543 function testCachedResultsWithExpire() {
michael@0 544 setupCachedResults(["foo.com/a"], function() {
michael@0 545 // install a new completer, this one should never be queried.
michael@0 546 var newCompleter = installCompleter('test-phish-simple', [[1, []]], []);
michael@0 547
michael@0 548 var expireUpdate =
michael@0 549 "n:1000\n" +
michael@0 550 "i:test-phish-simple\n" +
michael@0 551 "ad:1\n";
michael@0 552
michael@0 553 var assertions = {
michael@0 554 "urlsDontExist" : ["foo.com/a"],
michael@0 555 "completerQueried" : [newCompleter, []]
michael@0 556 }
michael@0 557 doTest([expireUpdate], assertions);
michael@0 558 });
michael@0 559 }
michael@0 560
michael@0 561 function testCachedResultsUpdate()
michael@0 562 {
michael@0 563 var existUrls = ["foo.com/a"];
michael@0 564 setupCachedResults(existUrls, function() {
michael@0 565 // This is called after setupCachedResults(). Verify that
michael@0 566 // checking the url again does not cause a completer request.
michael@0 567
michael@0 568 // install a new completer, this one should never be queried.
michael@0 569 var newCompleter = installCompleter('test-phish-simple', [[1, []]], []);
michael@0 570
michael@0 571 var assertions = {
michael@0 572 "urlsExist" : existUrls,
michael@0 573 "completerQueried" : [newCompleter, []]
michael@0 574 };
michael@0 575
michael@0 576 var addUrls = ["foobar.org/a"];
michael@0 577
michael@0 578 var update2 = buildPhishingUpdate(
michael@0 579 [
michael@0 580 { "chunkNum" : 2,
michael@0 581 "urls" : addUrls
michael@0 582 }],
michael@0 583 4);
michael@0 584
michael@0 585 checkAssertions(assertions, function () {
michael@0 586 // Apply the update. The cached completes should be gone.
michael@0 587 doStreamUpdate(update2, function() {
michael@0 588 // Now the completer gets queried again.
michael@0 589 var newCompleter2 = installCompleter('test-phish-simple', [[1, existUrls]], []);
michael@0 590 var assertions2 = {
michael@0 591 "tableData" : "test-phish-simple;a:1-2",
michael@0 592 "urlsExist" : existUrls,
michael@0 593 "completerQueried" : [newCompleter2, existUrls]
michael@0 594 };
michael@0 595 checkAssertions(assertions2, runNextTest);
michael@0 596 }, updateError);
michael@0 597 });
michael@0 598 });
michael@0 599 }
michael@0 600
michael@0 601 function testCachedResultsFailure()
michael@0 602 {
michael@0 603 var existUrls = ["foo.com/a"];
michael@0 604 setupCachedResults(existUrls, function() {
michael@0 605 // This is called after setupCachedResults(). Verify that
michael@0 606 // checking the url again does not cause a completer request.
michael@0 607
michael@0 608 // install a new completer, this one should never be queried.
michael@0 609 var newCompleter = installCompleter('test-phish-simple', [[1, []]], []);
michael@0 610
michael@0 611 var assertions = {
michael@0 612 "urlsExist" : existUrls,
michael@0 613 "completerQueried" : [newCompleter, []]
michael@0 614 };
michael@0 615
michael@0 616 var addUrls = ["foobar.org/a"];
michael@0 617
michael@0 618 var update2 = buildPhishingUpdate(
michael@0 619 [
michael@0 620 { "chunkNum" : 2,
michael@0 621 "urls" : addUrls
michael@0 622 }],
michael@0 623 4);
michael@0 624
michael@0 625 checkAssertions(assertions, function() {
michael@0 626 // Apply the update. The cached completes should be gone.
michael@0 627 doErrorUpdate("test-phish-simple,test-malware-simple", function() {
michael@0 628 // Now the completer gets queried again.
michael@0 629 var newCompleter2 = installCompleter('test-phish-simple', [[1, existUrls]], []);
michael@0 630 var assertions2 = {
michael@0 631 "tableData" : "test-phish-simple;a:1",
michael@0 632 "urlsExist" : existUrls,
michael@0 633 "completerQueried" : [newCompleter2, existUrls]
michael@0 634 };
michael@0 635 checkAssertions(assertions2, runNextTest);
michael@0 636 }, updateError);
michael@0 637 });
michael@0 638 });
michael@0 639 }
michael@0 640
michael@0 641 function testErrorList()
michael@0 642 {
michael@0 643 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 644 var update = buildPhishingUpdate(
michael@0 645 [
michael@0 646 { "chunkNum" : 1,
michael@0 647 "urls" : addUrls
michael@0 648 }],
michael@0 649 4);
michael@0 650 // The update failure should will kill the completes, so the above
michael@0 651 // must be a prefix to get any hit at all past the update failure.
michael@0 652
michael@0 653 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 654
michael@0 655 var assertions = {
michael@0 656 "tableData" : "test-phish-simple;a:1",
michael@0 657 "urlsExist" : addUrls,
michael@0 658 // These are complete urls, and will only be completed if the
michael@0 659 // list is stale.
michael@0 660 "completerQueried" : [completer, addUrls]
michael@0 661 };
michael@0 662
michael@0 663 // Apply the update.
michael@0 664 doStreamUpdate(update, function() {
michael@0 665 // Now the test-phish-simple and test-malware-simple tables are marked
michael@0 666 // as fresh. Fake an update failure to mark them stale.
michael@0 667 doErrorUpdate("test-phish-simple,test-malware-simple", function() {
michael@0 668 // Now the lists should be marked stale. Check assertions.
michael@0 669 checkAssertions(assertions, runNextTest);
michael@0 670 }, updateError);
michael@0 671 }, updateError);
michael@0 672 }
michael@0 673
michael@0 674
michael@0 675 function testStaleList()
michael@0 676 {
michael@0 677 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 678 var update = buildPhishingUpdate(
michael@0 679 [
michael@0 680 { "chunkNum" : 1,
michael@0 681 "urls" : addUrls
michael@0 682 }],
michael@0 683 32);
michael@0 684
michael@0 685 var completer = installCompleter('test-phish-simple', [[1, addUrls]], []);
michael@0 686
michael@0 687 var assertions = {
michael@0 688 "tableData" : "test-phish-simple;a:1",
michael@0 689 "urlsExist" : addUrls,
michael@0 690 // These are complete urls, and will only be completed if the
michael@0 691 // list is stale.
michael@0 692 "completerQueried" : [completer, addUrls]
michael@0 693 };
michael@0 694
michael@0 695 // Consider a match stale after one second.
michael@0 696 prefBranch.setIntPref("urlclassifier.max-complete-age", 1);
michael@0 697
michael@0 698 // Apply the update.
michael@0 699 doStreamUpdate(update, function() {
michael@0 700 // Now the test-phish-simple and test-malware-simple tables are marked
michael@0 701 // as fresh. Wait three seconds to make sure the list is marked stale.
michael@0 702 new Timer(3000, function() {
michael@0 703 // Now the lists should be marked stale. Check assertions.
michael@0 704 checkAssertions(assertions, function() {
michael@0 705 prefBranch.setIntPref("urlclassifier.max-complete-age", 2700);
michael@0 706 runNextTest();
michael@0 707 });
michael@0 708 }, updateError);
michael@0 709 }, updateError);
michael@0 710 }
michael@0 711
michael@0 712 // Same as testStaleList, but verifies that an empty response still
michael@0 713 // unconfirms the entry.
michael@0 714 function testStaleListEmpty()
michael@0 715 {
michael@0 716 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 717 var update = buildPhishingUpdate(
michael@0 718 [
michael@0 719 { "chunkNum" : 1,
michael@0 720 "urls" : addUrls
michael@0 721 }],
michael@0 722 32);
michael@0 723
michael@0 724 var completer = installCompleter('test-phish-simple', [], []);
michael@0 725
michael@0 726 var assertions = {
michael@0 727 "tableData" : "test-phish-simple;a:1",
michael@0 728 // None of these should match, because they won't be completed
michael@0 729 "urlsDontExist" : addUrls,
michael@0 730 // These are complete urls, and will only be completed if the
michael@0 731 // list is stale.
michael@0 732 "completerQueried" : [completer, addUrls]
michael@0 733 };
michael@0 734
michael@0 735 // Consider a match stale after one second.
michael@0 736 prefBranch.setIntPref("urlclassifier.max-complete-age", 1);
michael@0 737
michael@0 738 // Apply the update.
michael@0 739 doStreamUpdate(update, function() {
michael@0 740 // Now the test-phish-simple and test-malware-simple tables are marked
michael@0 741 // as fresh. Wait three seconds to make sure the list is marked stale.
michael@0 742 new Timer(3000, function() {
michael@0 743 // Now the lists should be marked stale. Check assertions.
michael@0 744 checkAssertions(assertions, function() {
michael@0 745 prefBranch.setIntPref("urlclassifier.max-complete-age", 2700);
michael@0 746 runNextTest();
michael@0 747 });
michael@0 748 }, updateError);
michael@0 749 }, updateError);
michael@0 750 }
michael@0 751
michael@0 752
michael@0 753 // Verify that different lists (test-phish-simple,
michael@0 754 // test-malware-simple) maintain their freshness separately.
michael@0 755 function testErrorListIndependent()
michael@0 756 {
michael@0 757 var phishUrls = [ "phish.com/a" ];
michael@0 758 var malwareUrls = [ "attack.com/a" ];
michael@0 759 var update = buildPhishingUpdate(
michael@0 760 [
michael@0 761 { "chunkNum" : 1,
michael@0 762 "urls" : phishUrls
michael@0 763 }],
michael@0 764 4);
michael@0 765 // These have to persist past the update failure, so they must be prefixes,
michael@0 766 // not completes.
michael@0 767
michael@0 768 update += buildMalwareUpdate(
michael@0 769 [
michael@0 770 { "chunkNum" : 2,
michael@0 771 "urls" : malwareUrls
michael@0 772 }],
michael@0 773 32);
michael@0 774
michael@0 775 var completer = installCompleter('test-phish-simple', [[1, phishUrls]], []);
michael@0 776
michael@0 777 var assertions = {
michael@0 778 "tableData" : "test-malware-simple;a:2\ntest-phish-simple;a:1",
michael@0 779 "urlsExist" : phishUrls,
michael@0 780 "malwareUrlsExist" : malwareUrls,
michael@0 781 // Only this phishing urls should be completed, because only the phishing
michael@0 782 // urls will be stale.
michael@0 783 "completerQueried" : [completer, phishUrls]
michael@0 784 };
michael@0 785
michael@0 786 // Apply the update.
michael@0 787 doStreamUpdate(update, function() {
michael@0 788 // Now the test-phish-simple and test-malware-simple tables are
michael@0 789 // marked as fresh. Fake an update failure to mark *just*
michael@0 790 // phishing data as stale.
michael@0 791 doErrorUpdate("test-phish-simple", function() {
michael@0 792 // Now the lists should be marked stale. Check assertions.
michael@0 793 checkAssertions(assertions, runNextTest);
michael@0 794 }, updateError);
michael@0 795 }, updateError);
michael@0 796 }
michael@0 797
michael@0 798 function run_test()
michael@0 799 {
michael@0 800 runTests([
michael@0 801 testPartialAdds,
michael@0 802 testPartialAddsWithConflicts,
michael@0 803 testFragments,
michael@0 804 testSpecFragments,
michael@0 805 testMoreSpecFragments,
michael@0 806 testFalsePositives,
michael@0 807 testEmptyCompleter,
michael@0 808 testCompleterFailure,
michael@0 809 testMixedSizesSameDomain,
michael@0 810 testMixedSizesDifferentDomains,
michael@0 811 testInvalidHashSize,
michael@0 812 testWrongTable,
michael@0 813 testCachedResults,
michael@0 814 testCachedResultsWithSub,
michael@0 815 testCachedResultsWithExpire,
michael@0 816 testCachedResultsUpdate,
michael@0 817 testCachedResultsFailure,
michael@0 818 testStaleList,
michael@0 819 testStaleListEmpty,
michael@0 820 testErrorList,
michael@0 821 testErrorListIndependent
michael@0 822 ]);
michael@0 823 }
michael@0 824
michael@0 825 do_test_pending();

mercurial