2020-11-27 20:17:59 +00:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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.")
|
2020-11-28 17:15:56 +00:00
|
|
|
testPacket := proxy.SimplePacket(testContent)
|
2020-11-27 20:17:59 +00:00
|
|
|
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(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,
|
2020-11-28 17:15:56 +00:00
|
|
|
data: proxy.SimplePacket(testContent),
|
2020-11-27 20:17:59 +00:00
|
|
|
}
|
|
|
|
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.acceptPacket(testConn.SideB())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}()
|
|
|
|
p, err := flowA.Produce(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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|