-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse_test.go
71 lines (61 loc) · 1.61 KB
/
parse_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
package proxyprotocol
import (
"bufio"
"bytes"
"net"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParse_Malformed(t *testing.T) {
data := []byte{
// PROXY protocol v2 magic header
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A,
// v2 version, PROXY cmd
0x21,
// TCP, IPv4 (also works with 0x13,0x21,0x22,0x31,0x32)
0x12,
// Length
0x00, 0x00,
// src/dest address data _should_ be here but is omitted.
}
_, err := Parse(
bufio.NewReader(
bytes.NewReader(data)))
assert.Error(t, err)
}
func TestParse_HeaderV1(t *testing.T) {
check := func(name string, hdr HeaderV1, exp string) {
t.Helper()
h, err := Parse(bufio.NewReader(strings.NewReader(exp)))
assert.NoError(t, err, name)
assert.Equal(t, 1, h.Version(), name+" version")
h1 := h.(*HeaderV1)
assert.Equal(t, hdr, *h1, name)
}
check("blank", HeaderV1{}, "PROXY UNKNOWN\r\n")
check("ipv4", HeaderV1{
SrcPort: 1234,
DestPort: 5678,
SrcIP: net.ParseIP("192.168.0.1"),
DestIP: net.ParseIP("192.168.0.2"),
},
"PROXY TCP4 192.168.0.1 192.168.0.2 1234 5678\r\n",
)
check("ipv6", HeaderV1{
SrcPort: 1234,
DestPort: 5678,
SrcIP: net.ParseIP("2001:db8:85a3::8a2e:370:7334"),
DestIP: net.ParseIP("2002:db8:85a3::8a2e:370:7334"),
},
"PROXY TCP6 2001:db8:85a3::8a2e:370:7334 2002:db8:85a3::8a2e:370:7334 1234 5678\r\n",
)
check("ipv6-mapped-ipv4", HeaderV1{
SrcPort: 53740,
DestPort: 10001,
SrcIP: net.ParseIP("::ffff:192.168.0.1"),
DestIP: net.ParseIP("::ffff:192.168.0.1"),
},
"PROXY TCP6 ::ffff:192.168.0.1 ::ffff:192.168.0.1 53740 10001\r\n",
)
}