Left: | ||
Right: |
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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 10 matching lines...) Expand all Loading... | |
21 var url = require("url"); | 21 var url = require("url"); |
22 var getDecodedHostname = url.getDecodedHostname; | 22 var getDecodedHostname = url.getDecodedHostname; |
23 var extractHostFromFrame = url.extractHostFromFrame; | 23 var extractHostFromFrame = url.extractHostFromFrame; |
24 var stringifyURL = url.stringifyURL; | 24 var stringifyURL = url.stringifyURL; |
25 var isThirdParty = url.isThirdParty; | 25 var isThirdParty = url.isThirdParty; |
26 | 26 |
27 module("URL/host tools"); | 27 module("URL/host tools"); |
28 | 28 |
29 test("Extracting hostname from URL", function() | 29 test("Extracting hostname from URL", function() |
30 { | 30 { |
31 equal(getDecodedHostname(new URL("http://example.com/foo")), "example.com", "with path"); | 31 function testURLHostname(url, expectedHostname, message) |
32 equal(getDecodedHostname(new URL("http://example.com/?foo=bar")), "example.c om", "with query"); | 32 { |
33 equal(getDecodedHostname(new URL("http://example.com/#top")), "example.com", "with hash"); | 33 equal(getDecodedHostname(new URL(url)), expectedHostname, message); |
34 equal(getDecodedHostname(new URL("http://example.com/:8080")), "example.com" , "with port"); | 34 } |
Wladimir Palant
2015/02/09 12:54:29
Port number before slash please.
Sebastian Noack
2015/02/11 10:55:51
Done.
| |
35 equal(getDecodedHostname(new URL("http://user:password@example.com/")), "exa mple.com", "with auth credentials"); | 35 |
36 equal(getDecodedHostname(new URL("http://xn--f-1gaa.com/")), "f\u00f6\u00f6. com", "with punycode"); | 36 testURLHostname("http://example.com/foo", "example.com", "with path"); |
37 equal(getDecodedHostname(new URL("about:blank")), "", "about:blank"); | 37 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); |
38 equal(getDecodedHostname(new URL("data:text/plain,foo")), "", "data: URL"); | 38 testURLHostname("http://example.com/#top", "example.com", "with hash"); |
Wladimir Palant
2015/02/09 12:54:29
Could you add back tests that you removed? In part
Sebastian Noack
2015/02/11 10:55:51
Not really removed them, since the function we tes
| |
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"); | |
39 }); | 47 }); |
40 | 48 |
41 test("Extracting hostname from frame", function() | 49 test("Extracting hostname from frame", function() |
42 { | 50 { |
43 function makeFrame(hierarchy) | 51 function testFrameHostname(hierarchy, expectedHostname, message) |
44 { | 52 { |
45 var frame = null; | 53 var frame = null; |
46 | 54 |
47 for (var i = 0; i < hierarchy.length; i++) | 55 for (var i = 0; i < hierarchy.length; i++) |
48 frame = {parent: frame, url: new URL(hierarchy[i])}; | 56 frame = {parent: frame, url: new URL(hierarchy[i])}; |
49 | 57 |
50 return frame; | 58 equal(extractHostFromFrame(frame), expectedHostname, message); |
51 } | 59 } |
52 | 60 |
53 equal(extractHostFromFrame(makeFrame(["http://example.com/"])), "example.com ", "single frame"); | 61 testFrameHostname(["http://example.com/"], "example.com", "single frame"); |
54 equal(extractHostFromFrame(makeFrame(["http://example.com/", "http://example .org/"])), "example.org", "with parent frame"); | 62 testFrameHostname(["http://example.com/", "http://example.org/"], "example.o rg", "with parent frame"); |
55 equal(extractHostFromFrame(makeFrame(["http://example.com/", "data:text/plai n,foo"])), "example.com", "data: URL, hostname in parent"); | 63 testFrameHostname(["http://example.com/", "data:text/plain,foo"], "example.c om", "data: URL, hostname in parent"); |
56 equal(extractHostFromFrame(makeFrame(["http://example.com/", "about:blank", "about:blank"])), "example.com", "about:blank, hostname in ancestor"); | 64 testFrameHostname(["http://example.com/", "about:blank", "about:blank"], "ex ample.com", "about:blank, hostname in ancestor"); |
57 equal(extractHostFromFrame(makeFrame(["about:blank", "about:blank"])), "", " about:blank, no hostname"); | 65 testFrameHostname(["about:blank", "about:blank"], "", "about:blank, no hostn ame"); |
58 equal(extractHostFromFrame(makeFrame(["http://xn--f-1gaa.com/"])), "f\u00f6\ u00f6.com", "with punycode"); | 66 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun ycode"); |
59 }); | 67 }); |
60 | 68 |
61 test("Stringifying URLs", function() | 69 test("Stringifying URLs", function() |
62 { | 70 { |
63 equal(stringifyURL(new URL("http://example.com/foo")),"http://example.com/fo o", "includes path"); | 71 function testNormalizedURL(url, expectedURL, message) |
64 equal(stringifyURL(new URL("http://example.com/?foo=bar")),"http://example.c om/?foo=bar", "includes query"); | 72 { |
65 equal(stringifyURL(new URL("http://example.com:8080/")),"http://example.com: 8080/", "includes port"); | 73 equal(stringifyURL(new URL(url)), expectedURL, message); |
66 equal(stringifyURL(new URL("http://example.com/#top")),"http://example.com/" , "stripped hash"); | 74 } |
67 equal(stringifyURL(new URL("http://user:password@example.com/")),"http://exa mple.com/", "stripped auth credentials"); | 75 |
68 equal(stringifyURL(new URL("http://xn--f-1gaa.com/")),"http://f\u00f6\u00f6. com/", "decoded punycode"); | 76 function testPreservedURL(url, message) |
69 equal(stringifyURL(new URL("about:blank")), "about:blank", "about:blank"); | 77 { |
70 equal(stringifyURL(new URL("data:text/plain,foo")), "data:text/plain,foo", " data: URL"); | 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"); | |
71 }); | 89 }); |
72 | 90 |
73 test("Third-party checks", function() | 91 test("Third-party checks", function() |
74 { | 92 { |
75 equal(isThirdParty(new URL("http://foo/"), "foo"), false, "same domain isn't third-party"); | 93 function hostnameToURL(hostname) |
76 equal(isThirdParty(new URL("http://foo/"), "bar"), true, "different domain i s third-party"); | 94 { |
77 equal(isThirdParty(new URL("http://foo.com/"), "foo.com"), false, "same doma in with TLD (.com) isn't third-party"); | 95 return new URL("http://" + hostname); |
78 equal(isThirdParty(new URL("http://foo.com/"), "bar.com"), true, "same TLD ( .com) but different domain is third-party"); | 96 } |
79 equal(isThirdParty(new URL("http://foo.example.com/"), "bar.example.com"), f alse, "same basedomain (example.com) isn't third-party"); | 97 |
Wladimir Palant
2015/02/09 12:54:29
Please add back http://foo.com/, www.foo.com scena
Sebastian Noack
2015/02/11 10:55:51
Done.
| |
80 equal(isThirdParty(new URL("http://foo.uk/"), "bar.uk"), true, "same TLD (.u k) but different domain is third-party"); | 98 function testThirdParty(requestHost, documentHost, expected, message) |
81 equal(isThirdParty(new URL("http://foo.co.uk/"), "bar.co.uk"), true, "same T LD (.co.uk) but different domain is third-party"); | 99 { |
82 equal(isThirdParty(new URL("http://foo.example.co.uk/"), "bar.example.co.uk" ), false, "same basedomain (example.co.uk) isn't third-party"); | 100 equal( |
83 equal(isThirdParty(new URL("http://1.2.3.4/"), "1.2.3.4"), false, "same IP a ddress isn't third-party"); | 101 isThirdParty( |
84 equal(isThirdParty(new URL("http://1.1.1.1/"), "2.1.1.1"), true, "different IP address is third-party"); | 102 hostnameToURL(requestHost), |
85 equal(isThirdParty(new URL("http://xn--f-1gaa.com/"), "f\u00f6\u00f6.com"), false, "same IDN isn't third-party"); | 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("0x01ff0101", "0x01ff0101", false, "same IPv4 hexadecimal add ress isn't third-party"); | |
125 testThirdParty("0x01ff0101", "0x01ff0102", true, "different IPv4 hexadecimal address is third-party"); | |
126 testThirdParty("1.0xff.3.4", "1.0xff.3.4", false, "same IPv4 address with he xadecimal octet isn't third-party"); | |
127 testThirdParty("1.0xff.1.1", "2.0xff.1.1", true, "different IPv4 address wit h hexadecimal octet is third-party"); | |
128 testThirdParty("0xff.example.com", "example.com", false, "domain starts like a hexadecimal IPv4 address but isn't one"); | |
129 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7 334]", false, "same IPv6 address isn't third-party"); | |
130 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7 334]", true, "different IPv6 address is third-party"); | |
131 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, "same IPv4-mapped IPv6 address isn't third-party"); | |
132 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, "differ ent IPv4-mapped IPv6 address is third-party"); | |
133 testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, "same IDN isn't third-party"); | |
134 testThirdParty("example.com..", "example.com....", false, "traling dots are ignored"); | |
86 }); | 135 }); |
Wladimir Palant
2015/02/09 12:54:29
The original unit tests had a bunch more scenarios
Sebastian Noack
2015/02/11 10:55:51
Done.
| |
87 })(); | 136 })(); |
Wladimir Palant
2015/02/09 12:54:29
Nit: there is a reason why the original unit tests
Sebastian Noack
2015/02/11 10:55:51
The reason I didn't like that approach was that it
| |
LEFT | RIGHT |