toolkit/components/url-classifier/tests/unit/test_streamupdater.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.

     1 function doTest(updates, assertions, expectError)
     2 {
     3   if (expectError) {
     4     doUpdateTest(updates, assertions, updateError, runNextTest);
     5   } else {
     6     doUpdateTest(updates, assertions, runNextTest, updateError);
     7   }
     8 }
    10 function testFillDb() {
    11   var add1Urls = [ "zaz.com/a", "yxz.com/c" ];
    13   var update = "n:1000\n";
    14   update += "i:test-phish-simple\n";
    16   var update1 = buildBareUpdate(
    17     [{ "chunkNum" : 1,
    18        "urls" : add1Urls }]);
    19   update += "u:data:," + encodeURIComponent(update1) + "\n";
    21   var assertions = {
    22     "tableData" : "test-phish-simple;a:1",
    23     "urlsExist" : add1Urls
    24   };
    26   doTest([update], assertions, false);
    27 }
    29 function testSimpleForward() {
    30   var add1Urls = [ "foo.com/a", "bar.com/c" ];
    31   var add2Urls = [ "foo.com/b" ];
    32   var add3Urls = [ "bar.com/d" ];
    34   var update = "n:1000\n";
    35   update += "i:test-phish-simple\n";
    37   var update1 = buildBareUpdate(
    38     [{ "chunkNum" : 1,
    39        "urls" : add1Urls }]);
    40   update += "u:data:," + encodeURIComponent(update1) + "\n";
    42   var update2 = buildBareUpdate(
    43     [{ "chunkNum" : 2,
    44        "urls" : add2Urls }]);
    45   update += "u:data:," + encodeURIComponent(update2) + "\n";
    47   var update3 = buildBareUpdate(
    48     [{ "chunkNum" : 3,
    49        "urls" : add3Urls }]);
    50   update += "u:data:," + encodeURIComponent(update3) + "\n";
    52   var assertions = {
    53     "tableData" : "test-phish-simple;a:1-3",
    54     "urlsExist" : add1Urls.concat(add2Urls).concat(add3Urls)
    55   };
    57   doTest([update], assertions, false);
    58 }
    60 // Make sure that a nested forward (a forward within a forward) causes
    61 // the update to fail.
    62 function testNestedForward() {
    63   var add1Urls = [ "foo.com/a", "bar.com/c" ];
    64   var add2Urls = [ "foo.com/b" ];
    66   var update = "n:1000\n";
    67   update += "i:test-phish-simple\n";
    69   var update1 = buildBareUpdate(
    70     [{ "chunkNum" : 1,
    71        "urls" : add1Urls }]);
    72   update += "u:data:," + encodeURIComponent(update1) + "\n";
    74   var update2 = buildBareUpdate(
    75     [{ "chunkNum" : 2 }]);
    76   var update3 = buildBareUpdate(
    77     [{ "chunkNum" : 3,
    78        "urls" : add1Urls }]);
    80   update2 += "u:data:," + encodeURIComponent(update3) + "\n";
    82   update += "u:data:," + encodeURIComponent(update2) + "\n";
    84   var assertions = {
    85     "tableData" : "",
    86     "urlsDontExist" : add1Urls.concat(add2Urls)
    87   };
    89   doTest([update], assertions, true);
    90 }
    92 // An invalid URL forward causes the update to fail.
    93 function testInvalidUrlForward() {
    94   var add1Urls = [ "foo.com/a", "bar.com/c" ];
    96   var update = buildPhishingUpdate(
    97     [{ "chunkNum" : 1,
    98        "urls" : add1Urls }]);
    99   update += "u:asdf://blah/blah\n";  // invalid URL scheme
   101   // The first part of the update should have succeeded.
   103   var assertions = {
   104     "tableData" : "test-phish-simple;a:1",
   105     "urlsExist" : add1Urls
   106   };
   108   doTest([update], assertions, false);
   109 }
   111 // A failed network request causes the update to fail.
   112 function testErrorUrlForward() {
   113   var add1Urls = [ "foo.com/a", "bar.com/c" ];
   115   var update = buildPhishingUpdate(
   116     [{ "chunkNum" : 1,
   117        "urls" : add1Urls }]);
   118   update += "u:http://test.invalid/asdf/asdf\n";  // invalid URL scheme
   120   // The first part of the update should have succeeded
   122   var assertions = {
   123     "tableData" : "test-phish-simple;a:1",
   124     "urlsExist" : add1Urls
   125   };
   127   doTest([update], assertions, false);
   128 }
   130 function testMultipleTables() {
   131   var add1Urls = [ "foo.com/a", "bar.com/c" ];
   132   var add2Urls = [ "foo.com/b" ];
   133   var add3Urls = [ "bar.com/d" ];
   135   var update = "n:1000\n";
   136   update += "i:test-phish-simple\n";
   138   var update1 = buildBareUpdate(
   139     [{ "chunkNum" : 1,
   140        "urls" : add1Urls }]);
   141   update += "u:data:," + encodeURIComponent(update1) + "\n";
   143   var update2 = buildBareUpdate(
   144     [{ "chunkNum" : 2,
   145        "urls" : add2Urls }]);
   146   update += "u:data:," + encodeURIComponent(update2) + "\n";
   148   update += "i:test-malware-simple\n";
   150   var update3 = buildBareUpdate(
   151     [{ "chunkNum" : 3,
   152        "urls" : add3Urls }]);
   153   update += "u:data:," + encodeURIComponent(update3) + "\n";
   155   var assertions = {
   156     "tableData" : "test-malware-simple;a:3\ntest-phish-simple;a:1-2",
   157     "urlsExist" : add1Urls.concat(add2Urls),
   158     "malwareUrlsExist" : add3Urls
   159   };
   161   doTest([update], assertions, false);
   162 }
   164 function Observer(callback) {
   165   this.observe = callback;
   166 }
   168 Observer.prototype =
   169 {
   170 QueryInterface: function(iid)
   171 {
   172   if (!iid.equals(Ci.nsISupports) &&
   173       !iid.equals(Ci.nsIObserver)) {
   174     throw Cr.NS_ERROR_NO_INTERFACE;
   175   }
   176   return this;
   177 }
   178 };
   180 // Tests a database reset request.
   181 function testReset() {
   182   var addUrls1 = [ "foo.com/a", "foo.com/b" ];
   183   var update1 = buildPhishingUpdate(
   184     [
   185       { "chunkNum" : 1,
   186         "urls" : addUrls1
   187       }]);
   189   var update2 = "n:1000\nr:pleasereset\n";
   191   var addUrls3 = [ "bar.com/a", "bar.com/b" ];
   192   var update3 = buildPhishingUpdate(
   193     [
   194       { "chunkNum" : 3,
   195         "urls" : addUrls3
   196       }]);
   198   var assertions = {
   199     "tableData" : "test-phish-simple;a:3",
   200     "urlsExist" : addUrls3,
   201     "urlsDontExist" : addUrls1
   202   };
   204   doTest([update1, update2, update3], assertions, false);
   205 }
   208 function run_test()
   209 {
   210   runTests([
   211     testSimpleForward,
   212     testNestedForward,
   213     testInvalidUrlForward,
   214     testErrorUrlForward,
   215     testMultipleTables,
   216     testReset
   217   ]);
   218 }
   220 do_test_pending();

mercurial