1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 10:37:09 -05:00
v2fly/proxy/shadowsocks/protocol_test.go

137 lines
4.4 KiB
Go
Raw Normal View History

2016-01-29 09:09:51 -05:00
package shadowsocks_test
import (
2016-06-03 18:38:22 -04:00
"io"
2016-01-29 09:09:51 -05:00
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
2016-01-29 09:09:51 -05:00
. "github.com/v2ray/v2ray-core/proxy/shadowsocks"
"github.com/v2ray/v2ray-core/testing/assert"
2016-02-04 16:10:52 -05:00
"github.com/v2ray/v2ray-core/transport"
2016-01-29 09:09:51 -05:00
)
func TestNormalRequestParsing(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-29 09:09:51 -05:00
buffer := alloc.NewSmallBuffer().Clear()
buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80)
2016-01-29 15:55:42 -05:00
request, err := ReadRequest(buffer, nil, false)
2016-01-29 09:09:51 -05:00
assert.Error(err).IsNil()
2016-05-24 09:29:08 -04:00
assert.Address(request.Address).Equals(v2net.LocalHostIP)
assert.Port(request.Port).Equals(v2net.Port(80))
2016-01-29 09:09:51 -05:00
assert.Bool(request.OTA).IsFalse()
}
2016-02-04 16:10:52 -05:00
func TestEmptyPayload(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
buffer := alloc.NewSmallBuffer().Clear()
_, err := ReadRequest(buffer, nil, false)
2016-06-03 18:38:22 -04:00
assert.Error(err).Equals(io.EOF)
2016-02-04 16:10:52 -05:00
}
func TestSingleBytePayload(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1)
_, err := ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
}
func TestWrongAddressType(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
buffer := alloc.NewSmallBuffer().Clear().AppendBytes(5)
_, err := ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
}
func TestInsufficientAddressRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1, 1)
_, err := ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
buffer = alloc.NewSmallBuffer().Clear().AppendBytes(4, 1)
_, err = ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
buffer = alloc.NewSmallBuffer().Clear().AppendBytes(3, 255, 1)
_, err = ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
}
func TestInsufficientPortRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1, 1, 2, 3, 4, 5)
_, err := ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
}
2016-01-29 09:09:51 -05:00
func TestOTARequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-29 09:09:51 -05:00
buffer := alloc.NewSmallBuffer().Clear()
buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 0)
2016-01-29 09:09:51 -05:00
auth := NewAuthenticator(HeaderKeyGenerator(
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
2016-01-29 15:55:42 -05:00
request, err := ReadRequest(buffer, auth, false)
2016-01-29 09:09:51 -05:00
assert.Error(err).IsNil()
2016-05-24 09:29:08 -04:00
assert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
2016-01-29 09:09:51 -05:00
assert.Bool(request.OTA).IsTrue()
}
2016-01-29 15:55:42 -05:00
2016-02-04 16:10:52 -05:00
func TestInvalidOTARequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-04 16:10:52 -05:00
buffer := alloc.NewSmallBuffer().Clear()
buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 1)
auth := NewAuthenticator(HeaderKeyGenerator(
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
_, err := ReadRequest(buffer, auth, false)
assert.Error(err).Equals(proxy.ErrorInvalidAuthentication)
2016-02-04 16:10:52 -05:00
}
2016-01-29 15:55:42 -05:00
func TestUDPRequestParsing(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-29 15:55:42 -05:00
buffer := alloc.NewSmallBuffer().Clear()
buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80, 1, 2, 3, 4, 5, 6)
request, err := ReadRequest(buffer, nil, true)
assert.Error(err).IsNil()
2016-05-24 09:29:08 -04:00
assert.Address(request.Address).Equals(v2net.LocalHostIP)
assert.Port(request.Port).Equals(v2net.Port(80))
2016-01-29 15:55:42 -05:00
assert.Bool(request.OTA).IsFalse()
assert.Bytes(request.UDPPayload.Value).Equals([]byte{1, 2, 3, 4, 5, 6})
}
func TestUDPRequestWithOTA(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-29 15:55:42 -05:00
buffer := alloc.NewSmallBuffer().Clear()
buffer.AppendBytes(
0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
58, 32, 223, 30, 57, 199, 50, 139, 143, 101)
auth := NewAuthenticator(HeaderKeyGenerator(
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
request, err := ReadRequest(buffer, auth, true)
assert.Error(err).IsNil()
2016-05-24 09:29:08 -04:00
assert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
2016-01-29 15:55:42 -05:00
assert.Bool(request.OTA).IsTrue()
assert.Bytes(request.UDPPayload.Value).Equals([]byte{
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
}