|
| 1 | +package net |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + |
| 7 | + pp "github.com/pires/go-proxyproto" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBuildProxyProtocolHeader(t *testing.T) { |
| 12 | + require := require.New(t) |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + srcAddr net.Addr |
| 17 | + dstAddr net.Addr |
| 18 | + version string |
| 19 | + expectError bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "UDP IPv4 v2", |
| 23 | + srcAddr: &net.UDPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}, |
| 24 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("10.0.0.1"), Port: 3306}, |
| 25 | + version: "v2", |
| 26 | + expectError: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "TCP IPv4 v1", |
| 30 | + srcAddr: &net.TCPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}, |
| 31 | + dstAddr: &net.TCPAddr{IP: net.ParseIP("10.0.0.1"), Port: 80}, |
| 32 | + version: "v1", |
| 33 | + expectError: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "UDP IPv6 v2", |
| 37 | + srcAddr: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 12345}, |
| 38 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("::1"), Port: 3306}, |
| 39 | + version: "v2", |
| 40 | + expectError: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "TCP IPv6 v1", |
| 44 | + srcAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 12345}, |
| 45 | + dstAddr: &net.TCPAddr{IP: net.ParseIP("2001:db8::1"), Port: 80}, |
| 46 | + version: "v1", |
| 47 | + expectError: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "nil source address", |
| 51 | + srcAddr: nil, |
| 52 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("10.0.0.1"), Port: 3306}, |
| 53 | + version: "v2", |
| 54 | + expectError: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "nil destination address", |
| 58 | + srcAddr: &net.TCPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}, |
| 59 | + dstAddr: nil, |
| 60 | + version: "v2", |
| 61 | + expectError: false, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "unsupported address type", |
| 65 | + srcAddr: &net.UnixAddr{Name: "/tmp/test.sock", Net: "unix"}, |
| 66 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("10.0.0.1"), Port: 3306}, |
| 67 | + version: "v2", |
| 68 | + expectError: false, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tt := range tests { |
| 73 | + header, err := BuildProxyProtocolHeader(tt.srcAddr, tt.dstAddr, tt.version) |
| 74 | + |
| 75 | + if tt.expectError { |
| 76 | + require.Error(err, "test case: %s", tt.name) |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + require.NoError(err, "test case: %s", tt.name) |
| 81 | + require.NotEmpty(header, "test case: %s", tt.name) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestBuildProxyProtocolHeaderStruct(t *testing.T) { |
| 86 | + require := require.New(t) |
| 87 | + |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + srcAddr net.Addr |
| 91 | + dstAddr net.Addr |
| 92 | + version string |
| 93 | + expectedProtocol pp.AddressFamilyAndProtocol |
| 94 | + expectedVersion byte |
| 95 | + expectedCommand pp.ProtocolVersionAndCommand |
| 96 | + expectedSourceAddr net.Addr |
| 97 | + expectedDestAddr net.Addr |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "TCP IPv4 v2", |
| 101 | + srcAddr: &net.TCPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}, |
| 102 | + dstAddr: &net.TCPAddr{IP: net.ParseIP("10.0.0.1"), Port: 80}, |
| 103 | + version: "v2", |
| 104 | + expectedProtocol: pp.TCPv4, |
| 105 | + expectedVersion: 2, |
| 106 | + expectedCommand: pp.PROXY, |
| 107 | + expectedSourceAddr: &net.TCPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}, |
| 108 | + expectedDestAddr: &net.TCPAddr{IP: net.ParseIP("10.0.0.1"), Port: 80}, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "UDP IPv6 v1", |
| 112 | + srcAddr: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 12345}, |
| 113 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("::1"), Port: 3306}, |
| 114 | + version: "v1", |
| 115 | + expectedProtocol: pp.UDPv6, |
| 116 | + expectedVersion: 1, |
| 117 | + expectedCommand: pp.PROXY, |
| 118 | + expectedSourceAddr: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 12345}, |
| 119 | + expectedDestAddr: &net.UDPAddr{IP: net.ParseIP("::1"), Port: 3306}, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "TCP IPv6 default version", |
| 123 | + srcAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 12345}, |
| 124 | + dstAddr: &net.TCPAddr{IP: net.ParseIP("2001:db8::1"), Port: 80}, |
| 125 | + version: "", |
| 126 | + expectedProtocol: pp.TCPv6, |
| 127 | + expectedVersion: 2, // default to v2 |
| 128 | + expectedCommand: pp.PROXY, |
| 129 | + expectedSourceAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 12345}, |
| 130 | + expectedDestAddr: &net.TCPAddr{IP: net.ParseIP("2001:db8::1"), Port: 80}, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "nil source address", |
| 134 | + srcAddr: nil, |
| 135 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("10.0.0.1"), Port: 3306}, |
| 136 | + version: "v2", |
| 137 | + expectedProtocol: pp.UNSPEC, |
| 138 | + expectedVersion: 2, |
| 139 | + expectedCommand: pp.LOCAL, |
| 140 | + expectedSourceAddr: nil, // go-proxyproto sets both to nil when srcAddr is nil |
| 141 | + expectedDestAddr: nil, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "nil destination address", |
| 145 | + srcAddr: &net.TCPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}, |
| 146 | + dstAddr: nil, |
| 147 | + version: "v2", |
| 148 | + expectedProtocol: pp.UNSPEC, |
| 149 | + expectedVersion: 2, |
| 150 | + expectedCommand: pp.LOCAL, |
| 151 | + expectedSourceAddr: nil, // go-proxyproto sets both to nil when dstAddr is nil |
| 152 | + expectedDestAddr: nil, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "unsupported address type", |
| 156 | + srcAddr: &net.UnixAddr{Name: "/tmp/test.sock", Net: "unix"}, |
| 157 | + dstAddr: &net.UDPAddr{IP: net.ParseIP("10.0.0.1"), Port: 3306}, |
| 158 | + version: "v2", |
| 159 | + expectedProtocol: pp.UNSPEC, |
| 160 | + expectedVersion: 2, |
| 161 | + expectedCommand: pp.LOCAL, |
| 162 | + expectedSourceAddr: nil, // go-proxyproto sets both to nil for unsupported types |
| 163 | + expectedDestAddr: nil, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for _, tt := range tests { |
| 168 | + header := BuildProxyProtocolHeaderStruct(tt.srcAddr, tt.dstAddr, tt.version) |
| 169 | + |
| 170 | + require.NotNil(header, "test case: %s", tt.name) |
| 171 | + |
| 172 | + require.Equal(tt.expectedCommand, header.Command, "test case: %s", tt.name) |
| 173 | + require.Equal(tt.expectedSourceAddr, header.SourceAddr, "test case: %s", tt.name) |
| 174 | + require.Equal(tt.expectedDestAddr, header.DestinationAddr, "test case: %s", tt.name) |
| 175 | + require.Equal(tt.expectedProtocol, header.TransportProtocol, "test case: %s", tt.name) |
| 176 | + require.Equal(tt.expectedVersion, header.Version, "test case: %s", tt.name) |
| 177 | + } |
| 178 | +} |
0 commit comments