OLD | NEW |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 // Only starting NodeJS 10 that URL is in the global space. | 20 // Only starting NodeJS 10 that URL is in the global space. |
21 const {URL} = require("url"); | 21 const {URL} = require("url"); |
22 const {createSandbox} = require("./_common"); | 22 const {createSandbox} = require("./_common"); |
23 | 23 |
24 const publicSuffixes = require("../data/publicSuffixList.json"); | 24 const publicSuffixes = require("../data/publicSuffixList.json"); |
25 | 25 |
| 26 let parseURL = null; |
26 let normalizeHostname = null; | 27 let normalizeHostname = null; |
27 let domainSuffixes = null; | 28 let domainSuffixes = null; |
28 let isThirdParty = null; | 29 let isThirdParty = null; |
29 let getBaseDomain = null; | 30 let getBaseDomain = null; |
30 | 31 |
31 exports.setUp = function(callback) | 32 exports.setUp = function(callback) |
32 { | 33 { |
33 let sandboxedRequire = createSandbox({ | 34 let sandboxedRequire = createSandbox({ |
34 extraExports: { | 35 extraExports: { |
35 domain: ["getBaseDomain"] | 36 domain: ["getBaseDomain"] |
36 } | 37 } |
37 }); | 38 }); |
38 ( | 39 ( |
39 {normalizeHostname, domainSuffixes, isThirdParty, | 40 {parseURL, normalizeHostname, domainSuffixes, isThirdParty, |
40 getBaseDomain} = sandboxedRequire("../lib/url") | 41 getBaseDomain} = sandboxedRequire("../lib/url") |
41 ); | 42 ); |
42 | 43 |
43 callback(); | 44 callback(); |
44 }; | 45 }; |
45 | 46 |
46 function hostnameToURL(hostname) | 47 function hostnameToURL(hostname) |
47 { | 48 { |
48 return new URL("http://" + hostname); | 49 return new URL("http://" + hostname); |
49 } | 50 } |
50 | 51 |
| 52 function testURLParsing(test, url) |
| 53 { |
| 54 // The function expects a normalized URL. |
| 55 url = new URL(url).href; |
| 56 |
| 57 let urlInfo = parseURL(url); |
| 58 |
| 59 // We need to ensure only that our implementation matches that of the URL |
| 60 // object. |
| 61 let urlObject = new URL(url); |
| 62 |
| 63 test.equal(urlInfo.href, urlObject.href); |
| 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 + ""); |
| 70 } |
| 71 |
51 function testThirdParty(test, requestHostname, documentHostname, expected, | 72 function testThirdParty(test, requestHostname, documentHostname, expected, |
52 message) | 73 message) |
53 { | 74 { |
54 test.equal( | 75 test.equal( |
55 isThirdParty( | 76 isThirdParty( |
56 hostnameToURL(requestHostname), | 77 hostnameToURL(requestHostname), |
57 | 78 |
58 // Chrome's URL object normalizes IP addresses. So some test | 79 // Chrome's URL object normalizes IP addresses. So some test |
59 // 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. |
60 hostnameToURL(documentHostname).hostname | 81 hostnameToURL(documentHostname).hostname |
61 ), | 82 ), |
62 expected, | 83 expected, |
63 message | 84 message |
64 ); | 85 ); |
65 } | 86 } |
66 | 87 |
| 88 exports.testParseURL = function(test) |
| 89 { |
| 90 testURLParsing(test, "https://example.com"); |
| 91 testURLParsing(test, "https://example.com/"); |
| 92 testURLParsing(test, "https://example.com/foo"); |
| 93 testURLParsing(test, "https://example.com/foo/bar"); |
| 94 testURLParsing( |
| 95 test, |
| 96 "https://example.com/foo/bar?https://random/foo/bar" |
| 97 ); |
| 98 |
| 99 testURLParsing(test, "https://example.com:8080"); |
| 100 testURLParsing(test, "https://example.com:8080/"); |
| 101 testURLParsing(test, "https://example.com:8080/foo"); |
| 102 testURLParsing(test, "https://example.com:8080/foo/bar"); |
| 103 testURLParsing( |
| 104 test, |
| 105 "https://example.com:8080/foo/bar?https://random/foo/bar" |
| 106 ); |
| 107 |
| 108 testURLParsing(test, "http://localhost"); |
| 109 testURLParsing(test, "http://localhost/"); |
| 110 testURLParsing(test, "http://localhost/foo"); |
| 111 testURLParsing(test, "http://localhost/foo/bar"); |
| 112 testURLParsing( |
| 113 test, |
| 114 "http://localhost/foo/bar?https://random/foo/bar" |
| 115 ); |
| 116 |
| 117 testURLParsing(test, "http://192.168.1.1"); |
| 118 testURLParsing(test, "http://192.168.1.1/"); |
| 119 testURLParsing(test, "http://192.168.1.1/foo"); |
| 120 testURLParsing(test, "http://192.168.1.1/foo/bar"); |
| 121 testURLParsing( |
| 122 test, |
| 123 "http://192.168.1.1/foo/bar?https://random/foo/bar" |
| 124 ); |
| 125 |
| 126 testURLParsing(test, "https://user@example.com"); |
| 127 testURLParsing(test, "https://user@example.com/"); |
| 128 testURLParsing(test, "https://user@example.com/foo"); |
| 129 testURLParsing(test, "https://user@example.com/foo/bar"); |
| 130 testURLParsing( |
| 131 test, |
| 132 "https://user@example.com/foo/bar?https://random/foo/bar" |
| 133 ); |
| 134 |
| 135 testURLParsing(test, "https://user@example.com:8080"); |
| 136 testURLParsing(test, "https://user@example.com:8080/"); |
| 137 testURLParsing(test, "https://user@example.com:8080/foo"); |
| 138 testURLParsing(test, "https://user@example.com:8080/foo/bar"); |
| 139 testURLParsing( |
| 140 test, |
| 141 "https://user@example.com:8080/foo/bar?https://random/foo/bar" |
| 142 ); |
| 143 |
| 144 testURLParsing(test, "https://user:pass@example.com"); |
| 145 testURLParsing(test, "https://user:pass@example.com/"); |
| 146 testURLParsing(test, "https://user:pass@example.com/foo"); |
| 147 testURLParsing(test, "https://user:pass@example.com/foo/bar"); |
| 148 testURLParsing( |
| 149 test, |
| 150 "https://user:pass@example.com/foo/bar?https://random/foo/bar" |
| 151 ); |
| 152 |
| 153 testURLParsing(test, "https://user:pass@example.com:8080"); |
| 154 testURLParsing(test, "https://user:pass@example.com:8080/"); |
| 155 testURLParsing(test, "https://user:pass@example.com:8080/foo"); |
| 156 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); |
| 157 testURLParsing( |
| 158 test, |
| 159 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" |
| 160 ); |
| 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/"); |
| 182 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
| 183 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
| 184 |
| 185 testURLParsing(test, "about:blank"); |
| 186 testURLParsing(test, "chrome://extensions"); |
| 187 testURLParsing( |
| 188 test, |
| 189 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
| 190 ); |
| 191 testURLParsing(test, "mailto:john.doe@mail.example.com"); |
| 192 |
| 193 testURLParsing(test, "news:newsgroup"); |
| 194 testURLParsing(test, "news:message-id"); |
| 195 testURLParsing(test, "nntp://example.com:8119/newsgroup"); |
| 196 testURLParsing(test, "nntp://example.com:8119/message-id"); |
| 197 |
| 198 testURLParsing(test, "data:,"); |
| 199 testURLParsing( |
| 200 test, |
| 201 "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh" |
| 202 ); |
| 203 testURLParsing( |
| 204 test, |
| 205 "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678" |
| 206 ); |
| 207 |
| 208 testURLParsing(test, "javascript:"); |
| 209 testURLParsing(test, "javascript:alert();"); |
| 210 testURLParsing(test, "javascript:foo/bar/"); |
| 211 testURLParsing(test, "javascript://foo/bar/"); |
| 212 |
| 213 testURLParsing(test, "file:///dev/random"); |
| 214 |
| 215 test.done(); |
| 216 }; |
| 217 |
67 exports.testNormalizeHostname = function(test) | 218 exports.testNormalizeHostname = function(test) |
68 { | 219 { |
69 test.equal(normalizeHostname("example.com"), "example.com"); | 220 test.equal(normalizeHostname("example.com"), "example.com"); |
70 test.equal(normalizeHostname("example.com."), "example.com"); | 221 test.equal(normalizeHostname("example.com."), "example.com"); |
71 test.equal(normalizeHostname("example.com.."), "example.com"); | 222 test.equal(normalizeHostname("example.com.."), "example.com"); |
72 test.equal(normalizeHostname("example.com..."), "example.com"); | 223 test.equal(normalizeHostname("example.com..."), "example.com"); |
73 | 224 |
74 test.equal(normalizeHostname("Example.com"), "example.com"); | 225 test.equal(normalizeHostname("Example.com"), "example.com"); |
75 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); | 226 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); |
76 test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); | 227 test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 "us-east-1.amazonaws.com" | 439 "us-east-1.amazonaws.com" |
289 ); | 440 ); |
290 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 441 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |
291 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 442 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |
292 | 443 |
293 // Edge case. | 444 // Edge case. |
294 test.equal(getBaseDomain(""), ""); | 445 test.equal(getBaseDomain(""), ""); |
295 | 446 |
296 test.done(); | 447 test.done(); |
297 }; | 448 }; |
OLD | NEW |