Debian Codegraph @

src/tests/tests/baseDomain.js

raw
1 /*
2 * This file is part of Privacy Badger <https://privacybadger.org/>
3 * Copyright (C) 2014 Electronic Frontier Foundation
4 *
5 * Derived from Adblock Plus
6 * Copyright (C) 2006-2013 Eyeo GmbH
7 *
8 * Privacy Badger is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
11 *
12 * Privacy Badger is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 import { extractHostFromURL, getBaseDomain, ipAddressToNumber, isPrivateDomain,
22 isThirdParty, URI } from "../../lib/basedomain.js";
23
24 import utils from "../../js/utils.js";
25
26 QUnit.module("URL/host tools");
27
28 QUnit.test("Host name extraction", function (assert) {
29 var tests = [
30 [null, ""],
31 ["/foo/bar", ""],
32 ["http://example.com/", "example.com"],
33 ["http://example.com:8000/", "example.com"],
34 ["http://foo:[email protected]:8000/foo:bar/bas", "example.com"],
35 ["ftp://example.com/", "example.com"],
36 ["http://1.2.3.4:8000/", "1.2.3.4"],
37 ["http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"],
38 ["http://[2001::7334]:8000/[email protected]/bar", "2001::7334"],
39 ];
40
41 for (var i = 0; i < tests.length; i++) {
42 assert.equal(extractHostFromURL(tests[i][0]), tests[i][1], tests[i][0]);
43 }
44 });
45
46 QUnit.test("Invalid URI recognition", function (assert) {
47 var tests = [
48 null,
49 "",
50 "http:",
51 "http:foo.bar/",
52 "http://foo.bar"
53 ];
54 for (var i = 0; i < tests.length; i++) {
55 assert.raises(function() { // eslint-disable-line no-loop-func
56 return new URI(tests[i]);
57 }, Error, "Invalid URI recognition.");
58 }
59 });
60
61 QUnit.test("URI parsing", function (assert) {
62 var tests = [
63 ["http://example.com/", {
64 scheme: "http",
65 host: "example.com",
66 hostPort: "example.com",
67 port: -1,
68 path: "/",
69 prePath: "http://example.com"
70 }],
71 ["http://example.com:8000/", {
72 scheme: "http",
73 host: "example.com",
74 hostPort: "example.com:8000",
75 port: 8000,
76 path: "/",
77 prePath: "http://example.com:8000"
78 }],
79 ["http://foo:bar@\u0440\u043E\u0441\u0441\u0438\u044F.\u0440\u0444:8000/foo:bar/bas", {
80 scheme: "http",
81 host: "\u0440\u043E\u0441\u0441\u0438\u044F.\u0440\u0444",
82 hostPort: "\u0440\u043E\u0441\u0441\u0438\u044F.\u0440\u0444:8000",
83 port: 8000,
84 path: "/foo:bar/bas",
85 prePath: "http://foo:bar@\u0440\u043E\u0441\u0441\u0438\u044F.\u0440\u0444:8000"
86 }],
87 ["ftp://m\xFCller.de/", {
88 scheme: "ftp",
89 host: "m\xFCller.de",
90 hostPort: "m\xFCller.de",
91 port: -1,
92 path: "/",
93 prePath: "ftp://m\xFCller.de"
94 }],
95 ["http://1.2.3.4:8000/", {
96 scheme: "http",
97 host: "1.2.3.4",
98 hostPort: "1.2.3.4:8000",
99 port: 8000,
100 path: "/",
101 prePath: "http://1.2.3.4:8000"
102 }],
103 ["http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/", {
104 scheme: "http",
105 host: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
106 hostPort: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
107 port: -1,
108 path: "/",
109 prePath: "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"
110 }],
111 ["http://[2001::7334]:8000/[email protected]/bar", {
112 scheme: "http",
113 host: "2001::7334",
114 hostPort: "[2001::7334]:8000",
115 port: 8000,
116 path: "/[email protected]/bar",
117 prePath: "http://[2001::7334]:8000"
118 }],
119 ["filesystem:http://example.com/temporary/myfile.png", {
120 scheme: "filesystem:http",
121 host: "example.com",
122 hostPort: "example.com",
123 port: -1,
124 path: "/temporary/myfile.png",
125 prePath: "filesystem:http://example.com"
126 }],
127 ["blob:https://www.daringgourmet.com/69587cd0-01e1-417b-819d-8e2ecbefc1f9", {
128 scheme: "blob:https",
129 host: "www.daringgourmet.com",
130 hostPort: "www.daringgourmet.com",
131 port: -1,
132 path: "/69587cd0-01e1-417b-819d-8e2ecbefc1f9",
133 prePath: "blob:https://www.daringgourmet.com"
134 }],
135 ];
136
137 for (var i = 0; i < tests.length; i++) {
138 var url = tests[i][0];
139 var uri = new URI(url);
140 assert.equal(uri.spec, url, "URI(" + url + ").spec");
141 for (var k in tests[i][1]) {
142 assert.equal(uri[k], tests[i][1][k], "URI(" + url + ")." + k);
143 }
144 }
145 });
146
147 QUnit.test("Determining base domain", function (assert) {
148 var tests = [
149 ["com", "com"],
150 ["example.com", "example.com"],
151 ["www.example.com", "example.com"],
152 ["www.example.com.", "example.com"],
153 ["www.example.co.uk", "example.co.uk"],
154 ["www.example.co.uk.", "example.co.uk"],
155 ["www.example.bl.uk", "bl.uk"],
156 ["foo.bar.example.co.uk", "example.co.uk"],
157 ["1.2.3.4.com", "4.com"],
158 ["1.2.3.4.bg", "3.4.bg"],
159 ["1.2.3.4", "1.2.3.4"],
160 ["1.2.0x3.0x4", "1.2.0x3.0x4"],
161 ["1.2.3", "2.3"],
162 ["1.2.0x3g.0x4", "0x3g.0x4"],
163 ["2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"],
164 ["2001::7334", "2001::7334"],
165 ["::ffff:1.2.3.4", "::ffff:1.2.3.4"],
166 ["foo.bar.2001::7334", "bar.2001::7334"],
167 ["test.xn--e1aybc.xn--p1ai", "xn--e1aybc.xn--p1ai"],
168 ];
169
170 for (var i = 0; i < tests.length; i++) {
171 assert.equal(getBaseDomain(tests[i][0]), tests[i][1], tests[i][0]);
172 }
173 });
174
175 QUnit.test("Converting IP address to number checks", function (assert) {
176 var testResults = {
177 "127.0.0.1": 2130706433,
178 "8.8.8.8": 134744072,
179 "192.168.0.1": 3232235521,
180 "256.0.0.1": 0,
181 "privacybadger.org": 0,
182 };
183
184 for (var ip in testResults) {
185 // Ignore object properties.
186 if (!utils.hasOwn(testResults, ip)) {
187 continue;
188 }
189
190 assert.equal(ipAddressToNumber(ip), testResults[ip], ip);
191 }
192 });
193
194 QUnit.test("Private domain checks", function (assert) {
195 var testResults = {
196 localhost: true,
197 "126.0.0.13": false,
198 "127.0.0.1": true,
199 "128.0.2.27": false,
200 "9.4.201.150": false,
201 "10.3.0.99": true,
202 "11.240.84.107": false,
203 "171.20.103.65": false,
204 "172.15.2.0": false,
205 "172.16.25.30": true,
206 "172.31.16.2": true,
207 "172.32.3.4": false,
208 "173.28.86.211": false,
209 "191.168.33.41": false,
210 "192.167.101.111": false,
211 "192.168.1.5": true,
212 "192.169.204.154": false,
213 "193.168.28.139": false,
214 "privacybadger.org": false,
215 };
216
217 for (var domain in testResults) {
218 // Ignore object properties.
219 if (!utils.hasOwn(testResults, domain)) {
220 continue;
221 }
222
223 assert.equal(isPrivateDomain(domain), testResults[domain], domain);
224 }
225 });
226
227 QUnit.test("Third party checks", function (assert) {
228 var tests = [
229 ["foo", "foo", false],
230 ["foo", "bar", true],
231 ["foo.com", "bar.com", true],
232 ["foo.com", "foo.com", false],
233 ["foo.com", "www.foo.com", false],
234 ["foo.example.com", "bar.example.com", false],
235 ["foo.uk", "bar.uk", true],
236 ["foo.co.uk", "bar.co.uk", true],
237 ["foo.example.co.uk", "bar.example.co.uk", false],
238 ["1.2.3.4", "2.2.3.4", true],
239 [null, "example.com", true],
240 [undefined, "example.com", true],
241 [null, null, true],
242 ["example.com", null, true],
243 ["example.com", undefined, true],
244 ["example.com.", "example.com", false], // first param has trailing dot
245 ["example.com", "example.com.", false], // second param has trailing dot
246 ];
247
248 for (var i = 0; i < tests.length; i++) {
249 assert.equal(isThirdParty(tests[i][0], tests[i][1]), tests[i][2], tests[i][0] + " and " + tests[i][1]);
250 }
251 });