2022-08-22 16:41:51 +00:00
|
|
|
//go:build darwin
|
|
|
|
|
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSingleEchoIDTracker(t *testing.T) {
|
|
|
|
tracker := newEchoIDTracker()
|
2022-09-09 15:48:42 +00:00
|
|
|
key := flow3Tuple{
|
|
|
|
srcIP: netip.MustParseAddr("172.16.0.1"),
|
|
|
|
dstIP: netip.MustParseAddr("172.16.0.2"),
|
|
|
|
originalEchoID: 5182,
|
|
|
|
}
|
2022-08-22 16:41:51 +00:00
|
|
|
|
|
|
|
// not assigned yet, so nothing to release
|
2022-09-09 15:48:42 +00:00
|
|
|
require.False(t, tracker.release(key, 0))
|
2022-08-22 16:41:51 +00:00
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
echoID, ok := tracker.getOrAssign(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, uint16(0), echoID)
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
// Second time should return the same echo ID
|
|
|
|
echoID, ok = tracker.getOrAssign(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, uint16(0), echoID)
|
|
|
|
|
|
|
|
// releasing a different ID returns false
|
2022-09-09 15:48:42 +00:00
|
|
|
require.False(t, tracker.release(key, 1999))
|
|
|
|
require.True(t, tracker.release(key, echoID))
|
2022-08-22 16:41:51 +00:00
|
|
|
// releasing the second time returns false
|
2022-09-09 15:48:42 +00:00
|
|
|
require.False(t, tracker.release(key, echoID))
|
2022-08-22 16:41:51 +00:00
|
|
|
|
|
|
|
// Move to the next IP
|
2022-09-09 15:48:42 +00:00
|
|
|
echoID, ok = tracker.getOrAssign(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, uint16(1), echoID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFullEchoIDTracker(t *testing.T) {
|
2022-09-09 15:48:42 +00:00
|
|
|
var (
|
|
|
|
dstIP = netip.MustParseAddr("192.168.0.1")
|
|
|
|
originalEchoID = 41820
|
|
|
|
)
|
2022-08-22 16:41:51 +00:00
|
|
|
tracker := newEchoIDTracker()
|
2022-09-09 15:48:42 +00:00
|
|
|
firstSrcIP := netip.MustParseAddr("172.16.0.1")
|
|
|
|
srcIP := firstSrcIP
|
2022-08-22 16:41:51 +00:00
|
|
|
|
|
|
|
for i := uint16(0); i < math.MaxUint16; i++ {
|
2022-09-09 15:48:42 +00:00
|
|
|
key := flow3Tuple{
|
|
|
|
srcIP: srcIP,
|
|
|
|
dstIP: dstIP,
|
|
|
|
originalEchoID: originalEchoID,
|
|
|
|
}
|
|
|
|
echoID, ok := tracker.getOrAssign(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, i, echoID)
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
echoID, ok = tracker.get(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, i, echoID)
|
2022-09-09 15:48:42 +00:00
|
|
|
|
2022-08-22 16:41:51 +00:00
|
|
|
srcIP = srcIP.Next()
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
key := flow3Tuple{
|
|
|
|
srcIP: srcIP.Next(),
|
|
|
|
dstIP: dstIP,
|
|
|
|
originalEchoID: originalEchoID,
|
|
|
|
}
|
2022-08-22 16:41:51 +00:00
|
|
|
// All echo IDs are assigned
|
2022-09-09 15:48:42 +00:00
|
|
|
echoID, ok := tracker.getOrAssign(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.False(t, ok)
|
|
|
|
require.Equal(t, uint16(0), echoID)
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
srcIP = firstSrcIP
|
2022-08-22 16:41:51 +00:00
|
|
|
for i := uint16(0); i < math.MaxUint16; i++ {
|
2022-09-09 15:48:42 +00:00
|
|
|
key := flow3Tuple{
|
|
|
|
srcIP: srcIP,
|
|
|
|
dstIP: dstIP,
|
|
|
|
originalEchoID: originalEchoID,
|
|
|
|
}
|
|
|
|
ok := tracker.release(key, i)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
echoID, ok = tracker.get(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.False(t, ok)
|
|
|
|
require.Equal(t, uint16(0), echoID)
|
|
|
|
srcIP = srcIP.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The IDs are assignable again
|
2022-09-09 15:48:42 +00:00
|
|
|
srcIP = firstSrcIP
|
2022-08-22 16:41:51 +00:00
|
|
|
for i := uint16(0); i < math.MaxUint16; i++ {
|
2022-09-09 15:48:42 +00:00
|
|
|
key := flow3Tuple{
|
|
|
|
srcIP: srcIP,
|
|
|
|
dstIP: dstIP,
|
|
|
|
originalEchoID: originalEchoID,
|
|
|
|
}
|
|
|
|
echoID, ok := tracker.getOrAssign(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, i, echoID)
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
echoID, ok = tracker.get(key)
|
2022-08-22 16:41:51 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, i, echoID)
|
|
|
|
srcIP = srcIP.Next()
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 15:48:42 +00:00
|
|
|
|
|
|
|
func (eit *echoIDTracker) get(key flow3Tuple) (id uint16, exist bool) {
|
|
|
|
eit.lock.Lock()
|
|
|
|
defer eit.lock.Unlock()
|
|
|
|
id, exists := eit.mapping[key]
|
|
|
|
return id, exists
|
|
|
|
}
|