Browse Source

test case for write address

Darien Raymond 7 years ago
parent
commit
a42b4b513e
1 changed files with 38 additions and 1 deletions
  1. 38 1
      common/protocol/address_test.go

+ 38 - 1
common/protocol/address_test.go

@@ -10,7 +10,7 @@ import (
 	. "v2ray.com/ext/assert"
 )
 
-func TestAddressParser(t *testing.T) {
+func TestAddressReading(t *testing.T) {
 	assert := With(t)
 
 	data := []struct {
@@ -22,6 +22,11 @@ func TestAddressParser(t *testing.T) {
 	}{
 		{
 			Options: []AddressOption{},
+			Input:   []byte{},
+			Error:   true,
+		},
+		{
+			Options: []AddressOption{},
 			Input:   []byte{0, 0, 0, 0, 0},
 			Error:   true,
 		},
@@ -65,6 +70,38 @@ func TestAddressParser(t *testing.T) {
 		} else {
 			assert(addr, Equals, tc.Address)
 			assert(port, Equals, tc.Port)
+			assert(err, IsNil)
+		}
+	}
+}
+
+func TestAddressWriting(t *testing.T) {
+	assert := With(t)
+
+	data := []struct {
+		Options []AddressOption
+		Address net.Address
+		Port    net.Port
+		Bytes   []byte
+		Error   bool
+	}{
+		{
+			Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
+			Address: net.LocalHostIP,
+			Port:    net.Port(80),
+			Bytes:   []byte{1, 127, 0, 0, 1, 0, 80},
+		},
+	}
+
+	for _, tc := range data {
+		parser := NewAddressParser(tc.Options...)
+
+		b := buf.New()
+		err := parser.WriteAddressPort(b, tc.Address, tc.Port)
+		if tc.Error {
+			assert(err, IsNotNil)
+		} else {
+			assert(b.Bytes(), Equals, tc.Bytes)
 		}
 	}
 }