1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/common/mux/mux_test.go

142 lines
3.8 KiB
Go
Raw Normal View History

2017-04-02 03:48:30 -04:00
package mux_test
import (
2017-05-02 16:23:07 -04:00
"io"
2017-04-02 03:48:30 -04:00
"testing"
2019-02-02 16:19:30 -05:00
"v2ray.com/core/common"
2017-04-02 03:48:30 -04:00
"v2ray.com/core/common/buf"
2018-10-23 06:21:12 -04:00
. "v2ray.com/core/common/mux"
2017-04-02 03:48:30 -04:00
"v2ray.com/core/common/net"
2017-05-02 16:23:07 -04:00
"v2ray.com/core/common/protocol"
2018-04-16 18:40:27 -04:00
"v2ray.com/core/transport/pipe"
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
}
2018-11-17 03:12:20 -05:00
mb = append(mb, b...)
2017-05-02 16:23:07 -04:00
}
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
2018-05-25 06:50:33 -04:00
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
2017-04-19 05:17:41 -04:00
dest := net.TCPDestination(net.DomainAddress("v2ray.com"), 80)
2018-04-16 18:40:27 -04:00
writer := NewWriter(1, dest, pWriter, protocol.TransferTypeStream)
2017-04-02 03:48:30 -04:00
2017-04-19 05:17:41 -04:00
dest2 := net.TCPDestination(net.LocalHostIP, 443)
2018-04-16 18:40:27 -04:00
writer2 := NewWriter(2, dest2, pWriter, protocol.TransferTypeStream)
2017-04-19 05:17:41 -04:00
dest3 := net.TCPDestination(net.LocalHostIPv6, 18374)
2018-04-16 18:40:27 -04:00
writer3 := NewWriter(3, dest3, pWriter, protocol.TransferTypeStream)
2017-04-19 05:17:41 -04:00
writePayload := func(writer *Writer, payload ...byte) error {
b := buf.New()
2018-04-19 16:56:55 -04:00
b.Write(payload)
2018-11-16 05:08:12 -05:00
return writer.WriteMultiBuffer(buf.MultiBuffer{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
2018-04-20 18:54:53 -04:00
bytesReader := &buf.BufferedReader{Reader: pReader}
2017-05-02 16:23:07 -04:00
2018-09-26 07:01:12 -04:00
var meta FrameMetadata
2018-10-14 02:05:23 -04:00
err := meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-04-05 15:09:57 -04:00
data, err := readAll(NewStreamReader(bytesReader))
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "abcd")
2017-04-02 03:48:30 -04:00
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-04-05 15:09:57 -04:00
data, err = readAll(NewStreamReader(bytesReader))
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "efgh")
2017-04-19 05:17:41 -04:00
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-04-05 15:09:57 -04:00
data, err = readAll(NewStreamReader(bytesReader))
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "x")
2017-04-19 05:17:41 -04:00
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-04-05 15:09:57 -04:00
data, err = readAll(NewStreamReader(bytesReader))
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
assert(len(data), Equals, 1)
assert(data[0].String(), Equals, "y")
2017-04-19 05:17:41 -04:00
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
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
2018-04-16 18:40:27 -04:00
pWriter.Close()
2017-04-19 05:17:41 -04:00
2018-10-14 02:05:23 -04:00
err = meta.Unmarshal(bytesReader)
2017-10-24 10:15:35 -04:00
assert(err, IsNotNil)
2017-04-02 03:48:30 -04:00
}