mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-26 12:05:35 -05:00
use buffer for reading user id in socks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package socks_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"v2ray.com/core/common/buf"
|
||||
@@ -33,3 +34,76 @@ func TestUDPEncoding(t *testing.T) {
|
||||
assert(err, IsNil)
|
||||
assert(decodedPayload[0].Bytes(), Equals, content)
|
||||
}
|
||||
|
||||
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 passowrd ", 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user