ZTC-234: macOS tests

This commit is contained in:
Joel May 2022-11-10 09:09:47 -08:00
parent 2baea15387
commit 6a1dad0ce2
4 changed files with 83 additions and 11 deletions

View File

@ -148,7 +148,6 @@ func (ip *icmpProxy) Request(ctx context.Context, pk *packet.ICMP, responder *pa
dstIP: pk.Dst, dstIP: pk.Dst,
originalEchoID: originalEcho.ID, originalEchoID: originalEcho.ID,
} }
// TODO: TUN-6744 assign unique flow per (src, echo ID)
assignedEchoID, success := ip.echoIDTracker.getOrAssign(echoIDTrackerKey) assignedEchoID, success := ip.echoIDTracker.getOrAssign(echoIDTrackerKey)
if !success { if !success {
err := fmt.Errorf("failed to assign unique echo ID") err := fmt.Errorf("failed to assign unique echo ID")

View File

@ -8,6 +8,8 @@ import (
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/cloudflare/cloudflared/packet"
) )
func TestSingleEchoIDTracker(t *testing.T) { func TestSingleEchoIDTracker(t *testing.T) {
@ -119,3 +121,9 @@ func (eit *echoIDTracker) get(key flow3Tuple) (id uint16, exist bool) {
id, exists := eit.mapping[key] id, exists := eit.mapping[key]
return id, exists return id, exists
} }
func getFunnel(t *testing.T, proxy *icmpProxy, tuple flow3Tuple) (packet.Funnel, bool) {
assignedEchoID, success := proxy.echoIDTracker.getOrAssign(tuple)
require.True(t, success)
return proxy.srcFunnelTracker.Get(echoFunnelID(assignedEchoID))
}

View File

@ -0,0 +1,13 @@
//go:build linux
package ingress
import (
"testing"
"github.com/cloudflare/cloudflared/packet"
)
func getFunnel(t *testing.T, proxy *icmpProxy, tuple flow3Tuple) (packet.Funnel, bool) {
return proxy.srcFunnelTracker.Get(tuple)
}

View File

@ -52,28 +52,18 @@ func TestFunnelIdleTimeout(t *testing.T) {
}, },
}, },
} }
funnelID := flow3Tuple{
srcIP: pk.Src,
dstIP: pk.Dst,
originalEchoID: echoID,
}
muxer := newMockMuxer(0) muxer := newMockMuxer(0)
responder := packetResponder{ responder := packetResponder{
datagramMuxer: muxer, datagramMuxer: muxer,
} }
require.NoError(t, proxy.Request(ctx, &pk, &responder)) require.NoError(t, proxy.Request(ctx, &pk, &responder))
validateEchoFlow(t, <-muxer.cfdToEdge, &pk) validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
funnel1, found := proxy.srcFunnelTracker.Get(funnelID)
require.True(t, found)
// Send second request, should reuse the funnel // Send second request, should reuse the funnel
require.NoError(t, proxy.Request(ctx, &pk, &packetResponder{ require.NoError(t, proxy.Request(ctx, &pk, &packetResponder{
datagramMuxer: muxer, datagramMuxer: muxer,
})) }))
validateEchoFlow(t, <-muxer.cfdToEdge, &pk) validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
funnel2, found := proxy.srcFunnelTracker.Get(funnelID)
require.True(t, found)
require.Equal(t, funnel1, funnel2)
time.Sleep(idleTimeout * 2) time.Sleep(idleTimeout * 2)
newMuxer := newMockMuxer(0) newMuxer := newMockMuxer(0)
@ -86,3 +76,65 @@ func TestFunnelIdleTimeout(t *testing.T) {
cancel() cancel()
<-proxyDone <-proxyDone
} }
func TestReuseFunnel(t *testing.T) {
const (
idleTimeout = time.Second
echoID = 42573
startSeq = 8129
)
logger := zerolog.New(os.Stderr)
proxy, err := newICMPProxy(localhostIP, "", &logger, idleTimeout)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
proxyDone := make(chan struct{})
go func() {
proxy.Serve(ctx)
close(proxyDone)
}()
// Send a packet to register the flow
pk := packet.ICMP{
IP: &packet.IP{
Src: localhostIP,
Dst: localhostIP,
Protocol: layers.IPProtocolICMPv4,
},
Message: &icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &icmp.Echo{
ID: echoID,
Seq: startSeq,
Data: []byte(t.Name()),
},
},
}
tuple := flow3Tuple{
srcIP: pk.Src,
dstIP: pk.Dst,
originalEchoID: echoID,
}
muxer := newMockMuxer(0)
responder := packetResponder{
datagramMuxer: muxer,
}
require.NoError(t, proxy.Request(ctx, &pk, &responder))
validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
funnel1, found := getFunnel(t, proxy, tuple)
require.True(t, found)
// Send second request, should reuse the funnel
require.NoError(t, proxy.Request(ctx, &pk, &packetResponder{
datagramMuxer: muxer,
}))
validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
funnel2, found := getFunnel(t, proxy, tuple)
require.True(t, found)
require.Equal(t, funnel1, funnel2)
cancel()
<-proxyDone
}