github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/trojan/protocol_test.go (about) 1 package trojan_test 2 3 import ( 4 "crypto/rand" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 9 "github.com/v2fly/v2ray-core/v5/common" 10 "github.com/v2fly/v2ray-core/v5/common/buf" 11 "github.com/v2fly/v2ray-core/v5/common/net" 12 "github.com/v2fly/v2ray-core/v5/common/protocol" 13 . "github.com/v2fly/v2ray-core/v5/proxy/trojan" 14 ) 15 16 func toAccount(a *Account) protocol.Account { 17 account, err := a.AsAccount() 18 common.Must(err) 19 return account 20 } 21 22 func TestTCPRequest(t *testing.T) { 23 user := &protocol.MemoryUser{ 24 Email: "love@v2fly.org", 25 Account: toAccount(&Account{ 26 Password: "password", 27 }), 28 } 29 payload := []byte("test string") 30 data := buf.New() 31 common.Must2(data.Write(payload)) 32 33 buffer := buf.New() 34 defer buffer.Release() 35 36 destination := net.Destination{Network: net.Network_TCP, Address: net.LocalHostIP, Port: 1234} 37 writer := &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)} 38 common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data})) 39 40 reader := &ConnReader{Reader: buffer} 41 common.Must(reader.ParseHeader()) 42 43 if r := cmp.Diff(reader.Target, destination); r != "" { 44 t.Error("destination: ", r) 45 } 46 47 decodedData, err := reader.ReadMultiBuffer() 48 common.Must(err) 49 if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" { 50 t.Error("data: ", r) 51 } 52 } 53 54 func TestUDPRequest(t *testing.T) { 55 user := &protocol.MemoryUser{ 56 Email: "love@v2fly.org", 57 Account: toAccount(&Account{ 58 Password: "password", 59 }), 60 } 61 payload := []byte("test string") 62 data := buf.New() 63 common.Must2(data.Write(payload)) 64 65 buffer := buf.New() 66 defer buffer.Release() 67 68 destination := net.Destination{Network: net.Network_UDP, Address: net.LocalHostIP, Port: 1234} 69 writer := &PacketWriter{Writer: &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)}, Target: destination} 70 common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data})) 71 72 connReader := &ConnReader{Reader: buffer} 73 common.Must(connReader.ParseHeader()) 74 75 packetReader := &PacketReader{Reader: connReader} 76 p, err := packetReader.ReadMultiBufferWithMetadata() 77 common.Must(err) 78 79 if p.Buffer.IsEmpty() { 80 t.Error("no request data") 81 } 82 83 if r := cmp.Diff(p.Target, destination); r != "" { 84 t.Error("destination: ", r) 85 } 86 87 mb, decoded := buf.SplitFirst(p.Buffer) 88 buf.ReleaseMulti(mb) 89 90 if r := cmp.Diff(decoded.Bytes(), payload); r != "" { 91 t.Error("data: ", r) 92 } 93 } 94 95 func TestLargeUDPRequest(t *testing.T) { 96 user := &protocol.MemoryUser{ 97 Email: "love@v2fly.org", 98 Account: toAccount(&Account{ 99 Password: "password", 100 }), 101 } 102 103 payload := make([]byte, 4096) 104 common.Must2(rand.Read(payload)) 105 data := buf.NewWithSize(int32(len(payload))) 106 common.Must2(data.Write(payload)) 107 108 buffer := buf.NewWithSize(2*data.Len() + 1) 109 defer buffer.Release() 110 111 destination := net.Destination{Network: net.Network_UDP, Address: net.LocalHostIP, Port: 1234} 112 writer := &PacketWriter{Writer: &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)}, Target: destination} 113 common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data, data})) 114 115 connReader := &ConnReader{Reader: buffer} 116 common.Must(connReader.ParseHeader()) 117 118 packetReader := &PacketReader{Reader: connReader} 119 for i := 0; i < 2; i++ { 120 p, err := packetReader.ReadMultiBufferWithMetadata() 121 common.Must(err) 122 123 if p.Buffer.IsEmpty() { 124 t.Error("no request data") 125 } 126 127 if r := cmp.Diff(p.Target, destination); r != "" { 128 t.Error("destination: ", r) 129 } 130 131 mb, decoded := buf.SplitFirst(p.Buffer) 132 buf.ReleaseMulti(mb) 133 134 if r := cmp.Diff(decoded.Bytes(), payload); r != "" { 135 t.Error("data: ", r) 136 } 137 } 138 }