diff --git a/common/net/address_test.go b/common/net/address_test.go index 03152c5dd..e1acfafcc 100644 --- a/common/net/address_test.go +++ b/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() +} diff --git a/common/net/destination.go b/common/net/destination.go index 14e3d5dca..7c8d24371 100644 --- a/common/net/destination.go +++ b/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 } diff --git a/common/net/destination_test.go b/common/net/destination_test.go index e17b6dbb8..a3e25f335 100644 --- a/common/net/destination_test.go +++ b/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() +}