github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/socks/protocol_test.go (about) 1 package socks_test 2 3 import ( 4 "bytes" 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/socks" 14 ) 15 16 func TestUDPEncoding(t *testing.T) { 17 b := buf.New() 18 19 request := &protocol.RequestHeader{ 20 Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}), 21 Port: 1024, 22 } 23 writer := &buf.SequentialWriter{Writer: NewUDPWriter(request, b)} 24 25 content := []byte{'a'} 26 payload := buf.New() 27 payload.Write(content) 28 common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{payload})) 29 30 reader := NewUDPReader(b) 31 32 decodedPayload, err := reader.ReadMultiBuffer() 33 common.Must(err) 34 if r := cmp.Diff(decodedPayload[0].Bytes(), content); r != "" { 35 t.Error(r) 36 } 37 } 38 39 func TestReadUsernamePassword(t *testing.T) { 40 testCases := []struct { 41 Input []byte 42 Username string 43 Password string 44 Error bool 45 }{ 46 { 47 Input: []byte{0x05, 0x01, 'a', 0x02, 'b', 'c'}, 48 Username: "a", 49 Password: "bc", 50 }, 51 { 52 Input: []byte{0x05, 0x18, 'a', 0x02, 'b', 'c'}, 53 Error: true, 54 }, 55 } 56 57 for _, testCase := range testCases { 58 reader := bytes.NewReader(testCase.Input) 59 username, password, err := ReadUsernamePassword(reader) 60 if testCase.Error { 61 if err == nil { 62 t.Error("for input: ", testCase.Input, " expect error, but actually nil") 63 } 64 } else { 65 if err != nil { 66 t.Error("for input: ", testCase.Input, " expect no error, but actually ", err.Error()) 67 } 68 if testCase.Username != username { 69 t.Error("for input: ", testCase.Input, " expect username ", testCase.Username, " but actually ", username) 70 } 71 if testCase.Password != password { 72 t.Error("for input: ", testCase.Input, " expect password ", testCase.Password, " but actually ", password) 73 } 74 } 75 } 76 } 77 78 func TestReadUntilNull(t *testing.T) { 79 testCases := []struct { 80 Input []byte 81 Output string 82 Error bool 83 }{ 84 { 85 Input: []byte{'a', 'b', 0x00}, 86 Output: "ab", 87 }, 88 { 89 Input: []byte{'a'}, 90 Error: true, 91 }, 92 } 93 94 for _, testCase := range testCases { 95 reader := bytes.NewReader(testCase.Input) 96 value, err := ReadUntilNull(reader) 97 if testCase.Error { 98 if err == nil { 99 t.Error("for input: ", testCase.Input, " expect error, but actually nil") 100 } 101 } else { 102 if err != nil { 103 t.Error("for input: ", testCase.Input, " expect no error, but actually ", err.Error()) 104 } 105 if testCase.Output != value { 106 t.Error("for input: ", testCase.Input, " expect output ", testCase.Output, " but actually ", value) 107 } 108 } 109 } 110 } 111 112 func BenchmarkReadUsernamePassword(b *testing.B) { 113 input := []byte{0x05, 0x01, 'a', 0x02, 'b', 'c'} 114 buffer := buf.New() 115 buffer.Write(input) 116 117 b.ResetTimer() 118 for i := 0; i < b.N; i++ { 119 _, _, err := ReadUsernamePassword(buffer) 120 common.Must(err) 121 buffer.Clear() 122 buffer.Extend(int32(len(input))) 123 } 124 }