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.

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

mercurial