| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 | |
| 19 (function() | |
| 20 { | |
| 21 var url = require("url"); | |
| 22 var getDecodedHostname = url.getDecodedHostname; | |
| 23 var extractHostFromFrame = url.extractHostFromFrame; | |
| 24 var stringifyURL = url.stringifyURL; | |
| 25 var isThirdParty = url.isThirdParty; | |
| 26 | |
| 27 module("URL/host tools"); | |
| 28 | |
| 29 test("Extracting hostname from URL", function() | |
| 30 { | |
| 31 function testURLHostname(url, expectedHostname, message) | |
| 32 { | |
| 33 equal(getDecodedHostname(new URL(url)), expectedHostname, message); | |
| 34 } | |
| 35 | |
| 36 testURLHostname("http://example.com/foo", "example.com", "with path"); | |
| 37 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); | |
| 38 testURLHostname("http://example.com/#top", "example.com", "with hash"); | |
| 39 testURLHostname("http://example.com:8080/", "example.com", "with port"); | |
| 40 testURLHostname("http://user:password@example.com/", "example.com", "with au th credentials"); | |
| 41 testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", "with punycod e"); | |
| 42 testURLHostname("about:blank", "", "about:blank"); | |
| 43 testURLHostname("data:text/plain,foo", "", "data: URL"); | |
| 44 testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); | |
| 45 testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); | |
| 46 testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", "[2001:db8:85a3::8 a2e:370:7334]", "IPv6 address"); | |
| 47 }); | |
| 48 | |
| 49 test("Extracting hostname from frame", function() | |
| 50 { | |
| 51 function testFrameHostname(hierarchy, expectedHostname, message) | |
| 52 { | |
| 53 var frame = null; | |
| 54 | |
| 55 for (var i = 0; i < hierarchy.length; i++) | |
| 56 frame = {parent: frame, url: new URL(hierarchy[i])}; | |
| 57 | |
| 58 equal(extractHostFromFrame(frame), expectedHostname, message); | |
| 59 } | |
| 60 | |
| 61 testFrameHostname(["http://example.com/"], "example.com", "single frame"); | |
| 62 testFrameHostname(["http://example.com/", "http://example.org/"], "example.o rg", "with parent frame"); | |
| 63 testFrameHostname(["http://example.com/", "data:text/plain,foo"], "example.c om", "data: URL, hostname in parent"); | |
| 64 testFrameHostname(["http://example.com/", "about:blank", "about:blank"], "ex ample.com", "about:blank, hostname in ancestor"); | |
| 65 testFrameHostname(["about:blank", "about:blank"], "", "about:blank, no hostn ame"); | |
| 66 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun ycode"); | |
| 67 }); | |
| 68 | |
| 69 test("Stringifying URLs", function() | |
| 70 { | |
| 71 function testNormalizedURL(url, expectedURL, message) | |
| 72 { | |
| 73 equal(stringifyURL(new URL(url)), expectedURL, message); | |
| 74 } | |
| 75 | |
| 76 function testPreservedURL(url, message) | |
| 77 { | |
| 78 testNormalizedURL(url, url, message); | |
| 79 } | |
| 80 | |
| 81 testPreservedURL("http://example.com/foo", "includes path"); | |
| 82 testPreservedURL("http://example.com/?foo=bar", "includes query"); | |
| 83 testPreservedURL("http://example.com:8080/", "includes port"); | |
| 84 testNormalizedURL("http://example.com/#top","http://example.com/", "stripped hash"); | |
| 85 testNormalizedURL("http://user:password@example.com/","http://example.com/", "stripped auth credentials"); | |
| 86 testNormalizedURL("http://xn--f-1gaa.com/","http://f\u00f6\u00f6.com/", "dec oded punycode"); | |
| 87 testPreservedURL("about:blank", "about:blank"); | |
| 88 testPreservedURL("data:text/plain,foo", "data: URL"); | |
| 89 }); | |
| 90 | |
| 91 test("Third-party checks", function() | |
| 92 { | |
| 93 function hostnameToURL(hostname) | |
| 94 { | |
| 95 return new URL("http://" + hostname); | |
| 96 } | |
| 97 | |
| 98 function testThirdParty(requestHost, documentHost, expected, message) | |
| 99 { | |
| 100 equal( | |
| 101 isThirdParty( | |
| 102 hostnameToURL(requestHost), | |
| 103 | |
| 104 // Chrome's URL object normalizes IP addresses. So some test | |
| 105 // will fail if we don't normalize the document host as well. | |
| 106 getDecodedHostname(hostnameToURL(documentHost)) | |
| 107 ), | |
| 108 expected, | |
| 109 message | |
| 110 ); | |
| 111 } | |
| 112 | |
| 113 testThirdParty("foo", "foo", false, "same domain isn't third-party"); | |
| 114 testThirdParty("foo", "bar", true, "different domain is third-party"); | |
| 115 testThirdParty("foo.com", "foo.com", false, "same domain with TLD (.com) isn 't third-party"); | |
| 116 testThirdParty("foo.com", "bar.com", true, "same TLD (.com) but different do main is third-party"); | |
| 117 testThirdParty("foo.com", "www.foo.com", false, "same domain but differend s ubdomain isn't third-party"); | |
| 118 testThirdParty("foo.example.com", "bar.example.com", false, "same basedomain (example.com) isn't third-party"); | |
| 119 testThirdParty("foo.uk", "bar.uk", true, "same TLD (.uk) but different domai n is third-party"); | |
| 120 testThirdParty("foo.co.uk", "bar.co.uk", true, "same TLD (.co.uk) but differ ent domain is third-party"); | |
| 121 testThirdParty("foo.example.co.uk", "bar.example.co.uk", false, "same basedo main (example.co.uk) isn't third-party"); | |
| 122 testThirdParty("1.2.3.4", "1.2.3.4", false, "same IPv4 address isn't third-p arty"); | |
| 123 testThirdParty("1.1.1.1", "2.1.1.1", true, "different IPv4 address is third- party"); | |
| 124 testThirdParty("1.0xff.3.4", "1.0xff.3.4", false, "same IPv4 address with he xadecimal octet isn't third-party"); | |
| 125 testThirdParty("1.0xff.1.1", "2.0xff.1.1", true, "different IPv4 address wit h hexadecimal octet is third-party"); | |
| 
 
Wladimir Palant
2015/02/11 13:45:45
I guess we should test "0x01ff0101" as well.
 
Sebastian Noack
2015/02/11 17:07:06
Done.
 
 | |
| 126 testThirdParty("0xff.example.com", "example.com", false, "domain starts like a hexadecimal IPv4 address but isn't one"); | |
| 127 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7 334]", false, "same IPv6 address isn't third-party"); | |
| 128 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7 334]", true, "different IPv6 address is third-party"); | |
| 129 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, "same IPv4-mapped IPv6 address isn't third-party"); | |
| 130 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, "differ ent IPv4-mapped IPv6 address is third-party"); | |
| 131 testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, "same IDN isn't third-party"); | |
| 132 testThirdParty("example.com..", "example.com....", false, "traling dots are ignored"); | |
| 
 
Wladimir Palant
2015/02/11 13:45:45
Interestingly, all browsers will indeed accept an
 
 | |
| 133 }); | |
| 134 })(); | |
| OLD | NEW |