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"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
|
. "github.com/v2fly/v2ray-core/v5/common/mux"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/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
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
dest := net.TCPDestination(net.DomainAddress("v2fly.org"), 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
|
|
|
}
|