File: url_util_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (686 lines) | stat: -rw-r--r-- 30,806 bytes parent folder | download | duplicates (3)
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/url_matcher/url_util.h"

#include <memory>

#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace url_matcher {
namespace util {

namespace {

GURL GetEmbeddedURL(const std::string& url) {
  return url_matcher::util::GetEmbeddedURL(GURL(url));
}

// Parameters for the FilterToComponents test.
struct FilterTestParams {
 public:
  FilterTestParams(const std::string& filter,
                   const std::string& scheme,
                   const std::string& host,
                   bool match_subdomains,
                   uint16_t port,
                   const std::string& path)
      : filter_(filter),
        scheme_(scheme),
        host_(host),
        match_subdomains_(match_subdomains),
        port_(port),
        path_(path) {}

  FilterTestParams(const FilterTestParams& params)
      : filter_(params.filter_),
        scheme_(params.scheme_),
        host_(params.host_),
        match_subdomains_(params.match_subdomains_),
        port_(params.port_),
        path_(params.path_) {}

  const FilterTestParams& operator=(const FilterTestParams& params) {
    filter_ = params.filter_;
    scheme_ = params.scheme_;
    host_ = params.host_;
    match_subdomains_ = params.match_subdomains_;
    port_ = params.port_;
    path_ = params.path_;
    return *this;
  }

  const std::string& filter() const { return filter_; }
  const std::string& scheme() const { return scheme_; }
  const std::string& host() const { return host_; }
  bool match_subdomains() const { return match_subdomains_; }
  uint16_t port() const { return port_; }
  const std::string& path() const { return path_; }

