Browse Source

test cases for Equals()

Darien Raymond 9 years ago
parent
commit
7f4ff04483
3 changed files with 94 additions and 2 deletions
  1. 50 2
      common/net/address_test.go
  2. 12 0
      common/net/destination.go
  3. 32 0
      common/net/destination_test.go

+ 50 - 2
common/net/address_test.go

@@ -42,8 +42,8 @@ func TestIPv6Address(t *testing.T) {
 }
 
 func TestIPv4Asv6(t *testing.T) {
-  v2testing.Current(t)
-  ip := []byte{
+	v2testing.Current(t)
+	ip := []byte{
 		byte(0), byte(0), byte(0), byte(0),
 		byte(0), byte(0), byte(0), byte(0),
 		byte(0), byte(0), byte(255), byte(255),
@@ -74,3 +74,51 @@ func TestNetIPv4Address(t *testing.T) {
 	v2netassert.Address(addr).IsIPv4()
 	assert.String(addr).Equals("1.2.3.4")
 }
+
+func TestIPv4AddressEquals(t *testing.T) {
+	v2testing.Current(t)
+
+	addr := v2net.IPAddress([]byte{1, 2, 3, 4})
+	assert.Bool(addr.Equals(nil)).IsFalse()
+
+	addr2 := v2net.IPAddress([]byte{1, 2, 3, 4})
+	assert.Bool(addr.Equals(addr2)).IsTrue()
+
+	addr3 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
+	assert.Bool(addr.Equals(addr3)).IsFalse()
+
+	addr4 := v2net.IPAddress([]byte{1, 2, 3, 5})
+	assert.Bool(addr.Equals(addr4)).IsFalse()
+}
+
+func TestIPv6AddressEquals(t *testing.T) {
+	v2testing.Current(t)
+
+	addr := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
+	assert.Bool(addr.Equals(nil)).IsFalse()
+
+	addr2 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
+	assert.Bool(addr.Equals(addr2)).IsTrue()
+
+	addr3 := v2net.IPAddress([]byte{1, 2, 3, 4})
+	assert.Bool(addr.Equals(addr3)).IsFalse()
+
+	addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
+	assert.Bool(addr.Equals(addr4)).IsFalse()
+}
+
+func TestDomainAddressEquals(t *testing.T) {
+	v2testing.Current(t)
+
+	addr := v2net.DomainAddress("v2ray.com")
+	assert.Bool(addr.Equals(nil)).IsFalse()
+
+	addr2 := v2net.DomainAddress("v2ray.com")
+	assert.Bool(addr.Equals(addr2)).IsTrue()
+
+	addr3 := v2net.DomainAddress("www.v2ray.com")
+	assert.Bool(addr.Equals(addr3)).IsFalse()
+
+	addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
+	assert.Bool(addr.Equals(addr4)).IsFalse()
+}

+ 12 - 0
common/net/destination.go

@@ -57,6 +57,12 @@ func (dest *tcpDestination) Port() Port {
 }
 
 func (dest *tcpDestination) Equals(another Destination) bool {
+	if dest == nil && another == nil {
+		return true
+	}
+	if dest == nil || another == nil {
+		return false
+	}
 	if !another.IsTCP() {
 		return false
 	}
@@ -97,6 +103,12 @@ func (dest *udpDestination) Port() Port {
 }
 
 func (dest *udpDestination) Equals(another Destination) bool {
+	if dest == nil && another == nil {
+		return true
+	}
+	if dest == nil || another == nil {
+		return false
+	}
 	if !another.IsUDP() {
 		return false
 	}

+ 32 - 0
common/net/destination_test.go

@@ -26,3 +26,35 @@ func TestUDPDestination(t *testing.T) {
 	v2netassert.Destination(dest).IsUDP()
 	assert.String(dest).Equals("udp:[2001:4860:4860::8888]:53")
 }
+
+func TestTCPDestinationEquals(t *testing.T) {
+	v2testing.Current(t)
+
+	dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
+	assert.Bool(dest.Equals(nil)).IsFalse()
+
+	dest2 := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
+	assert.Bool(dest.Equals(dest2)).IsTrue()
+
+	dest3 := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
+	assert.Bool(dest.Equals(dest3)).IsFalse()
+
+	dest4 := v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)
+	assert.Bool(dest.Equals(dest4)).IsFalse()
+}
+
+func TestUDPDestinationEquals(t *testing.T) {
+	v2testing.Current(t)
+
+	dest := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
+	assert.Bool(dest.Equals(nil)).IsFalse()
+
+	dest2 := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
+	assert.Bool(dest.Equals(dest2)).IsTrue()
+
+	dest3 := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
+	assert.Bool(dest.Equals(dest3)).IsFalse()
+
+	dest4 := v2net.UDPDestination(v2net.DomainAddress("v2ray.com"), 80)
+	assert.Bool(dest.Equals(dest4)).IsFalse()
+}