v2ray-core/proxy/shadowsocks/protocol_test.go

137 lines
4.5 KiB
Go
Raw Normal View History

2016-01-29 14:09:51 +00:00
package shadowsocks_test
import (
2016-06-03 22:38:22 +00:00
"io"
2016-01-29 14:09:51 +00: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 14:09:51 +00:00
. "github.com/v2ray/v2ray-core/proxy/shadowsocks"
"github.com/v2ray/v2ray-core/testing/assert"
2016-02-04 21:10:52 +00:00
"github.com/v2ray/v2ray-core/transport"
2016-01-29 14:09:51 +00:00
)
func TestNormalRequestParsing(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-29 14:09:51 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear()
2016-01-29 14:09:51 +00:00
buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80)
2016-01-29 20:55:42 +00:00
request, err := ReadRequest(buffer, nil, false)
2016-01-29 14:09:51 +00:00
assert.Error(err).IsNil()
2016-05-24 13:29:08 +00:00
assert.Address(request.Address).Equals(v2net.LocalHostIP)
assert.Port(request.Port).Equals(v2net.Port(80))
2016-01-29 14:09:51 +00:00
assert.Bool(request.OTA).IsFalse()
}
2016-02-04 21:10:52 +00:00
func TestEmptyPayload(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear()
2016-02-04 21:10:52 +00:00
_, err := ReadRequest(buffer, nil, false)
2016-06-03 22:38:22 +00:00
assert.Error(err).Equals(io.EOF)
2016-02-04 21:10:52 +00:00
}
func TestSingleBytePayload(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear().AppendBytes(1)
2016-02-04 21:10:52 +00:00
_, err := ReadRequest(buffer, nil, false)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(transport.ErrCorruptedPacket)
2016-02-04 21:10:52 +00:00
}
func TestWrongAddressType(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear().AppendBytes(5)
2016-02-04 21:10:52 +00:00
_, err := ReadRequest(buffer, nil, false)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(transport.ErrCorruptedPacket)
2016-02-04 21:10:52 +00:00
}
func TestInsufficientAddressRequest(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear().AppendBytes(1, 1)
2016-02-04 21:10:52 +00:00
_, err := ReadRequest(buffer, nil, false)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(transport.ErrCorruptedPacket)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer = alloc.NewLocalBuffer(2048).Clear().AppendBytes(4, 1)
2016-02-04 21:10:52 +00:00
_, err = ReadRequest(buffer, nil, false)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(transport.ErrCorruptedPacket)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer = alloc.NewLocalBuffer(2048).Clear().AppendBytes(3, 255, 1)
2016-02-04 21:10:52 +00:00
_, err = ReadRequest(buffer, nil, false)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(transport.ErrCorruptedPacket)
2016-02-04 21:10:52 +00:00
}
func TestInsufficientPortRequest(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear().AppendBytes(1, 1, 2, 3, 4, 5)
2016-02-04 21:10:52 +00:00
_, err := ReadRequest(buffer, nil, false)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(transport.ErrCorruptedPacket)
2016-02-04 21:10:52 +00:00
}
2016-01-29 14:09:51 +00:00
func TestOTARequest(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-29 14:09:51 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).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 14:09:51 +00: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 20:55:42 +00:00
request, err := ReadRequest(buffer, auth, false)
2016-01-29 14:09:51 +00:00
assert.Error(err).IsNil()
2016-05-24 13:29:08 +00:00
assert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
2016-01-29 14:09:51 +00:00
assert.Bool(request.OTA).IsTrue()
}
2016-01-29 20:55:42 +00:00
2016-02-04 21:10:52 +00:00
func TestInvalidOTARequest(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-02-04 21:10:52 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear()
2016-02-04 21:10:52 +00:00
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)
2016-06-27 06:53:35 +00:00
assert.Error(err).Equals(proxy.ErrInvalidAuthentication)
2016-02-04 21:10:52 +00:00
}
2016-01-29 20:55:42 +00:00
func TestUDPRequestParsing(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-29 20:55:42 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear()
2016-01-29 20:55:42 +00:00
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 13:29:08 +00:00
assert.Address(request.Address).Equals(v2net.LocalHostIP)
assert.Port(request.Port).Equals(v2net.Port(80))
2016-01-29 20:55:42 +00: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 19:55:46 +00:00
assert := assert.On(t)
2016-01-29 20:55:42 +00:00
2016-08-01 20:59:18 +00:00
buffer := alloc.NewLocalBuffer(2048).Clear()
2016-01-29 20:55:42 +00:00
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 13:29:08 +00:00
assert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
2016-01-29 20:55:42 +00: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})
}