You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/mux/mux_test.go

141 lines
3.8 KiB

8 years ago
package mux_test
import (
"io"
8 years ago
"testing"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/mux"
8 years ago
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
7 years ago
"v2ray.com/core/transport/pipe"
. "v2ray.com/ext/assert"
8 years ago
)
func readAll(reader buf.Reader) (buf.MultiBuffer, error) {
var mb buf.MultiBuffer
for {
b, err := reader.ReadMultiBuffer()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
mb = append(mb, b...)
}
return mb, nil
}
8 years ago
func TestReaderWriter(t *testing.T) {
assert := With(t)
8 years ago
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
dest := net.TCPDestination(net.DomainAddress("v2ray.com"), 80)
7 years ago
writer := NewWriter(1, dest, pWriter, protocol.TransferTypeStream)
8 years ago
dest2 := net.TCPDestination(net.LocalHostIP, 443)
7 years ago
writer2 := NewWriter(2, dest2, pWriter, protocol.TransferTypeStream)
dest3 := net.TCPDestination(net.LocalHostIPv6, 18374)
7 years ago
writer3 := NewWriter(3, dest3, pWriter, protocol.TransferTypeStream)
writePayload := func(writer *Writer, payload ...byte) error {
b := buf.New()
b.Write(payload)
return writer.WriteMultiBuffer(buf.MultiBuffer{b})
}
assert(writePayload(writer, 'a', 'b', 'c', 'd'), IsNil)
assert(writePayload(writer2), IsNil)
assert(writePayload(writer, 'e', 'f', 'g', 'h'), IsNil)
assert(writePayload(writer3, 'x'), IsNil)
8 years ago
writer.Close()
writer3.Close()
assert(writePayload(writer2, 'y'), IsNil)
writer2.Close()
8 years ago
bytesReader := &buf.BufferedReader{Reader: pReader}
var meta FrameMetadata
err := meta.Unmarshal(bytesReader)
assert(err, IsNil)
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))
8 years ago
7 years ago
data, err := readAll(NewStreamReader(bytesReader))
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "abcd")
8 years ago
err = meta.Unmarshal(bytesReader)
assert(err, IsNil)
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)
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep))
assert(meta.SessionID, Equals, uint16(1))
assert(byte(meta.Option), Equals, byte(1))
7 years ago
data, err = readAll(NewStreamReader(bytesReader))
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "efgh")
err = meta.Unmarshal(bytesReader)
assert(err, IsNil)
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)
7 years ago
data, err = readAll(NewStreamReader(bytesReader))
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "x")
err = meta.Unmarshal(bytesReader)
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
assert(meta.SessionID, Equals, uint16(1))
assert(byte(meta.Option), Equals, byte(0))
err = meta.Unmarshal(bytesReader)
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
assert(meta.SessionID, Equals, uint16(3))
assert(byte(meta.Option), Equals, byte(0))
err = meta.Unmarshal(bytesReader)
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep))
assert(meta.SessionID, Equals, uint16(2))
assert(byte(meta.Option), Equals, byte(1))
7 years ago
data, err = readAll(NewStreamReader(bytesReader))
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "y")
err = meta.Unmarshal(bytesReader)
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
assert(meta.SessionID, Equals, uint16(2))
assert(byte(meta.Option), Equals, byte(0))
7 years ago
pWriter.Close()
err = meta.Unmarshal(bytesReader)
assert(err, IsNotNil)
8 years ago
}