170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
package udp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"mpbl3p/mocks"
|
|
"mpbl3p/proxy"
|
|
"mpbl3p/udp/congestion"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFlow_Consume(t *testing.T) {
|
|
testContent := []byte("A test string is the content of this packet.")
|
|
testPacket := proxy.SimplePacket(testContent)
|
|
testMac := mocks.AlmostUselessMac("abcd")
|
|
|
|
t.Run("SingleGeneratorLength", func(t *testing.T) {
|
|
testConn := mocks.NewMockPerfectBiPacketConn(10)
|
|
|
|
flowA := newFlow(congestion.NewNone(), []proxy.MacVerifier{testMac}, []proxy.MacGenerator{testMac})
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
err := flowA.Consume(context.Background(), testPacket)
|
|
require.Nil(t, err)
|
|
|
|
buf := make([]byte, 100)
|
|
n, _, err := testConn.SideA().ReadFromUDP(buf)
|
|
require.Nil(t, err)
|
|
|
|
// 12 header, 8 timestamp, 4 MAC
|
|
assert.Equal(t, len(testContent)+12+4, n)
|
|
})
|
|
|
|
t.Run("MultipleGeneratorsLength", func(t *testing.T) {
|
|
testMac2 := mocks.AlmostUselessMac("efgh")
|
|
testConn := mocks.NewMockPerfectBiPacketConn(10)
|
|
|
|
flowA := newFlow(congestion.NewNone(), []proxy.MacVerifier{testMac, testMac2}, []proxy.MacGenerator{testMac, testMac2})
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
err := flowA.Consume(context.Background(), testPacket)
|
|
require.Nil(t, err)
|
|
|
|
buf := make([]byte, 100)
|
|
n, _, err := testConn.SideA().ReadFromUDP(buf)
|
|
require.Nil(t, err)
|
|
|
|
// 12 header, 8 timestamp, 4 MAC
|
|
assert.Equal(t, len(testContent)+12+4+4, n)
|
|
})
|
|
|
|
t.Run("MultipleGeneratorsOrder", func(t *testing.T) {
|
|
testMac2 := mocks.AlmostUselessMac("efgh")
|
|
testConn := mocks.NewMockPerfectBiPacketConn(10)
|
|
|
|
flowA := newFlow(congestion.NewNone(), []proxy.MacVerifier{testMac, testMac2}, []proxy.MacGenerator{testMac, testMac2})
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
err := flowA.Consume(context.Background(), testPacket)
|
|
require.Nil(t, err)
|
|
|
|
buf := make([]byte, 100)
|
|
n, _, err := testConn.SideA().ReadFromUDP(buf)
|
|
require.Nil(t, err)
|
|
|
|
// 12 header, 8 timestamp, 4 MAC
|
|
require.Equal(t, len(testContent)+12+4+4, n)
|
|
|
|
macs := string(buf[n-8 : n])
|
|
assert.Equal(t, "abcdefgh", macs)
|
|
})
|
|
}
|
|
|
|
func TestFlow_Produce(t *testing.T) {
|
|
testContent := []byte("A test string is the content of this packet.")
|
|
testPacket := Packet{
|
|
ack: 42,
|
|
nack: 26,
|
|
seq: 128,
|
|
data: proxy.SimplePacket(testContent),
|
|
}
|
|
testMac := mocks.AlmostUselessMac("abcd")
|
|
|
|
testMarshalled := proxy.AppendMac(testPacket.Marshal(), testMac)
|
|
|
|
t.Run("Length", func(t *testing.T) {
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
testConn := mocks.NewMockPerfectBiPacketConn(10)
|
|
|
|
_, err := testConn.SideA().Write(testMarshalled)
|
|
require.Nil(t, err)
|
|
|
|
flowA := newFlow(congestion.NewNone(), []proxy.MacVerifier{testMac}, []proxy.MacGenerator{testMac})
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
go func() {
|
|
p, err := flowA.readPacket(context.Background(), testConn.SideB())
|
|
assert.Nil(t, err)
|
|
err = flowA.queueDatagram(context.Background(), p)
|
|
assert.Nil(t, err)
|
|
}()
|
|
p, err := flowA.Produce(context.Background())
|
|
|
|
require.Nil(t, err)
|
|
assert.Len(t, p.Contents(), len(testContent))
|
|
|
|
done <- struct{}{}
|
|
}()
|
|
|
|
timer := time.NewTimer(500 * time.Millisecond)
|
|
select {
|
|
case <-done:
|
|
case <-timer.C:
|
|
fmt.Println("timed out")
|
|
t.FailNow()
|
|
}
|
|
})
|
|
|
|
t.Run("MultipleVerifiersStrip", func(t *testing.T) {
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
testMac2 := mocks.AlmostUselessMac("efgh")
|
|
testConn := mocks.NewMockPerfectBiPacketConn(10)
|
|
|
|
_, err := testConn.SideA().Write(proxy.AppendMac(testMarshalled, testMac2))
|
|
require.Nil(t, err)
|
|
|
|
flowA := newFlow(congestion.NewNone(), []proxy.MacVerifier{testMac, testMac2}, []proxy.MacGenerator{testMac, testMac2})
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
go func() {
|
|
p, err := flowA.readPacket(context.Background(), testConn.SideB())
|
|
assert.Nil(t, err)
|
|
err = flowA.queueDatagram(context.Background(), p)
|
|
assert.Nil(t, err)
|
|
}()
|
|
p, err := flowA.Produce(context.Background())
|
|
|
|
require.Nil(t, err)
|
|
assert.Len(t, p.Contents(), len(testContent))
|
|
|
|
done <- struct{}{}
|
|
}()
|
|
|
|
timer := time.NewTimer(500 * time.Millisecond)
|
|
select {
|
|
case <-done:
|
|
case <-timer.C:
|
|
fmt.Println("timed out")
|
|
t.FailNow()
|
|
}
|
|
})
|
|
}
|