unify all address reading and writing

This commit is contained in:
Darien Raymond
2018-02-23 23:42:01 +01:00
parent a059ee2c00
commit af1abf687c
10 changed files with 333 additions and 267 deletions

View File

@@ -1,7 +1,6 @@
package socks_test
import (
"bytes"
"testing"
"v2ray.com/core/common/buf"
@@ -34,56 +33,3 @@ func TestUDPEncoding(t *testing.T) {
assert(err, IsNil)
assert(decodedPayload[0].Bytes(), Equals, content)
}
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)
}
}
}