OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present 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 "use strict"; |
| 19 |
| 20 const {createSandbox} = require("./_common"); |
| 21 |
| 22 let isThirdParty = null; |
| 23 |
| 24 exports.setUp = function(callback) |
| 25 { |
| 26 let sandboxedRequire = createSandbox(); |
| 27 ( |
| 28 {isThirdParty} = sandboxedRequire("../lib/domain") |
| 29 ); |
| 30 |
| 31 callback(); |
| 32 }; |
| 33 |
| 34 function hostnameToURL(hostname) |
| 35 { |
| 36 return new URL("http://" + hostname); |
| 37 } |
| 38 |
| 39 function testThirdParty(test, requestHostname, documentHostname, expected, |
| 40 message) |
| 41 { |
| 42 test.equal( |
| 43 isThirdParty( |
| 44 hostnameToURL(requestHostname), |
| 45 |
| 46 // Chrome's URL object normalizes IP addresses. So some test |
| 47 // will fail if we don't normalize the document host as well. |
| 48 hostnameToURL(documentHostname).hostname |
| 49 ), |
| 50 expected, |
| 51 message |
| 52 ); |
| 53 } |
| 54 |
| 55 exports.testIsThirdParty = function(test) |
| 56 { |
| 57 testThirdParty(test, "foo", "foo", false, "same domain isn't third-party"); |
| 58 testThirdParty(test, "foo", "bar", true, "different domain is third-party"); |
| 59 testThirdParty(test, "foo.com", "foo.com", false, |
| 60 "same domain with TLD (.com) isn't third-party"); |
| 61 testThirdParty(test, "foo.com", "bar.com", true, |
| 62 "same TLD (.com) but different domain is third-party"); |
| 63 testThirdParty(test, "foo.com", "www.foo.com", false, |
| 64 "same domain but differend subdomain isn't third-party"); |
| 65 testThirdParty(test, "foo.example.com", "bar.example.com", false, |
| 66 "same basedomain (example.com) isn't third-party"); |
| 67 testThirdParty(test, "foo.uk", "bar.uk", true, |
| 68 "same TLD (.uk) but different domain is third-party"); |
| 69 testThirdParty(test, "foo.co.uk", "bar.co.uk", true, |
| 70 "same TLD (.co.uk) but different domain is third-party"); |
| 71 testThirdParty(test, "foo.example.co.uk", "bar.example.co.uk", false, |
| 72 "same basedomain (example.co.uk) isn't third-party"); |
| 73 testThirdParty(test, "1.2.3.4", "1.2.3.4", false, |
| 74 "same IPv4 address isn't third-party"); |
| 75 testThirdParty(test, "1.1.1.1", "2.1.1.1", true, |
| 76 "different IPv4 address is third-party"); |
| 77 testThirdParty(test, "0x01ff0101", "0x01ff0101", false, |
| 78 "same IPv4 hexadecimal address isn't third-party"); |
| 79 testThirdParty(test, "0x01ff0101", "0x01ff0102", true, |
| 80 "different IPv4 hexadecimal address is third-party"); |
| 81 testThirdParty( |
| 82 test, |
| 83 "1.0xff.3.4", "1.0xff.3.4", false, |
| 84 "same IPv4 address with hexadecimal octet isn't third-party" |
| 85 ); |
| 86 testThirdParty( |
| 87 test, |
| 88 "1.0xff.1.1", "2.0xff.1.1", true, |
| 89 "different IPv4 address with hexadecimal octet is third-party" |
| 90 ); |
| 91 testThirdParty( |
| 92 test, |
| 93 "0xff.example.com", "example.com", false, |
| 94 "domain starts like a hexadecimal IPv4 address but isn't one" |
| 95 ); |
| 96 testThirdParty( |
| 97 test, |
| 98 "[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]", false, |
| 99 "same IPv6 address isn't third-party" |
| 100 ); |
| 101 testThirdParty( |
| 102 test, |
| 103 "[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7334]", true, |
| 104 "different IPv6 address is third-party" |
| 105 ); |
| 106 testThirdParty( |
| 107 test, |
| 108 "[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, |
| 109 "same IPv4-mapped IPv6 address isn't third-party" |
| 110 ); |
| 111 testThirdParty( |
| 112 test, |
| 113 "[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, |
| 114 "different IPv4-mapped IPv6 address is third-party" |
| 115 ); |
| 116 testThirdParty(test, "xn--f-1gaa.com", "f\u00f6\u00f6.com", false, |
| 117 "same IDN isn't third-party"); |
| 118 testThirdParty(test, "example.com..", "example.com....", false, |
| 119 "traling dots are ignored"); |
| 120 |
| 121 test.done(); |
| 122 }; |
OLD | NEW |