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

197 lines
4.1 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-10 09:02:28 -05:00
"github.com/google/go-cmp/cmp"
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-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) {
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
}
2019-02-10 09:02:28 -05:00
common.Must(writePayload(writer, 'a', 'b', 'c', 'd'))
common.Must(writePayload(writer2))
2017-04-19 05:17:41 -04:00
2019-02-10 09:02:28 -05:00
common.Must(writePayload(writer, 'e', 'f', 'g', 'h'))
common.Must(writePayload(writer3, 'x'))
2017-04-02 03:48:30 -04:00
writer.Close()
2017-04-19 05:17:41 -04:00
writer3.Close()
2019-02-10 09:02:28 -05:00
common.Must(writePayload(writer2, 'y'))
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
2019-02-10 09:02:28 -05:00
{
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)
}
}
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
2019-02-10 09:02:28 -05:00
{
var meta FrameMetadata
err := meta.Unmarshal(bytesReader)
if err == nil {
t.Error("nil error")
}
}
2017-04-02 03:48:30 -04:00
}