2021-07-08 09:29:49 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-12 09:37:28 +00:00
|
|
|
"context"
|
2021-07-08 09:29:49 +00:00
|
|
|
"errors"
|
2021-11-12 09:37:28 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2021-07-08 09:29:49 +00:00
|
|
|
"testing"
|
2021-12-02 11:02:27 +00:00
|
|
|
"time"
|
2021-07-08 09:29:49 +00:00
|
|
|
|
2021-11-12 09:37:28 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/rs/zerolog"
|
2021-07-08 09:29:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
const (
|
|
|
|
testCloseIdleAfterHint = time.Minute * 2
|
|
|
|
)
|
|
|
|
|
2021-07-08 09:29:49 +00:00
|
|
|
func TestConnectRequestData(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
hostname string
|
|
|
|
connectionType ConnectionType
|
|
|
|
metadata []Metadata
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Signature verified and request metadata is unmarshaled and read correctly",
|
|
|
|
hostname: "tunnel.com",
|
|
|
|
connectionType: ConnectionTypeHTTP,
|
|
|
|
metadata: []Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-07-08 09:29:49 +00:00
|
|
|
Key: "key",
|
|
|
|
Val: "1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
b := &bytes.Buffer{}
|
2021-11-12 09:37:28 +00:00
|
|
|
reqClientStream := RequestClientStream{noopCloser{b}}
|
|
|
|
err := reqClientStream.WriteConnectRequestData(test.hostname, test.connectionType, test.metadata...)
|
2021-07-08 09:29:49 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-12 09:37:28 +00:00
|
|
|
protocol, err := DetermineProtocol(b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
reqServerStream, err := NewRequestServerStream(noopCloser{b}, protocol)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
reqMeta, err := reqServerStream.ReadConnectRequestData()
|
2021-07-08 09:29:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.metadata, reqMeta.Metadata)
|
|
|
|
assert.Equal(t, test.hostname, reqMeta.Dest)
|
|
|
|
assert.Equal(t, test.connectionType, reqMeta.Type)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectResponseMeta(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
err error
|
|
|
|
metadata []Metadata
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Signature verified and response metadata is unmarshaled and read correctly",
|
|
|
|
metadata: []Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-07-08 09:29:49 +00:00
|
|
|
Key: "key",
|
|
|
|
Val: "1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "If error is not empty, other fields should be blank",
|
|
|
|
err: errors.New("something happened"),
|
|
|
|
metadata: []Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-07-08 09:29:49 +00:00
|
|
|
Key: "key",
|
|
|
|
Val: "1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
b := &bytes.Buffer{}
|
2021-11-12 09:37:28 +00:00
|
|
|
reqServerStream := RequestServerStream{noopCloser{b}}
|
|
|
|
err := reqServerStream.WriteConnectResponseData(test.err, test.metadata...)
|
2021-07-08 09:29:49 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-12 09:37:28 +00:00
|
|
|
|
|
|
|
reqClientStream := RequestClientStream{noopCloser{b}}
|
|
|
|
respMeta, err := reqClientStream.ReadConnectResponseData()
|
2021-07-08 09:29:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if respMeta.Error == "" {
|
|
|
|
assert.Equal(t, test.metadata, respMeta.Metadata)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, 0, len(respMeta.Metadata))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-12 09:37:28 +00:00
|
|
|
|
|
|
|
func TestRegisterUdpSession(t *testing.T) {
|
|
|
|
clientReader, serverWriter := io.Pipe()
|
|
|
|
serverReader, clientWriter := io.Pipe()
|
|
|
|
|
|
|
|
clientStream := mockRPCStream{clientReader, clientWriter}
|
|
|
|
serverStream := mockRPCStream{serverReader, serverWriter}
|
|
|
|
|
|
|
|
rpcServer := mockRPCServer{
|
2021-12-02 11:02:27 +00:00
|
|
|
sessionID: uuid.New(),
|
|
|
|
dstIP: net.IP{172, 16, 0, 1},
|
|
|
|
dstPort: 8000,
|
|
|
|
closeIdleAfter: testCloseIdleAfterHint,
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
logger := zerolog.Nop()
|
|
|
|
sessionRegisteredChan := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
protocol, err := DetermineProtocol(serverStream)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rpcServerStream, err := NewRPCServerStream(serverStream, protocol)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = rpcServerStream.Serve(rpcServer, &logger)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
serverStream.Close()
|
|
|
|
close(sessionRegisteredChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
rpcClientStream, err := NewRPCClientStream(context.Background(), clientStream, &logger)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
assert.NoError(t, rpcClientStream.RegisterUdpSession(context.Background(), rpcServer.sessionID, rpcServer.dstIP, rpcServer.dstPort, testCloseIdleAfterHint))
|
2021-11-12 09:37:28 +00:00
|
|
|
|
|
|
|
// Different sessionID, the RPC server should reject the registraion
|
2021-12-02 11:02:27 +00:00
|
|
|
assert.Error(t, rpcClientStream.RegisterUdpSession(context.Background(), uuid.New(), rpcServer.dstIP, rpcServer.dstPort, testCloseIdleAfterHint))
|
2021-11-30 19:58:11 +00:00
|
|
|
|
|
|
|
assert.NoError(t, rpcClientStream.UnregisterUdpSession(context.Background(), rpcServer.sessionID))
|
|
|
|
|
|
|
|
// Different sessionID, the RPC server should reject the unregistraion
|
|
|
|
assert.Error(t, rpcClientStream.UnregisterUdpSession(context.Background(), uuid.New()))
|
2021-11-12 09:37:28 +00:00
|
|
|
|
|
|
|
rpcClientStream.Close()
|
|
|
|
<-sessionRegisteredChan
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockRPCServer struct {
|
2021-12-02 11:02:27 +00:00
|
|
|
sessionID uuid.UUID
|
|
|
|
dstIP net.IP
|
|
|
|
dstPort uint16
|
|
|
|
closeIdleAfter time.Duration
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
func (s mockRPCServer) RegisterUdpSession(ctx context.Context, sessionID uuid.UUID, dstIP net.IP, dstPort uint16, closeIdleAfter time.Duration) error {
|
2021-11-12 09:37:28 +00:00
|
|
|
if s.sessionID != sessionID {
|
|
|
|
return fmt.Errorf("expect session ID %s, got %s", s.sessionID, sessionID)
|
|
|
|
}
|
|
|
|
if !s.dstIP.Equal(dstIP) {
|
|
|
|
return fmt.Errorf("expect destination IP %s, got %s", s.dstIP, dstIP)
|
|
|
|
}
|
|
|
|
if s.dstPort != dstPort {
|
2021-12-02 11:02:27 +00:00
|
|
|
return fmt.Errorf("expect destination port %d, got %d", s.dstPort, dstPort)
|
|
|
|
}
|
|
|
|
if s.closeIdleAfter != closeIdleAfter {
|
|
|
|
return fmt.Errorf("expect closeIdleAfter %d, got %d", s.closeIdleAfter, closeIdleAfter)
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-30 19:58:11 +00:00
|
|
|
func (s mockRPCServer) UnregisterUdpSession(ctx context.Context, sessionID uuid.UUID) error {
|
|
|
|
if s.sessionID != sessionID {
|
|
|
|
return fmt.Errorf("expect session ID %s, got %s", s.sessionID, sessionID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:37:28 +00:00
|
|
|
type mockRPCStream struct {
|
|
|
|
io.ReadCloser
|
|
|
|
io.WriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s mockRPCStream) Close() error {
|
|
|
|
_ = s.ReadCloser.Close()
|
|
|
|
_ = s.WriteCloser.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopCloser struct {
|
|
|
|
io.ReadWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (noopCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|