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