2020-02-11 00:26:04 +00:00
|
|
|
|
package udp
|
|
|
|
|
|
|
|
|
|
import (
|
2021-11-09 14:12:07 +00:00
|
|
|
|
"crypto/rand"
|
2020-02-11 00:26:04 +00:00
|
|
|
|
"net"
|
2021-11-09 14:12:07 +00:00
|
|
|
|
"runtime"
|
2020-02-11 00:26:04 +00:00
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-09 14:12:07 +00:00
|
|
|
|
func TestProxy_ServeUDP(t *testing.T) {
|
2020-02-11 00:26:04 +00:00
|
|
|
|
backendAddr := ":8081"
|
2021-11-09 14:12:07 +00:00
|
|
|
|
go newServer(t, backendAddr, HandlerFunc(func(conn *Conn) {
|
2020-02-11 00:26:04 +00:00
|
|
|
|
for {
|
|
|
|
|
b := make([]byte, 1024*1024)
|
|
|
|
|
n, err := conn.Read(b)
|
|
|
|
|
require.NoError(t, err)
|
2021-11-09 14:12:07 +00:00
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
|
_, err = conn.Write(b[:n])
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
proxy, err := NewProxy(backendAddr)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
proxyAddr := ":8080"
|
|
|
|
|
go newServer(t, proxyAddr, proxy)
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
2021-11-09 14:12:07 +00:00
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
|
udpConn, err := net.Dial("udp", proxyAddr)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
_, err = udpConn.Write([]byte("DATAWRITE"))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
b := make([]byte, 1024*1024)
|
|
|
|
|
n, err := udpConn.Read(b)
|
|
|
|
|
require.NoError(t, err)
|
2021-11-09 14:12:07 +00:00
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
|
assert.Equal(t, "DATAWRITE", string(b[:n]))
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 14:12:07 +00:00
|
|
|
|
func TestProxy_ServeUDP_MaxDataSize(t *testing.T) {
|
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
|
// sudo sysctl -w net.inet.udp.maxdgram=65507
|
|
|
|
|
t.Skip("Skip test on darwin as the maximum dgram size is set to 9216 bytes by default")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Theoretical maximum size of data in a UDP datagram.
|
|
|
|
|
// 65535 − 8 (UDP header) − 20 (IP header).
|
|
|
|
|
dataSize := 65507
|
|
|
|
|
|
|
|
|
|
backendAddr := ":8083"
|
|
|
|
|
go newServer(t, backendAddr, HandlerFunc(func(conn *Conn) {
|
|
|
|
|
buffer := make([]byte, dataSize)
|
|
|
|
|
|
|
|
|
|
n, err := conn.Read(buffer)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
_, err = conn.Write(buffer[:n])
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
proxy, err := NewProxy(backendAddr)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
proxyAddr := ":8082"
|
|
|
|
|
go newServer(t, proxyAddr, proxy)
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
|
|
udpConn, err := net.Dial("udp", proxyAddr)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
want := make([]byte, dataSize)
|
|
|
|
|
|
|
|
|
|
_, err = rand.Read(want)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
_, err = udpConn.Write(want)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
got := make([]byte, dataSize)
|
|
|
|
|
|
|
|
|
|
_, err = udpConn.Read(got)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
|
func newServer(t *testing.T, addr string, handler Handler) {
|
2020-12-29 09:54:03 +00:00
|
|
|
|
t.Helper()
|
|
|
|
|
|
2024-01-30 13:56:05 +00:00
|
|
|
|
listener, err := Listen(net.ListenConfig{}, "udp", addr, 3*time.Second)
|
2020-02-11 00:26:04 +00:00
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
|
require.NoError(t, err)
|
2021-11-09 14:12:07 +00:00
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
|
go handler.ServeUDP(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|