@@ -705,3 +705,35 @@ func TestIPv6WriteMsgUDPAddrPortTargetAddrIPVersion(t *testing.T) {
705
705
t .Fatal (err )
706
706
}
707
707
}
708
+
709
+ // TestIPv4WriteMsgUDPAddrPortTargetAddrIPVersion verifies that
710
+ // WriteMsgUDPAddrPort accepts IPv4 and IPv4-mapped IPv6 destination addresses,
711
+ // and rejects IPv6 destination addresses on a "udp4" connection.
712
+ func TestIPv4WriteMsgUDPAddrPortTargetAddrIPVersion (t * testing.T ) {
713
+ if ! testableNetwork ("udp4" ) {
714
+ t .Skipf ("skipping: udp4 not available" )
715
+ }
716
+
717
+ conn , err := ListenUDP ("udp4" , & UDPAddr {IP : IPv4 (127 , 0 , 0 , 1 )})
718
+ if err != nil {
719
+ t .Fatal (err )
720
+ }
721
+ defer conn .Close ()
722
+
723
+ daddr4 := netip .AddrPortFrom (netip .MustParseAddr ("127.0.0.1" ), 12345 )
724
+ daddr4in6 := netip .AddrPortFrom (netip .MustParseAddr ("::ffff:127.0.0.1" ), 12345 )
725
+ daddr6 := netip .AddrPortFrom (netip .MustParseAddr ("::1" ), 12345 )
726
+ buf := make ([]byte , 8 )
727
+
728
+ if _ , _ , err = conn .WriteMsgUDPAddrPort (buf , nil , daddr4 ); err != nil {
729
+ t .Errorf ("conn.WriteMsgUDPAddrPort(buf, nil, daddr4) failed: %v" , err )
730
+ }
731
+
732
+ if _ , _ , err = conn .WriteMsgUDPAddrPort (buf , nil , daddr4in6 ); err != nil {
733
+ t .Errorf ("conn.WriteMsgUDPAddrPort(buf, nil, daddr4in6) failed: %v" , err )
734
+ }
735
+
736
+ if _ , _ , err = conn .WriteMsgUDPAddrPort (buf , nil , daddr6 ); err == nil {
737
+ t .Errorf ("conn.WriteMsgUDPAddrPort(buf, nil, daddr6) should have failed, but got no error" )
738
+ }
739
+ }
0 commit comments