-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverifier_test.go
131 lines (117 loc) · 4.82 KB
/
verifier_test.go
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
package goodbot_test
import (
"context"
"testing"
"github.com/golang/mock/gomock"
goodbot "github.com/rynmccrmck/good-bot"
"github.com/rynmccrmck/good-bot/mocks"
)
func TestIsUserAgentMatch(t *testing.T) {
userAgent := "TestBot/1.0"
uaPattern := "^TestBot"
if !goodbot.IsUserAgentMatch(userAgent, uaPattern) {
t.Errorf("User agent %s should match pattern %s", userAgent, uaPattern)
}
// Test a negative case
userAgent = "AnotherBot/1.0"
if goodbot.IsUserAgentMatch(userAgent, uaPattern) {
t.Errorf("User agent %s should not match pattern %s", userAgent, uaPattern)
}
}
func TestCheckBotIdentity(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockNetworkUtils := mocks.NewMockNetworkUtils(mockCtrl)
botService := goodbot.NewBotService(mockNetworkUtils)
mockNetworkUtils.EXPECT().GetHosts("66.249.66.1").Return([]string{"crawl-66-249-66-1.googlebot.com"}).AnyTimes()
mockNetworkUtils.EXPECT().DoesHostnameResolveBackToIP("66.249.66.1", "crawl-66-249-66-1.googlebot.com").Return(true).AnyTimes()
mockNetworkUtils.EXPECT().GetHosts("127.0.0.1").Return([]string{"localhost"}).AnyTimes()
mockNetworkUtils.EXPECT().DoesHostnameResolveBackToIP("127.0.0.1", "localhost").Return(true).AnyTimes()
mockNetworkUtils.EXPECT().GetASN("66.249.66.2").Return("32934", nil).AnyTimes()
mockNetworkUtils.EXPECT().GetASN("66.249.66.3").Return("12345", nil).AnyTimes()
mockNetworkUtils.EXPECT().GetHosts("66.249.66.4").Return([]string{"crawl-66-249-66-1.googlebot.com"}).AnyTimes()
mockNetworkUtils.EXPECT().DoesHostnameResolveBackToIP("66.249.66.4", "crawl-66-249-66-1.googlebot.com").Return(false).AnyTimes()
tests := []struct {
name string
userAgent string
ipAddress string
expectedBotStatus goodbot.BotStatus
expectedBotName string
}{
{
name: "Googlebot",
userAgent: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
ipAddress: "66.249.66.1",
expectedBotStatus: goodbot.BotStatusFriendly,
expectedBotName: "Googlebot",
},
{
name: "Googlebot Wrong IP",
userAgent: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
ipAddress: "127.0.0.1",
expectedBotStatus: goodbot.BotStatusPotentialImposter,
expectedBotName: "Googlebot",
},
{
name: "Googlebot Spoofed reverse DNS",
userAgent: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
ipAddress: "66.249.66.4",
expectedBotStatus: goodbot.BotStatusPotentialImposter,
expectedBotName: "Googlebot",
},
{
name: "Not A known Useragent",
userAgent: "Mozilla/5.0 (compatible; nothing-weve-seen-before)",
ipAddress: "127.0.0.1",
expectedBotStatus: goodbot.BotStatusUnknown,
expectedBotName: "",
},
{
name: "Facebook external hit",
userAgent: "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
ipAddress: "66.249.66.2",
expectedBotStatus: goodbot.BotStatusFriendly,
expectedBotName: "Facebook external hit",
},
{
name: "FacebookBot UA Wrong ASN",
userAgent: "facebookexternalhit/2.0",
ipAddress: "66.249.66.3",
expectedBotStatus: goodbot.BotStatusPotentialImposter,
expectedBotName: "Facebook external hit",
},
{
name: "Unknown UA and IP",
userAgent: "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N)",
ipAddress: "192.168.1.1",
expectedBotStatus: goodbot.BotStatusUnknown,
expectedBotName: "",
},
{
name: "Grapeshot CIDR match",
userAgent: "Mozilla/5.0 (compatible; GrapeshotCrawler/2.0; +http://www.grapeshot.co.uk/crawler.php)",
ipAddress: "132.145.9.5",
expectedBotStatus: goodbot.BotStatusFriendly,
expectedBotName: "Grapeshot",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
botResult, _ := botService.CheckBotStatus(ctx, tc.userAgent, tc.ipAddress)
if botResult.BotStatus != tc.expectedBotStatus || botResult.BotName != tc.expectedBotName {
t.Errorf("CheckBotIdentity(%q, %q) = (%v, %q), want (%v, %q)",
tc.userAgent, tc.ipAddress, botResult.BotStatus, botResult.BotName, tc.expectedBotStatus, tc.expectedBotName)
}
})
}
}
func TestAdhocTest(t *testing.T) {
result, _ := goodbot.CheckBotStatus("facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
"173.252.127.15")
// fmt.Printf("Is Good Bot: %v, Bot Name: %s\n", result.BotName, result.BotStatus)
// Output: Is Good Bot: true, Bot Name: Facebook external hit
if result.BotStatus != goodbot.BotStatusFriendly || result.BotName != "Facebook external hit" {
t.Errorf("Problem with Facebook external hit %v", result.BotName)
}
}