From 4689e38b3cb7fb0261ce9536e5ff4222634c0664 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:37:20 -0300 Subject: [PATCH] test(ipv6): the link-local order of getipaddr has no test site.master takes the value getipaddr returns for the management node name. A management node resolves its own name to its link-local address as well, and a node on another link cannot reach fe80::/10. Two commits on this branch order the resolver answer so a routable address comes first, one for each of the two resolver paths getipaddr has. Neither order had a test that can fail. networkutils_linklocal_preference.t calls the two ordering helpers as pure functions and never calls getipaddr, so deleting either call in getipaddr left all 298 unit files passing. Add one file per resolver path. Each stubs the resolver at the Socket or Socket6 level, hands getipaddr an answer that lists a link-local address first, and asserts on what getipaddr returns. The Socket6 file carries a Socket6 stub in %INC, so it measures that path on a host with no Socket6, which is the condition the unit suite runs in. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../networkutils_getipaddr_routable_first.t | 108 +++++++++++++++++ ...rkutils_getipaddr_socket6_routable_first.t | 111 ++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 xCAT-test/unit/networkutils_getipaddr_routable_first.t create mode 100644 xCAT-test/unit/networkutils_getipaddr_socket6_routable_first.t diff --git a/xCAT-test/unit/networkutils_getipaddr_routable_first.t b/xCAT-test/unit/networkutils_getipaddr_routable_first.t new file mode 100644 index 0000000000..fb40b98933 --- /dev/null +++ b/xCAT-test/unit/networkutils_getipaddr_routable_first.t @@ -0,0 +1,108 @@ +#!/usr/bin/env perl +# xcatconfig writes site.master from getipaddr of the management node name, and +# a management node resolves its own name to its link-local address as well as +# to a routable one. A node on another link cannot reach fe80::/10, so getipaddr +# must answer the routable address. getipaddr reaches the resolver through two +# paths and each orders its own answer. This file covers the core Socket path; +# networkutils_getipaddr_socket6_routable_first.t covers the Socket6 path. +use strict; +use warnings; + +# The path getipaddr takes is decided by a file-scoped lexical that NetworkUtils +# reads once at load. An undef %INC entry makes its "require Socket6" die, so +# the core Socket path runs here whether or not the host carries Socket6. +BEGIN { $INC{'Socket6.pm'} = undef; } + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Socket (); +use Test::More; +use xCAT::NetworkUtils; + +plan skip_all => 'The core Socket resolver is not available' + unless defined(&Socket::getaddrinfo) && defined(&Socket::getnameinfo); + +# Socket::getaddrinfo answers a list of hashes whose addr is a packed +# sockaddr. The stub carries the literal in that field, and the getnameinfo +# stub reads it back, so a case states its resolver answer as addresses. +my @resolver_answer; + +sub addrinfo_entry { + my $address = shift; + return { + family => ( $address =~ /:/ ? Socket::AF_INET6() : Socket::AF_INET() ), + socktype => Socket::SOCK_STREAM(), + protocol => 6, + addr => "SOCKADDR:$address", + }; +} + +my @asked_family; + +no warnings qw(once redefine); +local *Socket::getaddrinfo = sub { + my ( $host, $service, $hints ) = @_; + push @asked_family, $hints->{family}; + return ('Name or service not known') unless @resolver_answer; + return ( undef, map { addrinfo_entry($_) } @resolver_answer ); +}; +local *Socket::getnameinfo = sub { + my $addr = shift; + return ('Unreadable address') unless defined($addr) && $addr =~ /^SOCKADDR:(.+)$/; + return ( undef, $1 ); +}; + +sub resolve { + my ( $answer, @extra ) = @_; + @resolver_answer = @{$answer}; + %::hostiphash = (); + return xCAT::NetworkUtils->getipaddr( 'mn.example.com', @extra ); +} + +is( resolve( [ 'fe80::4054:ff:fec9:9ad2', '2001:db8::50' ] ), + '2001:db8::50', + 'getipaddr answers the routable address when the resolver lists link-local first' +); + +is( resolve( [ 'fe80::4054:ff:fec9:9ad2', '192.0.2.154' ] ), + '192.0.2.154', + 'getipaddr answers the IPv4 address when the resolver lists link-local first' +); + +is( resolve( [ '169.254.3.4', '192.0.2.154' ] ), + '192.0.2.154', + 'getipaddr skips an IPv4 link-local address for a routable one' +); + +is( resolve( [ 'fe80::1' ] ), + 'fe80::1', + 'getipaddr still answers a link-local address when it is the only one' +); + +is( resolve( [ '2001:db8::50', '192.0.2.154' ] ), + '2001:db8::50', + 'getipaddr keeps the resolver order when no address is link-local' +); + +is_deeply( + [ resolve( [ 'fe80::1', '2001:db8::50', '192.0.2.154' ], GetAllAddresses => 1 ) ], + [ '2001:db8::50', '192.0.2.154', 'fe80::1' ], + 'GetAllAddresses lists every routable address before the link-local one' +); + +is( resolve( [ 'fe80::4054:ff:fec9:9ad2', '2001:db8::50' ], OnlyV6 => 1 ), + '2001:db8::50', + 'an OnlyV6 lookup answers the routable IPv6 address, not the link-local one' +); + +is( + resolve( [ 'fe80::1', '2001:db8::50' ], GetNumber => 1 )->bstr(), + Math::BigInt->new('42540766411282592856903984951653826640')->bstr(), + 'GetNumber answers the number of the routable address' +); + +is( resolve( [] ), undef, + 'getipaddr answers undef when the resolver reports an error' ); + +done_testing(); diff --git a/xCAT-test/unit/networkutils_getipaddr_socket6_routable_first.t b/xCAT-test/unit/networkutils_getipaddr_socket6_routable_first.t new file mode 100644 index 0000000000..3ce65550fc --- /dev/null +++ b/xCAT-test/unit/networkutils_getipaddr_socket6_routable_first.t @@ -0,0 +1,111 @@ +#!/usr/bin/env perl +# The Socket6 half of the getipaddr link-local order. A management node that +# carries Socket6 takes this path and no other, so site.master gets its value +# from here. The core Socket half is in networkutils_getipaddr_routable_first.t. +# +# Socket6::getaddrinfo answers a flat list of five-element records, which is a +# different shape from the core resolver, and the branch orders each shape with +# its own helper. +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Test::More; + +# NetworkUtils decides which resolver path to use from "require Socket6", read +# once at load into a file-scoped lexical. The stub below satisfies that +# require, so this file measures the Socket6 path on a host that has no +# Socket6 - which is the condition the unit suite runs in. +our @resolver_answer; + +BEGIN { + package Socket6; + use strict; + use warnings; + sub AI_NUMERICHOST { return 4 } + sub NI_NUMERICHOST { return 1 } + sub AF_INET { return 2 } + + sub getaddrinfo { + my ( $host, $service, $family, $socktype, $protocol, $flags ) = @_; + return () unless @main::resolver_answer; + my @flat; + foreach my $address (@main::resolver_answer) { + push @flat, + ( $address =~ /:/ ? 10 : 2 ), + 1, 6, "SOCKADDR:$address", "$address.example.com"; + } + return @flat; + } + + sub getnameinfo { + my $addr = shift; + return () unless defined($addr) && $addr =~ /^SOCKADDR:(.+)$/; + return ($1); + } + + sub inet_pton { + my ( $family, $address ) = @_; + return Socket::inet_pton( $family == 10 ? Socket::AF_INET6() : Socket::AF_INET(), + $address ); + } + $INC{'Socket6.pm'} = __FILE__; +} + +use xCAT::NetworkUtils; + +sub resolve { + my ( $answer, @extra ) = @_; + @resolver_answer = @{$answer}; + %::hostiphash = (); + return xCAT::NetworkUtils->getipaddr( 'mn.example.com', @extra ); +} + +is( resolve( [ 'fe80::4054:ff:fec9:9ad2', '2001:db8::50' ] ), + '2001:db8::50', + 'getipaddr answers the routable address when the resolver lists link-local first' +); + +is( resolve( [ 'fe80::4054:ff:fec9:9ad2', '192.0.2.154' ] ), + '192.0.2.154', + 'getipaddr answers the IPv4 address when the resolver lists link-local first' +); + +is( resolve( [ '169.254.3.4', '192.0.2.154' ] ), + '192.0.2.154', + 'getipaddr skips an IPv4 link-local address for a routable one' +); + +is( resolve( ['fe80::1'] ), + 'fe80::1', + 'getipaddr still answers a link-local address when it is the only one' +); + +is( resolve( [ '2001:db8::50', '192.0.2.154' ] ), + '2001:db8::50', + 'getipaddr keeps the resolver order when no address is link-local' +); + +is_deeply( + [ resolve( [ 'fe80::1', '2001:db8::50', '192.0.2.154' ], GetAllAddresses => 1 ) ], + [ '2001:db8::50', '192.0.2.154', 'fe80::1' ], + 'GetAllAddresses lists every routable address before the link-local one' +); + +is( resolve( [ 'fe80::4054:ff:fec9:9ad2', '2001:db8::50' ], OnlyV6 => 1 ), + '2001:db8::50', + 'an OnlyV6 lookup answers the routable IPv6 address, not the link-local one' +); + +is( + resolve( [ 'fe80::1', '2001:db8::50' ], GetNumber => 1 )->bstr(), + Math::BigInt->new('42540766411282592856903984951653826640')->bstr(), + 'GetNumber answers the number of the routable address' +); + +is( resolve( [] ), undef, + 'getipaddr answers undef when the resolver answers nothing' ); + +done_testing();