michael@0: michael@0: function doTest(updates, assertions) michael@0: { michael@0: doUpdateTest(updates, assertions, runNextTest, updateError); michael@0: } michael@0: michael@0: // Test an add of two urls to a fresh database michael@0: function testSimpleAdds() { michael@0: var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; michael@0: var update = buildPhishingUpdate( michael@0: [ michael@0: { "chunkNum" : 1, michael@0: "urls" : addUrls michael@0: }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1", michael@0: "urlsExist" : addUrls michael@0: }; michael@0: michael@0: doTest([update], assertions); michael@0: } michael@0: michael@0: // Same as testSimpleAdds, but make the same-domain URLs come from different michael@0: // chunks. michael@0: function testMultipleAdds() { michael@0: var add1Urls = [ "foo.com/a", "bar.com/c" ]; michael@0: var add2Urls = [ "foo.com/b" ]; michael@0: michael@0: var update = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "urls" : add1Urls }, michael@0: { "chunkNum" : 2, michael@0: "urls" : add2Urls }]); michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1-2", michael@0: "urlsExist" : add1Urls.concat(add2Urls) michael@0: }; michael@0: michael@0: doTest([update], assertions); michael@0: } michael@0: michael@0: // Test that a sub will remove an existing add michael@0: function testSimpleSub() michael@0: { michael@0: var addUrls = ["foo.com/a", "bar.com/b"]; michael@0: var subUrls = ["1:foo.com/a"]; michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space michael@0: "urls": addUrls }]); michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 50, michael@0: "chunkType" : "s", michael@0: "urls": subUrls }]); michael@0: michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1:s:50", michael@0: "urlsExist" : [ "bar.com/b" ], michael@0: "urlsDontExist": ["foo.com/a" ], michael@0: "subsDontExist" : [ "foo.com/a" ] michael@0: } michael@0: michael@0: doTest([addUpdate, subUpdate], assertions); michael@0: michael@0: } michael@0: michael@0: // Same as testSimpleSub(), but the sub comes in before the add. michael@0: function testSubEmptiesAdd() michael@0: { michael@0: var subUrls = ["1:foo.com/a"]; michael@0: var addUrls = ["foo.com/a", "bar.com/b"]; michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 50, michael@0: "chunkType" : "s", michael@0: "urls": subUrls }]); michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "urls": addUrls }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1:s:50", michael@0: "urlsExist" : [ "bar.com/b" ], michael@0: "urlsDontExist": ["foo.com/a" ], michael@0: "subsDontExist" : [ "foo.com/a" ] // this sub was found, it shouldn't exist anymore michael@0: } michael@0: michael@0: doTest([subUpdate, addUpdate], assertions); michael@0: } michael@0: michael@0: // Very similar to testSubEmptiesAdd, except that the domain entry will michael@0: // still have an item left over that needs to be synced. michael@0: function testSubPartiallyEmptiesAdd() michael@0: { michael@0: var subUrls = ["1:foo.com/a"]; michael@0: var addUrls = ["foo.com/a", "foo.com/b", "bar.com/b"]; michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls": subUrls }]); michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space michael@0: "urls": addUrls }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1:s:1", michael@0: "urlsExist" : [ "foo.com/b", "bar.com/b" ], michael@0: "urlsDontExist" : ["foo.com/a" ], michael@0: "subsDontExist" : [ "foo.com/a" ] // this sub was found, it shouldn't exist anymore michael@0: } michael@0: michael@0: doTest([subUpdate, addUpdate], assertions); michael@0: } michael@0: michael@0: // We SHOULD be testing that pending subs are removed using michael@0: // subsDontExist assertions. Since we don't have a good interface for getting michael@0: // at sub entries, we'll verify it by side-effect. Subbing a url once michael@0: // then adding it twice should leave the url intact. michael@0: function testPendingSubRemoved() michael@0: { michael@0: var subUrls = ["1:foo.com/a", "2:foo.com/b"]; michael@0: var addUrls = ["foo.com/a", "foo.com/b"]; michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls": subUrls }]); michael@0: michael@0: var addUpdate1 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space michael@0: "urls": addUrls }]); michael@0: michael@0: var addUpdate2 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 2, michael@0: "urls": addUrls }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1-2:s:1", michael@0: "urlsExist" : [ "foo.com/a", "foo.com/b" ], michael@0: "subsDontExist" : [ "foo.com/a", "foo.com/b" ] // this sub was found, it shouldn't exist anymore michael@0: } michael@0: michael@0: doTest([subUpdate, addUpdate1, addUpdate2], assertions); michael@0: } michael@0: michael@0: // Make sure that a saved sub is removed when the sub chunk is expired. michael@0: function testPendingSubExpire() michael@0: { michael@0: var subUrls = ["1:foo.com/a", "1:foo.com/b"]; michael@0: var addUrls = ["foo.com/a", "foo.com/b"]; michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls": subUrls }]); michael@0: michael@0: var expireUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "sd" }]); michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space michael@0: "urls": addUrls }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1", michael@0: "urlsExist" : [ "foo.com/a", "foo.com/b" ], michael@0: "subsDontExist" : [ "foo.com/a", "foo.com/b" ] // this sub was expired michael@0: } michael@0: michael@0: doTest([subUpdate, expireUpdate, addUpdate], assertions); michael@0: } michael@0: michael@0: // Make sure that the sub url removes from only the chunk that it specifies michael@0: function testDuplicateAdds() michael@0: { michael@0: var urls = ["foo.com/a"]; michael@0: michael@0: var addUpdate1 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "urls": urls }]); michael@0: var addUpdate2 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 2, michael@0: "urls": urls }]); michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 3, michael@0: "chunkType" : "s", michael@0: "urls": ["2:foo.com/a"]}]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1-2:s:3", michael@0: "urlsExist" : [ "foo.com/a"], michael@0: "subsDontExist" : [ "foo.com/a"] michael@0: } michael@0: michael@0: doTest([addUpdate1, addUpdate2, subUpdate], assertions); michael@0: } michael@0: michael@0: // Tests a sub which matches some existing adds but leaves others. michael@0: function testSubPartiallyMatches() michael@0: { michael@0: var subUrls = ["foo.com/a"]; michael@0: var addUrls = ["1:foo.com/a", "2:foo.com/b"]; michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "urls" : addUrls }]); michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls" : addUrls }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1:s:1", michael@0: "urlsDontExist" : ["foo.com/a"], michael@0: "subsDontExist" : ["foo.com/a"], michael@0: "subsExist" : ["foo.com/b"] michael@0: }; michael@0: michael@0: doTest([addUpdate, subUpdate], assertions); michael@0: } michael@0: michael@0: // XXX: because subsExist isn't actually implemented, this is the same michael@0: // test as above but with a second add chunk that should fail to be added michael@0: // because of a pending sub chunk. michael@0: function testSubPartiallyMatches2() michael@0: { michael@0: var addUrls = ["foo.com/a"]; michael@0: var subUrls = ["1:foo.com/a", "2:foo.com/b"]; michael@0: var addUrls2 = ["foo.com/b"]; michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "urls" : addUrls }]); michael@0: michael@0: var subUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls" : subUrls }]); michael@0: michael@0: var addUpdate2 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 2, michael@0: "urls" : addUrls2 }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1-2:s:1", michael@0: "urlsDontExist" : ["foo.com/a", "foo.com/b"], michael@0: "subsDontExist" : ["foo.com/a", "foo.com/b"] michael@0: }; michael@0: michael@0: doTest([addUpdate, subUpdate, addUpdate2], assertions); michael@0: } michael@0: michael@0: // Verify that two subs for the same domain but from different chunks michael@0: // match (tests that existing sub entries are properly updated) michael@0: function testSubsDifferentChunks() { michael@0: var subUrls1 = [ "3:foo.com/a" ]; michael@0: var subUrls2 = [ "3:foo.com/b" ]; michael@0: michael@0: var addUrls = [ "foo.com/a", "foo.com/b", "foo.com/c" ]; michael@0: michael@0: var subUpdate1 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls": subUrls1 }]); michael@0: var subUpdate2 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 2, michael@0: "chunkType" : "s", michael@0: "urls" : subUrls2 }]); michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 3, michael@0: "urls" : addUrls }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:3:s:1-2", michael@0: "urlsExist" : [ "foo.com/c" ], michael@0: "urlsDontExist" : [ "foo.com/a", "foo.com/b" ], michael@0: "subsDontExist" : [ "foo.com/a", "foo.com/b" ] michael@0: }; michael@0: michael@0: doTest([subUpdate1, subUpdate2, addUpdate], assertions); michael@0: } michael@0: michael@0: // for bug 534079 michael@0: function testSubsDifferentChunksSameHostId() { michael@0: var subUrls1 = [ "1:foo.com/a" ]; michael@0: var subUrls2 = [ "1:foo.com/b", "2:foo.com/c" ]; michael@0: michael@0: var addUrls = [ "foo.com/a", "foo.com/b" ]; michael@0: var addUrls2 = [ "foo.com/c" ]; michael@0: michael@0: var subUpdate1 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls": subUrls1 }]); michael@0: var subUpdate2 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 2, michael@0: "chunkType" : "s", michael@0: "urls" : subUrls2 }]); michael@0: michael@0: var addUpdate = buildPhishingUpdate( michael@0: [{ "chunkNum" : 1, michael@0: "urls" : addUrls }]); michael@0: var addUpdate2 = buildPhishingUpdate( michael@0: [{ "chunkNum" : 2, michael@0: "urls" : addUrls2 }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1-2:s:1-2", michael@0: "urlsDontExist" : [ "foo.com/c", "foo.com/b", "foo.com/a", ], michael@0: }; michael@0: michael@0: doTest([addUpdate, addUpdate2, subUpdate1, subUpdate2], assertions); michael@0: } michael@0: michael@0: // Test lists of expired chunks michael@0: function testExpireLists() { michael@0: var addUpdate = buildPhishingUpdate( michael@0: [ michael@0: { "chunkNum" : 1, michael@0: "urls" : [ "foo.com/a" ] michael@0: }, michael@0: { "chunkNum" : 3, michael@0: "urls" : [ "bar.com/a" ] michael@0: }, michael@0: { "chunkNum" : 4, michael@0: "urls" : [ "baz.com/a" ] michael@0: }, michael@0: { "chunkNum" : 5, michael@0: "urls" : [ "blah.com/a" ] michael@0: }, michael@0: ]); michael@0: var subUpdate = buildPhishingUpdate( michael@0: [ michael@0: { "chunkNum" : 1, michael@0: "chunkType" : "s", michael@0: "urls" : [ "50:foo.com/1" ] michael@0: }, michael@0: { "chunkNum" : 2, michael@0: "chunkType" : "s", michael@0: "urls" : [ "50:bar.com/1" ] michael@0: }, michael@0: { "chunkNum" : 3, michael@0: "chunkType" : "s", michael@0: "urls" : [ "50:baz.com/1" ] michael@0: }, michael@0: { "chunkNum" : 5, michael@0: "chunkType" : "s", michael@0: "urls" : [ "50:blah.com/1" ] michael@0: }, michael@0: ]); michael@0: michael@0: var expireUpdate = buildPhishingUpdate( michael@0: [ { "chunkType" : "ad:1,3-5" }, michael@0: { "chunkType" : "sd:1-3,5" }]); michael@0: michael@0: var assertions = { michael@0: // "tableData" : "test-phish-simple;" michael@0: "tableData": "" michael@0: }; michael@0: michael@0: doTest([addUpdate, subUpdate, expireUpdate], assertions); michael@0: } michael@0: michael@0: // Test a duplicate add chunk. michael@0: function testDuplicateAddChunks() { michael@0: var addUrls1 = [ "foo.com/a" ]; michael@0: var addUrls2 = [ "bar.com/b" ]; michael@0: var update = buildPhishingUpdate( michael@0: [ michael@0: { "chunkNum" : 1, michael@0: "urls" : addUrls1 michael@0: }, michael@0: { "chunkNum" : 1, michael@0: "urls" : addUrls2 michael@0: }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1", michael@0: "urlsExist" : addUrls1, michael@0: "urlsDontExist" : addUrls2 michael@0: }; michael@0: michael@0: doTest([update], assertions); michael@0: } michael@0: michael@0: // This test is a bit tricky. We want to test that an add removes all michael@0: // subs with the same add chunk id, even if there is no match. To do michael@0: // that we need to add the same add chunk twice, with an expiration michael@0: // in the middle. This would be easier if subsDontExist actually michael@0: // worked... michael@0: function testExpireWholeSub() michael@0: { michael@0: var subUrls = ["1:foo.com/a"]; michael@0: michael@0: var update = buildPhishingUpdate( michael@0: [{ "chunkNum" : 5, michael@0: "chunkType" : "s", michael@0: "urls" : subUrls michael@0: }, michael@0: // empty add chunk should still cause foo.com/a to go away. michael@0: { "chunkNum" : 1, michael@0: "urls" : [] michael@0: }, michael@0: // and now adding chunk 1 again with foo.com/a should succeed, michael@0: // because the sub should have been expired with the empty michael@0: // add chunk. michael@0: michael@0: // we need to expire this chunk to let us add chunk 1 again. michael@0: { michael@0: "chunkType" : "ad:1" michael@0: }, michael@0: { "chunkNum" : 1, michael@0: "urls" : [ "foo.com/a" ] michael@0: }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1:s:5", michael@0: "urlsExist" : ["foo.com/a"] michael@0: }; michael@0: michael@0: doTest([update], assertions); michael@0: } michael@0: michael@0: michael@0: // This test is roughly the opposite of testExpireWholeSub(). We add michael@0: // the empty add first, and make sure that it prevents a sub for that michael@0: // add from being applied. michael@0: function testPreventWholeSub() michael@0: { michael@0: var subUrls = ["1:foo.com/a"]; michael@0: michael@0: var update = buildPhishingUpdate( michael@0: [ // empty add chunk should cause foo.com/a to not be saved michael@0: { "chunkNum" : 1, michael@0: "urls" : [] michael@0: }, michael@0: { "chunkNum" : 5, michael@0: "chunkType" : "s", michael@0: "urls" : subUrls michael@0: }, michael@0: // and now adding chunk 1 again with foo.com/a should succeed, michael@0: // because the sub should have been expired with the empty michael@0: // add chunk. michael@0: michael@0: // we need to expire this chunk to let us add chunk 1 again. michael@0: { michael@0: "chunkType" : "ad:1" michael@0: }, michael@0: { "chunkNum" : 1, michael@0: "urls" : [ "foo.com/a" ] michael@0: }]); michael@0: michael@0: var assertions = { michael@0: "tableData" : "test-phish-simple;a:1:s:5", michael@0: "urlsExist" : ["foo.com/a"] michael@0: }; michael@0: michael@0: doTest([update], assertions); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: runTests([ michael@0: testSimpleAdds, michael@0: testMultipleAdds, michael@0: testSimpleSub, michael@0: testSubEmptiesAdd, michael@0: testSubPartiallyEmptiesAdd, michael@0: testPendingSubRemoved, michael@0: testPendingSubExpire, michael@0: testDuplicateAdds, michael@0: testSubPartiallyMatches, michael@0: testSubPartiallyMatches2, michael@0: testSubsDifferentChunks, michael@0: testSubsDifferentChunksSameHostId, michael@0: testExpireLists michael@0: ]); michael@0: } michael@0: michael@0: do_test_pending();