github.com/xraypb/xray-core@v1.6.6/proxy/trojan/protocol_test.go (about)

     1  package trojan_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/xraypb/xray-core/common"
     8  	"github.com/xraypb/xray-core/common/buf"
     9  	"github.com/xraypb/xray-core/common/net"
    10  	"github.com/xraypb/xray-core/common/protocol"
    11  	. "github.com/xraypb/xray-core/proxy/trojan"
    12  )
    13  
    14  func toAccount(a *Account) protocol.Account {
    15  	account, err := a.AsAccount()
    16  	common.Must(err)
    17  	return account
    18  }
    19  
    20  func TestTCPRequest(t *testing.T) {
    21  	user := &protocol.MemoryUser{
    22  		Email: "love@example.com",
    23  		Account: toAccount(&Account{
    24  			Password: "password",
    25  		}),
    26  	}
    27  	payload := []byte("test string")
    28  	data := buf.New()
    29  	common.Must2(data.Write(payload))
    30  
    31  	buffer := buf.New()
    32  	defer buffer.Release()
    33  
    34  	destination := net.Destination{Network: net.Network_TCP, Address: net.LocalHostIP, Port: 1234}
    35  	writer := &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)}
    36  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
    37  
    38  	reader := &ConnReader{Reader: buffer}
    39  	common.Must(reader.ParseHeader())
    40  
    41  	if r := cmp.Diff(reader.Target, destination); r != "" {
    42  		t.Error("destination: ", r)
    43  	}
    44  
    45  	decodedData, err := reader.ReadMultiBuffer()
    46  	common.Must(err)
    47  	if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
    48  		t.Error("data: ", r)
    49  	}
    50  }
    51  
    52  func TestUDPRequest(t *testing.T) {
    53  	user := &protocol.MemoryUser{
    54  		Email: "love@example.com",
    55  		Account: toAccount(&Account{
    56  			Password: "password",
    57  		}),
    58  	}
    59  	payload := []byte("test string")
    60  	data := buf.New()
    61  	common.Must2(data.Write(payload))
    62  
    63  	buffer := buf.New()
    64  	defer buffer.Release()
    65  
    66  	destination := net.Destination{Network: net.Network_UDP, Address: net.LocalHostIP, Port: 1234}
    67  	writer := &PacketWriter{Writer: &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)}, Target: destination}
    68  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
    69  
    70  	connReader := &ConnReader{Reader: buffer}
    71  	common.Must(connReader.ParseHeader())
    72  
    73  	packetReader := &PacketReader{Reader: connReader}
    74  	mb, err := packetReader.ReadMultiBuffer()
    75  	common.Must(err)
    76  
    77  	if mb.IsEmpty() {
    78  		t.Error("no request data")
    79  	}
    80  
    81  	mb2, b := buf.SplitFirst(mb)
    82  	defer buf.ReleaseMulti(mb2)
    83  
    84  	dest := *b.UDP
    85  	if r := cmp.Diff(dest, destination); r != "" {
    86  		t.Error("destination: ", r)
    87  	}
    88  
    89  	if r := cmp.Diff(b.Bytes(), payload); r != "" {
    90  		t.Error("data: ", r)
    91  	}
    92  }