-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain_test.go
More file actions
126 lines (116 loc) · 2.75 KB
/
Copy pathmain_test.go
File metadata and controls
126 lines (116 loc) · 2.75 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
package main
import (
"net"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestWithTokenOrIP(t *testing.T) {
allowedCIDRs, err := parseAllowedIPs("192.168.1.0/24,10.0.0.1")
if err != nil {
t.Fatalf("unexpected error in test setup: %v", err)
}
validToken := "secret-token"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
protected := withTokenOrIP(validToken, allowedCIDRs, handler)
tests := []struct {
name string
remoteAddr string
authToken string
wantCode int
}{
{"Valid IP match subnet", "192.168.1.42:1234", "", http.StatusOK},
{"Valid IP exact match", "10.0.0.1:9999", "", http.StatusOK},
{"Valid token", "8.8.8.8:1111", "secret-token", http.StatusOK},
{"Invalid IP and token", "8.8.8.8:1111", "wrong-token", http.StatusForbidden},
{"No auth at all", "8.8.8.8:1111", "", http.StatusForbidden},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/metrics", nil)
req.RemoteAddr = tc.remoteAddr
if tc.authToken != "" {
req.Header.Set("X-Auth-Token", tc.authToken)
}
rec := httptest.NewRecorder()
protected.ServeHTTP(rec, req)
if rec.Code != tc.wantCode {
t.Errorf("Expected %d, got %d", tc.wantCode, rec.Code)
}
})
}
}
func TestParseAllowedIPs(t *testing.T) {
tests := []struct {
name string
input string
expected []*net.IPNet
wantErr bool
}{
{
name: "empty string",
input: "",
expected: nil,
wantErr: false,
},
{
name: "valid single IP",
input: "192.168.1.10",
expected: []*net.IPNet{
{
IP: net.ParseIP("192.168.1.10"),
Mask: net.CIDRMask(32, 32),
},
},
wantErr: false,
},
{
name: "valid CIDR",
input: "10.0.0.0/8",
expected: func() []*net.IPNet {
_, cidr, _ := net.ParseCIDR("10.0.0.0/8")
return []*net.IPNet{cidr}
}(),
wantErr: false,
},
{
name: "valid mix IP and CIDR",
input: "192.168.1.1,10.0.0.0/24",
expected: func() []*net.IPNet {
_, cidr, _ := net.ParseCIDR("10.0.0.0/24")
return []*net.IPNet{
{
IP: net.ParseIP("192.168.1.1"),
Mask: net.CIDRMask(32, 32),
},
cidr,
}
}(),
wantErr: false,
},
{
name: "invalid entry",
input: "not-an-ip",
wantErr: true,
},
{
name: "mixed valid and invalid",
input: "192.168.1.1,not-an-ip",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseAllowedIPs(tt.input)
if (err != nil) != tt.wantErr {
t.Fatalf("expected error = %v, got = %v", tt.wantErr, err)
}
if !tt.wantErr && !reflect.DeepEqual(got, tt.expected) {
t.Errorf("expected %+v, got %+v", tt.expected, got)
}
})
}
}