1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/app/proxyman/mux/mux_test.go

143 lines
3.9 KiB
Go
Raw Normal View History

2017-04-02 03:48:30 -04:00
package mux_test
import (
"context"
2017-05-02 16:23:07 -04:00
"io"
2017-04-02 03:48:30 -04:00
"testing"
. "v2ray.com/core/app/proxyman/mux"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2017-05-02 16:23:07 -04:00
"v2ray.com/core/common/protocol"
2017-04-02 03:48:30 -04:00
"v2ray.com/core/transport/ray"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2017-04-02 03:48:30 -04:00
)
2017-05-02 16:23:07 -04:00
func readAll(reader buf.Reader) (buf.MultiBuffer, error) {
2017-11-08 18:55:28 -05:00
var mb buf.MultiBuffer
2017-05-02 16:23:07 -04:00
for {
2017-11-09 16:33:15 -05:00
b, err := reader.ReadMultiBuffer()
2017-05-02 16:23:07 -04:00
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
mb.AppendMulti(b)
}
return mb, nil
}
2017-04-02 03:48:30 -04:00
func TestReaderWriter(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-02 03:48:30 -04:00
stream := ray.NewStream(context.Background())
2017-04-19 05:17:41 -04:00
dest := net.TCPDestination(net.DomainAddress("v2ray.com"), 80)
2017-05-02 16:23:07 -04:00
writer := NewWriter(1, dest, stream, protocol.TransferTypeStream)
2017-04-02 03:48:30 -04:00
2017-04-19 05:17:41 -04:00
dest2 := net.TCPDestination(net.LocalHostIP, 443)
2017-05-02 16:23:07 -04:00
writer2 := NewWriter(2, dest2, stream, protocol.TransferTypeStream)
2017-04-19 05:17:41 -04:00
dest3 := net.TCPDestination(net.LocalHostIPv6, 18374)
2017-05-02 16:23:07 -04:00
writer3 := NewWriter(3, dest3, stream, protocol.TransferTypeStream)
2017-04-19 05:17:41 -04:00
writePayload := func(writer *Writer, payload ...byte) error {
b := buf.New()
b.Append(payload)
2017-11-09 16:33:15 -05:00
return writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
2017-04-19 05:17:41 -04:00
}
2017-10-24 10:15:35 -04:00
assert(writePayload(writer, 'a', 'b', 'c', 'd'), IsNil)
assert(writePayload(writer2), IsNil)
2017-04-19 05:17:41 -04:00
2017-10-24 10:15:35 -04:00
assert(writePayload(writer, 'e', 'f', 'g', 'h'), IsNil)
assert(writePayload(writer3, 'x'), IsNil)
2017-04-02 03:48:30 -04:00
writer.Close()
2017-04-19 05:17:41 -04:00
writer3.Close()
2017-10-24 10:15:35 -04:00
assert(writePayload(writer2, 'y'), IsNil)
2017-04-19 05:17:41 -04:00
writer2.Close()
2017-04-02 03:48:30 -04:00
2017-11-09 16:33:15 -05:00
bytesReader := buf.NewBufferedReader(stream)
2017-05-02 16:23:07 -04:00
streamReader := NewStreamReader(bytesReader)
2017-10-27 10:32:00 -04:00
meta, err := ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
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))
2017-04-02 03:48:30 -04:00
2017-05-02 16:23:07 -04:00
data, err := readAll(streamReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "abcd")
2017-04-02 03:48:30 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
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)
2017-04-19 05:17:41 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep))
assert(meta.SessionID, Equals, uint16(1))
assert(byte(meta.Option), Equals, byte(1))
2017-04-19 05:17:41 -04:00
2017-05-02 16:23:07 -04:00
data, err = readAll(streamReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "efgh")
2017-04-19 05:17:41 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
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)
2017-04-19 05:17:41 -04:00
2017-05-02 16:23:07 -04:00
data, err = readAll(streamReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "x")
2017-04-19 05:17:41 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
assert(meta.SessionID, Equals, uint16(1))
assert(byte(meta.Option), Equals, byte(0))
2017-04-19 05:17:41 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
assert(meta.SessionID, Equals, uint16(3))
assert(byte(meta.Option), Equals, byte(0))
2017-04-19 05:17:41 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep))
assert(meta.SessionID, Equals, uint16(2))
assert(byte(meta.Option), Equals, byte(1))
2017-04-19 05:17:41 -04:00
2017-05-02 16:23:07 -04:00
data, err = readAll(streamReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "y")
2017-04-19 05:17:41 -04:00
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd))
assert(meta.SessionID, Equals, uint16(2))
assert(byte(meta.Option), Equals, byte(0))
2017-04-19 05:17:41 -04:00
stream.Close()
2017-10-27 10:32:00 -04:00
meta, err = ReadMetadata(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNotNil)
assert(meta, IsNil)
2017-04-02 03:48:30 -04:00
}