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, "http://192.168.1.1"); | 114 testURLParsing(test, "https://user@example.com/"); |
| 115 testURLParsing(test, "https://user@example.com/foo"); |
| 116 testURLParsing(test, "https://user@example.com/foo/bar"); |
| 117 testURLParsing( |
| 118 test, |
| 119 "https://user@example.com/foo/bar?https://random/foo/bar" |
| 120 ); |
| 121 |
| 122 testURLParsing(test, "https://user@example.com:8080/"); |
| 123 testURLParsing(test, "https://user@example.com:8080/foo"); |
| 124 testURLParsing(test, "https://user@example.com:8080/foo/bar"); |
| 125 testURLParsing( |
| 126 test, |
| 127 "https://user@example.com:8080/foo/bar?https://random/foo/bar" |
| 128 ); |
| 129 |
| 130 testURLParsing(test, "https://user:pass@example.com/"); |
| 131 testURLParsing(test, "https://user:pass@example.com/foo"); |
| 132 testURLParsing(test, "https://user:pass@example.com/foo/bar"); |
| 133 testURLParsing( |
| 134 test, |
| 135 "https://user:pass@example.com/foo/bar?https://random/foo/bar" |
| 136 ); |
| 137 |
| 138 testURLParsing(test, "https://user:pass@example.com:8080/"); |
| 139 testURLParsing(test, "https://user:pass@example.com:8080/foo"); |
| 140 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); |
| 141 testURLParsing( |
| 142 test, |
| 143 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" |
| 144 ); |
| 145 |
| 146 testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); |
| 147 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); |
| 148 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); |
| 149 testURLParsing( |
| 150 test, |
| 151 "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" |
| 152 ); |
| 153 |
| 154 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); |
| 155 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); |
| 156 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); |
| 157 testURLParsing( |
| 158 test, |
| 159 "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" |
| 160 ); |
| 161 |
118 testURLParsing(test, "http://192.168.1.1/"); | 162 testURLParsing(test, "http://192.168.1.1/"); |
119 testURLParsing(test, "http://192.168.1.1/foo"); | 163 testURLParsing(test, "http://192.168.1.1/foo"); |
120 testURLParsing(test, "http://192.168.1.1/foo/bar"); | 164 testURLParsing(test, "http://192.168.1.1/foo/bar"); |
121 testURLParsing( | 165 testURLParsing( |
122 test, | 166 test, |
123 "http://192.168.1.1/foo/bar?https://random/foo/bar" | 167 "http://192.168.1.1/foo/bar?https://random/foo/bar" |
124 ); | 168 ); |
125 | 169 testURLParsing( |
126 testURLParsing(test, "https://user@example.com"); | 170 test, |
127 testURLParsing(test, "https://user@example.com/"); | 171 "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" |
128 testURLParsing(test, "https://user@example.com/foo"); | 172 ); |
129 testURLParsing(test, "https://user@example.com/foo/bar"); | 173 testURLParsing( |
130 testURLParsing( | 174 test, |
131 test, | 175 "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
132 "https://user@example.com/foo/bar?https://random/foo/bar" | 176 ); |
133 ); | 177 testURLParsing( |
134 | 178 test, |
135 testURLParsing(test, "https://user@example.com:8080"); | 179 "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
136 testURLParsing(test, "https://user@example.com:8080/"); | 180 ); |
137 testURLParsing(test, "https://user@example.com:8080/foo"); | 181 |
138 testURLParsing(test, "https://user@example.com:8080/foo/bar"); | 182 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/"); |
139 testURLParsing( | 183 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/foo"); |
140 test, | 184 testURLParsing( |
141 "https://user@example.com:8080/foo/bar?https://random/foo/bar" | 185 test, |
142 ); | 186 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar" |
143 | 187 ); |
144 testURLParsing(test, "https://user:pass@example.com"); | 188 testURLParsing( |
145 testURLParsing(test, "https://user:pass@example.com/"); | 189 test, |
146 testURLParsing(test, "https://user:pass@example.com/foo"); | 190 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar?https://random/foo/bar" |
147 testURLParsing(test, "https://user:pass@example.com/foo/bar"); | 191 ); |
148 testURLParsing( | 192 testURLParsing( |
149 test, | 193 test, |
150 "https://user:pass@example.com/foo/bar?https://random/foo/bar" | 194 "http://[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo/bar" |
151 ); | 195 ); |
152 | 196 testURLParsing( |
153 testURLParsing(test, "https://user:pass@example.com:8080"); | 197 test, |
154 testURLParsing(test, "https://user:pass@example.com:8080/"); | 198 "http://user@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo
/bar" |
155 testURLParsing(test, "https://user:pass@example.com:8080/foo"); | 199 ); |
156 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); | 200 testURLParsing( |
157 testURLParsing( | 201 test, |
158 test, | 202 "http://user:pass@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://rando
m/foo/bar" |
159 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" | 203 ); |
160 ); | 204 |
161 | |
162 testURLParsing(test, "https://us%40er:pa%40ss@example.com"); | |
163 testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); | |
164 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); | |
165 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); | |
166 testURLParsing( | |
167 test, | |
168 "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" | |
169 ); | |
170 | |
171 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080"); | |
172 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); | |
173 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); | |
174 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); | |
175 testURLParsing( | |
176 test, | |
177 "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" | |
178 ); | |
179 | |
180 testURLParsing(test, "ftp://user:pass@example.com:8021"); | |
181 testURLParsing(test, "ftp://user:pass@example.com:8021/"); | 205 testURLParsing(test, "ftp://user:pass@example.com:8021/"); |
182 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); | 206 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
183 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); | 207 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
184 | 208 |
185 testURLParsing(test, "about:blank"); | 209 testURLParsing(test, "about:blank"); |
186 testURLParsing(test, "chrome://extensions"); | 210 testURLParsing(test, "chrome://extensions"); |
187 testURLParsing( | 211 testURLParsing( |
188 test, | 212 test, |
189 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" | 213 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
190 ); | 214 ); |
(...skipping 13 matching lines...) Expand all Loading... |
204 test, | 228 test, |
205 "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" |
206 ); | 230 ); |
207 | 231 |
208 testURLParsing(test, "javascript:"); | 232 testURLParsing(test, "javascript:"); |
209 testURLParsing(test, "javascript:alert();"); | 233 testURLParsing(test, "javascript:alert();"); |
210 testURLParsing(test, "javascript:foo/bar/"); | 234 testURLParsing(test, "javascript:foo/bar/"); |
211 testURLParsing(test, "javascript://foo/bar/"); | 235 testURLParsing(test, "javascript://foo/bar/"); |
212 | 236 |
213 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 ); |
214 | 289 |
215 test.done(); | 290 test.done(); |
216 }; | 291 }; |
217 | 292 |
218 exports.testNormalizeHostname = function(test) | 293 exports.testNormalizeHostname = function(test) |
219 { | 294 { |
220 test.equal(normalizeHostname("example.com"), "example.com"); | 295 test.equal(normalizeHostname("example.com"), "example.com"); |
221 test.equal(normalizeHostname("example.com."), "example.com"); | 296 test.equal(normalizeHostname("example.com."), "example.com"); |
222 test.equal(normalizeHostname("example.com.."), "example.com"); | 297 test.equal(normalizeHostname("example.com.."), "example.com"); |
223 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... |
439 "us-east-1.amazonaws.com" | 514 "us-east-1.amazonaws.com" |
440 ); | 515 ); |
441 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 516 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |
442 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 517 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |
443 | 518 |
444 // Edge case. | 519 // Edge case. |
445 test.equal(getBaseDomain(""), ""); | 520 test.equal(getBaseDomain(""), ""); |
446 | 521 |
447 test.done(); | 522 test.done(); |
448 }; | 523 }; |
LEFT | RIGHT |