toolkit/components/url-classifier/tests/unit/test_dbservice.js

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

mercurial