 private:
  std::string filter_;
  std::string scheme_;
  std::string host_;
  bool match_subdomains_;
  uint16_t port_;
  std::string path_;
};

// Prints better debug information for Valgrind. Without this function, a
// generic one will print the raw bytes in FilterTestParams, which due to some
// likely padding will access uninitialized memory.
void PrintTo(const FilterTestParams& params, std::ostream* os) {
  *os << params.filter();
}

bool MatchFilters(const std::vector<std::string>& patterns,
                  const std::string& url) {
  // Add the pattern to the matcher.
  URLMatcher matcher;
  base::Value::List list;
  for (const auto& pattern : patterns)
    list.Append(pattern);
  AddAllowFiltersWithLimit(&matcher, list);
  return !matcher.MatchURL(GURL(url)).empty();
}

class FilterToComponentsTest : public testing::TestWithParam<FilterTestParams> {
 public:
  FilterToComponentsTest() = default;
  FilterToComponentsTest(const FilterToComponentsTest&) = delete;
  FilterToComponentsTest& operator=(const FilterToComponentsTest&) = delete;
};

class OnlyWildcardTest
    : public testing::TestWithParam<std::tuple<std::string /* scheme */,
                                               std::string /* opt_host */,
                                               std::string /* port */,
                                               std::string /* path */,
                                               std::string /* query*/>> {
 public:
  OnlyWildcardTest() = default;
  OnlyWildcardTest(const OnlyWildcardTest&) = delete;
  OnlyWildcardTest& operator=(const OnlyWildcardTest&) = delete;
};

}  // namespace

TEST(URLUtilTest, Normalize) {
  // Username is cleared.
  EXPECT_EQ(Normalize(GURL("http://dino@example/foo")),
            GURL("http://example/foo"));

  // Username and password are cleared.
  EXPECT_EQ(Normalize(GURL("http://dino:hunter2@example/")),
            GURL("http://example/"));

  // Query string is cleared.
  EXPECT_EQ(Normalize(GURL("http://example.com/foo?widgetId=42")),
            GURL("http://example.com/foo"));
  EXPECT_EQ(Normalize(GURL("https://example.com/?widgetId=42&frobinate=true")),
            GURL("https://example.com/"));

  // Ref is cleared.
  EXPECT_EQ(Normalize(GURL("http://example.com/foo#widgetSection")),
            GURL("http://example.com/foo"));

  // Port is NOT cleared.
  EXPECT_EQ(Normalize(GURL("http://example.com:443/")),
            GURL("http://example.com:443/"));

  // All together now.
  EXPECT_EQ(
      Normalize(GURL("https://dino:hunter2@example.com:443/foo?widgetId=42")),
      GURL("https://example.com:443/foo"));
}

TEST(URLUtilTest, GetEmbeddedURLAmpCache) {
  // Base case.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://cdn.ampproject.org/c/example.com"));
  // "s/" means "use https".
  EXPECT_EQ(GURL("https://example.com"),
            GetEmbeddedURL("https://cdn.ampproject.org/c/s/example.com"));
  // With path and query. Fragment is not extracted.
  EXPECT_EQ(GURL("https://example.com/path/to/file.html?q=asdf"),
            GetEmbeddedURL("https://cdn.ampproject.org/c/s/example.com/path/to/"
                           "file.html?q=asdf#baz"));
  // Publish subdomain can be included but doesn't affect embedded URL.
  EXPECT_EQ(
      GURL("http://example.com"),
      GetEmbeddedURL("https://example-com.cdn.ampproject.org/c/example.com"));
  EXPECT_EQ(
      GURL("http://example.com"),
      GetEmbeddedURL("https://example-org.cdn.ampproject.org/c/example.com"));

  // Different host is not supported.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://www.ampproject.org/c/example.com"));
  // Different TLD is not supported.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://cdn.ampproject.com/c/example.com"));
  // Content type ("c/") is missing.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://cdn.ampproject.org/example.com"));
  // Content type is mis-formatted, must be a single character.
  EXPECT_EQ(GURL(),
            GetEmbeddedURL("https://cdn.ampproject.org/cd/example.com"));
}

TEST(URLUtilTest, GetEmbeddedURLGoogleAmpViewer) {
  // Base case.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://www.google.com/amp/example.com"));
  // "s/" means "use https".
  EXPECT_EQ(GURL("https://example.com"),
            GetEmbeddedURL("https://www.google.com/amp/s/example.com"));
  // Different Google TLDs are supported.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://www.google.de/amp/example.com"));
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://www.google.co.uk/amp/example.com"));
  // With path.
  EXPECT_EQ(GURL("http://example.com/path"),
            GetEmbeddedURL("https://www.google.com/amp/example.com/path"));
  // Query is *not* part of the embedded URL.
  EXPECT_EQ(
      GURL("http://example.com/path"),
      GetEmbeddedURL("https://www.google.com/amp/example.com/path?q=baz"));
  // Query and fragment in percent-encoded form *are* part of the embedded URL.
  EXPECT_EQ(
      GURL("http://example.com/path?q=foo#bar"),
      GetEmbeddedURL(
          "https://www.google.com/amp/example.com/path%3fq=foo%23bar?q=baz"));
  // "/" may also be percent-encoded.
  EXPECT_EQ(GURL("http://example.com/path?q=foo#bar"),
            GetEmbeddedURL("https://www.google.com/amp/"
                           "example.com%2fpath%3fq=foo%23bar?q=baz"));

  // Missing "amp/".
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://www.google.com/example.com"));
  // Path component before the "amp/".
  EXPECT_EQ(GURL(),
            GetEmbeddedURL("https://www.google.com/foo/amp/example.com"));
  // Different host.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://www.other.com/amp/example.com"));
  // Different subdomain.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://mail.google.com/amp/example.com"));
  // Invalid TLD.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://www.google.nope/amp/example.com"));

  // Valid TLD that is not considered safe to display to the user by
  // UnescapeURLComponent(). Note that when UTF-8 characters appear in a domain
  // name, as is the case here, they're replaced by equivalent punycode by the
  // GURL constructor.
  EXPECT_EQ(GURL("http://www.xn--iv8h.com/"),
            GetEmbeddedURL("https://www.google.com/amp/www.%F0%9F%94%8F.com/"));
  // Invalid UTF-8 characters.
  EXPECT_EQ(GURL("http://example.com/%81%82%83"),
            GetEmbeddedURL("https://www.google.com/amp/example.com/%81%82%83"));
}

TEST(URLUtilTest, GetEmbeddedURLGoogleWebCache) {
  // Base case.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://webcache.googleusercontent.com/"
                           "search?q=cache:ABCDEFGHI-JK:example.com/"));
  // With search query.
  EXPECT_EQ(
      GURL("http://example.com"),
      GetEmbeddedURL("https://webcache.googleusercontent.com/"
                     "search?q=cache:ABCDEFGHI-JK:example.com/+search_query"));
  // Without fingerprint.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://webcache.googleusercontent.com/"
                           "search?q=cache:example.com/"));
  // With search query, without fingerprint.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://webcache.googleusercontent.com/"
                           "search?q=cache:example.com/+search_query"));
  // Query params other than "q=" don't matter.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://webcache.googleusercontent.com/"
                           "search?a=b&q=cache:example.com/&c=d"));
  // With scheme.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://webcache.googleusercontent.com/"
                           "search?q=cache:http://example.com/"));
  // Preserve https.
  EXPECT_EQ(GURL("https://example.com"),
            GetEmbeddedURL("https://webcache.googleusercontent.com/"
                           "search?q=cache:https://example.com/"));

  // Wrong host.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://www.googleusercontent.com/"
                                   "search?q=cache:example.com/"));
  // Wrong path.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://webcache.googleusercontent.com/"
                                   "path?q=cache:example.com/"));
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://webcache.googleusercontent.com/"
                                   "path/search?q=cache:example.com/"));
  // Missing "cache:".
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://webcache.googleusercontent.com/"
                                   "search?q=example.com"));
  // Wrong fingerprint.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://webcache.googleusercontent.com/"
                                   "search?q=cache:123:example.com/"));
  // Wrong query param.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://webcache.googleusercontent.com/"
                                   "search?a=cache:example.com/"));
  // Invalid scheme.
  EXPECT_EQ(GURL(), GetEmbeddedURL("https://webcache.googleusercontent.com/"
                                   "search?q=cache:abc://example.com/"));
}

