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.

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

mercurial