Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/url.js

Issue 30009560: Noissue - Rename lib/domain.js to lib/url.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests for domainSuffixes Created Feb. 19, 2019, 2:36 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/url.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Only starting NodeJS 10 that URL is in the global space. 20 // Only starting NodeJS 10 that URL is in the global space.
21 const {URL} = require("url"); 21 const {URL} = require("url");
22 const {createSandbox} = require("./_common"); 22 const {createSandbox} = require("./_common");
23 23
24 const publicSuffixes = require("../data/publicSuffixList.json"); 24 const publicSuffixes = require("../data/publicSuffixList.json");
25 25
26 let normalizeHostname = null; 26 let normalizeHostname = null;
27 let domainSuffixes = null;
27 let isThirdParty = null; 28 let isThirdParty = null;
28 let getDomain = null; 29 let getBaseDomain = null;
29 30
30 exports.setUp = function(callback) 31 exports.setUp = function(callback)
31 { 32 {
32 let sandboxedRequire = createSandbox({ 33 let sandboxedRequire = createSandbox({
33 extraExports: { 34 extraExports: {
34 domain: ["getDomain"] 35 domain: ["getBaseDomain"]
35 } 36 }
36 }); 37 });
37 ( 38 (
38 {normalizeHostname, isThirdParty, 39 {normalizeHostname, domainSuffixes, isThirdParty,
39 getDomain} = sandboxedRequire("../lib/domain") 40 getBaseDomain} = sandboxedRequire("../lib/url")
40 ); 41 );
41 42
42 callback(); 43 callback();
43 }; 44 };
44 45
45 function hostnameToURL(hostname) 46 function hostnameToURL(hostname)
46 { 47 {
47 return new URL("http://" + hostname); 48 return new URL("http://" + hostname);
48 } 49 }
49 50
(...skipping 30 matching lines...) Expand all
80 test.equal(normalizeHostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 81 test.equal(normalizeHostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
81 "2001:0db8:85a3:0000:0000:8a2e:0370:7334"); 82 "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
82 test.equal(normalizeHostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334."), 83 test.equal(normalizeHostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334."),
83 "2001:0db8:85a3:0000:0000:8a2e:0370:7334"); 84 "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
84 test.equal(normalizeHostname("2001:0DB8:85A3:0000:0000:8A2E:0370:7334"), 85 test.equal(normalizeHostname("2001:0DB8:85A3:0000:0000:8A2E:0370:7334"),
85 "2001:0db8:85a3:0000:0000:8a2e:0370:7334"); 86 "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
86 87
87 test.done(); 88 test.done();
88 }; 89 };
89 90
91 exports.testDomainSuffixes = function(test)
92 {
93 test.deepEqual([...domainSuffixes("localhost")], ["localhost"]);
94 test.deepEqual([...domainSuffixes("example.com")], ["example.com", "com"]);
95 test.deepEqual([...domainSuffixes("www.example.com")],
96 ["www.example.com", "example.com", "com"]);
97 test.deepEqual([...domainSuffixes("www.example.co.in")],
98 ["www.example.co.in", "example.co.in", "co.in", "in"]);
99
100 // With blank.
101 test.deepEqual([...domainSuffixes("localhost", true)], ["localhost", ""]);
102 test.deepEqual([...domainSuffixes("example.com", true)],
103 ["example.com", "com", ""]);
104 test.deepEqual([...domainSuffixes("www.example.com", true)],
105 ["www.example.com", "example.com", "com", ""]);
106 test.deepEqual([...domainSuffixes("www.example.co.in", true)],
107 ["www.example.co.in", "example.co.in", "co.in", "in", ""]);
108
109 // Quirks and edge cases.
110 test.deepEqual([...domainSuffixes("")], []);
111 test.deepEqual([...domainSuffixes(".")], ["."]);
112 test.deepEqual([...domainSuffixes(".localhost")],
113 [".localhost", "localhost"]);
114 test.deepEqual([...domainSuffixes(".example.com")],
115 [".example.com", "example.com", "com"]);
116 test.deepEqual([...domainSuffixes("localhost.")],
117 ["localhost."]);
118 test.deepEqual([...domainSuffixes("example.com.")],
119 ["example.com.", "com."]);
120 test.deepEqual([...domainSuffixes("..localhost")],
121 ["..localhost", ".localhost", "localhost"]);
122 test.deepEqual([...domainSuffixes("..example..com")],
123 ["..example..com", ".example..com", "example..com", ".com",
124 "com"]);
125 test.deepEqual([...domainSuffixes("localhost..")], ["localhost..", "."]);
126 test.deepEqual([...domainSuffixes("example..com..")],
127 ["example..com..", ".com..", "com..", "."]);
128
129 test.done();
130 };
131
90 exports.testIsThirdParty = function(test) 132 exports.testIsThirdParty = function(test)
91 { 133 {
92 testThirdParty(test, "foo", "foo", false, "same domain isn't third-party"); 134 testThirdParty(test, "foo", "foo", false, "same domain isn't third-party");
93 testThirdParty(test, "foo", "bar", true, "different domain is third-party"); 135 testThirdParty(test, "foo", "bar", true, "different domain is third-party");
94 testThirdParty(test, "foo.com", "foo.com", false, 136 testThirdParty(test, "foo.com", "foo.com", false,
95 "same domain with TLD (.com) isn't third-party"); 137 "same domain with TLD (.com) isn't third-party");
96 testThirdParty(test, "foo.com", "bar.com", true, 138 testThirdParty(test, "foo.com", "bar.com", true,
97 "same TLD (.com) but different domain is third-party"); 139 "same TLD (.com) but different domain is third-party");
98 testThirdParty(test, "foo.com", "www.foo.com", false, 140 testThirdParty(test, "foo.com", "www.foo.com", false,
99 "same domain but differend subdomain isn't third-party"); 141 "same domain but differend subdomain isn't third-party");
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 "different IPv4-mapped IPv6 address is third-party" 191 "different IPv4-mapped IPv6 address is third-party"
150 ); 192 );
151 testThirdParty(test, "xn--f-1gaa.com", "f\u00f6\u00f6.com", false, 193 testThirdParty(test, "xn--f-1gaa.com", "f\u00f6\u00f6.com", false,
152 "same IDN isn't third-party"); 194 "same IDN isn't third-party");
153 testThirdParty(test, "example.com..", "example.com....", false, 195 testThirdParty(test, "example.com..", "example.com....", false,
154 "traling dots are ignored"); 196 "traling dots are ignored");
155 197
156 test.done(); 198 test.done();
157 }; 199 };
158 200
159 exports.testGetDomain = function(test) 201 exports.testGetBaseDomain = function(test)
160 { 202 {
161 let parts = ["aaa", "bbb", "ccc", "ddd", "eee"]; 203 let parts = ["aaa", "bbb", "ccc", "ddd", "eee"];
162 let levels = 3; 204 let levels = 3;
163 205
164 for (let suffix in publicSuffixes) 206 for (let suffix in publicSuffixes)
165 { 207 {
166 let offset = publicSuffixes[suffix]; 208 let offset = publicSuffixes[suffix];
167 209
168 // If this fails, add more parts. 210 // If this fails, add more parts.
169 test.ok(offset <= parts.length - levels, 211 test.ok(offset <= parts.length - levels,
170 "Not enough domain parts for testing"); 212 "Not enough domain parts for testing");
171 213
172 for (let i = 0; i < offset + levels; i++) 214 for (let i = 0; i < offset + levels; i++)
173 { 215 {
174 let hostname = parts.slice(0, i).join("."); 216 let hostname = parts.slice(0, i).join(".");
175 hostname += (hostname ? "." : "") + suffix; 217 hostname += (hostname ? "." : "") + suffix;
176 218
177 let expected = parts.slice(Math.max(0, i - offset), i).join("."); 219 let expected = parts.slice(Math.max(0, i - offset), i).join(".");
178 expected += (expected ? "." : "") + suffix; 220 expected += (expected ? "." : "") + suffix;
179 221
180 test.equal(getDomain(hostname), expected, 222 test.equal(getBaseDomain(hostname), expected,
181 `getDomain("${hostname}") == "${expected}"` + 223 `getBaseDomain("${hostname}") == "${expected}"` +
182 ` with {suffix: "${suffix}", offset: ${offset}}`); 224 ` with {suffix: "${suffix}", offset: ${offset}}`);
183 } 225 }
184 } 226 }
185 227
186 // Unknown suffixes. 228 // Unknown suffixes.
187 test.equal(typeof publicSuffixes["localhost"], "undefined"); 229 test.equal(typeof publicSuffixes["localhost"], "undefined");
188 test.equal(typeof publicSuffixes["localhost.localdomain"], "undefined"); 230 test.equal(typeof publicSuffixes["localhost.localdomain"], "undefined");
189 231
190 test.equal(getDomain("localhost"), "localhost"); 232 test.equal(getBaseDomain("localhost"), "localhost");
191 test.equal(getDomain("localhost.localdomain"), "localhost.localdomain"); 233 test.equal(getBaseDomain("localhost.localdomain"), "localhost.localdomain");
192 test.equal(getDomain("mail.localhost.localdomain"), "localhost.localdomain"); 234 test.equal(
193 test.equal(getDomain("www.example.localhost.localdomain"), 235 getBaseDomain("mail.localhost.localdomain"),
236 "localhost.localdomain"
237 );
238 test.equal(getBaseDomain("www.example.localhost.localdomain"),
194 "localhost.localdomain"); 239 "localhost.localdomain");
195 240
196 // Unknown suffixes that overlap partly with known suffixes. 241 // Unknown suffixes that overlap partly with known suffixes.
197 test.equal(typeof publicSuffixes["example.com"], "undefined"); 242 test.equal(typeof publicSuffixes["example.com"], "undefined");
198 test.equal(typeof publicSuffixes["africa.com"], "number"); 243 test.equal(typeof publicSuffixes["africa.com"], "number");
199 test.equal(typeof publicSuffixes["compute.amazonaws.com"], "number"); 244 test.equal(typeof publicSuffixes["compute.amazonaws.com"], "number");
200 245
201 test.equal(getDomain("example.com"), "example.com"); 246 test.equal(getBaseDomain("example.com"), "example.com");
202 test.equal(getDomain("mail.example.com"), "example.com"); 247 test.equal(getBaseDomain("mail.example.com"), "example.com");
203 test.equal(getDomain("secure.mail.example.com"), "example.com"); 248 test.equal(getBaseDomain("secure.mail.example.com"), "example.com");
204 249
205 // Cascading offsets. 250 // Cascading offsets.
206 251
207 // If these sanity checks fail, look for other examles of cascading offsets 252 // If these sanity checks fail, look for other examles of cascading offsets
208 // from the public suffix list. 253 // from the public suffix list.
209 test.equal( 254 test.equal(
210 typeof publicSuffixes[ 255 typeof publicSuffixes[
211 "images.example.s3.dualstack.us-east-1.amazonaws.com" 256 "images.example.s3.dualstack.us-east-1.amazonaws.com"
212 ], 257 ],
213 "undefined" 258 "undefined"
214 ); 259 );
215 test.equal( 260 test.equal(
216 typeof publicSuffixes["example.s3.dualstack.us-east-1.amazonaws.com"], 261 typeof publicSuffixes["example.s3.dualstack.us-east-1.amazonaws.com"],
217 "undefined" 262 "undefined"
218 ); 263 );
219 test.equal(publicSuffixes["s3.dualstack.us-east-1.amazonaws.com"], 1); 264 test.equal(publicSuffixes["s3.dualstack.us-east-1.amazonaws.com"], 1);
220 test.equal(typeof publicSuffixes["dualstack.us-east-1.amazonaws.com"], 265 test.equal(typeof publicSuffixes["dualstack.us-east-1.amazonaws.com"],
221 "undefined"); 266 "undefined");
222 test.equal(typeof publicSuffixes["example.us-east-1.amazonaws.com"], 267 test.equal(typeof publicSuffixes["example.us-east-1.amazonaws.com"],
223 "undefined"); 268 "undefined");
224 test.equal(publicSuffixes["us-east-1.amazonaws.com"], 1); 269 test.equal(publicSuffixes["us-east-1.amazonaws.com"], 1);
225 test.equal(typeof publicSuffixes["example.amazonaws.com"], "undefined"); 270 test.equal(typeof publicSuffixes["example.amazonaws.com"], "undefined");
226 test.equal(typeof publicSuffixes["amazonaws.com"], "undefined"); 271 test.equal(typeof publicSuffixes["amazonaws.com"], "undefined");
227 272
228 test.equal(getDomain("images.example.s3.dualstack.us-east-1.amazonaws.com"), 273 test.equal(
274 getBaseDomain("images.example.s3.dualstack.us-east-1.amazonaws.com"),
275 "example.s3.dualstack.us-east-1.amazonaws.com"
276 );
277 test.equal(getBaseDomain("example.s3.dualstack.us-east-1.amazonaws.com"),
229 "example.s3.dualstack.us-east-1.amazonaws.com"); 278 "example.s3.dualstack.us-east-1.amazonaws.com");
230 test.equal(getDomain("example.s3.dualstack.us-east-1.amazonaws.com"), 279 test.equal(getBaseDomain("s3.dualstack.us-east-1.amazonaws.com"),
231 "example.s3.dualstack.us-east-1.amazonaws.com");
232 test.equal(getDomain("s3.dualstack.us-east-1.amazonaws.com"),
233 "s3.dualstack.us-east-1.amazonaws.com"); 280 "s3.dualstack.us-east-1.amazonaws.com");
234 test.equal(getDomain("dualstack.us-east-1.amazonaws.com"), 281 test.equal(getBaseDomain("dualstack.us-east-1.amazonaws.com"),
235 "dualstack.us-east-1.amazonaws.com"); 282 "dualstack.us-east-1.amazonaws.com");
236 test.equal(getDomain("example.us-east-1.amazonaws.com"), 283 test.equal(getBaseDomain("example.us-east-1.amazonaws.com"),
237 "example.us-east-1.amazonaws.com"); 284 "example.us-east-1.amazonaws.com");
238 test.equal(getDomain("us-east-1.amazonaws.com"), "us-east-1.amazonaws.com"); 285 test.equal(
239 test.equal(getDomain("example.amazonaws.com"), "amazonaws.com"); 286 getBaseDomain("us-east-1.amazonaws.com"),
240 test.equal(getDomain("amazonaws.com"), "amazonaws.com"); 287 "us-east-1.amazonaws.com"
288 );
289 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com");
290 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com");
241 291
242 // Edge case. 292 // Edge case.
243 test.equal(getDomain(""), ""); 293 test.equal(getBaseDomain(""), "");
244 294
245 test.done(); 295 test.done();
246 }; 296 };
OLDNEW
« no previous file with comments | « lib/url.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld