1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-05 17:57:42 -05:00
v2fly/app/proxyman/mux/mux_test.go

143 lines
4.0 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/testing/assert"
"v2ray.com/core/transport/ray"
)
2017-05-02 16:23:07 -04:00
func readAll(reader buf.Reader) (buf.MultiBuffer, error) {
mb := buf.NewMultiBuffer()
for {
b, err := reader.Read()
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) {
assert := assert.On(t)
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)
return writer.Write(buf.NewMultiBufferValue(b))
}
assert.Error(writePayload(writer, 'a', 'b', 'c', 'd')).IsNil()
assert.Error(writePayload(writer2)).IsNil()
assert.Error(writePayload(writer, 'e', 'f', 'g', 'h')).IsNil()
assert.Error(writePayload(writer3, 'x')).IsNil()
2017-04-02 03:48:30 -04:00
writer.Close()
2017-04-19 05:17:41 -04:00
writer3.Close()
assert.Error(writePayload(writer2, 'y')).IsNil()
writer2.Close()
2017-04-02 03:48:30 -04:00
2017-05-02 16:23:07 -04:00
bytesReader := buf.ToBytesReader(stream)
metaReader := NewMetadataReader(bytesReader)
streamReader := NewStreamReader(bytesReader)
meta, err := metaReader.Read()
2017-04-02 03:48:30 -04:00
assert.Error(err).IsNil()
assert.Uint16(meta.SessionID).Equals(1)
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusNew))
assert.Destination(meta.Target).Equals(dest)
assert.Byte(byte(meta.Option)).Equals(byte(OptionData))
2017-05-02 16:23:07 -04:00
data, err := readAll(streamReader)
2017-04-02 03:48:30 -04:00
assert.Error(err).IsNil()
2017-04-19 05:17:41 -04:00
assert.Int(len(data)).Equals(1)
2017-04-15 15:07:23 -04:00
assert.String(data[0].String()).Equals("abcd")
2017-04-02 03:48:30 -04:00
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusNew))
assert.Uint16(meta.SessionID).Equals(2)
assert.Byte(byte(meta.Option)).Equals(0)
assert.Destination(meta.Target).Equals(dest2)
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusKeep))
assert.Uint16(meta.SessionID).Equals(1)
assert.Byte(byte(meta.Option)).Equals(1)
2017-05-02 16:23:07 -04:00
data, err = readAll(streamReader)
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Int(len(data)).Equals(1)
assert.String(data[0].String()).Equals("efgh")
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusNew))
assert.Uint16(meta.SessionID).Equals(3)
assert.Byte(byte(meta.Option)).Equals(1)
assert.Destination(meta.Target).Equals(dest3)
2017-05-02 16:23:07 -04:00
data, err = readAll(streamReader)
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Int(len(data)).Equals(1)
assert.String(data[0].String()).Equals("x")
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-02 03:48:30 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusEnd))
assert.Uint16(meta.SessionID).Equals(1)
2017-04-19 05:17:41 -04:00
assert.Byte(byte(meta.Option)).Equals(0)
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusEnd))
assert.Uint16(meta.SessionID).Equals(3)
assert.Byte(byte(meta.Option)).Equals(0)
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusKeep))
assert.Uint16(meta.SessionID).Equals(2)
assert.Byte(byte(meta.Option)).Equals(1)
2017-05-02 16:23:07 -04:00
data, err = readAll(streamReader)
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Int(len(data)).Equals(1)
assert.String(data[0].String()).Equals("y")
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNil()
assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusEnd))
assert.Uint16(meta.SessionID).Equals(2)
assert.Byte(byte(meta.Option)).Equals(0)
stream.Close()
2017-05-02 16:23:07 -04:00
meta, err = metaReader.Read()
2017-04-19 05:17:41 -04:00
assert.Error(err).IsNotNil()
2017-04-02 03:48:30 -04:00
}