2020-10-25 21:44:56 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2021-03-30 20:57:53 +01:00
|
|
|
"context"
|
2020-10-25 21:44:56 +00:00
|
|
|
"encoding/binary"
|
2020-11-27 20:17:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-25 21:44:56 +00:00
|
|
|
"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.")
|
2020-11-28 17:15:56 +00:00
|
|
|
testPacket := proxy.SimplePacket(testContent)
|
2021-05-13 22:26:03 +01:00
|
|
|
testMac := mocks.AlmostUselessMac("abcd")
|
|
|
|
testMac2 := mocks.AlmostUselessMac("efgh")
|
2020-10-25 21:44:56 +00:00
|
|
|
|
2020-10-25 22:05:35 +00:00
|
|
|
t.Run("Length", func(t *testing.T) {
|
2020-11-27 20:17:59 +00:00
|
|
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
2020-10-25 21:44:56 +00:00
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
flowA := NewFlowConn(context.Background(), testConn.SideA(), []proxy.MacVerifier{testMac}, []proxy.MacGenerator{testMac})
|
2020-10-25 21:44:56 +00:00
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
err := flowA.Consume(context.Background(), testPacket)
|
2020-10-25 21:44:56 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
n, err := testConn.SideB().Read(buf)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
assert.Equal(t, len(testContent)+4+4, n)
|
|
|
|
assert.Equal(t, uint32(len(testContent)+4), binary.LittleEndian.Uint32(buf[:len(buf)-4]))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MultipleGeneratorsLength", func(t *testing.T) {
|
|
|
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
|
|
|
|
|
|
|
flowA := NewFlowConn(context.Background(), testConn.SideA(), []proxy.MacVerifier{testMac, testMac2}, []proxy.MacGenerator{testMac, testMac2})
|
|
|
|
|
|
|
|
err := flowA.Consume(context.Background(), testPacket)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
n, err := testConn.SideB().Read(buf)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, len(testContent)+4+4+4, n)
|
|
|
|
assert.Equal(t, uint32(len(testContent)+4+4), binary.LittleEndian.Uint32(buf[:len(buf)-4]))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MultipleGeneratorsOrder", func(t *testing.T) {
|
|
|
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
|
|
|
|
|
|
|
flowA := NewFlowConn(context.Background(), testConn.SideA(), []proxy.MacVerifier{testMac, testMac2}, []proxy.MacGenerator{testMac, testMac2})
|
|
|
|
|
|
|
|
err := flowA.Consume(context.Background(), testPacket)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
n, err := testConn.SideB().Read(buf)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, len(testContent)+4+4+4, n)
|
|
|
|
assert.Equal(t, "abcdefgh", string(buf[n-8:n]))
|
2020-10-25 22:05:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlow_Produce(t *testing.T) {
|
|
|
|
testContent := "A test string is the content of this packet."
|
2021-05-13 22:26:03 +01:00
|
|
|
testMarshalled := []byte("0000" + testContent + "abcd")
|
2020-10-25 22:05:35 +00:00
|
|
|
binary.LittleEndian.PutUint32(testMarshalled, uint32(len(testMarshalled)-4))
|
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
testMac := mocks.AlmostUselessMac("abcd")
|
|
|
|
testMac2 := mocks.AlmostUselessMac("efgh")
|
2020-10-25 22:05:35 +00:00
|
|
|
|
|
|
|
t.Run("Length", func(t *testing.T) {
|
2020-11-27 20:17:59 +00:00
|
|
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
2020-10-25 22:05:35 +00:00
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
flowA := NewFlowConn(context.Background(), testConn.SideA(), []proxy.MacVerifier{testMac}, []proxy.MacGenerator{testMac})
|
2020-10-25 22:05:35 +00:00
|
|
|
|
|
|
|
_, err := testConn.SideB().Write(testMarshalled)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
p, err := flowA.Produce(context.Background())
|
2020-10-25 22:12:38 +00:00
|
|
|
require.Nil(t, err)
|
2020-11-27 20:17:59 +00:00
|
|
|
assert.Equal(t, len(testContent), len(p.Contents()))
|
2020-10-25 22:12:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Value", func(t *testing.T) {
|
2020-11-27 20:17:59 +00:00
|
|
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
2020-10-25 22:12:38 +00:00
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
flowA := NewFlowConn(context.Background(), testConn.SideA(), []proxy.MacVerifier{testMac}, []proxy.MacGenerator{testMac})
|
|
|
|
|
|
|
|
_, err := testConn.SideB().Write(testMarshalled)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
p, err := flowA.Produce(context.Background())
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, testContent, string(p.Contents()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MultipleVerifiersStrip", func(t *testing.T) {
|
|
|
|
testContent := "A test string is the content of this packet."
|
|
|
|
testMarshalled := []byte("0000" + testContent + "abcdefgh")
|
|
|
|
binary.LittleEndian.PutUint32(testMarshalled, uint32(len(testMarshalled)-4))
|
|
|
|
|
|
|
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
|
|
|
|
|
|
|
flowA := NewFlowConn(context.Background(), testConn.SideA(), []proxy.MacVerifier{testMac, testMac2}, []proxy.MacGenerator{testMac, testMac2})
|
2020-10-25 22:12:38 +00:00
|
|
|
|
|
|
|
_, err := testConn.SideB().Write(testMarshalled)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-05-13 22:26:03 +01:00
|
|
|
p, err := flowA.Produce(context.Background())
|
2020-10-25 22:05:35 +00:00
|
|
|
require.Nil(t, err)
|
2020-11-27 20:17:59 +00:00
|
|
|
assert.Equal(t, testContent, string(p.Contents()))
|
2020-10-25 21:44:56 +00:00
|
|
|
})
|
|
|
|
}
|