2017-01-08 15:31:28 +00:00
|
|
|
package socks_test
|
|
|
|
|
|
|
|
import (
|
2018-02-22 09:31:08 +00:00
|
|
|
"bytes"
|
2017-01-08 15:31:28 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/net"
|
2018-02-22 09:31:08 +00:00
|
|
|
_ "v2ray.com/core/common/net/testing"
|
2017-01-08 15:31:28 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
. "v2ray.com/core/proxy/socks"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2017-01-08 15:31:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUDPEncoding(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2017-01-08 15:31:28 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2017-04-21 12:51:09 +00:00
|
|
|
writer := buf.NewSequentialWriter(NewUDPWriter(request, b))
|
2017-01-08 15:31:28 +00:00
|
|
|
|
|
|
|
content := []byte{'a'}
|
|
|
|
payload := buf.New()
|
|
|
|
payload.Append(content)
|
2017-11-09 21:33:15 +00:00
|
|
|
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil)
|
2017-01-08 15:31:28 +00:00
|
|
|
|
|
|
|
reader := NewUDPReader(b)
|
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
decodedPayload, err := reader.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(decodedPayload[0].Bytes(), Equals, content)
|
2017-01-08 15:31:28 +00:00
|
|
|
}
|
2018-02-22 09:31:08 +00:00
|
|
|
|
|
|
|
func TestReadAddress(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
data := []struct {
|
|
|
|
AddrType byte
|
|
|
|
Input []byte
|
|
|
|
Address net.Address
|
|
|
|
Port net.Port
|
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
AddrType: 0,
|
|
|
|
Input: []byte{0, 0, 0, 0},
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AddrType: 1,
|
|
|
|
Input: []byte{0, 0, 0, 0, 0, 53},
|
|
|
|
Address: net.IPAddress([]byte{0, 0, 0, 0}),
|
|
|
|
Port: net.Port(53),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AddrType: 4,
|
|
|
|
Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80},
|
|
|
|
Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
|
|
|
|
Port: net.Port(80),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AddrType: 3,
|
|
|
|
Input: []byte{9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 80},
|
|
|
|
Address: net.DomainAddress("v2ray.com"),
|
|
|
|
Port: net.Port(80),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AddrType: 3,
|
|
|
|
Input: []byte{9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0},
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range data {
|
|
|
|
b := buf.New()
|
|
|
|
addr, port, err := ReadAddress(b, tc.AddrType, bytes.NewBuffer(tc.Input))
|
|
|
|
b.Release()
|
|
|
|
if tc.Error {
|
|
|
|
assert(err, IsNotNil)
|
|
|
|
} else {
|
|
|
|
assert(addr, Equals, tc.Address)
|
|
|
|
assert(port, Equals, tc.Port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|