From 785b09cd4d25d9ff8d3f1de428c6688c3fde6e39 Mon Sep 17 00:00:00 2001 From: yuzumone Date: Sun, 12 Jul 2026 13:32:50 +0900 Subject: [PATCH] fix: Include prefix length in interface equality Make IPv4Interface and IPv6Interface equality and hash codes include the prefix length. Add regression tests for interfaces with the same IP address and different prefixes. --- lib/src/ipaddr_base.dart | 20 ++++++++++++++++++++ test/ipv4interface_test.dart | 7 +++++++ test/ipv6interface_test.dart | 7 +++++++ 3 files changed, 34 insertions(+) diff --git a/lib/src/ipaddr_base.dart b/lib/src/ipaddr_base.dart index b756d71..daf161c 100644 --- a/lib/src/ipaddr_base.dart +++ b/lib/src/ipaddr_base.dart @@ -587,6 +587,16 @@ class IPv4Interface extends _BaseIPv4Interface implements _Interface { @override String get withPrefixlen => '$ip/$_prefixlen'; + @override + bool operator ==(other) => + identical(this, other) || + other is IPv4Interface && + _ip == other._ip && + _prefixlen == other._prefixlen; + + @override + int get hashCode => _ip.hashCode ^ _prefixlen; + /// Creates a new IPv4Interface. IPv4Interface(String addr) { var ap = _splitAddrPrefix(addr); @@ -756,6 +766,16 @@ class IPv6Interface extends _BaseIPv6Interface implements _Interface { @override String get withPrefixlen => '$ip/$_prefixlen'; + @override + bool operator ==(other) => + identical(this, other) || + other is IPv6Interface && + _ip == other._ip && + _prefixlen == other._prefixlen; + + @override + int get hashCode => _ip.hashCode ^ _prefixlen; + /// Creates a new IPv6Interface. IPv6Interface(String addr) { var ap = _splitAddrPrefix(addr); diff --git a/test/ipv4interface_test.dart b/test/ipv4interface_test.dart index 93ea994..30fb8e0 100644 --- a/test/ipv4interface_test.dart +++ b/test/ipv4interface_test.dart @@ -48,6 +48,13 @@ void main() { true); }); + test('OK: not equal with different prefixlen', () { + var interface1 = IPv4Interface('192.168.10.0/24'); + var interface2 = IPv4Interface('192.168.10.0/30'); + expect(interface1 == interface2, false); + expect(interface1.hashCode == interface2.hashCode, false); + }); + test('OK: not equal false test', () { var ipStr = '192.168.10.10/30'; expect(IPv4Interface(ipStr) != IPv4Interface(ipStr), false); diff --git a/test/ipv6interface_test.dart b/test/ipv6interface_test.dart index e25bb53..082693a 100644 --- a/test/ipv6interface_test.dart +++ b/test/ipv6interface_test.dart @@ -50,6 +50,13 @@ void main() { true); }); + test('OK: not equal with different prefixlen', () { + var interface1 = IPv6Interface('2001:4860:4860::8888/64'); + var interface2 = IPv6Interface('2001:4860:4860::8888/126'); + expect(interface1 == interface2, false); + expect(interface1.hashCode == interface2.hashCode, false); + }); + test('OK: not equal false test', () { var ipStr = '2001:4860:4860::8888/126'; expect(IPv6Interface(ipStr) != IPv6Interface(ipStr), false);