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 let {protocol, hostname} = parseURL(url); | 54 // Note: The function expects a normalized URL. |
| 55 // e.g. "http:example.com:80?foo" should already be normalized to |
| 56 // "http://example.com/?foo". If not, the tests will fail. |
| 57 let urlInfo = parseURL(url); |
55 | 58 |
56 // 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 |
57 // object. | 60 // object. |
58 let urlObject = new URL(url); | 61 let urlObject = new URL(url); |
59 | 62 |
60 test.equal(protocol, urlObject.protocol); | 63 test.equal(urlInfo.href, urlObject.href); |
61 test.equal(hostname, urlObject.hostname); | 64 test.equal(urlInfo.protocol, urlObject.protocol); |
| 65 test.equal(urlInfo.hostname, urlObject.hostname); |
| 66 |
| 67 test.equal(urlInfo.toString(), urlObject.toString()); |
| 68 test.equal(String(urlInfo), String(urlObject)); |
| 69 test.equal(urlInfo + "", urlObject + ""); |
62 } | 70 } |
63 | 71 |
64 function testThirdParty(test, requestHostname, documentHostname, expected, | 72 function testThirdParty(test, requestHostname, documentHostname, expected, |
65 message) | 73 message) |
66 { | 74 { |
67 test.equal( | 75 test.equal( |
68 isThirdParty( | 76 isThirdParty( |
69 hostnameToURL(requestHostname), | 77 hostnameToURL(requestHostname), |
70 | 78 |
71 // Chrome's URL object normalizes IP addresses. So some test | 79 // Chrome's URL object normalizes IP addresses. So some test |
72 // 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. |
73 hostnameToURL(documentHostname).hostname | 81 hostnameToURL(documentHostname).hostname |
74 ), | 82 ), |
75 expected, | 83 expected, |
76 message | 84 message |
77 ); | 85 ); |
78 } | 86 } |
79 | 87 |
80 exports.testParseURL = function(test) | 88 exports.testParseURL = function(test) |
81 { | 89 { |
82 testURLParsing(test, "https://example.com"); | |
83 testURLParsing(test, "https://example.com/"); | 90 testURLParsing(test, "https://example.com/"); |
84 testURLParsing(test, "https://example.com/foo"); | 91 testURLParsing(test, "https://example.com/foo"); |
85 testURLParsing(test, "https://example.com/foo/bar"); | 92 testURLParsing(test, "https://example.com/foo/bar"); |
86 testURLParsing( | 93 testURLParsing( |
87 test, | 94 test, |
88 "https://example.com/foo/bar?https://random.com/foo/bar" | 95 "https://example.com/foo/bar?https://random/foo/bar" |
89 ); | 96 ); |
90 | 97 |
91 testURLParsing(test, "https://example.com:8080"); | |
92 testURLParsing(test, "https://example.com:8080/"); | 98 testURLParsing(test, "https://example.com:8080/"); |
93 testURLParsing(test, "https://example.com:8080/foo"); | 99 testURLParsing(test, "https://example.com:8080/foo"); |
94 testURLParsing(test, "https://example.com:8080/foo/bar"); | 100 testURLParsing(test, "https://example.com:8080/foo/bar"); |
95 testURLParsing( | 101 testURLParsing( |
96 test, | 102 test, |
97 "https://example.com:8080/foo/bar?https://random.com/foo/bar" | 103 "https://example.com:8080/foo/bar?https://random/foo/bar" |
98 ); | 104 ); |
99 | 105 |
100 testURLParsing(test, "http://localhost"); | |
101 testURLParsing(test, "http://localhost/"); | 106 testURLParsing(test, "http://localhost/"); |
102 testURLParsing(test, "http://localhost/foo"); | 107 testURLParsing(test, "http://localhost/foo"); |
103 testURLParsing(test, "http://localhost/foo/bar"); | 108 testURLParsing(test, "http://localhost/foo/bar"); |
104 testURLParsing( | 109 testURLParsing( |
105 test, | 110 test, |
106 "http://localhost/foo/bar?https://random.com/foo/bar" | 111 "http://localhost/foo/bar?https://random/foo/bar" |
107 ); | 112 ); |
108 | 113 |
109 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 |
110 testURLParsing(test, "http://192.168.1.1/"); | 162 testURLParsing(test, "http://192.168.1.1/"); |
111 testURLParsing(test, "http://192.168.1.1/foo"); | 163 testURLParsing(test, "http://192.168.1.1/foo"); |
112 testURLParsing(test, "http://192.168.1.1/foo/bar"); | 164 testURLParsing(test, "http://192.168.1.1/foo/bar"); |
113 testURLParsing( | 165 testURLParsing( |
114 test, | 166 test, |
115 "http://192.168.1.1/foo/bar?https://random.com/foo/bar" | 167 "http://192.168.1.1/foo/bar?https://random/foo/bar" |
116 ); | 168 ); |
117 | 169 testURLParsing( |
118 testURLParsing(test, "https://user@example.com"); | 170 test, |
119 testURLParsing(test, "https://user@example.com/"); | 171 "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" |
120 testURLParsing(test, "https://user@example.com/foo"); | 172 ); |
121 testURLParsing(test, "https://user@example.com/foo/bar"); | 173 testURLParsing( |
122 testURLParsing( | 174 test, |
123 test, | 175 "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
124 "https://user@example.com/foo/bar?https://random.com/foo/bar" | 176 ); |
125 ); | 177 testURLParsing( |
126 | 178 test, |
127 testURLParsing(test, "https://user@example.com:8080"); | 179 "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
128 testURLParsing(test, "https://user@example.com:8080/"); | 180 ); |
129 testURLParsing(test, "https://user@example.com:8080/foo"); | 181 |
130 testURLParsing(test, "https://user@example.com:8080/foo/bar"); | 182 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/"); |
131 testURLParsing( | 183 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/foo"); |
132 test, | 184 testURLParsing( |
133 "https://user@example.com:8080/foo/bar?https://random.com/foo/bar" | 185 test, |
134 ); | 186 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar" |
135 | 187 ); |
136 testURLParsing(test, "https://user:pass@example.com"); | 188 testURLParsing( |
137 testURLParsing(test, "https://user:pass@example.com/"); | 189 test, |
138 testURLParsing(test, "https://user:pass@example.com/foo"); | 190 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar?https://random/foo/bar" |
139 testURLParsing(test, "https://user:pass@example.com/foo/bar"); | 191 ); |
140 testURLParsing( | 192 testURLParsing( |
141 test, | 193 test, |
142 "https://user:pass@example.com/foo/bar?https://random.com/foo/bar" | 194 "http://[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo/bar" |
143 ); | 195 ); |
144 | 196 testURLParsing( |
145 testURLParsing(test, "https://user:pass@example.com:8080"); | 197 test, |
146 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" |
147 testURLParsing(test, "https://user:pass@example.com:8080/foo"); | 199 ); |
148 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); | 200 testURLParsing( |
149 testURLParsing( | 201 test, |
150 test, | 202 "http://user:pass@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://rando
m/foo/bar" |
151 "https://user:pass@example.com:8080/foo/bar?https://random.com/foo/bar" | 203 ); |
152 ); | 204 |
153 | 205 testURLParsing(test, "ftp://user:pass@example.com:8021/"); |
154 testURLParsing(test, "https://us@er:pa@ss@example.com"); | 206 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
155 testURLParsing(test, "https://us@er:pa@ss@example.com/"); | 207 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
156 testURLParsing(test, "https://us@er:pa@ss@example.com/foo"); | |
157 testURLParsing(test, "https://us@er:pa@ss@example.com/foo/bar"); | |
158 testURLParsing( | |
159 test, | |
160 "https://us@er:pa@ss@example.com/foo/bar?https://random.com/foo/bar" | |
161 ); | |
162 | |
163 testURLParsing(test, "https://us@er:pa@ss@example.com:8080"); | |
164 testURLParsing(test, "https://us@er:pa@ss@example.com:8080/"); | |
165 testURLParsing(test, "https://us@er:pa@ss@example.com:8080/foo"); | |
166 testURLParsing(test, "https://us@er:pa@ss@example.com:8080/foo/bar"); | |
167 testURLParsing( | |
168 test, | |
169 "https://us@er:pa@ss@example.com:8080/foo/bar?https://random.com/foo/bar" | |
170 ); | |
171 | |
172 testURLParsing(test, "ftp://user:pass@example.com:21"); | |
173 testURLParsing(test, "ftp://user:pass@example.com:21/"); | |
174 testURLParsing(test, "ftp://user:pass@example.com:21/foo"); | |
175 testURLParsing(test, "ftp://user:pass@example.com:21/foo/bar"); | |
176 | 208 |
177 testURLParsing(test, "about:blank"); | 209 testURLParsing(test, "about:blank"); |
178 testURLParsing(test, "chrome://extensions"); | 210 testURLParsing(test, "chrome://extensions"); |
179 testURLParsing( | 211 testURLParsing( |
180 test, | 212 test, |
181 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" | 213 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
182 ); | 214 ); |
183 testURLParsing(test, "mailto:john.doe@mail.example.com"); | 215 testURLParsing(test, "mailto:john.doe@mail.example.com"); |
184 | 216 |
185 testURLParsing(test, "news:newsgroup"); | 217 testURLParsing(test, "news:newsgroup"); |
186 testURLParsing(test, "news:message-id"); | 218 testURLParsing(test, "news:message-id"); |
187 testURLParsing(test, "nntp://example.com:119/newsgroup"); | 219 testURLParsing(test, "nntp://example.com:8119/newsgroup"); |
188 testURLParsing(test, "nntp://example.com:119/message-id"); | 220 testURLParsing(test, "nntp://example.com:8119/message-id"); |
189 | 221 |
190 testURLParsing(test, "data:,"); | 222 testURLParsing(test, "data:,"); |
191 testURLParsing( | 223 testURLParsing( |
192 test, | 224 test, |
193 "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh" | 225 "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh" |
194 ); | 226 ); |
195 testURLParsing( | 227 testURLParsing( |
196 test, | 228 test, |
197 "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" |
198 ); | 230 ); |
199 | 231 |
200 testURLParsing(test, "javascript:"); | 232 testURLParsing(test, "javascript:"); |
201 testURLParsing(test, "javascript:alert();"); | 233 testURLParsing(test, "javascript:alert();"); |
202 testURLParsing(test, "javascript:foo/bar/"); | 234 testURLParsing(test, "javascript:foo/bar/"); |
203 testURLParsing(test, "javascript://foo/bar/"); | 235 testURLParsing(test, "javascript://foo/bar/"); |
204 | 236 |
205 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 ); |
206 | 289 |
207 test.done(); | 290 test.done(); |
208 }; | 291 }; |
209 | 292 |
210 exports.testNormalizeHostname = function(test) | 293 exports.testNormalizeHostname = function(test) |
211 { | 294 { |
212 test.equal(normalizeHostname("example.com"), "example.com"); | 295 test.equal(normalizeHostname("example.com"), "example.com"); |
213 test.equal(normalizeHostname("example.com."), "example.com"); | 296 test.equal(normalizeHostname("example.com."), "example.com"); |
214 test.equal(normalizeHostname("example.com.."), "example.com"); | 297 test.equal(normalizeHostname("example.com.."), "example.com"); |
215 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... |
431 "us-east-1.amazonaws.com" | 514 "us-east-1.amazonaws.com" |
432 ); | 515 ); |
433 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 516 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |
434 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 517 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |
435 | 518 |
436 // Edge case. | 519 // Edge case. |
437 test.equal(getBaseDomain(""), ""); | 520 test.equal(getBaseDomain(""), ""); |
438 | 521 |
439 test.done(); | 522 test.done(); |
440 }; | 523 }; |
LEFT | RIGHT |