toolkit/components/url-classifier/tests/unit/test_dbservice.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 var checkUrls = [];
michael@0 2 var checkExpect;
michael@0 3
michael@0 4 var chunk1Urls = [
michael@0 5 "test.com/aba",
michael@0 6 "test.com/foo/bar",
michael@0 7 "foo.bar.com/a/b/c"
michael@0 8 ];
michael@0 9 var chunk1 = chunk1Urls.join("\n");
michael@0 10
michael@0 11 var chunk2Urls = [
michael@0 12 "blah.com/a",
michael@0 13 "baz.com/",
michael@0 14 "255.255.0.1/",
michael@0 15 "www.foo.com/test2?param=1"
michael@0 16 ];
michael@0 17 var chunk2 = chunk2Urls.join("\n");
michael@0 18
michael@0 19 var chunk3Urls = [
michael@0 20 "test.com/a",
michael@0 21 "foo.bar.com/a",
michael@0 22 "blah.com/a",
michael@0 23 ];
michael@0 24 var chunk3 = chunk3Urls.join("\n");
michael@0 25
michael@0 26 var chunk3SubUrls = [
michael@0 27 "1:test.com/a",
michael@0 28 "1:foo.bar.com/a",
michael@0 29 "2:blah.com/a" ];
michael@0 30 var chunk3Sub = chunk3SubUrls.join("\n");
michael@0 31
michael@0 32 var chunk4Urls = [
michael@0 33 "a.com/b",
michael@0 34 "b.com/c",
michael@0 35 ];
michael@0 36 var chunk4 = chunk4Urls.join("\n");
michael@0 37
michael@0 38 var chunk5Urls = [
michael@0 39 "d.com/e",
michael@0 40 "f.com/g",
michael@0 41 ];
michael@0 42 var chunk5 = chunk5Urls.join("\n");
michael@0 43
michael@0 44 var chunk6Urls = [
michael@0 45 "h.com/i",
michael@0 46 "j.com/k",
michael@0 47 ];
michael@0 48 var chunk6 = chunk6Urls.join("\n");
michael@0 49
michael@0 50 // we are going to add chunks 1, 2, 4, 5, and 6 to phish-simple, and
michael@0 51 // chunk 2 to malware-simple. Then we'll remove the urls in chunk3
michael@0 52 // from phish-simple, then expire chunk 1 and chunks 4-6 from
michael@0 53 // phish-simple.
michael@0 54 var phishExpected = {};
michael@0 55 var phishUnexpected = {};
michael@0 56 var malwareExpected = {};
michael@0 57 for (var i = 0; i < chunk2Urls.length; i++) {
michael@0 58 phishExpected[chunk2Urls[i]] = true;
michael@0 59 malwareExpected[chunk2Urls[i]] = true;
michael@0 60 }
michael@0 61 for (var i = 0; i < chunk3Urls.length; i++) {
michael@0 62 delete phishExpected[chunk3Urls[i]];
michael@0 63 phishUnexpected[chunk3Urls[i]] = true;
michael@0 64 }
michael@0 65 for (var i = 0; i < chunk1Urls.length; i++) {
michael@0 66 // chunk1 urls are expired
michael@0 67 phishUnexpected[chunk1Urls[i]] = true;
michael@0 68 }
michael@0 69 for (var i = 0; i < chunk4Urls.length; i++) {
michael@0 70 // chunk4 urls are expired
michael@0 71 phishUnexpected[chunk4Urls[i]] = true;
michael@0 72 }
michael@0 73 for (var i = 0; i < chunk5Urls.length; i++) {
michael@0 74 // chunk5 urls are expired
michael@0 75 phishUnexpected[chunk5Urls[i]] = true;
michael@0 76 }
michael@0 77 for (var i = 0; i < chunk6Urls.length; i++) {
michael@0 78 // chunk6 urls are expired
michael@0 79 phishUnexpected[chunk6Urls[i]] = true;
michael@0 80 }
michael@0 81
michael@0 82 // Check that the entries hit based on sub-parts
michael@0 83 phishExpected["baz.com/foo/bar"] = true;
michael@0 84 phishExpected["foo.bar.baz.com/foo"] = true;
michael@0 85 phishExpected["bar.baz.com/"] = true;
michael@0 86
michael@0 87 var numExpecting;
michael@0 88
michael@0 89 function testFailure(arg) {
michael@0 90 do_throw(arg);
michael@0 91 }
michael@0 92
michael@0 93 function checkNoHost()
michael@0 94 {
michael@0 95 // Looking up a no-host uri such as a data: uri should throw an exception.
michael@0 96 var exception;
michael@0 97 try {
michael@0 98 var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("data:text/html,<b>test</b>", null, null));
michael@0 99 dbservice.lookup(principal, allTables);
michael@0 100
michael@0 101 exception = false;
michael@0 102 } catch(e) {
michael@0 103 exception = true;
michael@0 104 }
michael@0 105 do_check_true(exception);
michael@0 106
michael@0 107 do_test_finished();
michael@0 108 }
michael@0 109
michael@0 110 function tablesCallbackWithoutSub(tables)
michael@0 111 {
michael@0 112 var parts = tables.split("\n");
michael@0 113 parts.sort();
michael@0 114
michael@0 115 // there's a leading \n here because splitting left an empty string
michael@0 116 // after the trailing newline, which will sort first
michael@0 117 do_check_eq(parts.join("\n"),
michael@0 118 "\ntest-malware-simple;a:1\ntest-phish-simple;a:2");
michael@0 119
michael@0 120 checkNoHost();
michael@0 121 }
michael@0 122
michael@0 123
michael@0 124 function expireSubSuccess(result) {
michael@0 125 dbservice.getTables(tablesCallbackWithoutSub);
michael@0 126 }
michael@0 127
michael@0 128 function tablesCallbackWithSub(tables)
michael@0 129 {
michael@0 130 var parts = tables.split("\n");
michael@0 131 parts.sort();
michael@0 132
michael@0 133 // there's a leading \n here because splitting left an empty string
michael@0 134 // after the trailing newline, which will sort first
michael@0 135 do_check_eq(parts.join("\n"),
michael@0 136 "\ntest-malware-simple;a:1\ntest-phish-simple;a:2:s:3");
michael@0 137
michael@0 138 // verify that expiring a sub chunk removes its name from the list
michael@0 139 var data =
michael@0 140 "n:1000\n" +
michael@0 141 "i:test-phish-simple\n" +
michael@0 142 "sd:3\n";
michael@0 143
michael@0 144 doSimpleUpdate(data, expireSubSuccess, testFailure);
michael@0 145 }
michael@0 146
michael@0 147 function checkChunksWithSub()
michael@0 148 {
michael@0 149 dbservice.getTables(tablesCallbackWithSub);
michael@0 150 }
michael@0 151
michael@0 152 function checkDone() {
michael@0 153 if (--numExpecting == 0)
michael@0 154 checkChunksWithSub();
michael@0 155 }
michael@0 156
michael@0 157 function phishExists(result) {
michael@0 158 dumpn("phishExists: " + result);
michael@0 159 try {
michael@0 160 do_check_true(result.indexOf("test-phish-simple") != -1);
michael@0 161 } finally {
michael@0 162 checkDone();
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166 function phishDoesntExist(result) {
michael@0 167 dumpn("phishDoesntExist: " + result);
michael@0 168 try {
michael@0 169 do_check_true(result.indexOf("test-phish-simple") == -1);
michael@0 170 } finally {
michael@0 171 checkDone();
michael@0 172 }
michael@0 173 }
michael@0 174
michael@0 175 function malwareExists(result) {
michael@0 176 dumpn("malwareExists: " + result);
michael@0 177
michael@0 178 try {
michael@0 179 do_check_true(result.indexOf("test-malware-simple") != -1);
michael@0 180 } finally {
michael@0 181 checkDone();
michael@0 182 }
michael@0 183 }
michael@0 184
michael@0 185 function checkState()
michael@0 186 {
michael@0 187 numExpecting = 0;
michael@0 188
michael@0 189 for (var key in phishExpected) {
michael@0 190 var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + key, null, null));
michael@0 191 dbservice.lookup(principal, allTables, phishExists, true);
michael@0 192 numExpecting++;
michael@0 193 }
michael@0 194
michael@0 195 for (var key in phishUnexpected) {
michael@0 196 var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + key, null, null));
michael@0 197 dbservice.lookup(principal, allTables, phishDoesntExist, true);
michael@0 198 numExpecting++;
michael@0 199 }
michael@0 200
michael@0 201 for (var key in malwareExpected) {
michael@0 202 var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + key, null, null));
michael@0 203 dbservice.lookup(principal, allTables, malwareExists, true);
michael@0 204 numExpecting++;
michael@0 205 }
michael@0 206 }
michael@0 207
michael@0 208 function testSubSuccess(result)
michael@0 209 {
michael@0 210 do_check_eq(result, "1000");
michael@0 211 checkState();
michael@0 212 }
michael@0 213
michael@0 214 function do_subs() {
michael@0 215 var data =
michael@0 216 "n:1000\n" +
michael@0 217 "i:test-phish-simple\n" +
michael@0 218 "s:3:32:" + chunk3Sub.length + "\n" +
michael@0 219 chunk3Sub + "\n" +
michael@0 220 "ad:1\n" +
michael@0 221 "ad:4-6\n";
michael@0 222
michael@0 223 doSimpleUpdate(data, testSubSuccess, testFailure);
michael@0 224 }
michael@0 225
michael@0 226 function testAddSuccess(arg) {
michael@0 227 do_check_eq(arg, "1000");
michael@0 228
michael@0 229 do_subs();
michael@0 230 }
michael@0 231
michael@0 232 function do_adds() {
michael@0 233 // This test relies on the fact that only -regexp tables are ungzipped,
michael@0 234 // and only -hash tables are assumed to be pre-md5'd. So we use
michael@0 235 // a 'simple' table type to get simple hostname-per-line semantics.
michael@0 236
michael@0 237 var data =
michael@0 238 "n:1000\n" +
michael@0 239 "i:test-phish-simple\n" +
michael@0 240 "a:1:32:" + chunk1.length + "\n" +
michael@0 241 chunk1 + "\n" +
michael@0 242 "a:2:32:" + chunk2.length + "\n" +
michael@0 243 chunk2 + "\n" +
michael@0 244 "a:4:32:" + chunk4.length + "\n" +
michael@0 245 chunk4 + "\n" +
michael@0 246 "a:5:32:" + chunk5.length + "\n" +
michael@0 247 chunk5 + "\n" +
michael@0 248 "a:6:32:" + chunk6.length + "\n" +
michael@0 249 chunk6 + "\n" +
michael@0 250 "i:test-malware-simple\n" +
michael@0 251 "a:1:32:" + chunk2.length + "\n" +
michael@0 252 chunk2 + "\n";
michael@0 253
michael@0 254 doSimpleUpdate(data, testAddSuccess, testFailure);
michael@0 255 }
michael@0 256
michael@0 257 function run_test() {
michael@0 258 do_adds();
michael@0 259 do_test_pending();
michael@0 260 }

mercurial