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

Side by Side Diff: test/domain.js

Issue 30002580: Noissue - Abstract hostname normalization (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests Created Feb. 8, 2019, 7:26 p.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/matcher.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 isThirdParty = null; 27 let isThirdParty = null;
27 let getDomain = null; 28 let getDomain = null;
28 29
29 exports.setUp = function(callback) 30 exports.setUp = function(callback)
30 { 31 {
31 let sandboxedRequire = createSandbox({ 32 let sandboxedRequire = createSandbox({
32 extraExports: { 33 extraExports: {
33 domain: ["getDomain"] 34 domain: ["getDomain"]
34 } 35 }
35 }); 36 });
36 ( 37 (
37 {isThirdParty, getDomain} = sandboxedRequire("../lib/domain") 38 {normalizeHostname, isThirdParty,
39 getDomain} = sandboxedRequire("../lib/domain")
38 ); 40 );
39 41
40 callback(); 42 callback();
41 }; 43 };
42 44
43 function hostnameToURL(hostname) 45 function hostnameToURL(hostname)
44 { 46 {
45 return new URL("http://" + hostname); 47 return new URL("http://" + hostname);
46 } 48 }
47 49
48 function testThirdParty(test, requestHostname, documentHostname, expected, 50 function testThirdParty(test, requestHostname, documentHostname, expected,
49 message) 51 message)
50 { 52 {
51 test.equal( 53 test.equal(
52 isThirdParty( 54 isThirdParty(
53 hostnameToURL(requestHostname), 55 hostnameToURL(requestHostname),
54 56
55 // Chrome's URL object normalizes IP addresses. So some test 57 // Chrome's URL object normalizes IP addresses. So some test
56 // will fail if we don't normalize the document host as well. 58 // will fail if we don't normalize the document host as well.
57 hostnameToURL(documentHostname).hostname 59 hostnameToURL(documentHostname).hostname
58 ), 60 ),
59 expected, 61 expected,
60 message 62 message
61 ); 63 );
62 } 64 }
63 65
66 exports.testNormalizeHostname = function(test)
67 {
68 test.equal(normalizeHostname("example.com"), "example.com");
69 test.equal(normalizeHostname("example.com."), "example.com");
70 test.equal(normalizeHostname("example.com.."), "example.com");
71 test.equal(normalizeHostname("example.com..."), "example.com");
72
73 test.equal(normalizeHostname("Example.com"), "example.com");
74 test.equal(normalizeHostname("ExaMple.Com"), "example.com");
75 test.equal(normalizeHostname("ExaMple.Com.."), "example.com");
76
77 test.equal(normalizeHostname("192.168.1.1"), "192.168.1.1");
78 test.equal(normalizeHostname("192.168.1.1."), "192.168.1.1");
79
80 test.equal(normalizeHostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
81 "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
82 test.equal(normalizeHostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334."),
83 "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
84 test.equal(normalizeHostname("2001:0DB8:85A3:0000:0000:8A2E:0370:7334"),
85 "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
86
87 test.done();
88 };
89
64 exports.testIsThirdParty = function(test) 90 exports.testIsThirdParty = function(test)
65 { 91 {
66 testThirdParty(test, "foo", "foo", false, "same domain isn't third-party"); 92 testThirdParty(test, "foo", "foo", false, "same domain isn't third-party");
67 testThirdParty(test, "foo", "bar", true, "different domain is third-party"); 93 testThirdParty(test, "foo", "bar", true, "different domain is third-party");
68 testThirdParty(test, "foo.com", "foo.com", false, 94 testThirdParty(test, "foo.com", "foo.com", false,
69 "same domain with TLD (.com) isn't third-party"); 95 "same domain with TLD (.com) isn't third-party");
70 testThirdParty(test, "foo.com", "bar.com", true, 96 testThirdParty(test, "foo.com", "bar.com", true,
71 "same TLD (.com) but different domain is third-party"); 97 "same TLD (.com) but different domain is third-party");
72 testThirdParty(test, "foo.com", "www.foo.com", false, 98 testThirdParty(test, "foo.com", "www.foo.com", false,
73 "same domain but differend subdomain isn't third-party"); 99 "same domain but differend subdomain isn't third-party");
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 "example.us-east-1.amazonaws.com"); 237 "example.us-east-1.amazonaws.com");
212 test.equal(getDomain("us-east-1.amazonaws.com"), "us-east-1.amazonaws.com"); 238 test.equal(getDomain("us-east-1.amazonaws.com"), "us-east-1.amazonaws.com");
213 test.equal(getDomain("example.amazonaws.com"), "amazonaws.com"); 239 test.equal(getDomain("example.amazonaws.com"), "amazonaws.com");
214 test.equal(getDomain("amazonaws.com"), "amazonaws.com"); 240 test.equal(getDomain("amazonaws.com"), "amazonaws.com");
215 241
216 // Edge case. 242 // Edge case.
217 test.equal(getDomain(""), ""); 243 test.equal(getDomain(""), "");
218 244
219 test.done(); 245 test.done();
220 }; 246 };
OLDNEW
« no previous file with comments | « lib/matcher.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld