|
|
|
@ -4,13 +4,14 @@ import (
|
|
|
|
|
"io"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
|
. "v2ray.com/core/common/mux"
|
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
|
"v2ray.com/core/transport/pipe"
|
|
|
|
|
. "v2ray.com/ext/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func readAll(reader buf.Reader) (buf.MultiBuffer, error) {
|
|
|
|
@ -29,8 +30,6 @@ func readAll(reader buf.Reader) (buf.MultiBuffer, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReaderWriter(t *testing.T) {
|
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
|
|
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
|
|
|
|
|
|
|
|
|
|
dest := net.TCPDestination(net.DomainAddress("v2ray.com"), 80)
|
|
|
|
@ -48,94 +47,150 @@ func TestReaderWriter(t *testing.T) {
|
|
|
|
|
return writer.WriteMultiBuffer(buf.MultiBuffer{b})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(writePayload(writer, 'a', 'b', 'c', 'd'), IsNil)
|
|
|
|
|
assert(writePayload(writer2), IsNil)
|
|
|
|
|
common.Must(writePayload(writer, 'a', 'b', 'c', 'd'))
|
|
|
|
|
common.Must(writePayload(writer2))
|
|
|
|
|
|
|
|
|
|
assert(writePayload(writer, 'e', 'f', 'g', 'h'), IsNil)
|
|
|
|
|
assert(writePayload(writer3, 'x'), IsNil)
|
|
|
|
|
common.Must(writePayload(writer, 'e', 'f', 'g', 'h'))
|
|
|
|
|
common.Must(writePayload(writer3, 'x'))
|
|
|
|
|
|
|
|
|
|
writer.Close()
|
|
|
|
|
writer3.Close()
|
|
|
|
|
|
|
|
|
|
assert(writePayload(writer2, 'y'), IsNil)
|
|
|
|
|
common.Must(writePayload(writer2, 'y'))
|
|
|
|
|
writer2.Close()
|
|
|
|
|
|
|
|
|
|
bytesReader := &buf.BufferedReader{Reader: pReader}
|
|
|
|
|
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
err := meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(1))
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusNew))
|
|
|
|
|
assert(meta.Target, Equals, dest)
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(OptionData))
|
|
|
|
|
|
|
|
|
|
data, err := readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(len(data), Equals, 1)
|
|
|
|
|
assert(data[0].String(), Equals, "abcd")
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusNew))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(2))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(0))
|
|
|
|
|
assert(meta.Target, Equals, dest2)
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(1))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(1))
|
|
|
|
|
|
|
|
|
|
data, err = readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(len(data), Equals, 1)
|
|
|
|
|
assert(data[0].String(), Equals, "efgh")
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusNew))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(3))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(1))
|
|
|
|
|
assert(meta.Target, Equals, dest3)
|
|
|
|
|
|
|
|
|
|
data, err = readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(len(data), Equals, 1)
|
|
|
|
|
assert(data[0].String(), Equals, "x")
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(1))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(0))
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(3))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(0))
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(2))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(1))
|
|
|
|
|
|
|
|
|
|
data, err = readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(len(data), Equals, 1)
|
|
|
|
|
assert(data[0].String(), Equals, "y")
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
common.Must(err)
|
|
|
|
|
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
|
|
|
|
|
assert(meta.SessionID, Equals, uint16(2))
|
|
|
|
|
assert(byte(meta.Option), Equals, byte(0))
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 1,
|
|
|
|
|
SessionStatus: SessionStatusNew,
|
|
|
|
|
Target: dest,
|
|
|
|
|
Option: OptionData,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("metadata: ", r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
if s := data.String(); s != "abcd" {
|
|
|
|
|
t.Error("data: ", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionStatus: SessionStatusNew,
|
|
|
|
|
SessionID: 2,
|
|
|
|
|
Option: 0,
|
|
|
|
|
Target: dest2,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 1,
|
|
|
|
|
SessionStatus: SessionStatusKeep,
|
|
|
|
|
Option: 1,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
if s := data.String(); s != "efgh" {
|
|
|
|
|
t.Error("data: ", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 3,
|
|
|
|
|
SessionStatus: SessionStatusNew,
|
|
|
|
|
Option: 1,
|
|
|
|
|
Target: dest3,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
if s := data.String(); s != "x" {
|
|
|
|
|
t.Error("data: ", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 1,
|
|
|
|
|
SessionStatus: SessionStatusEnd,
|
|
|
|
|
Option: 0,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 3,
|
|
|
|
|
SessionStatus: SessionStatusEnd,
|
|
|
|
|
Option: 0,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 2,
|
|
|
|
|
SessionStatus: SessionStatusKeep,
|
|
|
|
|
Option: 1,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := readAll(NewStreamReader(bytesReader))
|
|
|
|
|
common.Must(err)
|
|
|
|
|
if s := data.String(); s != "y" {
|
|
|
|
|
t.Error("data: ", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
common.Must(meta.Unmarshal(bytesReader))
|
|
|
|
|
if r := cmp.Diff(meta, FrameMetadata{
|
|
|
|
|
SessionID: 2,
|
|
|
|
|
SessionStatus: SessionStatusEnd,
|
|
|
|
|
Option: 0,
|
|
|
|
|
}); r != "" {
|
|
|
|
|
t.Error("meta: ", r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pWriter.Close()
|
|
|
|
|
|
|
|
|
|
err = meta.Unmarshal(bytesReader)
|
|
|
|
|
assert(err, IsNotNil)
|
|
|
|
|
{
|
|
|
|
|
var meta FrameMetadata
|
|
|
|
|
err := meta.Unmarshal(bytesReader)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Error("nil error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|