TEST(URLUtilTest, GetEmbeddedURLTranslate) {
  // Base case.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://translate.google.com/path?u=example.com"));
  // Different TLD.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL("https://translate.google.de/path?u=example.com"));
  // Alternate base URL.
  EXPECT_EQ(GURL("http://example.com"),
            GetEmbeddedURL(
                "https://translate.googleusercontent.com/path?u=example.com"));
  // With scheme.
  EXPECT_EQ(
      GURL("http://example.com"),
      GetEmbeddedURL("https://translate.google.com/path?u=http://example.com"));
  // With https scheme.
  EXPECT_EQ(GURL("https://example.com"),
            GetEmbeddedURL(
                "https://translate.google.com/path?u=https://example.com"));
  // With other parameters.
  EXPECT_EQ(
      GURL("http://example.com"),
      GetEmbeddedURL(
          "https://translate.google.com/path?a=asdf&u=example.com&b=fdsa"));

  // Different subdomain is not supported.
  EXPECT_EQ(GURL(), GetEmbeddedURL(
                        "https://translate.foo.google.com/path?u=example.com"));
  EXPECT_EQ(GURL(), GetEmbeddedURL(
                        "https://translate.www.google.com/path?u=example.com"));
  EXPECT_EQ(
      GURL(),
      GetEmbeddedURL("https://translate.google.google.com/path?u=example.com"));
  EXPECT_EQ(GURL(), GetEmbeddedURL(
                        "https://foo.translate.google.com/path?u=example.com"));
  EXPECT_EQ(GURL(),
            GetEmbeddedURL("https://translate2.google.com/path?u=example.com"));
  EXPECT_EQ(GURL(),
            GetEmbeddedURL(
                "https://translate2.googleusercontent.com/path?u=example.com"));
  // Different TLD is not supported for googleusercontent.
  EXPECT_EQ(GURL(),
            GetEmbeddedURL(
                "https://translate.googleusercontent.de/path?u=example.com"));
  // Query parameter ("u=...") is missing.
  EXPECT_EQ(GURL(),
            GetEmbeddedURL("https://translate.google.com/path?t=example.com"));
}

