1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

test case for socks udp

This commit is contained in:
Darien Raymond 2017-01-08 16:31:28 +01:00
parent f75d94817a
commit 7401807c5f
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 45 additions and 0 deletions

View File

@ -344,6 +344,10 @@ type UDPReader struct {
reader io.Reader
}
func NewUDPReader(reader io.Reader) *UDPReader {
return &UDPReader{reader: reader}
}
func (r *UDPReader) Read() (*buf.Buffer, error) {
b := buf.NewSmall()
if err := b.AppendSupplier(buf.ReadFrom(r.reader)); err != nil {
@ -363,6 +367,13 @@ type UDPWriter struct {
writer io.Writer
}
func NewUDPWriter(request *protocol.RequestHeader, writer io.Writer) *UDPWriter {
return &UDPWriter{
request: request,
writer: writer,
}
}
func (w *UDPWriter) Write(b *buf.Buffer) error {
eb := EncodeUDPPacket(w.request, b.Bytes())
b.Release()

View File

@ -0,0 +1,34 @@
package socks_test
import (
"testing"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
. "v2ray.com/core/proxy/socks"
"v2ray.com/core/testing/assert"
)
func TestUDPEncoding(t *testing.T) {
assert := assert.On(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 := NewUDPWriter(request, b)
content := []byte{'a'}
payload := buf.New()
payload.Append(content)
assert.Error(writer.Write(payload)).IsNil()
reader := NewUDPReader(b)
decodedPayload, err := reader.Read()
assert.Error(err).IsNil()
assert.Bytes(decodedPayload.Bytes()).Equals(content)
}