2020-10-25 21:44:56 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"github.com/go-playground/assert/v2"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"mpbl3p/mocks"
|
|
|
|
"mpbl3p/proxy"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFlow_Consume(t *testing.T) {
|
|
|
|
testContent := []byte("A test string is the content of this packet.")
|
|
|
|
testPacket := proxy.NewPacket(testContent)
|
|
|
|
testMac := mocks.AlmostUselessMac{}
|
|
|
|
|
2020-10-25 22:05:35 +00:00
|
|
|
t.Run("Length", func(t *testing.T) {
|
2020-10-25 21:44:56 +00:00
|
|
|
testConn := mocks.NewMockPerfectBiConn(100)
|
|
|
|
|
|
|
|
flowA := Flow{conn: testConn.SideA()}
|
|
|
|
|
|
|
|
err := flowA.Consume(testPacket, testMac)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
n, err := testConn.SideB().Read(buf)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-10-25 22:05:35 +00:00
|
|
|
assert.Equal(t, len(testContent)+8+4+4, n)
|
|
|
|
assert.Equal(t, uint32(len(testContent)+8+4), binary.LittleEndian.Uint32(buf[:len(buf)-4]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlow_Produce(t *testing.T) {
|
|
|
|
testContent := "A test string is the content of this packet."
|
|
|
|
testMarshalled := []byte("0000" + testContent + "00000000abcd")
|
|
|
|
binary.LittleEndian.PutUint32(testMarshalled, uint32(len(testMarshalled)-4))
|
|
|
|
|
|
|
|
testMac := mocks.AlmostUselessMac{}
|
|
|
|
|
|
|
|
t.Run("Length", func(t *testing.T) {
|
|
|
|
testConn := mocks.NewMockPerfectBiConn(100)
|
|
|
|
|
|
|
|
flowA := Flow{conn: testConn.SideA()}
|
|
|
|
|
|
|
|
_, err := testConn.SideB().Write(testMarshalled)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-10-25 22:12:38 +00:00
|
|
|
p, err := flowA.Produce(testMac)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, len(testContent), len(p.Raw()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Value", func(t *testing.T) {
|
|
|
|
testConn := mocks.NewMockPerfectBiConn(100)
|
|
|
|
|
|
|
|
flowA := Flow{conn: testConn.SideA()}
|
|
|
|
|
|
|
|
_, err := testConn.SideB().Write(testMarshalled)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-10-25 22:05:35 +00:00
|
|
|
p, err := flowA.Produce(testMac)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, testContent, string(p.Raw()))
|
2020-10-25 21:44:56 +00:00
|
|
|
})
|
|
|
|
}
|