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"
|
2022-02-02 12:27:49 +00:00
|
|
|
|
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2021-07-08 09:29:49 +00:00
|
|
|
)
|
|
|
|
|
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) {
|
2021-12-14 22:52:47 +00:00
|
|
|
unregisterMessage := "closed by eyeball"
|
2021-11-30 19:58:11 +00:00
|
|
|
|
2022-09-07 22:06:06 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
sessionRPCServer mockSessionRPCServer
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "RegisterUdpSession (no trace context)",
|
|
|
|
sessionRPCServer: mockSessionRPCServer{
|
|
|
|
sessionID: uuid.New(),
|
|
|
|
dstIP: net.IP{172, 16, 0, 1},
|
|
|
|
dstPort: 8000,
|
|
|
|
closeIdleAfter: testCloseIdleAfterHint,
|
|
|
|
unregisterMessage: unregisterMessage,
|
|
|
|
traceContext: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RegisterUdpSession (with trace context)",
|
|
|
|
sessionRPCServer: mockSessionRPCServer{
|
|
|
|
sessionID: uuid.New(),
|
|
|
|
dstIP: net.IP{172, 16, 0, 1},
|
|
|
|
dstPort: 8000,
|
|
|
|
closeIdleAfter: testCloseIdleAfterHint,
|
|
|
|
unregisterMessage: unregisterMessage,
|
|
|
|
traceContext: "1241ce3ecdefc68854e8514e69ba42ca:b38f1bf5eae406f3:0:1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-11-12 09:37:28 +00:00
|
|
|
|
2022-09-07 22:06:06 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
logger := zerolog.Nop()
|
|
|
|
clientStream, serverStream := newMockRPCStreams()
|
|
|
|
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(test.sessionRPCServer, nil, &logger)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
serverStream.Close()
|
|
|
|
close(sessionRegisteredChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
rpcClientStream, err := NewRPCClientStream(context.Background(), clientStream, &logger)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reg, err := rpcClientStream.RegisterUdpSession(context.Background(), test.sessionRPCServer.sessionID, test.sessionRPCServer.dstIP, test.sessionRPCServer.dstPort, testCloseIdleAfterHint, test.sessionRPCServer.traceContext)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, reg.Err)
|
|
|
|
|
|
|
|
// Different sessionID, the RPC server should reject the registraion
|
|
|
|
reg, err = rpcClientStream.RegisterUdpSession(context.Background(), uuid.New(), test.sessionRPCServer.dstIP, test.sessionRPCServer.dstPort, testCloseIdleAfterHint, test.sessionRPCServer.traceContext)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Error(t, reg.Err)
|
|
|
|
|
|
|
|
assert.NoError(t, rpcClientStream.UnregisterUdpSession(context.Background(), test.sessionRPCServer.sessionID, unregisterMessage))
|
|
|
|
|
|
|
|
// Different sessionID, the RPC server should reject the unregistraion
|
|
|
|
assert.Error(t, rpcClientStream.UnregisterUdpSession(context.Background(), uuid.New(), unregisterMessage))
|
|
|
|
|
|
|
|
rpcClientStream.Close()
|
|
|
|
<-sessionRegisteredChan
|
|
|
|
})
|
|
|
|
}
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 12:27:49 +00:00
|
|
|
func TestManageConfiguration(t *testing.T) {
|
|
|
|
var (
|
|
|
|
version int32 = 168
|
|
|
|
config = []byte(t.Name())
|
|
|
|
)
|
|
|
|
clientStream, serverStream := newMockRPCStreams()
|
|
|
|
|
|
|
|
configRPCServer := mockConfigRPCServer{
|
|
|
|
version: version,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := zerolog.Nop()
|
|
|
|
updatedChan := 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(nil, configRPCServer, &logger)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
serverStream.Close()
|
|
|
|
close(updatedChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
rpcClientStream, err := NewRPCClientStream(ctx, clientStream, &logger)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
result, err := rpcClientStream.UpdateConfiguration(ctx, version, config)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, version, result.LastAppliedVersion)
|
|
|
|
require.NoError(t, result.Err)
|
|
|
|
|
|
|
|
rpcClientStream.Close()
|
|
|
|
<-updatedChan
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockSessionRPCServer struct {
|
2021-12-14 22:52:47 +00:00
|
|
|
sessionID uuid.UUID
|
|
|
|
dstIP net.IP
|
|
|
|
dstPort uint16
|
|
|
|
closeIdleAfter time.Duration
|
|
|
|
unregisterMessage string
|
2022-09-07 22:06:06 +00:00
|
|
|
traceContext string
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 04:42:11 +00:00
|
|
|
func (s mockSessionRPCServer) RegisterUdpSession(_ context.Context, sessionID uuid.UUID, dstIP net.IP, dstPort uint16, closeIdleAfter time.Duration, traceContext string) (*tunnelpogs.RegisterUdpSessionResponse, error) {
|
2021-11-12 09:37:28 +00:00
|
|
|
if s.sessionID != sessionID {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, fmt.Errorf("expect session ID %s, got %s", s.sessionID, sessionID)
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
if !s.dstIP.Equal(dstIP) {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, fmt.Errorf("expect destination IP %s, got %s", s.dstIP, dstIP)
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
if s.dstPort != dstPort {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, fmt.Errorf("expect destination port %d, got %d", s.dstPort, dstPort)
|
2021-12-02 11:02:27 +00:00
|
|
|
}
|
|
|
|
if s.closeIdleAfter != closeIdleAfter {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, fmt.Errorf("expect closeIdleAfter %d, got %d", s.closeIdleAfter, closeIdleAfter)
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
2022-09-07 22:06:06 +00:00
|
|
|
if s.traceContext != traceContext {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, fmt.Errorf("expect traceContext %s, got %s", s.traceContext, traceContext)
|
2022-09-07 22:06:06 +00:00
|
|
|
}
|
2022-09-09 04:42:11 +00:00
|
|
|
return &tunnelpogs.RegisterUdpSessionResponse{}, nil
|
2021-11-12 09:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 12:27:49 +00:00
|
|
|
func (s mockSessionRPCServer) UnregisterUdpSession(_ context.Context, sessionID uuid.UUID, message string) error {
|
2021-11-30 19:58:11 +00:00
|
|
|
if s.sessionID != sessionID {
|
|
|
|
return fmt.Errorf("expect session ID %s, got %s", s.sessionID, sessionID)
|
|
|
|
}
|
2021-12-14 22:52:47 +00:00
|
|
|
if s.unregisterMessage != message {
|
|
|
|
return fmt.Errorf("expect unregister message %s, got %s", s.unregisterMessage, message)
|
|
|
|
}
|
2021-11-30 19:58:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-02 12:27:49 +00:00
|
|
|
type mockConfigRPCServer struct {
|
|
|
|
version int32
|
|
|
|
config []byte
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:46:04 +00:00
|
|
|
func (s mockConfigRPCServer) UpdateConfiguration(_ context.Context, version int32, config []byte) *tunnelpogs.UpdateConfigurationResponse {
|
2022-02-02 12:27:49 +00:00
|
|
|
if s.version != version {
|
2022-02-11 15:46:04 +00:00
|
|
|
return &tunnelpogs.UpdateConfigurationResponse{
|
|
|
|
Err: fmt.Errorf("expect version %d, got %d", s.version, version),
|
|
|
|
}
|
2022-02-02 12:27:49 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(s.config, config) {
|
2022-02-11 15:46:04 +00:00
|
|
|
return &tunnelpogs.UpdateConfigurationResponse{
|
|
|
|
Err: fmt.Errorf("expect config %v, got %v", s.config, config),
|
|
|
|
}
|
2022-02-02 12:27:49 +00:00
|
|
|
}
|
2022-02-11 15:46:04 +00:00
|
|
|
return &tunnelpogs.UpdateConfigurationResponse{LastAppliedVersion: version}
|
2022-02-02 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:37:28 +00:00
|
|
|
type mockRPCStream struct {
|
|
|
|
io.ReadCloser
|
|
|
|
io.WriteCloser
|
|
|
|
}
|
|
|
|
|
2022-02-02 12:27:49 +00:00
|
|
|
func newMockRPCStreams() (client io.ReadWriteCloser, server io.ReadWriteCloser) {
|
|
|
|
clientReader, serverWriter := io.Pipe()
|
|
|
|
serverReader, clientWriter := io.Pipe()
|
|
|
|
|
|
|
|
client = mockRPCStream{clientReader, clientWriter}
|
|
|
|
server = mockRPCStream{serverReader, serverWriter}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:37:28 +00:00
|
|
|
func (s mockRPCStream) Close() error {
|
|
|
|
_ = s.ReadCloser.Close()
|
|
|
|
_ = s.WriteCloser.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopCloser struct {
|
|
|
|
io.ReadWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (noopCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|