-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransport_test.go
More file actions
278 lines (257 loc) · 6.41 KB
/
transport_test.go
File metadata and controls
278 lines (257 loc) · 6.41 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
// Copyright 2026 AxonOps Limited.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audit_test
import (
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"github.com/axonops/audit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHintsFromContext_NilWithoutMiddleware(t *testing.T) {
h := audit.HintsFromContext(context.Background())
assert.Nil(t, h)
}
func TestClientIP(t *testing.T) {
tests := []struct {
name string
xff string
xri string
remote string
expected string
}{
{
name: "rightmost XFF",
xff: "1.2.3.4, 10.0.0.1, 192.168.1.1",
expected: "192.168.1.1",
},
{
name: "single XFF",
xff: "10.0.0.1",
expected: "10.0.0.1",
},
{
name: "XFF with whitespace",
xff: " 1.2.3.4 , 10.0.0.1 , 192.168.1.1 ",
expected: "192.168.1.1",
},
{
name: "XFF with invalid IP falls to RemoteAddr",
xff: "not-an-ip",
remote: "10.0.0.99:80",
expected: "10.0.0.99",
},
{
name: "X-Real-IP fallback",
xri: "10.0.0.5",
expected: "10.0.0.5",
},
{
name: "X-Real-IP invalid falls to RemoteAddr",
xri: "not-an-ip",
remote: "10.0.0.99:80",
expected: "10.0.0.99",
},
{
name: "RemoteAddr with port",
remote: "192.168.1.100:54321",
expected: "192.168.1.100",
},
{
name: "RemoteAddr IPv6",
remote: "[::1]:8080",
expected: "::1",
},
{
name: "empty everything",
expected: "",
},
{
name: "XFF takes precedence over X-Real-IP",
xff: "10.0.0.1",
xri: "10.0.0.2",
expected: "10.0.0.1",
},
{
name: "X-Real-IP takes precedence over RemoteAddr",
xri: "10.0.0.2",
remote: "10.0.0.3:80",
expected: "10.0.0.2",
},
{
name: "RemoteAddr without port",
remote: "10.0.0.1",
expected: "10.0.0.1",
},
{
name: "XFF IPv6",
xff: "::1",
expected: "::1",
},
{
name: "XFF trailing comma falls through",
xff: "10.0.0.1,",
remote: "10.0.0.99:80",
expected: "10.0.0.99",
},
{
name: "XFF bare comma falls through",
xff: ",",
remote: "10.0.0.99:80",
expected: "10.0.0.99",
},
{
name: "RemoteAddr unix socket path returned as-is",
remote: "/var/run/app.sock",
expected: "/var/run/app.sock",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/test", http.NoBody)
if tt.xff != "" {
r.Header.Set("X-Forwarded-For", tt.xff)
}
if tt.xri != "" {
r.Header.Set("X-Real-Ip", tt.xri)
}
r.RemoteAddr = tt.remote
got := audit.ClientIP(r)
assert.Equal(t, tt.expected, got)
})
}
}
func TestTransportSecurity(t *testing.T) {
tests := []struct {
name string
tls *tls.ConnectionState
expected string
}{
{
name: "nil TLS",
tls: nil,
expected: "none",
},
{
name: "TLS no peer certs",
tls: &tls.ConnectionState{},
expected: "tls",
},
{
name: "TLS with peer certs (mTLS)",
tls: &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{{}},
},
expected: "mtls",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/test", http.NoBody)
r.TLS = tt.tls
got := audit.TransportSecurity(r)
assert.Equal(t, tt.expected, got)
})
}
}
// uuidV4Pattern matches a v4 UUID: 8-4-4-4-12 hex digits with version 4
// and variant bits [89ab].
var uuidV4Pattern = regexp.MustCompile(
`^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,
)
func TestNewRequestID_Format(t *testing.T) {
id := audit.NewRequestID()
assert.Regexp(t, uuidV4Pattern, id)
}
func TestNewRequestID_Unique(t *testing.T) {
seen := make(map[string]struct{}, 100)
for range 100 {
id := audit.NewRequestID()
require.NotContains(t, seen, id, "duplicate request ID: %s", id)
seen[id] = struct{}{}
}
assert.Len(t, seen, 100)
}
func TestValidRequestID(t *testing.T) {
tests := []struct {
name string
id string
valid bool
}{
{"valid UUID", "550e8400-e29b-41d4-a716-446655440000", true},
{"valid short", "req-42", true},
{"empty", "", false},
{"too long", strings.Repeat("a", 129), false},
{"max length", strings.Repeat("a", 128), true},
{"contains newline", "req-id\n", false},
{"contains carriage return", "req-id\r", false},
{"contains null", "req-id\x00", false},
{"contains tab", "req-id\t", false},
{"contains DEL", "req-id\x7f", false},
{"non-ASCII unicode rejected", "req-\u00e9", false},
{"RTL override rejected", "req-\u202e", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.valid, audit.ValidRequestID(tt.id))
})
}
}
func TestTruncateString(t *testing.T) {
tests := []struct {
name string
input string
expect string
maxLen int
}{
{"under limit", "hello", "hello", 10},
{"at limit", "hello", "hello", 5},
{"over limit", "hello world", "hello", 5},
{"empty", "", "", 10},
{"multibyte not split", "caf\u00e9!", "caf\u00e9", 5},
{"multibyte split backed up", "caf\u00e9!", "caf", 4},
{"exact boundary 512", strings.Repeat("a", 512), strings.Repeat("a", 512), 512},
{"one over boundary 513", strings.Repeat("a", 513), strings.Repeat("a", 512), 512},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := audit.TruncateString(tt.input, tt.maxLen)
assert.Equal(t, tt.expect, got)
})
}
}
// --- Benchmarks ---
func BenchmarkNewRequestID(b *testing.B) {
for b.Loop() {
audit.NewRequestID()
}
}
func BenchmarkClientIP(b *testing.B) {
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
r.Header.Set("X-Forwarded-For", "1.2.3.4, 10.0.0.1, 192.168.1.1")
for b.Loop() {
audit.ClientIP(r)
}
}
func BenchmarkValidRequestID(b *testing.B) {
for b.Loop() {
audit.ValidRequestID("550e8400-e29b-41d4-a716-446655440000")
}
}