87 lines
1.9 KiB
Go
87 lines
1.9 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{}
|
|
|
|
t.Run("Length", func(t *testing.T) {
|
|
testConn := mocks.NewMockPerfectBiPacketConn(10)
|
|
|
|
flowA := newFlow(congestion.NewNone(), testMac)
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
err := flowA.Consume(context.Background(), testPacket, testMac)
|
|
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+8+4, n)
|
|
})
|
|
}
|
|
|
|
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{}
|
|
|
|
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(), testMac)
|
|
|
|
flowA.writer = testConn.SideB()
|
|
flowA.isAlive = true
|
|
|
|
go func() {
|
|
err := flowA.readQueuePacket(context.Background(), testConn.SideB())
|
|
assert.Nil(t, err)
|
|
}()
|
|
p, err := flowA.Produce(context.Background(), testMac)
|
|
|
|
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()
|
|
}
|
|
})
|
|
}
|