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

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:d2013c8a8882
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 }
9
10 function testFillDb() {
11 var add1Urls = [ "zaz.com/a", "yxz.com/c" ];
12
13 var update = "n:1000\n";
14 update += "i:test-phish-simple\n";
15
16 var update1 = buildBareUpdate(
17 [{ "chunkNum" : 1,
18 "urls" : add1Urls }]);
19 update += "u:data:," + encodeURIComponent(update1) + "\n";
20
21 var assertions = {
22 "tableData" : "test-phish-simple;a:1",
23 "urlsExist" : add1Urls
24 };
25
26 doTest([update], assertions, false);
27 }
28
29 function testSimpleForward() {
30 var add1Urls = [ "foo.com/a", "bar.com/c" ];
31 var add2Urls = [ "foo.com/b" ];
32 var add3Urls = [ "bar.com/d" ];
33
34 var update = "n:1000\n";
35 update += "i:test-phish-simple\n";
36
37 var update1 = buildBareUpdate(
38 [{ "chunkNum" : 1,
39 "urls" : add1Urls }]);
40 update += "u:data:," + encodeURIComponent(update1) + "\n";
41
42 var update2 = buildBareUpdate(
43 [{ "chunkNum" : 2,
44 "urls" : add2Urls }]);
45 update += "u:data:," + encodeURIComponent(update2) + "\n";
46
47 var update3 = buildBareUpdate(
48 [{ "chunkNum" : 3,
49 "urls" : add3Urls }]);
50 update += "u:data:," + encodeURIComponent(update3) + "\n";
51
52 var assertions = {
53 "tableData" : "test-phish-simple;a:1-3",
54 "urlsExist" : add1Urls.concat(add2Urls).concat(add3Urls)
55 };
56
57 doTest([update], assertions, false);
58 }
59
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" ];
65
66 var update = "n:1000\n";
67 update += "i:test-phish-simple\n";
68
69 var update1 = buildBareUpdate(
70 [{ "chunkNum" : 1,
71 "urls" : add1Urls }]);
72 update += "u:data:," + encodeURIComponent(update1) + "\n";
73
74 var update2 = buildBareUpdate(
75 [{ "chunkNum" : 2 }]);
76 var update3 = buildBareUpdate(
77 [{ "chunkNum" : 3,
78 "urls" : add1Urls }]);
79
80 update2 += "u:data:," + encodeURIComponent(update3) + "\n";
81
82 update += "u:data:," + encodeURIComponent(update2) + "\n";
83
84 var assertions = {
85 "tableData" : "",
86 "urlsDontExist" : add1Urls.concat(add2Urls)
87 };
88
89 doTest([update], assertions, true);
90 }
91
92 // An invalid URL forward causes the update to fail.
93 function testInvalidUrlForward() {
94 var add1Urls = [ "foo.com/a", "bar.com/c" ];
95
96 var update = buildPhishingUpdate(
97 [{ "chunkNum" : 1,
98 "urls" : add1Urls }]);
99 update += "u:asdf://blah/blah\n"; // invalid URL scheme
100
101 // The first part of the update should have succeeded.
102
103 var assertions = {
104 "tableData" : "test-phish-simple;a:1",
105 "urlsExist" : add1Urls
106 };
107
108 doTest([update], assertions, false);
109 }
110
111 // A failed network request causes the update to fail.
112 function testErrorUrlForward() {
113 var add1Urls = [ "foo.com/a", "bar.com/c" ];
114
115 var update = buildPhishingUpdate(
116 [{ "chunkNum" : 1,
117 "urls" : add1Urls }]);
118 update += "u:http://test.invalid/asdf/asdf\n"; // invalid URL scheme
119
120 // The first part of the update should have succeeded
121
122 var assertions = {
123 "tableData" : "test-phish-simple;a:1",
124 "urlsExist" : add1Urls
125 };
126
127 doTest([update], assertions, false);
128 }
129
130 function testMultipleTables() {
131 var add1Urls = [ "foo.com/a", "bar.com/c" ];
132 var add2Urls = [ "foo.com/b" ];
133 var add3Urls = [ "bar.com/d" ];
134
135 var update = "n:1000\n";
136 update += "i:test-phish-simple\n";
137
138 var update1 = buildBareUpdate(
139 [{ "chunkNum" : 1,
140 "urls" : add1Urls }]);
141 update += "u:data:," + encodeURIComponent(update1) + "\n";
142
143 var update2 = buildBareUpdate(
144 [{ "chunkNum" : 2,
145 "urls" : add2Urls }]);
146 update += "u:data:," + encodeURIComponent(update2) + "\n";
147
148 update += "i:test-malware-simple\n";
149
150 var update3 = buildBareUpdate(
151 [{ "chunkNum" : 3,
152 "urls" : add3Urls }]);
153 update += "u:data:," + encodeURIComponent(update3) + "\n";
154
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 };
160
161 doTest([update], assertions, false);
162 }
163
164 function Observer(callback) {
165 this.observe = callback;
166 }
167
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 };
179
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 }]);
188
189 var update2 = "n:1000\nr:pleasereset\n";
190
191 var addUrls3 = [ "bar.com/a", "bar.com/b" ];
192 var update3 = buildPhishingUpdate(
193 [
194 { "chunkNum" : 3,
195 "urls" : addUrls3
196 }]);
197
198 var assertions = {
199 "tableData" : "test-phish-simple;a:3",
200 "urlsExist" : addUrls3,
201 "urlsDontExist" : addUrls1
202 };
203
204 doTest([update1, update2, update3], assertions, false);
205 }
206
207
208 function run_test()
209 {
210 runTests([
211 testSimpleForward,
212 testNestedForward,
213 testInvalidUrlForward,
214 testErrorUrlForward,
215 testMultipleTables,
216 testReset
217 ]);
218 }
219
220 do_test_pending();

mercurial