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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 function doTest(updates, assertions)
michael@0 3 {
michael@0 4 doUpdateTest(updates, assertions, runNextTest, updateError);
michael@0 5 }
michael@0 6
michael@0 7 // Test an add of two urls to a fresh database
michael@0 8 function testSimpleAdds() {
michael@0 9 var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ];
michael@0 10 var update = buildPhishingUpdate(
michael@0 11 [
michael@0 12 { "chunkNum" : 1,
michael@0 13 "urls" : addUrls
michael@0 14 }]);
michael@0 15
michael@0 16 var assertions = {
michael@0 17 "tableData" : "test-phish-simple;a:1",
michael@0 18 "urlsExist" : addUrls
michael@0 19 };
michael@0 20
michael@0 21 doTest([update], assertions);
michael@0 22 }
michael@0 23
michael@0 24 // Same as testSimpleAdds, but make the same-domain URLs come from different
michael@0 25 // chunks.
michael@0 26 function testMultipleAdds() {
michael@0 27 var add1Urls = [ "foo.com/a", "bar.com/c" ];
michael@0 28 var add2Urls = [ "foo.com/b" ];
michael@0 29
michael@0 30 var update = buildPhishingUpdate(
michael@0 31 [{ "chunkNum" : 1,
michael@0 32 "urls" : add1Urls },
michael@0 33 { "chunkNum" : 2,
michael@0 34 "urls" : add2Urls }]);
michael@0 35 var assertions = {
michael@0 36 "tableData" : "test-phish-simple;a:1-2",
michael@0 37 "urlsExist" : add1Urls.concat(add2Urls)
michael@0 38 };
michael@0 39
michael@0 40 doTest([update], assertions);
michael@0 41 }
michael@0 42
michael@0 43 // Test that a sub will remove an existing add
michael@0 44 function testSimpleSub()
michael@0 45 {
michael@0 46 var addUrls = ["foo.com/a", "bar.com/b"];
michael@0 47 var subUrls = ["1:foo.com/a"];
michael@0 48
michael@0 49 var addUpdate = buildPhishingUpdate(
michael@0 50 [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space
michael@0 51 "urls": addUrls }]);
michael@0 52
michael@0 53 var subUpdate = buildPhishingUpdate(
michael@0 54 [{ "chunkNum" : 50,
michael@0 55 "chunkType" : "s",
michael@0 56 "urls": subUrls }]);
michael@0 57
michael@0 58
michael@0 59 var assertions = {
michael@0 60 "tableData" : "test-phish-simple;a:1:s:50",
michael@0 61 "urlsExist" : [ "bar.com/b" ],
michael@0 62 "urlsDontExist": ["foo.com/a" ],
michael@0 63 "subsDontExist" : [ "foo.com/a" ]
michael@0 64 }
michael@0 65
michael@0 66 doTest([addUpdate, subUpdate], assertions);
michael@0 67
michael@0 68 }
michael@0 69
michael@0 70 // Same as testSimpleSub(), but the sub comes in before the add.
michael@0 71 function testSubEmptiesAdd()
michael@0 72 {
michael@0 73 var subUrls = ["1:foo.com/a"];
michael@0 74 var addUrls = ["foo.com/a", "bar.com/b"];
michael@0 75
michael@0 76 var subUpdate = buildPhishingUpdate(
michael@0 77 [{ "chunkNum" : 50,
michael@0 78 "chunkType" : "s",
michael@0 79 "urls": subUrls }]);
michael@0 80
michael@0 81 var addUpdate = buildPhishingUpdate(
michael@0 82 [{ "chunkNum" : 1,
michael@0 83 "urls": addUrls }]);
michael@0 84
michael@0 85 var assertions = {
michael@0 86 "tableData" : "test-phish-simple;a:1:s:50",
michael@0 87 "urlsExist" : [ "bar.com/b" ],
michael@0 88 "urlsDontExist": ["foo.com/a" ],
michael@0 89 "subsDontExist" : [ "foo.com/a" ] // this sub was found, it shouldn't exist anymore
michael@0 90 }
michael@0 91
michael@0 92 doTest([subUpdate, addUpdate], assertions);
michael@0 93 }
michael@0 94
michael@0 95 // Very similar to testSubEmptiesAdd, except that the domain entry will
michael@0 96 // still have an item left over that needs to be synced.
michael@0 97 function testSubPartiallyEmptiesAdd()
michael@0 98 {
michael@0 99 var subUrls = ["1:foo.com/a"];
michael@0 100 var addUrls = ["foo.com/a", "foo.com/b", "bar.com/b"];
michael@0 101
michael@0 102 var subUpdate = buildPhishingUpdate(
michael@0 103 [{ "chunkNum" : 1,
michael@0 104 "chunkType" : "s",
michael@0 105 "urls": subUrls }]);
michael@0 106
michael@0 107 var addUpdate = buildPhishingUpdate(
michael@0 108 [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space
michael@0 109 "urls": addUrls }]);
michael@0 110
michael@0 111 var assertions = {
michael@0 112 "tableData" : "test-phish-simple;a:1:s:1",
michael@0 113 "urlsExist" : [ "foo.com/b", "bar.com/b" ],
michael@0 114 "urlsDontExist" : ["foo.com/a" ],
michael@0 115 "subsDontExist" : [ "foo.com/a" ] // this sub was found, it shouldn't exist anymore
michael@0 116 }
michael@0 117
michael@0 118 doTest([subUpdate, addUpdate], assertions);
michael@0 119 }
michael@0 120
michael@0 121 // We SHOULD be testing that pending subs are removed using
michael@0 122 // subsDontExist assertions. Since we don't have a good interface for getting
michael@0 123 // at sub entries, we'll verify it by side-effect. Subbing a url once
michael@0 124 // then adding it twice should leave the url intact.
michael@0 125 function testPendingSubRemoved()
michael@0 126 {
michael@0 127 var subUrls = ["1:foo.com/a", "2:foo.com/b"];
michael@0 128 var addUrls = ["foo.com/a", "foo.com/b"];
michael@0 129
michael@0 130 var subUpdate = buildPhishingUpdate(
michael@0 131 [{ "chunkNum" : 1,
michael@0 132 "chunkType" : "s",
michael@0 133 "urls": subUrls }]);
michael@0 134
michael@0 135 var addUpdate1 = buildPhishingUpdate(
michael@0 136 [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space
michael@0 137 "urls": addUrls }]);
michael@0 138
michael@0 139 var addUpdate2 = buildPhishingUpdate(
michael@0 140 [{ "chunkNum" : 2,
michael@0 141 "urls": addUrls }]);
michael@0 142
michael@0 143 var assertions = {
michael@0 144 "tableData" : "test-phish-simple;a:1-2:s:1",
michael@0 145 "urlsExist" : [ "foo.com/a", "foo.com/b" ],
michael@0 146 "subsDontExist" : [ "foo.com/a", "foo.com/b" ] // this sub was found, it shouldn't exist anymore
michael@0 147 }
michael@0 148
michael@0 149 doTest([subUpdate, addUpdate1, addUpdate2], assertions);
michael@0 150 }
michael@0 151
michael@0 152 // Make sure that a saved sub is removed when the sub chunk is expired.
michael@0 153 function testPendingSubExpire()
michael@0 154 {
michael@0 155 var subUrls = ["1:foo.com/a", "1:foo.com/b"];
michael@0 156 var addUrls = ["foo.com/a", "foo.com/b"];
michael@0 157
michael@0 158 var subUpdate = buildPhishingUpdate(
michael@0 159 [{ "chunkNum" : 1,
michael@0 160 "chunkType" : "s",
michael@0 161 "urls": subUrls }]);
michael@0 162
michael@0 163 var expireUpdate = buildPhishingUpdate(
michael@0 164 [{ "chunkNum" : 1,
michael@0 165 "chunkType" : "sd" }]);
michael@0 166
michael@0 167 var addUpdate = buildPhishingUpdate(
michael@0 168 [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space
michael@0 169 "urls": addUrls }]);
michael@0 170
michael@0 171 var assertions = {
michael@0 172 "tableData" : "test-phish-simple;a:1",
michael@0 173 "urlsExist" : [ "foo.com/a", "foo.com/b" ],
michael@0 174 "subsDontExist" : [ "foo.com/a", "foo.com/b" ] // this sub was expired
michael@0 175 }
michael@0 176
michael@0 177 doTest([subUpdate, expireUpdate, addUpdate], assertions);
michael@0 178 }
michael@0 179
michael@0 180 // Make sure that the sub url removes from only the chunk that it specifies
michael@0 181 function testDuplicateAdds()
michael@0 182 {
michael@0 183 var urls = ["foo.com/a"];
michael@0 184
michael@0 185 var addUpdate1 = buildPhishingUpdate(
michael@0 186 [{ "chunkNum" : 1,
michael@0 187 "urls": urls }]);
michael@0 188 var addUpdate2 = buildPhishingUpdate(
michael@0 189 [{ "chunkNum" : 2,
michael@0 190 "urls": urls }]);
michael@0 191 var subUpdate = buildPhishingUpdate(
michael@0 192 [{ "chunkNum" : 3,
michael@0 193 "chunkType" : "s",
michael@0 194 "urls": ["2:foo.com/a"]}]);
michael@0 195
michael@0 196 var assertions = {
michael@0 197 "tableData" : "test-phish-simple;a:1-2:s:3",
michael@0 198 "urlsExist" : [ "foo.com/a"],
michael@0 199 "subsDontExist" : [ "foo.com/a"]
michael@0 200 }
michael@0 201
michael@0 202 doTest([addUpdate1, addUpdate2, subUpdate], assertions);
michael@0 203 }
michael@0 204
michael@0 205 // Tests a sub which matches some existing adds but leaves others.
michael@0 206 function testSubPartiallyMatches()
michael@0 207 {
michael@0 208 var subUrls = ["foo.com/a"];
michael@0 209 var addUrls = ["1:foo.com/a", "2:foo.com/b"];
michael@0 210
michael@0 211 var addUpdate = buildPhishingUpdate(
michael@0 212 [{ "chunkNum" : 1,
michael@0 213 "urls" : addUrls }]);
michael@0 214
michael@0 215 var subUpdate = buildPhishingUpdate(
michael@0 216 [{ "chunkNum" : 1,
michael@0 217 "chunkType" : "s",
michael@0 218 "urls" : addUrls }]);
michael@0 219
michael@0 220 var assertions = {
michael@0 221 "tableData" : "test-phish-simple;a:1:s:1",
michael@0 222 "urlsDontExist" : ["foo.com/a"],
michael@0 223 "subsDontExist" : ["foo.com/a"],
michael@0 224 "subsExist" : ["foo.com/b"]
michael@0 225 };
michael@0 226
michael@0 227 doTest([addUpdate, subUpdate], assertions);
michael@0 228 }
michael@0 229
michael@0 230 // XXX: because subsExist isn't actually implemented, this is the same
michael@0 231 // test as above but with a second add chunk that should fail to be added
michael@0 232 // because of a pending sub chunk.
michael@0 233 function testSubPartiallyMatches2()
michael@0 234 {
michael@0 235 var addUrls = ["foo.com/a"];
michael@0 236 var subUrls = ["1:foo.com/a", "2:foo.com/b"];
michael@0 237 var addUrls2 = ["foo.com/b"];
michael@0 238
michael@0 239 var addUpdate = buildPhishingUpdate(
michael@0 240 [{ "chunkNum" : 1,
michael@0 241 "urls" : addUrls }]);
michael@0 242
michael@0 243 var subUpdate = buildPhishingUpdate(
michael@0 244 [{ "chunkNum" : 1,
michael@0 245 "chunkType" : "s",
michael@0 246 "urls" : subUrls }]);
michael@0 247
michael@0 248 var addUpdate2 = buildPhishingUpdate(
michael@0 249 [{ "chunkNum" : 2,
michael@0 250 "urls" : addUrls2 }]);
michael@0 251
michael@0 252 var assertions = {
michael@0 253 "tableData" : "test-phish-simple;a:1-2:s:1",
michael@0 254 "urlsDontExist" : ["foo.com/a", "foo.com/b"],
michael@0 255 "subsDontExist" : ["foo.com/a", "foo.com/b"]
michael@0 256 };
michael@0 257
michael@0 258 doTest([addUpdate, subUpdate, addUpdate2], assertions);
michael@0 259 }
michael@0 260
michael@0 261 // Verify that two subs for the same domain but from different chunks
michael@0 262 // match (tests that existing sub entries are properly updated)
michael@0 263 function testSubsDifferentChunks() {
michael@0 264 var subUrls1 = [ "3:foo.com/a" ];
michael@0 265 var subUrls2 = [ "3:foo.com/b" ];
michael@0 266
michael@0 267 var addUrls = [ "foo.com/a", "foo.com/b", "foo.com/c" ];
michael@0 268
michael@0 269 var subUpdate1 = buildPhishingUpdate(
michael@0 270 [{ "chunkNum" : 1,
michael@0 271 "chunkType" : "s",
michael@0 272 "urls": subUrls1 }]);
michael@0 273 var subUpdate2 = buildPhishingUpdate(
michael@0 274 [{ "chunkNum" : 2,
michael@0 275 "chunkType" : "s",
michael@0 276 "urls" : subUrls2 }]);
michael@0 277 var addUpdate = buildPhishingUpdate(
michael@0 278 [{ "chunkNum" : 3,
michael@0 279 "urls" : addUrls }]);
michael@0 280
michael@0 281 var assertions = {
michael@0 282 "tableData" : "test-phish-simple;a:3:s:1-2",
michael@0 283 "urlsExist" : [ "foo.com/c" ],
michael@0 284 "urlsDontExist" : [ "foo.com/a", "foo.com/b" ],
michael@0 285 "subsDontExist" : [ "foo.com/a", "foo.com/b" ]
michael@0 286 };
michael@0 287
michael@0 288 doTest([subUpdate1, subUpdate2, addUpdate], assertions);
michael@0 289 }
michael@0 290
michael@0 291 // for bug 534079
michael@0 292 function testSubsDifferentChunksSameHostId() {
michael@0 293 var subUrls1 = [ "1:foo.com/a" ];
michael@0 294 var subUrls2 = [ "1:foo.com/b", "2:foo.com/c" ];
michael@0 295
michael@0 296 var addUrls = [ "foo.com/a", "foo.com/b" ];
michael@0 297 var addUrls2 = [ "foo.com/c" ];
michael@0 298
michael@0 299 var subUpdate1 = buildPhishingUpdate(
michael@0 300 [{ "chunkNum" : 1,
michael@0 301 "chunkType" : "s",
michael@0 302 "urls": subUrls1 }]);
michael@0 303 var subUpdate2 = buildPhishingUpdate(
michael@0 304 [{ "chunkNum" : 2,
michael@0 305 "chunkType" : "s",
michael@0 306 "urls" : subUrls2 }]);
michael@0 307
michael@0 308 var addUpdate = buildPhishingUpdate(
michael@0 309 [{ "chunkNum" : 1,
michael@0 310 "urls" : addUrls }]);
michael@0 311 var addUpdate2 = buildPhishingUpdate(
michael@0 312 [{ "chunkNum" : 2,
michael@0 313 "urls" : addUrls2 }]);
michael@0 314
michael@0 315 var assertions = {
michael@0 316 "tableData" : "test-phish-simple;a:1-2:s:1-2",
michael@0 317 "urlsDontExist" : [ "foo.com/c", "foo.com/b", "foo.com/a", ],
michael@0 318 };
michael@0 319
michael@0 320 doTest([addUpdate, addUpdate2, subUpdate1, subUpdate2], assertions);
michael@0 321 }
michael@0 322
michael@0 323 // Test lists of expired chunks
michael@0 324 function testExpireLists() {
michael@0 325 var addUpdate = buildPhishingUpdate(
michael@0 326 [
michael@0 327 { "chunkNum" : 1,
michael@0 328 "urls" : [ "foo.com/a" ]
michael@0 329 },
michael@0 330 { "chunkNum" : 3,
michael@0 331 "urls" : [ "bar.com/a" ]
michael@0 332 },
michael@0 333 { "chunkNum" : 4,
michael@0 334 "urls" : [ "baz.com/a" ]
michael@0 335 },
michael@0 336 { "chunkNum" : 5,
michael@0 337 "urls" : [ "blah.com/a" ]
michael@0 338 },
michael@0 339 ]);
michael@0 340 var subUpdate = buildPhishingUpdate(
michael@0 341 [
michael@0 342 { "chunkNum" : 1,
michael@0 343 "chunkType" : "s",
michael@0 344 "urls" : [ "50:foo.com/1" ]
michael@0 345 },
michael@0 346 { "chunkNum" : 2,
michael@0 347 "chunkType" : "s",
michael@0 348 "urls" : [ "50:bar.com/1" ]
michael@0 349 },
michael@0 350 { "chunkNum" : 3,
michael@0 351 "chunkType" : "s",
michael@0 352 "urls" : [ "50:baz.com/1" ]
michael@0 353 },
michael@0 354 { "chunkNum" : 5,
michael@0 355 "chunkType" : "s",
michael@0 356 "urls" : [ "50:blah.com/1" ]
michael@0 357 },
michael@0 358 ]);
michael@0 359
michael@0 360 var expireUpdate = buildPhishingUpdate(
michael@0 361 [ { "chunkType" : "ad:1,3-5" },
michael@0 362 { "chunkType" : "sd:1-3,5" }]);
michael@0 363
michael@0 364 var assertions = {
michael@0 365 // "tableData" : "test-phish-simple;"
michael@0 366 "tableData": ""
michael@0 367 };
michael@0 368
michael@0 369 doTest([addUpdate, subUpdate, expireUpdate], assertions);
michael@0 370 }
michael@0 371
michael@0 372 // Test a duplicate add chunk.
michael@0 373 function testDuplicateAddChunks() {
michael@0 374 var addUrls1 = [ "foo.com/a" ];
michael@0 375 var addUrls2 = [ "bar.com/b" ];
michael@0 376 var update = buildPhishingUpdate(
michael@0 377 [
michael@0 378 { "chunkNum" : 1,
michael@0 379 "urls" : addUrls1
michael@0 380 },
michael@0 381 { "chunkNum" : 1,
michael@0 382 "urls" : addUrls2
michael@0 383 }]);
michael@0 384
michael@0 385 var assertions = {
michael@0 386 "tableData" : "test-phish-simple;a:1",
michael@0 387 "urlsExist" : addUrls1,
michael@0 388 "urlsDontExist" : addUrls2
michael@0 389 };
michael@0 390
michael@0 391 doTest([update], assertions);
michael@0 392 }
michael@0 393
michael@0 394 // This test is a bit tricky. We want to test that an add removes all
michael@0 395 // subs with the same add chunk id, even if there is no match. To do
michael@0 396 // that we need to add the same add chunk twice, with an expiration
michael@0 397 // in the middle. This would be easier if subsDontExist actually
michael@0 398 // worked...
michael@0 399 function testExpireWholeSub()
michael@0 400 {
michael@0 401 var subUrls = ["1:foo.com/a"];
michael@0 402
michael@0 403 var update = buildPhishingUpdate(
michael@0 404 [{ "chunkNum" : 5,
michael@0 405 "chunkType" : "s",
michael@0 406 "urls" : subUrls
michael@0 407 },
michael@0 408 // empty add chunk should still cause foo.com/a to go away.
michael@0 409 { "chunkNum" : 1,
michael@0 410 "urls" : []
michael@0 411 },
michael@0 412 // and now adding chunk 1 again with foo.com/a should succeed,
michael@0 413 // because the sub should have been expired with the empty
michael@0 414 // add chunk.
michael@0 415
michael@0 416 // we need to expire this chunk to let us add chunk 1 again.
michael@0 417 {
michael@0 418 "chunkType" : "ad:1"
michael@0 419 },
michael@0 420 { "chunkNum" : 1,
michael@0 421 "urls" : [ "foo.com/a" ]
michael@0 422 }]);
michael@0 423
michael@0 424 var assertions = {
michael@0 425 "tableData" : "test-phish-simple;a:1:s:5",
michael@0 426 "urlsExist" : ["foo.com/a"]
michael@0 427 };
michael@0 428
michael@0 429 doTest([update], assertions);
michael@0 430 }
michael@0 431
michael@0 432
michael@0 433 // This test is roughly the opposite of testExpireWholeSub(). We add
michael@0 434 // the empty add first, and make sure that it prevents a sub for that
michael@0 435 // add from being applied.
michael@0 436 function testPreventWholeSub()
michael@0 437 {
michael@0 438 var subUrls = ["1:foo.com/a"];
michael@0 439
michael@0 440 var update = buildPhishingUpdate(
michael@0 441 [ // empty add chunk should cause foo.com/a to not be saved
michael@0 442 { "chunkNum" : 1,
michael@0 443 "urls" : []
michael@0 444 },
michael@0 445 { "chunkNum" : 5,
michael@0 446 "chunkType" : "s",
michael@0 447 "urls" : subUrls
michael@0 448 },
michael@0 449 // and now adding chunk 1 again with foo.com/a should succeed,
michael@0 450 // because the sub should have been expired with the empty
michael@0 451 // add chunk.
michael@0 452
michael@0 453 // we need to expire this chunk to let us add chunk 1 again.
michael@0 454 {
michael@0 455 "chunkType" : "ad:1"
michael@0 456 },
michael@0 457 { "chunkNum" : 1,
michael@0 458 "urls" : [ "foo.com/a" ]
michael@0 459 }]);
michael@0 460
michael@0 461 var assertions = {
michael@0 462 "tableData" : "test-phish-simple;a:1:s:5",
michael@0 463 "urlsExist" : ["foo.com/a"]
michael@0 464 };
michael@0 465
michael@0 466 doTest([update], assertions);
michael@0 467 }
michael@0 468
michael@0 469 function run_test()
michael@0 470 {
michael@0 471 runTests([
michael@0 472 testSimpleAdds,
michael@0 473 testMultipleAdds,
michael@0 474 testSimpleSub,
michael@0 475 testSubEmptiesAdd,
michael@0 476 testSubPartiallyEmptiesAdd,
michael@0 477 testPendingSubRemoved,
michael@0 478 testPendingSubExpire,
michael@0 479 testDuplicateAdds,
michael@0 480 testSubPartiallyMatches,
michael@0 481 testSubPartiallyMatches2,
michael@0 482 testSubsDifferentChunks,
michael@0 483 testSubsDifferentChunksSameHostId,
michael@0 484 testExpireLists
michael@0 485 ]);
michael@0 486 }
michael@0 487
michael@0 488 do_test_pending();

mercurial