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 { | 20 { |
21 let {getDecodedHostname, | 21 let {extractHostFromFrame, |
22 extractHostFromFrame, | |
23 stringifyURL, | |
24 isThirdParty} = require("../../lib/url"); | 22 isThirdParty} = require("../../lib/url"); |
25 | 23 |
26 QUnit.module("URL/host tools"); | 24 QUnit.module("URL/host tools"); |
27 | 25 |
28 test("Extracting hostname from URL", () => | 26 test("Extracting hostname from URL", () => |
29 { | 27 { |
30 function testURLHostname(url, expectedHostname, message) | 28 function testURLHostname(url, expectedHostname, message) |
31 { | 29 { |
32 equal(getDecodedHostname(new URL(url)), expectedHostname, message); | 30 equal(new URL(url).hostname, expectedHostname, message); |
33 } | 31 } |
34 | 32 |
35 testURLHostname("http://example.com/foo", "example.com", "with path"); | 33 testURLHostname("http://example.com/foo", "example.com", "with path"); |
36 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); | 34 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); |
37 testURLHostname("http://example.com/#top", "example.com", "with hash"); | 35 testURLHostname("http://example.com/#top", "example.com", "with hash"); |
38 testURLHostname("http://example.com:8080/", "example.com", "with port"); | 36 testURLHostname("http://example.com:8080/", "example.com", "with port"); |
39 testURLHostname("http://user:password@example.com/", "example.com", | 37 testURLHostname("http://user:password@example.com/", "example.com", |
40 "with auth credentials"); | 38 "with auth credentials"); |
41 testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", | 39 testURLHostname("http://xn--f-1gaa.com/", "xn--f-1gaa.com", |
42 "with punycode"); | 40 "with punycode"); |
43 testURLHostname("about:blank", "", "about:blank"); | 41 testURLHostname("about:blank", "", "about:blank"); |
44 testURLHostname("data:text/plain,foo", "", "data: URL"); | 42 testURLHostname("data:text/plain,foo", "", "data: URL"); |
45 testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); | 43 testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); |
46 testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); | 44 testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); |
47 testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", | 45 testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", |
48 "[2001:db8:85a3::8a2e:370:7334]", "IPv6 address"); | 46 "[2001:db8:85a3::8a2e:370:7334]", "IPv6 address"); |
49 }); | 47 }); |
50 | 48 |
51 test("Extracting hostname from frame", () => | 49 test("Extracting hostname from frame", () => |
(...skipping 10 matching lines...) Expand all Loading... |
62 | 60 |
63 testFrameHostname(["http://example.com/"], "example.com", "single frame"); | 61 testFrameHostname(["http://example.com/"], "example.com", "single frame"); |
64 testFrameHostname(["http://example.com/", "http://example.org/"], | 62 testFrameHostname(["http://example.com/", "http://example.org/"], |
65 "example.org", "with parent frame"); | 63 "example.org", "with parent frame"); |
66 testFrameHostname(["http://example.com/", "data:text/plain,foo"], | 64 testFrameHostname(["http://example.com/", "data:text/plain,foo"], |
67 "example.com", "data: URL, hostname in parent"); | 65 "example.com", "data: URL, hostname in parent"); |
68 testFrameHostname(["http://example.com/", "about:blank", "about:blank"], | 66 testFrameHostname(["http://example.com/", "about:blank", "about:blank"], |
69 "example.com", "about:blank, hostname in ancestor"); | 67 "example.com", "about:blank, hostname in ancestor"); |
70 testFrameHostname(["about:blank", "about:blank"], "", | 68 testFrameHostname(["about:blank", "about:blank"], "", |
71 "about:blank, no hostname"); | 69 "about:blank, no hostname"); |
72 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", | 70 testFrameHostname(["http://xn--f-1gaa.com/"], "xn--f-1gaa.com", |
73 "with punycode"); | 71 "with punycode"); |
74 }); | 72 }); |
75 | 73 |
76 test("Stringifying URLs", () => | |
77 { | |
78 function testNormalizedURL(url, expectedURL, message) | |
79 { | |
80 equal(stringifyURL(new URL(url)), expectedURL, message); | |
81 } | |
82 | |
83 function testPreservedURL(url, message) | |
84 { | |
85 testNormalizedURL(url, url, message); | |
86 } | |
87 | |
88 testPreservedURL("http://example.com/foo", "includes path"); | |
89 testPreservedURL("http://example.com/?foo=bar", "includes query"); | |
90 testPreservedURL("http://example.com:8080/", "includes port"); | |
91 testPreservedURL("http://example.com/?", "with empty query string"); | |
92 testNormalizedURL("http://example.com/#top", "http://example.com/", | |
93 "stripped hash"); | |
94 testNormalizedURL("http://example.com/#top?", "http://example.com/", | |
95 "stripped hash with trailing question mark"); | |
96 testNormalizedURL("http://xn--f-1gaa.com/", "http://f\u00f6\u00f6.com/", | |
97 "decoded punycode"); | |
98 testPreservedURL("about:blank", "about:blank"); | |
99 testPreservedURL("data:text/plain,foo", "data: URL"); | |
100 }); | |
101 | |
102 test("Third-party checks", () => | 74 test("Third-party checks", () => |
103 { | 75 { |
104 function hostnameToURL(hostname) | 76 function hostnameToURL(hostname) |
105 { | 77 { |
106 return new URL("http://" + hostname); | 78 return new URL("http://" + hostname); |
107 } | 79 } |
108 | 80 |
109 function testThirdParty(requestHost, documentHost, expected, message) | 81 function testThirdParty(requestHost, documentHost, expected, message) |
110 { | 82 { |
111 equal( | 83 equal( |
112 isThirdParty( | 84 isThirdParty( |
113 hostnameToURL(requestHost), | 85 hostnameToURL(requestHost), |
114 | 86 |
115 // Chrome's URL object normalizes IP addresses. So some test | 87 // Chrome's URL object normalizes IP addresses. So some test |
116 // will fail if we don't normalize the document host as well. | 88 // will fail if we don't normalize the document host as well. |
117 getDecodedHostname(hostnameToURL(documentHost)) | 89 hostnameToURL(documentHost).hostname |
118 ), | 90 ), |
119 expected, | 91 expected, |
120 message | 92 message |
121 ); | 93 ); |
122 } | 94 } |
123 | 95 |
124 testThirdParty("foo", "foo", false, "same domain isn't third-party"); | 96 testThirdParty("foo", "foo", false, "same domain isn't third-party"); |
125 testThirdParty("foo", "bar", true, "different domain is third-party"); | 97 testThirdParty("foo", "bar", true, "different domain is third-party"); |
126 testThirdParty("foo.com", "foo.com", false, | 98 testThirdParty("foo.com", "foo.com", false, |
127 "same domain with TLD (.com) isn't third-party"); | 99 "same domain with TLD (.com) isn't third-party"); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 testThirdParty( | 144 testThirdParty( |
173 "[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, | 145 "[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, |
174 "different IPv4-mapped IPv6 address is third-party" | 146 "different IPv4-mapped IPv6 address is third-party" |
175 ); | 147 ); |
176 testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, | 148 testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, |
177 "same IDN isn't third-party"); | 149 "same IDN isn't third-party"); |
178 testThirdParty("example.com..", "example.com....", false, | 150 testThirdParty("example.com..", "example.com....", false, |
179 "traling dots are ignored"); | 151 "traling dots are ignored"); |
180 }); | 152 }); |
181 } | 153 } |
OLD | NEW |