LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 callback(); | 44 callback(); |
45 }; | 45 }; |
46 | 46 |
47 function hostnameToURL(hostname) | 47 function hostnameToURL(hostname) |
48 { | 48 { |
49 return new URL("http://" + hostname); | 49 return new URL("http://" + hostname); |
50 } | 50 } |
51 | 51 |
52 function testURLParsing(test, url) | 52 function testURLParsing(test, url) |
53 { | 53 { |
54 // The function expects a normalized URL. | 54 // Note: The function expects a normalized URL. |
55 url = new URL(url).href; | 55 // e.g. "http:example.com:80?foo" should already be normalized to |
56 | 56 // "http://example.com/?foo". If not, the tests will fail. |
57 let urlInfo = parseURL(url); | 57 let urlInfo = parseURL(url); |
58 | 58 |
59 // We need to ensure only that our implementation matches that of the URL | 59 // We need to ensure only that our implementation matches that of the URL |
60 // object. | 60 // object. |
61 let urlObject = new URL(url); | 61 let urlObject = new URL(url); |
62 | 62 |
63 test.equal(urlInfo.href, urlObject.href); | 63 test.equal(urlInfo.href, urlObject.href); |
64 test.equal(urlInfo.protocol, urlObject.protocol); | 64 test.equal(urlInfo.protocol, urlObject.protocol); |
65 test.equal(urlInfo.hostname, urlObject.hostname); | 65 test.equal(urlInfo.hostname, urlObject.hostname); |
66 | 66 |
(...skipping 13 matching lines...) Expand all Loading... |
80 // will fail if we don't normalize the document host as well. | 80 // will fail if we don't normalize the document host as well. |
81 hostnameToURL(documentHostname).hostname | 81 hostnameToURL(documentHostname).hostname |
82 ), | 82 ), |
83 expected, | 83 expected, |
84 message | 84 message |
85 ); | 85 ); |
86 } | 86 } |
87 | 87 |
88 exports.testParseURL = function(test) | 88 exports.testParseURL = function(test) |
89 { | 89 { |
90 testURLParsing(test, "https://example.com"); | |
91 testURLParsing(test, "https://example.com/"); | 90 testURLParsing(test, "https://example.com/"); |
92 testURLParsing(test, "https://example.com/foo"); | 91 testURLParsing(test, "https://example.com/foo"); |
93 testURLParsing(test, "https://example.com/foo/bar"); | 92 testURLParsing(test, "https://example.com/foo/bar"); |
94 testURLParsing( | 93 testURLParsing( |
95 test, | 94 test, |
96 "https://example.com/foo/bar?https://random/foo/bar" | 95 "https://example.com/foo/bar?https://random/foo/bar" |
97 ); | 96 ); |
98 | 97 |
99 testURLParsing(test, "https://example.com:8080"); | |
100 testURLParsing(test, "https://example.com:8080/"); | 98 testURLParsing(test, "https://example.com:8080/"); |
101 testURLParsing(test, "https://example.com:8080/foo"); | 99 testURLParsing(test, "https://example.com:8080/foo"); |
102 testURLParsing(test, "https://example.com:8080/foo/bar"); | 100 testURLParsing(test, "https://example.com:8080/foo/bar"); |
103 testURLParsing( | 101 testURLParsing( |
104 test, | 102 test, |
105 "https://example.com:8080/foo/bar?https://random/foo/bar" | 103 "https://example.com:8080/foo/bar?https://random/foo/bar" |
106 ); | 104 ); |
107 | 105 |
108 testURLParsing(test, "http://localhost"); | |
109 testURLParsing(test, "http://localhost/"); | 106 testURLParsing(test, "http://localhost/"); |
110 testURLParsing(test, "http://localhost/foo"); | 107 testURLParsing(test, "http://localhost/foo"); |
111 testURLParsing(test, "http://localhost/foo/bar"); | 108 testURLParsing(test, "http://localhost/foo/bar"); |
112 testURLParsing( | 109 testURLParsing( |
113 test, | 110 test, |
114 "http://localhost/foo/bar?https://random/foo/bar" | 111 "http://localhost/foo/bar?https://random/foo/bar" |
115 ); | 112 ); |
116 | 113 |
117 testURLParsing(test, "https://user@example.com"); | |
118 testURLParsing(test, "https://user@example.com/"); | 114 testURLParsing(test, "https://user@example.com/"); |
119 testURLParsing(test, "https://user@example.com/foo"); | 115 testURLParsing(test, "https://user@example.com/foo"); |
120 testURLParsing(test, "https://user@example.com/foo/bar"); | 116 testURLParsing(test, "https://user@example.com/foo/bar"); |
121 testURLParsing( | 117 testURLParsing( |
122 test, | 118 test, |
123 "https://user@example.com/foo/bar?https://random/foo/bar" | 119 "https://user@example.com/foo/bar?https://random/foo/bar" |
124 ); | 120 ); |
125 | 121 |
126 testURLParsing(test, "https://user@example.com:8080"); | |
127 testURLParsing(test, "https://user@example.com:8080/"); | 122 testURLParsing(test, "https://user@example.com:8080/"); |
128 testURLParsing(test, "https://user@example.com:8080/foo"); | 123 testURLParsing(test, "https://user@example.com:8080/foo"); |
129 testURLParsing(test, "https://user@example.com:8080/foo/bar"); | 124 testURLParsing(test, "https://user@example.com:8080/foo/bar"); |
130 testURLParsing( | 125 testURLParsing( |
131 test, | 126 test, |
132 "https://user@example.com:8080/foo/bar?https://random/foo/bar" | 127 "https://user@example.com:8080/foo/bar?https://random/foo/bar" |
133 ); | 128 ); |
134 | 129 |
135 testURLParsing(test, "https://user:pass@example.com"); | |
136 testURLParsing(test, "https://user:pass@example.com/"); | 130 testURLParsing(test, "https://user:pass@example.com/"); |
137 testURLParsing(test, "https://user:pass@example.com/foo"); | 131 testURLParsing(test, "https://user:pass@example.com/foo"); |
138 testURLParsing(test, "https://user:pass@example.com/foo/bar"); | 132 testURLParsing(test, "https://user:pass@example.com/foo/bar"); |
139 testURLParsing( | 133 testURLParsing( |
140 test, | 134 test, |
141 "https://user:pass@example.com/foo/bar?https://random/foo/bar" | 135 "https://user:pass@example.com/foo/bar?https://random/foo/bar" |
142 ); | 136 ); |
143 | 137 |
144 testURLParsing(test, "https://user:pass@example.com:8080"); | |
145 testURLParsing(test, "https://user:pass@example.com:8080/"); | 138 testURLParsing(test, "https://user:pass@example.com:8080/"); |
146 testURLParsing(test, "https://user:pass@example.com:8080/foo"); | 139 testURLParsing(test, "https://user:pass@example.com:8080/foo"); |
147 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); | 140 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); |
148 testURLParsing( | 141 testURLParsing( |
149 test, | 142 test, |
150 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" | 143 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" |
151 ); | 144 ); |
152 | 145 |
153 testURLParsing(test, "https://us%40er:pa%40ss@example.com"); | |
154 testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); | 146 testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); |
155 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); | 147 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); |
156 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); | 148 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); |
157 testURLParsing( | 149 testURLParsing( |
158 test, | 150 test, |
159 "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" | 151 "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" |
160 ); | 152 ); |
161 | 153 |
162 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080"); | |
163 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); | 154 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); |
164 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); | 155 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); |
165 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); | 156 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); |
166 testURLParsing( | 157 testURLParsing( |
167 test, | 158 test, |
168 "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" | 159 "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" |
169 ); | 160 ); |
170 | 161 |
171 testURLParsing(test, "http://192.168.1.1"); | |
172 testURLParsing(test, "http://192.168.1.1/"); | 162 testURLParsing(test, "http://192.168.1.1/"); |
173 testURLParsing(test, "http://192.168.1.1/foo"); | 163 testURLParsing(test, "http://192.168.1.1/foo"); |
174 testURLParsing(test, "http://192.168.1.1/foo/bar"); | 164 testURLParsing(test, "http://192.168.1.1/foo/bar"); |
175 testURLParsing( | 165 testURLParsing( |
176 test, | 166 test, |
177 "http://192.168.1.1/foo/bar?https://random/foo/bar" | 167 "http://192.168.1.1/foo/bar?https://random/foo/bar" |
178 ); | 168 ); |
179 testURLParsing( | 169 testURLParsing( |
180 test, | 170 test, |
181 "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" | 171 "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" |
182 ); | 172 ); |
183 testURLParsing( | 173 testURLParsing( |
184 test, | 174 test, |
185 "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" | 175 "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
186 ); | 176 ); |
187 testURLParsing( | 177 testURLParsing( |
188 test, | 178 test, |
189 "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" | 179 "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
190 ); | 180 ); |
191 | 181 |
192 testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]"); | 182 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/"); |
193 testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/"); | 183 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/foo"); |
194 testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo"); | 184 testURLParsing( |
195 testURLParsing( | 185 test, |
196 test, | 186 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar" |
197 "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo/bar" | 187 ); |
198 ); | 188 testURLParsing( |
199 testURLParsing( | 189 test, |
200 test, | 190 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar?https://random/foo/bar" |
201 "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo/bar?https://random/foo
/bar" | 191 ); |
202 ); | 192 testURLParsing( |
203 testURLParsing( | 193 test, |
204 test, | 194 "http://[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo/bar" |
205 "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://rando
m/foo/bar" | 195 ); |
206 ); | 196 testURLParsing( |
207 testURLParsing( | 197 test, |
208 test, | 198 "http://user@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo
/bar" |
209 "http://user@[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://
random/foo/bar" | 199 ); |
210 ); | 200 testURLParsing( |
211 testURLParsing( | 201 test, |
212 test, | 202 "http://user:pass@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://rando
m/foo/bar" |
213 "http://user:pass@[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?htt
ps://random/foo/bar" | 203 ); |
214 ); | 204 |
215 | |
216 testURLParsing(test, "ftp://user:pass@example.com:8021"); | |
217 testURLParsing(test, "ftp://user:pass@example.com:8021/"); | 205 testURLParsing(test, "ftp://user:pass@example.com:8021/"); |
218 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); | 206 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
219 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); | 207 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
220 | 208 |
221 testURLParsing(test, "about:blank"); | 209 testURLParsing(test, "about:blank"); |
222 testURLParsing(test, "chrome://extensions"); | 210 testURLParsing(test, "chrome://extensions"); |
223 testURLParsing( | 211 testURLParsing( |
224 test, | 212 test, |
225 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" | 213 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
226 ); | 214 ); |
(...skipping 13 matching lines...) Expand all Loading... |
240 test, | 228 test, |
241 "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678" | 229 "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678" |
242 ); | 230 ); |
243 | 231 |
244 testURLParsing(test, "javascript:"); | 232 testURLParsing(test, "javascript:"); |
245 testURLParsing(test, "javascript:alert();"); | 233 testURLParsing(test, "javascript:alert();"); |
246 testURLParsing(test, "javascript:foo/bar/"); | 234 testURLParsing(test, "javascript:foo/bar/"); |
247 testURLParsing(test, "javascript://foo/bar/"); | 235 testURLParsing(test, "javascript://foo/bar/"); |
248 | 236 |
249 testURLParsing(test, "file:///dev/random"); | 237 testURLParsing(test, "file:///dev/random"); |
| 238 |
| 239 testURLParsing(test, "wss://example.com/"); |
| 240 testURLParsing(test, "wss://example.com:8080/"); |
| 241 testURLParsing(test, "wss://user@example.com:8080/"); |
| 242 testURLParsing(test, "wss://user:pass@example.com:8080/"); |
| 243 |
| 244 testURLParsing(test, "stuns:stuns.example.com/"); |
| 245 testURLParsing(test, "stuns:stuns.example.com:8080/"); |
| 246 testURLParsing(test, "stuns:user@stuns.example.com:8080/"); |
| 247 testURLParsing(test, "stuns:user:pass@stuns.example.com:8080/"); |
| 248 |
| 249 // The following tests are based on |
| 250 // https://cs.chromium.org/chromium/src/url/gurl_unittest.cc?rcl=9ec7bc85e0f6a
0bf28eff6b2eca678067da547e9 |
| 251 // Note: We do not check for "canonicalization" (normalization). parseURL() |
| 252 // should be used with normalized URLs only. |
| 253 |
| 254 testURLParsing(test, "something:///example.com/"); |
| 255 testURLParsing(test, "something://example.com/"); |
| 256 |
| 257 testURLParsing(test, "file:///C:/foo.txt"); |
| 258 testURLParsing(test, "file://server/foo.txt"); |
| 259 |
| 260 testURLParsing(test, "http://user:pass@example.com:99/foo;bar?q=a#ref"); |
| 261 |
| 262 testURLParsing(test, "http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/"); |
| 263 |
| 264 testURLParsing(test, "filesystem:http://example.com/temporary/"); |
| 265 testURLParsing( |
| 266 test, |
| 267 "filesystem:http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/" |
| 268 ); |
| 269 |
| 270 testURLParsing(test, "javascript:window.alert('hello, world');"); |
| 271 testURLParsing(test, "javascript:#"); |
| 272 |
| 273 testURLParsing( |
| 274 test, |
| 275 "blob:https://example.com/7ce70a1e-9681-4148-87a8-43cb9171b994" |
| 276 ); |
| 277 |
| 278 testURLParsing(test, "http://[2001:db8::1]/"); |
| 279 testURLParsing(test, "http://[2001:db8::1]:8080/"); |
| 280 testURLParsing(test, "http://[::]:8080/"); |
| 281 |
| 282 testURLParsing(test, "not-a-standard-scheme:this is arbitrary content"); |
| 283 testURLParsing(test, "view-source:http://example.com/path"); |
| 284 |
| 285 testURLParsing( |
| 286 test, |
| 287 "data:text/html,Question?%3Cdiv%20style=%22color:%20#bad%22%3Eidea%3C/div%3E
" |
| 288 ); |
250 | 289 |
251 test.done(); | 290 test.done(); |
252 }; | 291 }; |
253 | 292 |
254 exports.testNormalizeHostname = function(test) | 293 exports.testNormalizeHostname = function(test) |
255 { | 294 { |
256 test.equal(normalizeHostname("example.com"), "example.com"); | 295 test.equal(normalizeHostname("example.com"), "example.com"); |
257 test.equal(normalizeHostname("example.com."), "example.com"); | 296 test.equal(normalizeHostname("example.com."), "example.com"); |
258 test.equal(normalizeHostname("example.com.."), "example.com"); | 297 test.equal(normalizeHostname("example.com.."), "example.com"); |
259 test.equal(normalizeHostname("example.com..."), "example.com"); | 298 test.equal(normalizeHostname("example.com..."), "example.com"); |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 "us-east-1.amazonaws.com" | 514 "us-east-1.amazonaws.com" |
476 ); | 515 ); |
477 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 516 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |
478 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 517 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |
479 | 518 |
480 // Edge case. | 519 // Edge case. |
481 test.equal(getBaseDomain(""), ""); | 520 test.equal(getBaseDomain(""), ""); |
482 | 521 |
483 test.done(); | 522 test.done(); |
484 }; | 523 }; |
LEFT | RIGHT |