INSTANTIATE_TEST_SUITE_P(URLUtilTest,
                         OnlyWildcardTest,
                         testing::Combine(testing::Values("", "https://"),
                                          testing::Values("", "dev."),
                                          testing::Values("", ":1234"),
                                          testing::Values("", "/path"),
                                          testing::Values("", "?query")));

TEST_P(OnlyWildcardTest, OnlyWildcard) {
  // Check wildcard filter works on any permutations of format
  // [scheme://][.]host[:port][/path][@query]
  const std::string scheme = std::get<0>(GetParam());
  const std::string opt_host = std::get<1>(GetParam());
  const std::string port = std::get<2>(GetParam());
  const std::string path = std::get<3>(GetParam());
  const std::string query = std::get<4>(GetParam());
  const std::string url =
      scheme + opt_host + "google.com" + port + path + query;
  EXPECT_TRUE(MatchFilters({"*"}, url));
}

TEST(URLUtilTest, SingleFilter) {
  // Match domain and all subdomains, for any filtered scheme.
  EXPECT_TRUE(MatchFilters({"google.com"}, "http://google.com"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "http://google.com/"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "http://google.com/whatever"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "https://google.com/"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "bogus://google.com/"));
  EXPECT_FALSE(MatchFilters({"google.com"}, "http://notgoogle.com/"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "http://mail.google.com"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "http://x.mail.google.com"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "https://x.mail.google.com/"));
  EXPECT_TRUE(MatchFilters({"google.com"}, "http://x.y.google.com/a/b"));
  EXPECT_FALSE(MatchFilters({"google.com"}, "http://youtube.com/"));

  // Filter only http, ftp and ws schemes.
  EXPECT_TRUE(MatchFilters({"http://secure.com"}, "http://secure.com"));
  EXPECT_TRUE(
      MatchFilters({"http://secure.com"}, "http://secure.com/whatever"));
  EXPECT_TRUE(MatchFilters({"ftp://secure.com"}, "ftp://secure.com/"));
  EXPECT_TRUE(MatchFilters({"ws://secure.com"}, "ws://secure.com"));
  EXPECT_FALSE(MatchFilters({"http://secure.com"}, "https://secure.com/"));
  EXPECT_FALSE(MatchFilters({"ws://secure.com"}, "wss://secure.com"));
  EXPECT_TRUE(MatchFilters({"http://secure.com"}, "http://www.secure.com"));
  EXPECT_FALSE(MatchFilters({"http://secure.com"}, "https://www.secure.com"));
  EXPECT_FALSE(MatchFilters({"ws://secure.com"}, "wss://www.secure.com"));

  // Filter only a certain path prefix.
  EXPECT_TRUE(MatchFilters({"path.to/ruin"}, "http://path.to/ruin"));
  EXPECT_TRUE(MatchFilters({"path.to/ruin"}, "https://path.to/ruin"));
  EXPECT_TRUE(MatchFilters({"path.to/ruin"}, "http://path.to/ruins"));
  EXPECT_TRUE(MatchFilters({"path.to/ruin"}, "http://path.to/ruin/signup"));
  EXPECT_TRUE(MatchFilters({"path.to/ruin"}, "http://www.path.to/ruin"));
  EXPECT_FALSE(MatchFilters({"path.to/ruin"}, "http://path.to/fortune"));

  // Filter only a certain path prefix and scheme.
  EXPECT_TRUE(
      MatchFilters({"https://s.aaa.com/path"}, "https://s.aaa.com/path"));
  EXPECT_TRUE(
      MatchFilters({"https://s.aaa.com/path"}, "https://s.aaa.com/path/bbb"));
  EXPECT_FALSE(
      MatchFilters({"https://s.aaa.com/path"}, "http://s.aaa.com/path"));
  EXPECT_FALSE(
      MatchFilters({"https://s.aaa.com/path"}, "https://aaa.com/path"));
  EXPECT_FALSE(
      MatchFilters({"https://s.aaa.com/path"}, "https://x.aaa.com/path"));
  EXPECT_FALSE(
      MatchFilters({"https://s.aaa.com/path"}, "https://s.aaa.com/bbb"));
  EXPECT_FALSE(MatchFilters({"https://s.aaa.com/path"}, "https://s.aaa.com/"));

  // Filter only ws and wss schemes.
  EXPECT_TRUE(MatchFilters({"ws://ws.aaa.com"}, "ws://ws.aaa.com"));
  EXPECT_TRUE(MatchFilters({"wss://ws.aaa.com"}, "wss://ws.aaa.com"));
  EXPECT_FALSE(MatchFilters({"ws://ws.aaa.com"}, "http://ws.aaa.com"));
  EXPECT_FALSE(MatchFilters({"ws://ws.aaa.com"}, "https://ws.aaa.com"));
  EXPECT_FALSE(MatchFilters({"ws://ws.aaa.com"}, "ftp://ws.aaa.com"));

  // Match an ip address.
  EXPECT_TRUE(MatchFilters({"123.123.123.123"}, "http://123.123.123.123/"));
  EXPECT_FALSE(MatchFilters({"123.123.123.123"}, "http://123.123.123.124/"));

  // Open an exception.
  EXPECT_FALSE(MatchFilters({"plus.google.com"}, "http://google.com/"));
  EXPECT_FALSE(MatchFilters({"plus.google.com"}, "http://www.google.com/"));
  EXPECT_TRUE(MatchFilters({"plus.google.com"}, "http://plus.google.com/"));

  // Match exactly "google.com", only for http.
  EXPECT_TRUE(MatchFilters({"http://.google.com"}, "http://google.com/"));
  EXPECT_FALSE(MatchFilters({"http://.google.com"}, "https://google.com/"));
  EXPECT_FALSE(MatchFilters({"http://.google.com"}, "http://www.google.com/"));
}

TEST(URLUtilTest, BasicCoverage) {
  // Tests to cover the documentation from
  // http://www.chromium.org/administrators/url-blocklist-filter-format

  // [scheme://][.]host[:port][/path][@query]
  // Scheme can be http, https, ftp, chrome, etc. This field is optional, and
  // must be followed by '://'.
  EXPECT_TRUE(MatchFilters({"file://*"}, "file:///abc.txt"));
  EXPECT_TRUE(MatchFilters({"file:*"}, "file:///usr/local/boot.txt"));
  EXPECT_TRUE(MatchFilters(
      {"data:*"},
      "data:image/svg+xml,<svg xmlns=\"http://www.w3.org/2000/svg\"/>"));
  EXPECT_TRUE(MatchFilters({"https://*"}, "https:///abc.txt"));
  EXPECT_TRUE(MatchFilters({"ftp://*"}, "ftp://ftp.txt"));
  EXPECT_TRUE(MatchFilters({"chrome://*"}, "chrome:policy"));
  EXPECT_TRUE(MatchFilters({"noscheme"}, "http://noscheme"));
  // Filter custom schemes.
  EXPECT_TRUE(MatchFilters({"custom://*"}, "custom://example_app"));
  EXPECT_TRUE(MatchFilters({"custom:*"}, "custom:example2_app"));
  EXPECT_FALSE(MatchFilters({"custom://*"}, "customs://example_apps"));
  EXPECT_FALSE(MatchFilters({"custom://*"}, "cust*://example_ap"));
  EXPECT_FALSE(MatchFilters({"custom://*"}, "ecustom:example_app"));
  EXPECT_TRUE(MatchFilters({"custom://*"}, "custom:///abc.txt"));
  // Tests for custom scheme patterns that are not supported.
  EXPECT_FALSE(MatchFilters({"wrong://app"}, "wrong://app"));
  EXPECT_FALSE(MatchFilters({"wrong ://*"}, "wrong ://app"));
  EXPECT_FALSE(MatchFilters({" wrong:*"}, " wrong://app"));

  // Omitting the scheme matches most standard schemes.
  EXPECT_TRUE(MatchFilters({"example.com"}, "chrome:example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "chrome://example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "file://example.com/"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "ftp://example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "http://example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "https://example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "ws://example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "wss://example.com"));

  // Some schemes are not matched when the scheme is omitted.
  EXPECT_FALSE(MatchFilters({"example.com"}, "about:example.com"));
  EXPECT_FALSE(MatchFilters({"example.com/*"}, "filesystem:///something"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "about://example.com"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "custom://example.com"));
  EXPECT_TRUE(MatchFilters({"example"}, "custom://example"));

  // An optional '.' (dot) can prefix the host field to disable subdomain
  // matching, see below for details.
  EXPECT_TRUE(MatchFilters({".example.com"}, "http://example.com/path"));
  EXPECT_FALSE(MatchFilters({".example.com"}, "http://mail.example.com/path"));
  EXPECT_TRUE(MatchFilters({"example.com"}, "http://mail.example.com/path"));
  EXPECT_TRUE(MatchFilters({"ftp://.ftp.file"}, "ftp://ftp.file"));
  EXPECT_FALSE(MatchFilters({"ftp://.ftp.file"}, "ftp://sub.ftp.file"));

  // The host field is required, and is a valid hostname or an IP address. It
  // can also take the special '*' value, see below for details.
  EXPECT_TRUE(MatchFilters({"*"}, "http://anything"));
  EXPECT_TRUE(MatchFilters({"*"}, "ftp://anything"));
  EXPECT_TRUE(MatchFilters({"*"}, "custom://anything"));
  EXPECT_TRUE(MatchFilters({"host"}, "http://host:8080"));
  EXPECT_FALSE(MatchFilters({"host"}, "file:///host"));
  EXPECT_TRUE(MatchFilters({"10.1.2.3"}, "http://10.1.2.3:8080/path"));
  // No host, will match nothing.
  EXPECT_FALSE(MatchFilters({":8080"}, "http://host:8080"));
  EXPECT_FALSE(MatchFilters({":8080"}, "http://:8080"));

  // An optional port can come after the host. It must be a valid port value
  // from 1 to 65535.
  EXPECT_TRUE(MatchFilters({"host:8080"}, "http://host:8080/path"));
  EXPECT_TRUE(MatchFilters({"host:1"}, "http://host:1/path"));
  // Out of range port.
  EXPECT_FALSE(MatchFilters({"host:65536"}, "http://host:65536/path"));
  // Star is not allowed in port numbers.
  EXPECT_FALSE(MatchFilters({"example.com:*"}, "http://example.com"));
  EXPECT_FALSE(MatchFilters({"example.com:*"}, "http://example.com:8888"));

  // An optional path can come after port.
  EXPECT_TRUE(MatchFilters({"host/path"}, "http://host:8080/path"));
  EXPECT_TRUE(MatchFilters({"host/path/path2"}, "http://host/path/path2"));
  EXPECT_TRUE(MatchFilters({"host/path"}, "http://host/path/path2"));

  // An optional query can come in the end, which is a set of key-value and
  // key-only tokens delimited by '&'. The key-value tokens are separated
  // by '='. A query token can optionally end with a '*' to indicate prefix
  // match. Token order is ignored during matching.
  EXPECT_TRUE(MatchFilters({"host?q1=1&q2=2"}, "http://host?q2=2&q1=1"));
  EXPECT_FALSE(MatchFilters({"host?q1=1&q2=2"}, "http://host?q2=1&q1=2"));
  EXPECT_FALSE(MatchFilters({"host?q1=1&q2=2"}, "http://host?Q2=2&Q1=1"));
  EXPECT_TRUE(MatchFilters({"host?q1=1&q2=2"}, "http://host?q2=2&q1=1&q3=3"));
  EXPECT_TRUE(MatchFilters({"host?q1=1&q2=2*"}, "http://host?q2=21&q1=1&q3=3"));

  // user:pass fields can be included but will be ignored
  // (e.g. http://user:pass@ftp.example.com/pub/bigfile.iso).
  EXPECT_TRUE(
      MatchFilters({"host.com/path"}, "http://user:pass@host.com:8080/path"));
  EXPECT_TRUE(MatchFilters({"ftp://host.com/path"},
                           "ftp://user:pass@host.com:8080/path"));

  // Case sensitivity.
  // Scheme is case insensitive.
  EXPECT_TRUE(MatchFilters({"suPPort://*"}, "support:example"));
  EXPECT_TRUE(MatchFilters({"FILE://*"}, "file:example"));
  EXPECT_TRUE(MatchFilters({"FILE://*"}, "FILE://example"));
  EXPECT_TRUE(MatchFilters({"FtP:*"}, "ftp://example"));
  EXPECT_TRUE(MatchFilters({"http://example.com"}, "HTTP://example.com"));
  EXPECT_TRUE(MatchFilters({"HTTP://example.com"}, "http://example.com"));
  // Host is case insensitive.
  EXPECT_TRUE(MatchFilters({"http://EXAMPLE.COM"}, "http://example.com"));
  EXPECT_TRUE(MatchFilters({"Example.com"}, "http://examplE.com/Path?Query=1"));
  // Path is case sensitive.
  EXPECT_FALSE(MatchFilters({"example.com/Path"}, "http://example.com/path"));
  EXPECT_TRUE(MatchFilters({"http://example.com/aB"}, "http://example.com/aB"));
  EXPECT_FALSE(
      MatchFilters({"http://example.com/aB"}, "http://example.com/Ab"));
  EXPECT_FALSE(
      MatchFilters({"http://example.com/aB"}, "http://example.com/ab"));
  EXPECT_FALSE(
      MatchFilters({"http://example.com/aB"}, "http://example.com/AB"));
  // Query is case sensitive.
  EXPECT_FALSE(MatchFilters({"host/path?Query=1"}, "http://host/path?query=1"));
}

TEST(URLUtilTest, MultipleFilters) {
  // Test exceptions to path prefixes, and most specific matches.
  std::vector<std::string> patterns = {"s.xxx.com/a/b",
                                       "https://s.xxx.com/a/b/c/d"};
  EXPECT_FALSE(MatchFilters(patterns, "http://s.xxx.com/a"));
  EXPECT_FALSE(MatchFilters(patterns, "http://s.xxx.com/a/x"));
  EXPECT_FALSE(MatchFilters(patterns, "https://s.xxx.com/a/x"));
  EXPECT_TRUE(MatchFilters(patterns, "http://s.xxx.com/a/b"));
  EXPECT_TRUE(MatchFilters(patterns, "https://s.xxx.com/a/b"));
  EXPECT_TRUE(MatchFilters(patterns, "http://s.xxx.com/a/b/x"));
  EXPECT_TRUE(MatchFilters(patterns, "http://s.xxx.com/a/b/c"));
  EXPECT_TRUE(MatchFilters(patterns, "https://s.xxx.com/a/b/c"));
  EXPECT_TRUE(MatchFilters(patterns, "https://s.xxx.com/a/b/c/x"));
  EXPECT_TRUE(MatchFilters(patterns, "https://s.xxx.com/a/b/c/d"));
  EXPECT_TRUE(MatchFilters(patterns, "http://s.xxx.com/a/b/c/d"));
  EXPECT_TRUE(MatchFilters(patterns, "https://s.xxx.com/a/b/c/d/x"));
  EXPECT_TRUE(MatchFilters(patterns, "http://s.xxx.com/a/b/c/d/x"));
  EXPECT_FALSE(MatchFilters(patterns, "http://xxx.com/a"));
  EXPECT_FALSE(MatchFilters(patterns, "http://xxx.com/a/b"));

  // Match queries.
  std::vector<std::string> queries = {"*?q=1234", "*?q=5678", "*?a=1&b=2",
                                      "youtube.com?foo=baz",
                                      "youtube.com?foo=bar*"};
  EXPECT_TRUE(MatchFilters(queries, "http://google.com?q=1234"));
  EXPECT_TRUE(MatchFilters(queries, "http://google.com?q=5678"));
  EXPECT_TRUE(MatchFilters(queries, "http://google.com?a=1&b=2"));
  EXPECT_TRUE(MatchFilters(queries, "http://google.com?b=2&a=1"));
  EXPECT_TRUE(MatchFilters(queries, "http://google.com?a=1&b=4&q=1234"));
  EXPECT_TRUE(MatchFilters(queries, "http://youtube.com?foo=baz"));
  EXPECT_TRUE(MatchFilters(queries, "http://youtube.com?foo=barbaz"));
  EXPECT_TRUE(MatchFilters(queries, "http://youtube.com?a=1&foo=barbaz"));
  EXPECT_FALSE(MatchFilters(queries, "http://google.com?r=1234"));
  EXPECT_FALSE(MatchFilters(queries, "http://google.com?r=5678"));
  EXPECT_FALSE(MatchFilters(queries, "http://google.com?a=2&b=1"));
  EXPECT_FALSE(MatchFilters(queries, "http://google.com?b=1&a=2"));
  EXPECT_FALSE(MatchFilters(queries, "http://google.com?a=1&b=3"));
  EXPECT_FALSE(MatchFilters(queries, "http://youtube.com?foo=meh"));
  EXPECT_FALSE(MatchFilters(queries, "http://youtube.com?foo=bazbar"));
  EXPECT_FALSE(MatchFilters(queries, "http://youtube.com?foo=ba"));
}

INSTANTIATE_TEST_SUITE_P(
    URLUtilTest,
    FilterToComponentsTest,
    testing::Values(
        FilterTestParams("google.com",
                         std::string(),
                         ".google.com",
                         true,
                         0u,
                         std::string()),
        FilterTestParams(".google.com",
                         std::string(),
                         "google.com",
                         false,
                         0u,
                         std::string()),
        FilterTestParams("http://google.com",
                         "http",
                         ".google.com",
                         true,
                         0u,
                         std::string()),
        FilterTestParams("google.com/",
                         std::string(),
                         ".google.com",
                         true,
                         0u,
                         "/"),
        FilterTestParams("http://google.com:8080/whatever",
                         "http",
                         ".google.com",
                         true,
                         8080u,
                         "/whatever"),
        FilterTestParams("http://user:pass@google.com:8080/whatever",
                         "http",
                         ".google.com",
                         true,
                         8080u,
                         "/whatever"),
        FilterTestParams("123.123.123.123",
                         std::string(),
                         "123.123.123.123",
                         false,
                         0u,
                         std::string()),
        FilterTestParams("https://123.123.123.123",
                         "https",
                         "123.123.123.123",
                         false,
                         0u,
                         std::string()),
        FilterTestParams("123.123.123.123/",
                         std::string(),
                         "123.123.123.123",
                         false,
                         0u,
                         "/"),
        FilterTestParams("http://123.123.123.123:123/whatever",
                         "http",
                         "123.123.123.123",
                         false,
                         123u,
                         "/whatever"),
        FilterTestParams("*",
                         std::string(),
                         std::string(),
                         true,
                         0u,
                         std::string()),
        FilterTestParams("ftp://*",
                         "ftp",
                         std::string(),
                         true,
                         0u,
                         std::string()),
        FilterTestParams("http://*/whatever",
                         "http",
                         std::string(),
                         true,
                         0u,
                         "/whatever"),
        FilterTestParams("data:image/png",
                         "data",
                         std::string(),
                         true,
                         0u,
                         "image/png")));

TEST_P(FilterToComponentsTest, FilterToComponents) {
  std::string scheme;
  std::string host;
  bool match_subdomains = true;
  uint16_t port = 42;
  std::string path;
  std::string query;

  FilterToComponents(GetParam().filter(), &scheme, &host, &match_subdomains,
                     &port, &path, &query);
  EXPECT_EQ(GetParam().scheme(), scheme);
  EXPECT_EQ(GetParam().host(), host);
  EXPECT_EQ(GetParam().match_subdomains(), match_subdomains);
  EXPECT_EQ(GetParam().port(), port);
  EXPECT_EQ(GetParam().path(), path);
}

}  // namespace util
}  // namespace url_matcher