This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathetld_test.cc
568 lines (510 loc) · 18.2 KB
/
etld_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* Copyright (c) 2019 The Brave Software Team. Distributed under the MPL2
* license. This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <string>
#include <iostream>
#include <vector>
#include <set>
#include <fstream>
#include "./CppUnitLite/TestHarness.h"
#include "./CppUnitLite/Test.h"
#include "./etld/internal/parser.h"
#include "./etld/internal/public_suffix_rule.h"
#include "./etld/internal/public_suffix_rule_set.h"
#include "./etld/types.h"
#include "./etld/matcher.h"
#include "./etld/domain.h"
using ::std::string;
using ::std::cout;
using ::std::endl;
using brave_etld::internal::PublicSuffixTextLineType;
using brave_etld::internal::PublicSuffixTextLineParseResult;
using brave_etld::internal::PublicSuffixRuleSetMatchResult;
using brave_etld::internal::PublicSuffixRule;
using brave_etld::internal::PublicSuffixRuleSet;
using brave_etld::internal::PublicSuffixParseResult;
using brave_etld::internal::parse_rule_line;
using brave_etld::internal::parse_rule_text;
using brave_etld::internal::parse_rule_file;
using brave_etld::Domain;
using brave_etld::Matcher;
using brave_etld::DomainInfo;
bool testParsePublicSuffixTestLineNoOps(const string &line,
PublicSuffixTextLineType expectedType) {
PublicSuffixTextLineParseResult result = parse_rule_line(line);
if (result.type != expectedType) {
cout << "Received unexpected parse result for " << line << endl;
return false;
}
return true;
}
bool testParsePublicSuffixTestLineError(const string &line,
const string &expected_error) {
PublicSuffixTextLineParseResult result = parse_rule_line(line);
if (result.type !=
PublicSuffixTextLineType::PublicSuffixTextLineTypeInvalidRule) {
cout << "Expected to receive an error when attempting to parse "
<< "invalid rule '" << line << "' but no error was thrown." << endl;
return false;
}
const size_t error_substr_pos = result.error.find_first_of(expected_error);
if (error_substr_pos == string::npos) {
cout << "Caught an unexpected error when parsing " << line << "\n"
<< "Expected '" << expected_error << "'\n"
<< "Received '" << result.error << endl;
return false;
}
return true;
}
bool testParsePublicSuffixTest(
const string &line,
std::vector<string> expected_labels,
bool expected_exception,
bool expected_wildcard) {
PublicSuffixRule rule(line);
PublicSuffixRule currect_rule(expected_labels, expected_exception,
expected_wildcard);
return currect_rule.Equals(rule);
}
bool _verifyParsePublicSuffixTest(
PublicSuffixParseResult result,
int num_expected_rules,
int num_expected_exception_rules,
int num_expected_wildcard_rules,
int num_expected_whitespace_lines,
int num_expected_comment_lines,
int num_expected_invalid_lines) {
if (num_expected_rules != result.Rules().size()) {
cout << "Incorrect num of rules parsed, expected '"
<< num_expected_rules << "' "
<< "but found '" << result.Rules().size() << "'" << endl;
return false;
}
int num_found_exception_rules = 0;
int num_found_wildcard_rules = 0;
for (auto &elm : result.Rules()) {
if (elm.IsException()) {
num_found_exception_rules += 1;
}
if (elm.IsWildcard()) {
num_found_wildcard_rules += 1;
}
}
if (num_found_exception_rules != num_expected_exception_rules) {
cout << "Found unexpected number of exception rules, expected '"
<< num_expected_exception_rules << "' "
<< "but found '" << num_found_exception_rules << "'" << endl;
return false;
}
if (num_found_exception_rules != num_expected_exception_rules) {
cout << "Found unexpected number of exception rules, expected '"
<< num_expected_exception_rules << "' "
<< "but found '" << num_found_exception_rules << "'" << endl;
return false;
}
if (result.NumWhitespaceLines() != num_expected_whitespace_lines) {
cout << "Found unexpected number of whitespace lines, expected '"
<< num_expected_whitespace_lines << "' "
<< "but found '" << result.NumWhitespaceLines() << "'" << endl;
return false;
}
if (result.NumInvalidRules() != num_expected_invalid_lines) {
cout << "Found unexpected number of invalid rules, expected '"
<< num_expected_invalid_lines << "' "
<< "but found '" << result.NumInvalidRules() << "'" << endl;
return false;
}
if (result.NumCommentLines() != num_expected_comment_lines) {
cout << "Found unexpected number of comment lines, expected '"
<< num_expected_comment_lines << "' "
<< "but found '" << result.NumCommentLines() << "'" << endl;
return false;
}
return true;
}
bool testParsePublicSuffixTextTest(
const string &text,
int num_expected_rules,
int num_expected_exception_rules,
int num_expected_wildcard_rules,
int num_expected_whitespace_lines,
int num_expected_comment_lines,
int num_expected_invalid_lines) {
PublicSuffixParseResult result = parse_rule_text(text);
return _verifyParsePublicSuffixTest(
result,
num_expected_rules,
num_expected_exception_rules,
num_expected_wildcard_rules,
num_expected_whitespace_lines,
num_expected_comment_lines,
num_expected_invalid_lines);
}
bool testParsePublicSuffixFileTest(
const string &file_path,
int num_expected_rules,
int num_expected_exception_rules,
int num_expected_wildcard_rules,
int num_expected_whitespace_lines,
int num_expected_comment_lines,
int num_expected_invalid_lines) {
std::ifstream rule_file;
rule_file.open(file_path, std::ifstream::in);
PublicSuffixParseResult result = parse_rule_file(&rule_file);
return _verifyParsePublicSuffixTest(
result,
num_expected_rules,
num_expected_exception_rules,
num_expected_wildcard_rules,
num_expected_whitespace_lines,
num_expected_comment_lines,
num_expected_invalid_lines);
}
bool testMatchingDomainRules(const string &rule_str, const string &domain_str,
bool should_match) {
Domain domain(domain_str);
PublicSuffixRule rule(rule_str);
bool does_match = rule.Matches(domain);
if (should_match != does_match) {
string desc(should_match
? "should have, but didn't match "
: "didn't, but should have matched ");
cout << "Domain: '" << domain.ToString() << "' "
<< desc
<< "Rule: '" << rule.ToString() << "'" << endl;
return false;
}
return true;
}
bool testPublicSuffixRuleSetMatch(const std::vector<string> &rules,
const string &domain_str, const string &expected_match) {
PublicSuffixRuleSet rule_set;
PublicSuffixRule rule;
for (auto &elm : rules) {
rule = PublicSuffixRule(elm);
rule_set.AddRule(rule);
}
Domain test_domain(domain_str);
PublicSuffixRuleSetMatchResult match_result = rule_set.Match(test_domain);
if (match_result.found_match == false) {
cout << "Expected rule: '" << expected_match << "' "
<< "to find a match, but received none." << endl;
return false;
}
string result_string = match_result.rule->DomainString();
if (result_string != expected_match) {
cout << "Expected rule: '" << expected_match << "' "
<< "to match '" << domain_str << "' "
<< "but received '" << result_string << "'" << endl;
return false;
}
return true;
}
bool testPublicSuffixRuleSetNoMatch(const std::vector<string> &rules,
const string &domain_str) {
PublicSuffixRuleSet rule_set;
PublicSuffixRule rule;
for (auto &elm : rules) {
rule = PublicSuffixRule(elm);
rule_set.AddRule(rule);
}
Domain test_domain(domain_str);
PublicSuffixRuleSetMatchResult match_result = rule_set.Match(test_domain);
if (match_result.found_match == true) {
cout << "Did not expect rule: '" << domain_str << "' to match any "
<< "rules, but found a match." << endl;
return false;
}
return true;
}
bool testPublicSuffixRuleTextApply(const string &rule_str,
const string &domain_str, const DomainInfo &expected) {
PublicSuffixRule rule(rule_str);
Domain domain(domain_str);
DomainInfo extracted = rule.Apply(domain);
if (extracted.tld != expected.tld) {
cout << "Expected tld of '" << expected.tld
<< "' when applying rule '"
<< rule_str << "' to domain '" << domain_str
<< "', but received tld '" << extracted.tld << "'" << endl;
return false;
}
if (extracted.domain != expected.domain) {
cout << "Expected domain of '" << expected.domain
<< "' when applying rule '"
<< rule_str << "' to domain '" << domain_str
<< "', but received domain '"
<< extracted.domain << "'" << endl;
return false;
}
if (extracted.subdomain != expected.subdomain) {
cout << "Expected subdomain of '" << expected.subdomain
<< "' when applying rule '"
<< rule_str << "' to domain '" << domain_str
<< "', but received subdomain '"
<< extracted.subdomain << "'" << endl;
return false;
}
return true;
}
bool testPublicSuffixRuleFileApply(const string &file_path,
const string &domain_str, const DomainInfo &expected) {
std::ifstream rule_file;
rule_file.open(file_path, std::ifstream::in);
Matcher matcher(&rule_file);
DomainInfo extracted = matcher.Match(Domain(domain_str));
if (extracted.tld != expected.tld) {
cout << "Expected tld of '" << expected.tld
<< "' when applying file '"
<< file_path << "' to domain '" << domain_str
<< "', but received tld '" << extracted.tld << "'" << endl;
return false;
}
if (extracted.domain != expected.domain) {
cout << "Expected domain of '" << expected.domain
<< "' when applying file '"
<< file_path << "' to domain '" << domain_str
<< "', but received domain '" << extracted.domain << "'" << endl;
return false;
}
if (extracted.subdomain != expected.subdomain) {
cout << "Expected subdomain of '" << expected.subdomain
<< "' when applying file '"
<< file_path << "' to domain '" << domain_str
<< "', but received subdomain '" << extracted.subdomain << "'" << endl;
return false;
}
return true;
}
TEST(eTLDParseRulesLine, basic) {
CHECK(testParsePublicSuffixTestLineNoOps(
"// this is an option",
brave_etld::internal::PublicSuffixTextLineTypeComment));
CHECK(testParsePublicSuffixTestLineNoOps(
"this.is.a.invalid..rule",
brave_etld::internal::PublicSuffixTextLineTypeInvalidRule));
CHECK(testParsePublicSuffixTestLineNoOps(
"",
brave_etld::internal::PublicSuffixTextLineTypeWhitespace));
// Since we read until the first white space, this will be read
// as "example.", which is invalid because rules cannot hae empty labels
CHECK(testParsePublicSuffixTestLineError(
"example. org",
"Rules cannot end with a delimiter"));
CHECK(testParsePublicSuffixTestLineError(
"example..org",
"Rules cannot contain adjectent delimitors"));
CHECK(testParsePublicSuffixTestLineError(
"example.org.",
"Rules cannot end with a delimiter"));
// Examples taken from https://publicsuffix.org/list/
CHECK(testParsePublicSuffixTest(
"com",
{"com"},
false,
false));
CHECK(testParsePublicSuffixTest(
"*.jp",
{"*", "jp"},
false,
true));
CHECK(testParsePublicSuffixTest(
"*.jp",
{"*", "jp"},
false,
true));
CHECK(testParsePublicSuffixTest(
"*.tokyo.jp",
{"*", "tokyo", "jp"},
false,
true));
CHECK(testParsePublicSuffixTest(
"!pref.hokkaido.jp",
{"pref", "hokkaido", "jp"},
true,
false));
}
TEST(eTLDParseRulesText, basic) {
// Just retesting the examples from above, to make sure they are parsed
// correctly when all in a line.
CHECK(testParsePublicSuffixTextTest(
"com\n*.jp\n*.jp\n*.tokyo.jp\n!pref.hokkaido.jp",
5, 1, 3, 0, 0, 0));
CHECK(testParsePublicSuffixTextTest(
"// ac : https://en.wikipedia.org/wiki/.ac\n"
"ac\n"
"com.ac\n"
"edu.ac\n"
"gov.ac\n"
"net.ac\n"
"mil.ac\n"
"org.ac\n",
7, 0, 0, 0, 1, 0));
CHECK(testParsePublicSuffixTextTest(
"// am : https://en.wikipedia.org/wiki/.am\n"
"am\n"
"\n"
"// ao : https://en.wikipedia.org/wiki/.ao \n"
"// http://www.dns.ao/REGISTR.DOC \n"
"ao\n"
"ed.ao\n"
"gv.ao\n"
"og.ao\n"
"co.ao\n"
"pb.ao\n"
"it.ao\n",
8, 0, 0, 1, 3, 0));
CHECK(testParsePublicSuffixTextTest(
"\n"
"*.wild.rule\n"
"!exception.rule \n"
"// coment line \n"
"example.org\n"
"...badrule",
3, 1, 1, 1, 1, 1));
}
TEST(eTLDParseRulesFile, basic) {
CHECK(testParsePublicSuffixFileTest(
"./test/data/public_suffix_list_short.txt",
32, 1, 1, 8, 12, 0));
}
TEST(eTLDMatchTests, basic) {
CHECK(testMatchingDomainRules("com", "com", true));
CHECK(testMatchingDomainRules("fp.com", "com", false));
CHECK(testMatchingDomainRules("com", "fp.com", true));
CHECK(testMatchingDomainRules("*.hokkaido.jp", "bar.hokkaido.jp", true));
CHECK(testMatchingDomainRules("!pref.hokkaido.jp", "pref.hokkaido.jp", true));
CHECK(testMatchingDomainRules("*.tokyo.jp", "tokyo.jp", false));
CHECK(testMatchingDomainRules("*.tokyo.jp", "other.tokyo.jp", true));
CHECK(testMatchingDomainRules("com", "foo.com", true));
}
TEST(eTLDPublicSuffixRuleSetMatchTests, basic) {
CHECK(testPublicSuffixRuleSetMatch(
{"com", "*.jp", "*.hokkaido.jp"},
"foo.com",
"com"));
CHECK(testPublicSuffixRuleSetMatch(
{"com", "*.jp", "*.hokkaido.jp"},
"hokkaido.jp",
"*.jp"));
CHECK(testPublicSuffixRuleSetMatch(
{"com", "*.jp", "*.hokkaido.jp"},
"pete.hokkaido.jp",
"*.hokkaido.jp"));
CHECK(testPublicSuffixRuleSetNoMatch(
{"com", "*.jp", "*.hokkaido.jp"},
"horse.shoes"));
}
TEST(eTLDPublicSuffixRuleApplyTestTests, basic) {
DomainInfo test_one_expected_match = {"example.jp", "shoes", "pete"};
CHECK(testPublicSuffixRuleTextApply(
"*.jp", "pete.shoes.example.jp", test_one_expected_match));
DomainInfo test_two_expected_match = {"horse", "the", "we.love"};
CHECK(testPublicSuffixRuleTextApply(
"horse", "we.love.the.horse", test_two_expected_match));
DomainInfo test_three_expected_match = {"tokyo.jp", "metro", "slate"};
CHECK(testPublicSuffixRuleTextApply(
"!metro.tokyo.jp", "slate.metro.tokyo.jp", test_three_expected_match));
}
TEST(eTLDPublicSuffixRuleApplyFileTests, basic) {
DomainInfo file_test_one = {"com", "google", "www"};
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
"www.google.com",
file_test_one));
DomainInfo file_test_two = {"co.uk", "google", ""};
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
"google.co.uk",
file_test_two));
// Tests taken from https://www.npmjs.com/package/tldts
DomainInfo file_test_three = {"s3.amazonaws.com", "spark-public", ""};
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
"spark-public.s3.amazonaws.com",
file_test_three));
DomainInfo file_test_four = {"org", "writethedocs", "www"};
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
"www.writethedocs.org",
file_test_four));
// Tests taken from https://www.npmjs.com/package/tld
DomainInfo file_test_five = {"bar.sch.uk", "foo", "www"};
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
"www.foo.bar.sch.uk",
file_test_five));
// Tests taken from https://www.npmjs.com/package/parse-domain
DomainInfo file_test_six = {"co.uk", "example", "some.subdomain"};
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
"some.subdomain.example.co.uk",
file_test_six));
// Tests adapted from
// https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt
struct DomainInfoTestCase {
string input;
DomainInfo expected;
};
DomainInfoTestCase file_test_cases[] = {
{"com", {"com", "", ""}},
{"example.com", {"com", "example", ""}},
{"www.example.com", {"com", "example", "www"}},
{"example", {"example", "", ""}},
{"example.example", {"example", "example", ""}},
{"b.example.example", {"example", "example", "b"}},
{"a.b.example.example", {"example", "example", "a.b"}},
{"biz", {"biz", "", ""}},
{"domain.biz", {"biz", "domain", ""}},
{"b.domain.biz", {"biz", "domain", "b"}},
{"uk.com", {"uk.com", "", ""}},
{"example.uk.com", {"uk.com", "example", ""}},
{"b.example.uk.com", {"uk.com", "example", "b"}},
{"a.b.example.uk.com", {"uk.com", "example", "a.b"}},
{"jp", {"jp", "", ""}},
{"test.jp", {"jp", "test", ""}},
{"www.test.jp", {"jp", "test", "www"}},
{"ac.jp", {"ac.jp", "", ""}},
{"test.ac.jp", {"ac.jp", "test", ""}},
{"www.test.ac.jp", {"ac.jp", "test", "www"}},
{"kyoto.jp", {"kyoto.jp", "", ""}},
{"test.kyoto.jp", {"kyoto.jp", "test", ""}},
{"ide.kyoto.jp", {"ide.kyoto.jp", "", ""}},
{"b.ide.kyoto.jp", {"ide.kyoto.jp", "b", ""}},
{"a.b.ide.kyoto.jp", {"ide.kyoto.jp", "b", "a"}},
{"c.kobe.jp", {"c.kobe.jp", "", ""}},
{"b.c.kobe.jp", {"c.kobe.jp", "b", ""}},
{"a.b.c.kobe.jp", {"c.kobe.jp", "b", "a"}},
{"city.kobe.jp", {"kobe.jp", "city", ""}},
{"www.city.kobe.jp", {"kobe.jp", "city", "www"}},
{"ck", {"ck", "", ""}},
{"test.ck", {"test.ck", "", ""}},
{"b.test.ck", {"test.ck", "b", ""}},
{"a.b.test.ck", {"test.ck", "b", "a"}},
{"www.ck", {"ck", "www", ""}},
{"www.www.ck", {"ck", "www", "www"}},
{"us", {"us", "", ""}},
{"test.us", {"us", "test", ""}},
{"www.test.us", {"us", "test", "www"}},
{"ak.us", {"ak.us", "", ""}},
{"test.ak.us", {"ak.us", "test", ""}},
{"www.test.ak.us", {"ak.us", "test", "www"}},
{"k12.ak.us", {"k12.ak.us", "", ""}},
{"test.k12.ak.us", {"k12.ak.us", "test", ""}},
{"www.test.k12.ak.us", {"k12.ak.us", "test", "www"}},
{"食狮.com.cn", {"com.cn", "食狮", ""}},
{"食狮.公司.cn", {"公司.cn", "食狮", ""}},
{"sushi.公司.cn", {"公司.cn", "sushi", ""}},
{"公司.cn", {"公司.cn", "", ""}},
{"食狮.中国", {"中国", "食狮", ""}},
{"shishi.中国", {"中国", "shishi", ""}},
{"中国", {"中国", "", ""}},
};
for (auto &elm : file_test_cases) {
CHECK(testPublicSuffixRuleFileApply(
"./test/data/public_suffix_list.dat",
elm.input,
elm.expected));
}
}