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