-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleTests.cs
More file actions
1026 lines (931 loc) · 41.1 KB
/
GoogleTests.cs
File metadata and controls
1026 lines (931 loc) · 41.1 KB
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Buffers;
using System.Diagnostics;
using System.Text;
using RobotsTxt;
using Xunit;
namespace TestRobotsTxt
{
public class GoogleTests
{
bool IsUserAgentAllowed(string robotsTxt, string userAgent, string url)
{
RobotsMatcher matcher = new RobotsMatcher();
return matcher.OneAgentAllowedByRobots(Encoding.UTF8.GetBytes(robotsTxt), new UTF8Encoding().GetBytes(userAgent), url);
}
// Google-specific: system test.
[Theory]
[InlineData("", "FooBot", true)] // Empty robots.txt: everything allowed.
[InlineData(
"user-agent: FooBot\n" +
"disallow: /\n", "", true
)] // Empty user-agent to be matched: everything allowed.
[InlineData(
"user-agent: FooBot\n" +
"disallow: /\n",
"FooBot",
false)] // Empty url: implicitly disallowed, see method comment for GetPathParamsQuery in robots.cc.
[InlineData("", "", true)] // All params empty: same as robots.txt empty, everything allowed.
public void GoogleOnly_SystemTest(string robotsTxt, string userAgent, bool expected)
{
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, userAgent, ""));
}
// Rules are colon separated name-value pairs. The following names are
// provisioned:
// user-agent: <value>
// allow: <value>
// disallow: <value>
// See REP RFC section "Protocol Definition".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.1
//
// Google specific: webmasters sometimes miss the colon separator, but it's
// obvious what they mean by "disallow /", so we assume the colon if it's
// missing.
[Theory]
[InlineData(
"user-agent: FooBot\n" +
"disallow: /\n",
false)]
[InlineData(
"foo: FooBot\n" +
"bar: /\n",
true
)]
[InlineData(
"user-agent FooBot\n" +
"disallow /\n",
false
)]
public void ID_LineSyntax_Line(string robotsTxt, bool expected)
{
var url = "http://foo.bar/x/y";
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, "FooBot", url));
}
// A group is one or more user-agent line followed by rules, and terminated
// by a another user-agent line. Rules for same user-agents are combined
// opaquely into one group. Rules outside groups are ignored.
// See REP RFC section "Protocol Definition".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.1
[Theory]
[InlineData("FooBot", "http://foo.bar/x/b", true)]
[InlineData("FooBot", "http://foo.bar/z/d", true)]
[InlineData("FooBot", "http://foo.bar/y/c", false)]
[InlineData("BarBot", "http://foo.bar/y/c", true)]
[InlineData("BarBot", "http://foo.bar/w/a", true)]
[InlineData("BarBot", "http://foo.bar/z/d", false)]
[InlineData("BazBot", "http://foo.bar/z/d", true)]
// Lines with rules outside groups are ignored.
[InlineData("FooBot", "http://foo.bar/foo/bar/", false)]
[InlineData("BarBot", "http://foo.bar/foo/bar/", false)]
[InlineData("BazBot", "http://foo.bar/foo/bar/", false)]
public void ID_LineSyntax_Groups(string userAgent, string url, bool expected)
{
var robotstxt =
"allow: /foo/bar/\n" +
"\n" +
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /x/\n" +
"user-agent: BarBot\n" +
"disallow: /\n" +
"allow: /y/\n" +
"\n" +
"\n" +
"allow: /w/\n" +
"user-agent: BazBot\n" +
"\n" +
"user-agent: FooBot\n" +
"allow: /z/\n" +
"disallow: /\n";
Assert.Equal(expected, IsUserAgentAllowed(robotstxt, userAgent, url));
}
// Group must not be closed by rules not explicitly defined in the REP RFC.
// See REP RFC section "Protocol Definition".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.1
[Theory]
[InlineData("FooBot", false)]
[InlineData("BarBot", false)]
public void ID_LineSyntax_Groups_OtherRules(string userAgent, bool expected)
{
var robotsTxt =
"User-agent: BarBot\n" +
"Sitemap: https://foo.bar/sitemap\n" +
"User-agent: *\n" +
"Disallow: /\n";
var url = "http://foo.bar/";
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, userAgent, url));
}
[Theory]
[InlineData("FooBot", false)]
[InlineData("BarBot", false)]
public void ID_LineSyntax_Groups_OtherRules_2(string userAgent, bool expected)
{
var robotsTxt =
"User-agent: FooBot\n" +
"Invalid-Unknown-Line: unknown\n" +
"User-agent: *\n" +
"Disallow: /\n";
var url = "http://foo.bar/";
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, userAgent, url));
}
// REP lines are case insensitive. See REP RFC section "Protocol Definition".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.1
[Theory]
[InlineData("http://foo.bar/x/y", true)]
[InlineData("http://foo.bar/a/b", false)]
public void ID_REPLineNamesCaseInsensitive(string url, bool expected)
{
const string robotsTxtUpper = "USER-AGENT: FooBot\n" +
"ALLOW: /x/\n" +
"DISALLOW: /\n";
const string robotsTxtLower = "user-agent: FooBot\n" +
"allow: /x/\n" +
"disallow: /\n";
const string robotsTxtCamel = "uSeR-aGeNt: FooBot\n" +
"AlLoW: /x/\n" +
"dIsAlLoW: /\n";
Assert.Equal(expected, IsUserAgentAllowed(robotsTxtUpper, "FooBot", url));
Assert.Equal(expected, IsUserAgentAllowed(robotsTxtLower, "FooBot", url));
Assert.Equal(expected, IsUserAgentAllowed(robotsTxtCamel, "FooBot", url));
}
// A user-agent line is expected to contain only [a-zA-Z_-] characters and must
// not be empty. See REP RFC section "The user-agent line".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.1
[Theory]
[InlineData("Foobot", true)]
[InlineData("Foo_Bar", true)]
[InlineData("Foobot-Bar", true)]
[InlineData("", false)]
[InlineData("ツ", false)]
[InlineData("Foobot*", false)]
[InlineData(" Foobot ", false)]
[InlineData("Foobot/2.1", false)]
[InlineData("Foobot Bar", false)]
public void ID_VerifyValidUserAgentsToObey(string userAgent, bool expected)
{
Assert.Equal(expected, RobotsMatcher.IsValidUserAgentToObey(userAgent));
}
// User-agent line values are case insensitive. See REP RFC section "The
// user-agent line".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.1
[Fact]
public void ID_UserAgentValueCaseInsensitive()
{
const string robotstxtUpper = "User-Agent: FOO BAR\n" +
"Allow: /x/\n" +
"Disallow: /\n";
const string robotstxtLower = "User-Agent: foo bar\n" +
"Allow: /x/\n" +
"Disallow: /\n";
const string robotstxtCamel = "User-Agent: FoO bAr\n" +
"Allow: /x/\n" +
"Disallow: /\n";
const string urlAllowed = "http://foo.bar/x/y";
const string urlDisallowed = "http://foo.bar/a/b";
Assert.True(IsUserAgentAllowed(robotstxtUpper, "Foo", urlAllowed));
Assert.True(IsUserAgentAllowed(robotstxtLower, "Foo", urlAllowed));
Assert.True(IsUserAgentAllowed(robotstxtCamel, "Foo", urlAllowed));
Assert.False(IsUserAgentAllowed(robotstxtUpper, "Foo", urlDisallowed));
Assert.False(IsUserAgentAllowed(robotstxtLower, "Foo", urlDisallowed));
Assert.False(IsUserAgentAllowed(robotstxtCamel, "Foo", urlDisallowed));
Assert.True(IsUserAgentAllowed(robotstxtUpper, "foo", urlAllowed));
Assert.True(IsUserAgentAllowed(robotstxtLower, "foo", urlAllowed));
Assert.True(IsUserAgentAllowed(robotstxtCamel, "foo", urlAllowed));
Assert.False(IsUserAgentAllowed(robotstxtUpper, "foo", urlDisallowed));
Assert.False(IsUserAgentAllowed(robotstxtLower, "foo", urlDisallowed));
Assert.False(IsUserAgentAllowed(robotstxtCamel, "foo", urlDisallowed));
}
// Google specific: accept user-agent value up to the first space. Space is not
// allowed in user-agent values, but that doesn't stop webmasters from using
// them. This is more restrictive than the RFC, since in case of the bad value
// "Googlebot Images" we'd still obey the rules with "Googlebot".
// Extends REP RFC section "The user-agent line"
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.1
[Theory]
[InlineData("Foo", true)]
[InlineData("Foo Bar", false)]
public void GoogleOnly_AcceptUserAgentUpToFirstSpace(string userAgent, bool expected)
{
Assert.False(RobotsMatcher.IsValidUserAgentToObey("Foobot Bar"));
var robotsTxt =
"User-Agent: *\n" +
"Disallow: /\n" +
"User-Agent: Foo Bar\n" +
"Allow: /x/\n" +
"Disallow: /\n";
var url = "http://foo.bar/x/y";
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, userAgent, url));
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, userAgent, url));
}
// If no group matches the user-agent, crawlers must obey the first group with a
// user-agent line with a "*" value, if present. If no group satisfies either
// condition, or no groups are present at all, no rules apply.
// See REP RFC section "The user-agent line".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.1
[Fact]
public void ID_GlobalGroups_Secondary()
{
const string robotsTxtEmpty = "";
const string robotsTxtGlobal = "user-agent: *\n" +
"allow: /\n" +
"user-agent: FooBot\n" +
"disallow: /\n";
const string robotsTxtOnlySpecific = "user-agent: FooBot\n" +
"allow: /\n" +
"user-agent: BarBot\n" +
"disallow: /\n" +
"user-agent: BazBot\n" +
"disallow: /\n";
var url = "http://foo.bar/x/y";
Assert.True(IsUserAgentAllowed(robotsTxtEmpty, "FooBot", url));
Assert.False(IsUserAgentAllowed(robotsTxtGlobal, "FooBot", url));
Assert.True(IsUserAgentAllowed(robotsTxtGlobal, "BarBot", url));
Assert.True(IsUserAgentAllowed(robotsTxtOnlySpecific, "QuxBot", url));
}
// Matching rules against URIs is case sensitive.
// See REP RFC section "The Allow and Disallow lines".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.2
[Fact]
public void ID_AllowDisallow_Value_CaseSensitive()
{
const string robotsTxtLowercaseUrl = "user-agent: FooBot\n" +
"disallow: /x/\n";
const string robotsTxtUppercaseUrl = "user-agent: FooBot\n" +
"disallow: /X/\n";
var url = "http://foo.bar/x/y";
Assert.False(IsUserAgentAllowed(robotsTxtLowercaseUrl, "FooBot", url));
Assert.True(IsUserAgentAllowed(robotsTxtUppercaseUrl, "FooBot", url));
}
// The most specific match found MUST be used. The most specific match is the
// match that has the most octets. In case of multiple rules with the same
// length, the least strict rule must be used.
// See REP RFC section "The Allow and Disallow lines".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.2
[Fact]
public void ID_LongestMatch()
{
var url = "http://foo.bar/x/page.html";
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /x/page.html\n" +
"allow: /x/\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", url));
robotsTxt =
"user-agent: FooBot\n" +
"allow: /x/page.html\n" +
"disallow: /x/\n";
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/x/"));
robotsTxt =
"user-agent: FooBot\n" +
"disallow: \n" +
"allow: \n";
// In case of equivalent disallow and allow patterns for the same
// user-agent, allow is used.
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /\n";
// In case of equivalent disallow and allow patterns for the same
// user-agent, allow is used.
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
const string urlA = "http://foo.bar/x";
const string urlB = "http://foo.bar/x/";
robotsTxt =
"user-agent: FooBot\n" +
"disallow: /x\n" +
"allow: /x/\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", urlA));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", urlB));
robotsTxt =
"user-agent: FooBot\n" +
"disallow: /x/page.html\n" +
"allow: /x/page.html\n";
// In case of equivalent disallow and allow patterns for the same
// user-agent, allow is used.
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
robotsTxt =
"user-agent: FooBot\n" +
"allow: /page\n" +
"disallow: /*.html\n";
// Longest match wins.
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/page.html"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/page"));
robotsTxt =
"user-agent: FooBot\n" +
"allow: /x/page.\n" +
"disallow: /*.html\n";
// Longest match wins.
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/x/y.html"));
robotsTxt =
"User-agent: *\n" +
"Disallow: /x/\n" +
"User-agent: FooBot\n" +
"Disallow: /y/\n";
// Most specific group for FooBot allows implicitly /x/page.
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/x/page"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/y/page"));
}
// Octets in the URI and robots.txt paths outside the range of the US-ASCII
// coded character set, and those in the reserved range defined by RFC3986,
// MUST be percent-encoded as defined by RFC3986 prior to comparison.
// See REP RFC section "The Allow and Disallow lines".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.2
//
// NOTE: It's up to the caller to percent encode a URL before passing it to the
// parser. Percent encoding URIs in the rules is unnecessary.
[Theory]
[InlineData("User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar?qux=taz&baz=http://foo.bar?tar&par\n",
"http://foo.bar/foo/bar?qux=taz&baz=http://foo.bar?tar&par",
true
)]
[InlineData(
"User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar/ツ\n",
"http://foo.bar/foo/bar/%E3%83%84",
true
)] // 3 byte character: /foo/bar/ツ -> /foo/bar/%E3%83%84
[InlineData(
"User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar/ツ\n",
"http://foo.bar/foo/bar/ツ",
false
)] // The parser encodes the 3-byte character, but the URL is not %-encoded.
[InlineData(
"User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar/%E3%83%84\n",
"http://foo.bar/foo/bar/%E3%83%84",
true
)] // Percent encoded 3 byte character: /foo/bar/%E3%83%84 -> /foo/bar/%E3%83%84
[InlineData(
"User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar/%E3%83%84\n",
"http://foo.bar/foo/bar/ツ",
false
)] // Percent encoded 3 byte character: /foo/bar/%E3%83%84 -> /foo/bar/%E3%83%84
[InlineData(
"User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar/%62%61%7A\n",
"http://foo.bar/foo/bar/baz",
false
)] // Percent encoded unreserved US-ASCII: /foo/bar/%62%61%7A -> NULL
// This is illegal according to RFC3986 and while it may work here due to
// simple string matching, it should not be relied on.
[InlineData(
"User-agent: FooBot\n" +
"Disallow: /\n" +
"Allow: /foo/bar/%62%61%7A\n",
"http://foo.bar/foo/bar/%62%61%7A",
true
)]
public void ID_Encoding(string robotsTxt, string url, bool expected)
{
Assert.Equal(expected, IsUserAgentAllowed(robotsTxt, "FooBot", url));
}
// The REP RFC defines the following characters that have special meaning in
// robots.txt:
// # - inline comment.
// $ - end of pattern.
// * - any number of characters.
// See REP RFC section "Special Characters".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.3
[Fact]
public void ID_SpecialCharacters()
{
var robotsTxt =
"User-agent: FooBot\n" +
"Disallow: /foo/bar/quz\n" +
"Allow: /foo/*/qux\n";
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bar/quz"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/quz"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo//quz"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bax/quz"));
robotsTxt =
"User-agent: FooBot\n" +
"Disallow: /foo/bar$\n" +
"Allow: /foo/bar/qux\n";
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bar"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bar/qux"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bar/"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bar/baz"));
robotsTxt =
"User-agent: FooBot\n" +
"# Disallow: /\n" +
"Disallow: /foo/quz#qux\n" +
"Allow: /\n";
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/bar"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/foo/quz"));
}
// Google-specific: "index.html" (and only that) at the end of a pattern is
// equivalent to "/".
[Theory]
[InlineData("http://foo.com/allowed-slash/",
true)] // If index.html is allowed, we interpret this as / being allowed too.
[InlineData("http://foo.com/allowed-slash/index.htm", false)] // Does not exactly match.
[InlineData("http://foo.com/allowed-slash/index.html", true)] // Exact match.
[InlineData("http://foo.com/anyother-url", false)]
public void GoogleOnly_IndexHTMLisDirectory(string url, bool expected)
{
var robotstxt =
"User-Agent: *\n" +
"Allow: /allowed-slash/index.html\n" +
"Disallow: /\n";
var isUserAgentAllowed = IsUserAgentAllowed(robotstxt, "foobot", url);
Assert.Equal(expected, isUserAgentAllowed);
}
[Fact]
public void GoogleOnly_LineTooLong_1()
{
var eolLen = "\n".Length;
const int maxLineLen = 2083 * 8 + 1;
const string disallow = "disallow: ";
var robotsTxt = new StringBuilder("user-agent: FooBot\n");
var longline = new StringBuilder("/x/");
var maxLength = maxLineLen - longline.Length - disallow.Length + eolLen;
while (longline.Length < maxLength)
{
longline.Append("a");
}
robotsTxt.Append(disallow).Append(longline).Append("/qux\n");
// Matches nothing, so URL is allowed.
Assert.True(IsUserAgentAllowed(robotsTxt.ToString(), "FooBot", "http://foo.bar/fux"));
// Matches cut off disallow rule.
var actual = IsUserAgentAllowed(robotsTxt.ToString(), "FooBot", "http://foo.bar" + longline + "/fux");
Assert.False(actual);
}
[Fact]
public void GoogleOnly_LineTooLong_2()
{
var eolLen = "\n".Length;
const int maxLineLen = 2083 * 8 + 1;
var allow = "allow: ";
var robotsTxt = new StringBuilder(
"user-agent: FooBot\n" +
"disallow: /\n");
var longlineA = new StringBuilder("/a/");
var longlineB = new StringBuilder("/b/");
var maxLength = maxLineLen - longlineA.Length - allow.Length + eolLen;
while (longlineA.Length < maxLength)
{
longlineA.Append("a");
longlineB.Append("a");
}
robotsTxt.Append(allow).Append(longlineA).Append("/qux\n");
robotsTxt.Append(allow).Append(longlineB).Append("/qux\n");
// URL matches the disallow rule.
Assert.False(IsUserAgentAllowed(robotsTxt.ToString(), "FooBot", "http://foo.bar/"));
// Matches the allow rule exactly.
Assert.True(
IsUserAgentAllowed(robotsTxt.ToString(), "FooBot",
"http://foo.bar" + longlineA + "/qux"));
// Matches cut off allow rule.
var actual = IsUserAgentAllowed(robotsTxt.ToString(), "FooBot",
"http://foo.bar" + longlineB + "/fux");
Assert.True(actual);
}
// Test documentation from
// https://developers.google.com/search/reference/robots_txt
// Section "URL matching based on path values".
[Fact]
public void GoogleOnly_DocumentationChecks_1()
{
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /fish\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/bar"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish.html"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fish/salmon.html"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fishheads"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fishheads/yummy.html"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fish.html?id=anything"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/Fish.asp"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/catfish"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/?id=fish"));
}
// "/fish*" equals "/fish"
[Fact]
public void GoogleOnly_DocumentationChecks_2()
{
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /fish*\n";
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/bar"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish.html"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fish/salmon.html"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fishheads"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fishheads/yummy.html"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fish.html?id=anything"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/Fish.bar"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/catfish"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/?id=fish"));
}
// "/fish/" does not equal "/fish"
[Fact]
public void GoogleOnly_DocumentationChecks_3()
{
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /fish/\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/bar"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish/"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish/salmon"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish/?salmon"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fish/salmon.html"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fish/?id=anything"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish.html"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/Fish/Salmon.html"));
}
// "/*.php"
[Fact]
public void GoogleOnly_DocumentationChecks_4()
{
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /*.php\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/bar"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/filename.php"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/folder/filename.php"));
Assert.True(IsUserAgentAllowed(
robotsTxt, "FooBot", "http://foo.bar/folder/filename.php?parameters"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar//folder/any.php.file.html"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/filename.php/"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/index?f=filename.php/"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/php/"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/index?php"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/windows.PHP"));
}
// "/*.php$"
[Fact]
public void GoogleOnly_DocumentationChecks_5()
{
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /*.php$\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/bar"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/filename.php"));
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/folder/filename.php"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/filename.php?parameters"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/filename.php/"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/filename.php5"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/php/"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/filename?php"));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/aaaphpaaa"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar//windows.PHP"));
}
// "/fish*.php"
[Fact]
public void GoogleOnly_DocumentationChecks_6()
{
var robotsTxt =
"user-agent: FooBot\n" +
"disallow: /\n" +
"allow: /fish*.php\n";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/bar"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/fish.php"));
Assert.True(
IsUserAgentAllowed(robotsTxt, "FooBot",
"http://foo.bar/fishheads/catfish.php?parameters"));
Assert.False(
IsUserAgentAllowed(robotsTxt, "FooBot", "http://foo.bar/Fish.PHP"));
}
// Section "Order of precedence for group-member records".
[Fact]
public void GoogleOnly_DocumentationChecks_7()
{
var robotsTxt =
"user-agent: FooBot\n" +
"allow: /p\n" +
"disallow: /\n";
var url = "http://example.com/page";
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
robotsTxt =
"user-agent: FooBot\n" +
"allow: /folder\n" +
"disallow: /folder\n";
url = "http://example.com/folder/page";
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
robotsTxt =
"user-agent: FooBot\n" +
"allow: /page\n" +
"disallow: /*.htm\n";
url = "http://example.com/page.htm";
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", url));
robotsTxt =
"user-agent: FooBot\n" +
"allow: /$\n" +
"disallow: /\n";
url = "http://example.com/";
const string urlPage = "http://example.com/page.html";
Assert.True(IsUserAgentAllowed(robotsTxt, "FooBot", url));
Assert.False(IsUserAgentAllowed(robotsTxt, "FooBot", urlPage));
}
class RobotsStatsReporter : IRobotsParseHandler
{
public int LastLineSeen { get; private set; }
public int ValidDirectives { get; private set; }
public int UnknownDirectives { get; private set; }
public string Sitemap { get; private set; } = "";
public void HandleRobotsStart()
{
LastLineSeen = 0;
ValidDirectives = 0;
UnknownDirectives = 0;
Sitemap = "";
}
public void HandleRobotsEnd()
{
}
public void HandleUserAgent(int lineNum, ReadOnlySpan<byte> value)
{
Digest(lineNum);
}
public void HandleAllow(int lineNum, ReadOnlySpan<byte> value)
{
Digest(lineNum);
}
public void HandleDisallow(int lineNum, ReadOnlySpan<byte> value)
{
Digest(lineNum);
}
public void HandleSitemap(int lineNum, ReadOnlySpan<byte> value)
{
Digest(lineNum);
Sitemap = Encoding.UTF8.GetString(value.ToArray());
}
public void HandleUnknownAction(int lineNum, ReadOnlySpan<byte> action, ReadOnlySpan<byte> value)
{
LastLineSeen = lineNum;
UnknownDirectives++;
}
private void Digest(int lineNum)
{
Debug.Assert(lineNum > LastLineSeen);
LastLineSeen = lineNum;
ValidDirectives++;
}
}
// Different kinds of line endings are all supported: %x0D / %x0A / %x0D.0A
[Theory]
[InlineData(
"User-Agent: foo\n" +
"Allow: /some/path\n" +
"User-Agent: bar\n" +
"\n" +
"\n" +
"Disallow: /\n",
4,
6
)]
[InlineData(
"User-Agent: foo\r\n" +
"Allow: /some/path\r\n" +
"User-Agent: bar\r\n" +
"\r\n" +
"\r\n" +
"Disallow: /\r\n",
4,
6
)]
[InlineData(
"User-Agent: foo\r" +
"Allow: /some/path\r" +
"User-Agent: bar\r" +
"\r" +
"\r" +
"Disallow: /\r",
4,
6
)]
[InlineData(
"User-Agent: foo\n" +
"Allow: /some/path\n" +
"User-Agent: bar\n" +
"\n" +
"\n" +
"Disallow: /",
4,
6
)]
[InlineData(
"User-Agent: foo\n" +
"Allow: /some/path\r\n" +
"User-Agent: bar\n" +
"\t\n" +
"\n" +
"Disallow: /",
4,
6
)]
public void ID_LinesNumbersAreCountedCorrectly(string file, int expectedValidDirective,
int expectedLastLineSeen)
{
var report = new RobotsStatsReporter();
RobotsMatcher.ParseRobotsTxt(Encoding.UTF8.GetBytes(file), report);
Assert.Equal(expectedValidDirective, report.ValidDirectives);
Assert.Equal(expectedLastLineSeen, report.LastLineSeen);
}
// BOM characters are unparseable and thus skipped. The rules following the line
// are used.
// We allow as well partial ByteOrderMarks.
// If the BOM is not the right sequence, the first line looks like garbage
// that is skipped (we essentially see "\x11\xBFUser-Agent").
// Some other messed up file: BOMs only valid in the beginning of the file.
[Theory]
[InlineData(
3,
2,
0)]
[InlineData(
2,
2,
0)]
[InlineData(
1,
2,
0)]
public void ID_UTF8ByteOrderMarkIsSkipped(int bomLength, int expectedValidDirective,
int expectedUnkmownDirectives)
{
byte[] bom = { 0xEF, 0xBB, 0xBF };
var rest = Encoding.UTF8.GetBytes("User-Agent: foo\n" +
"Allow: /AnyValue\n");
var ms = new MemoryStream();
ms.Write(bom, 0, bomLength);
ms.Write(rest, 0, rest.Length);
var report = new RobotsStatsReporter();
RobotsMatcher.ParseRobotsTxt(ms.GetBuffer(), report);
Assert.Equal(expectedValidDirective, report.ValidDirectives);
Assert.Equal(expectedUnkmownDirectives, report.UnknownDirectives);
}
[Fact]
public void ID_Utf8FileBrokenBOM()
{
byte[] brokenBom = { 0xEF, 0x11, 0xBF };
var rest = Encoding.UTF8.GetBytes("User-Agent: foo\n" +
"Allow: /AnyValue\n");
var ms = new MemoryStream();
ms.Write(brokenBom, 0, brokenBom.Length);
ms.Write(rest, 0, rest.Length);
var report = new RobotsStatsReporter();
RobotsMatcher.ParseRobotsTxt(ms.GetBuffer(), report);
Assert.Equal(1, report.ValidDirectives);
Assert.Equal(1, report.UnknownDirectives);
}
[Fact]
public void ID_Utf8BOMSomewhereInMiddleOfFile()
{
var part1 = Encoding.UTF8.GetBytes("User-Agent: foo\n");
byte[] bom = { 0xEF, 0xBB, 0xBF };
var part2 = Encoding.UTF8.GetBytes("Allow: /AnyValue\n");
var ms = new MemoryStream();
ms.Write(part1, 0, part1.Length);
ms.Write(bom, 0, bom.Length);
ms.Write(part2, 0, part2.Length);
var report = new RobotsStatsReporter();
RobotsMatcher.ParseRobotsTxt(ms.GetBuffer(), report);
Assert.Equal(1, report.ValidDirectives);
Assert.Equal(1, report.UnknownDirectives);
}
// Google specific: the RFC allows any line that crawlers might need, such as
// sitemaps, which Google supports.
// See REP RFC section "Other records".
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.2.4
[Theory]
[InlineData(
"User-Agent: foo\n" +
"Allow: /some/path\n" +
"User-Agent: bar\n" +
"\n" +
"\n" +
"Sitemap: http://foo.bar/sitemap.xml"
)]
[InlineData(
"Sitemap: http://foo.bar/sitemap.xml\n" +
"User-Agent: foo\n" +
"Allow: /some/path\n" +
"User-Agent: bar\n" +
"\n" +
"\n"
)]
public void ID_NonStandardLineExample_Sitemap(string robotstxt)
{
var report = new RobotsStatsReporter();
RobotsMatcher.ParseRobotsTxt(Encoding.UTF8.GetBytes(robotstxt), report);
Assert.Equal("http://foo.bar/sitemap.xml", report.Sitemap);
}
[Theory]
[InlineData("", "/")]
[InlineData("http://www.example.com", "/")]
[InlineData("http://www.example.com/", "/")]
[InlineData("http://www.example.com/a", "/a")]
[InlineData("http://www.example.com/a/", "/a/")]
[InlineData("http://www.example.com/a/b?c=http://d.e/", "/a/b?c=http://d.e/")]
[InlineData("http://www.example.com/a/b?c=d&e=f#fragment", "/a/b?c=d&e=f")]
[InlineData("example.com", "/")]
[InlineData("example.com/", "/")]
[InlineData("example.com/a", "/a")]
[InlineData("example.com/a/", "/a/")]
[InlineData("example.com/a/b?c=d&e=f#fragment", "/a/b?c=d&e=f")]
[InlineData("a", "/")]
[InlineData("a/", "/")]
[InlineData("/a", "/a")]
[InlineData("a/b", "/b")]