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

Side by Side Diff: test/domain.js

Issue 29993591: Noissue - Add tests for base domain extraction (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add one more test for cascading offsets Created Jan. 31, 2019, 3:46 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 | « no previous file | 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");
25
24 let isThirdParty = null; 26 let isThirdParty = null;
27 let getDomain = null;
25 28
26 exports.setUp = function(callback) 29 exports.setUp = function(callback)
27 { 30 {
28 let sandboxedRequire = createSandbox(); 31 let sandboxedRequire = createSandbox({
32 extraExports: {
33 domain: ["getDomain"]
34 }
35 });
29 ( 36 (
30 {isThirdParty} = sandboxedRequire("../lib/domain") 37 {isThirdParty, getDomain} = sandboxedRequire("../lib/domain")
31 ); 38 );
32 39
33 callback(); 40 callback();
34 }; 41 };
35 42
36 function hostnameToURL(hostname) 43 function hostnameToURL(hostname)
37 { 44 {
38 return new URL("http://" + hostname); 45 return new URL("http://" + hostname);
39 } 46 }
40 47
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 "[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, 122 "[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true,
116 "different IPv4-mapped IPv6 address is third-party" 123 "different IPv4-mapped IPv6 address is third-party"
117 ); 124 );
118 testThirdParty(test, "xn--f-1gaa.com", "f\u00f6\u00f6.com", false, 125 testThirdParty(test, "xn--f-1gaa.com", "f\u00f6\u00f6.com", false,
119 "same IDN isn't third-party"); 126 "same IDN isn't third-party");
120 testThirdParty(test, "example.com..", "example.com....", false, 127 testThirdParty(test, "example.com..", "example.com....", false,
121 "traling dots are ignored"); 128 "traling dots are ignored");
122 129
123 test.done(); 130 test.done();
124 }; 131 };
132
133 exports.testGetDomain = function(test)
134 {
135 let parts = ["aaa", "bbb", "ccc", "ddd", "eee"];
136 let levels = 3;
137
138 for (let suffix in publicSuffixes)
139 {
140 let offset = publicSuffixes[suffix];
141
142 // If this fails, add more parts.
143 test.ok(offset <= parts.length - levels,
144 "Not enough domain parts for testing");
145
146 for (let i = 0; i < offset + levels; i++)
147 {
148 let hostname = parts.slice(0, i).join(".");
149 hostname += (hostname ? "." : "") + suffix;
150
151 let expected = parts.slice(Math.max(0, i - offset), i).join(".");
152 expected += (expected ? "." : "") + suffix;
153
154 test.equal(getDomain(hostname), expected,
155 `getDomain("${hostname}") == "${expected}"` +
156 ` with {suffix: "${suffix}", offset: ${offset}}`);
157 }
158 }
159
160 // Unknown suffixes.
161 test.equal(typeof publicSuffixes["localhost"], "undefined");
162 test.equal(typeof publicSuffixes["localhost.localdomain"], "undefined");
163
164 test.equal(getDomain("localhost"), "localhost");
165 test.equal(getDomain("localhost.localdomain"), "localhost.localdomain");
166 test.equal(getDomain("mail.localhost.localdomain"), "localhost.localdomain");
167 test.equal(getDomain("www.example.localhost.localdomain"),
168 "localhost.localdomain");
169
170 // Unknown suffixes that overlap partly with known suffixes.
171 test.equal(typeof publicSuffixes["example.com"], "undefined");
172 test.equal(typeof publicSuffixes["africa.com"], "number");
173 test.equal(typeof publicSuffixes["compute.amazonaws.com"], "number");
174
175 test.equal(getDomain("example.com"), "example.com");
176 test.equal(getDomain("mail.example.com"), "example.com");
177 test.equal(getDomain("secure.mail.example.com"), "example.com");
178
179 // Cascading offsets.
180
181 // If these sanity checks fail, look for other examles of cascading offsets
182 // from the public suffix list.
183 test.equal(
184 typeof publicSuffixes[
185 "images.example.s3.dualstack.us-east-1.amazonaws.com"
186 ],
187 "undefined"
188 );
189 test.equal(
190 typeof publicSuffixes["example.s3.dualstack.us-east-1.amazonaws.com"],
191 "undefined"
192 );
193 test.equal(publicSuffixes["s3.dualstack.us-east-1.amazonaws.com"], 1);
194 test.equal(typeof publicSuffixes["dualstack.us-east-1.amazonaws.com"],
195 "undefined");
196 test.equal(typeof publicSuffixes["example.us-east-1.amazonaws.com"],
197 "undefined");
198 test.equal(publicSuffixes["us-east-1.amazonaws.com"], 1);
199 test.equal(typeof publicSuffixes["example.amazonaws.com"], "undefined");
200 test.equal(typeof publicSuffixes["amazonaws.com"], "undefined");
201
202 test.equal(getDomain("images.example.s3.dualstack.us-east-1.amazonaws.com"),
203 "example.s3.dualstack.us-east-1.amazonaws.com");
204 test.equal(getDomain("example.s3.dualstack.us-east-1.amazonaws.com"),
205 "example.s3.dualstack.us-east-1.amazonaws.com");
206 test.equal(getDomain("s3.dualstack.us-east-1.amazonaws.com"),
207 "s3.dualstack.us-east-1.amazonaws.com");
208 test.equal(getDomain("dualstack.us-east-1.amazonaws.com"),
209 "dualstack.us-east-1.amazonaws.com");
210 test.equal(getDomain("example.us-east-1.amazonaws.com"),
211 "example.us-east-1.amazonaws.com");
212 test.equal(getDomain("us-east-1.amazonaws.com"), "us-east-1.amazonaws.com");
213 test.equal(getDomain("example.amazonaws.com"), "amazonaws.com");
214 test.equal(getDomain("amazonaws.com"), "amazonaws.com");
215
216 // Edge case.
217 test.equal(getDomain(""), "");
218
219 test.done();
220 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld