-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscanner_test.go
More file actions
191 lines (168 loc) · 4.28 KB
/
Copy pathscanner_test.go
File metadata and controls
191 lines (168 loc) · 4.28 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
package main
import (
"net"
"testing"
"time"
)
func TestIsPrivateIP(t *testing.T) {
tests := []struct {
ip string
expected bool
desc string
}{
// Private ranges - should be detected
{"10.0.0.1", true, "10.x.x.x (RFC 1918)"},
{"10.255.255.255", true, "10.x.x.x upper bound"},
{"172.16.0.1", true, "172.16.x.x (RFC 1918)"},
{"172.31.255.255", true, "172.31.x.x upper bound"},
{"192.168.0.1", true, "192.168.x.x (RFC 1918)"},
{"192.168.255.255", true, "192.168.x.x upper bound"},
{"127.0.0.1", true, "loopback"},
{"169.254.1.1", true, "link-local"},
{"100.64.0.1", true, "CGNAT"},
{"100.127.255.255", true, "CGNAT upper bound"},
{"0.0.0.0", true, "zero address"},
// Public IPs - should not be detected
{"8.8.8.8", false, "Google DNS"},
{"1.1.1.1", false, "Cloudflare DNS"},
{"185.8.174.140", false, "Iranian DNS"},
{"172.15.255.255", false, "just below 172.16.0.0"},
{"172.32.0.0", false, "just above 172.31.255.255"},
{"100.63.255.255", false, "just below CGNAT"},
{"100.128.0.0", false, "just above CGNAT"},
}
for _, tt := range tests {
ip := net.ParseIP(tt.ip)
result := isPrivateIP(ip)
if result != tt.expected {
t.Errorf("isPrivateIP(%s) = %v, expected %v (%s)",
tt.ip, result, tt.expected, tt.desc)
}
}
}
func TestIsPrivateIPNil(t *testing.T) {
if isPrivateIP(nil) {
t.Error("isPrivateIP(nil) should return false")
}
}
func TestRandomSubdomain(t *testing.T) {
s1 := randomSubdomain()
s2 := randomSubdomain()
// Should be hex encoded (16 chars for 8 bytes)
if len(s1) != 16 {
t.Errorf("randomSubdomain length = %d, expected 16", len(s1))
}
// Should be different each time
if s1 == s2 {
t.Error("randomSubdomain should generate unique values")
}
}
func TestRandomBenchmarkSubdomain(t *testing.T) {
s1 := randomBenchmarkSubdomain()
s2 := randomBenchmarkSubdomain()
// Base32 encoded 32 bytes = 52 chars
if len(s1) != 52 {
t.Errorf("randomBenchmarkSubdomain length = %d, expected 52", len(s1))
}
// Should be different each time
if s1 == s2 {
t.Error("randomBenchmarkSubdomain should generate unique values")
}
}
func TestBenchmarkResultSuccessRate(t *testing.T) {
tests := []struct {
queries int
successful int
expected float64
}{
{20, 20, 100.0},
{20, 10, 50.0},
{20, 0, 0.0},
{0, 0, 0.0},
}
for _, tt := range tests {
r := &BenchmarkResult{
Queries: tt.queries,
Successful: tt.successful,
}
if r.SuccessRate() != tt.expected {
t.Errorf("SuccessRate(%d/%d) = %.1f, expected %.1f",
tt.successful, tt.queries, r.SuccessRate(), tt.expected)
}
}
}
func TestBenchmarkResultQPS(t *testing.T) {
r := &BenchmarkResult{
Successful: 10,
Duration: time.Second,
}
if r.QPS() != 10.0 {
t.Errorf("QPS = %.1f, expected 10.0", r.QPS())
}
// Zero duration edge case
r2 := &BenchmarkResult{Duration: 0}
if r2.QPS() != 0.0 {
t.Error("QPS with zero duration should be 0")
}
}
func TestBenchmarkResultP50(t *testing.T) {
r := &BenchmarkResult{
Latencies: []time.Duration{
10 * time.Millisecond,
20 * time.Millisecond,
30 * time.Millisecond,
40 * time.Millisecond,
50 * time.Millisecond,
},
}
p50 := r.P50()
if p50 != 30*time.Millisecond {
t.Errorf("P50 = %v, expected 30ms", p50)
}
// Empty latencies
r2 := &BenchmarkResult{}
if r2.P50() != 0 {
t.Error("P50 with no latencies should be 0")
}
}
func TestBenchmarkResultPassed(t *testing.T) {
tests := []struct {
queries int
successful int
expected bool
}{
{20, 14, true}, // 70% exactly
{20, 15, true}, // 75%
{20, 13, false}, // 65%
{20, 0, false}, // 0%
}
for _, tt := range tests {
r := &BenchmarkResult{
Queries: tt.queries,
Successful: tt.successful,
}
if r.Passed() != tt.expected {
t.Errorf("Passed(%d/%d = %.0f%%) = %v, expected %v",
tt.successful, tt.queries, r.SuccessRate(), r.Passed(), tt.expected)
}
}
}
func TestProgressStats(t *testing.T) {
p := NewProgress(100, true)
p.Increment()
p.Increment()
p.Success()
stats := p.Stats()
if stats.Processed != 2 {
t.Errorf("Processed = %d, expected 2", stats.Processed)
}
if stats.Success != 1 {
t.Errorf("Success = %d, expected 1", stats.Success)
}
if stats.Total != 100 {
t.Errorf("Total = %d, expected 100", stats.Total)
}
if stats.Elapsed < 0 {
t.Error("Elapsed should not be negative")
}
}