6565import static com .oracle .graal .python .runtime .PosixConstants .SO_PROTOCOL ;
6666import static com .oracle .graal .python .runtime .PosixConstants .SO_TYPE ;
6767import static com .oracle .graal .python .runtime .PosixConstants .TCP_USER_TIMEOUT ;
68+ import static org .hamcrest .CoreMatchers .anyOf ;
69+ import static org .hamcrest .CoreMatchers .equalTo ;
6870import static org .junit .Assert .assertArrayEquals ;
6971import static org .junit .Assert .assertEquals ;
7072import static org .junit .Assert .assertFalse ;
7173import static org .junit .Assert .assertNull ;
74+ import static org .junit .Assert .assertThat ;
7275import static org .junit .Assert .assertTrue ;
7376import static org .junit .Assert .fail ;
7477import static org .junit .Assume .assumeTrue ;
@@ -663,8 +666,16 @@ public void inet4Address() {
663666 static {
664667 ip4Addresses .put ("text" , null );
665668 ip4Addresses .put ("1.2.3." , null );
669+ ip4Addresses .put (".1.2.3" , null );
666670 ip4Addresses .put ("1.2.65536" , null );
667671 ip4Addresses .put ("1.2.3.4.5" , null );
672+ ip4Addresses .put ("1.2.3.4.5.6" , null );
673+ ip4Addresses .put ("1.2.-3.4" , null );
674+ ip4Addresses .put ("1.2. 3.4" , null );
675+ ip4Addresses .put (" 1.2.3.4" , null );
676+ ip4Addresses .put ("1.2.3.4@" , null );
677+ ip4Addresses .put ("1.2.3.4a" , null );
678+ ip4Addresses .put ("1.2..4" , null );
668679 ip4Addresses .put ("1.2.3.4" , 0x01020304 );
669680 ip4Addresses .put ("1.2.0x3456" , 0x01023456 );
670681 ip4Addresses .put ("1.2.0xffff" , 0x0102ffff );
@@ -674,11 +685,11 @@ public void inet4Address() {
674685 ip4Addresses .put ("0xff.0377.65535" , 0xffffffff );
675686 ip4Addresses .put ("0xa.012.10.0" , 0x0a0a0a00 );
676687 ip4Addresses .put ("00.0x00000.0" , 0x00000000 );
688+ ip4Addresses .put ("00.0x100.0" , null );
677689 }
678690
679691 @ Test
680692 public void inet_addr () {
681- assumeTrue ("native" .equals (backendName ));
682693 for (Map .Entry <String , Integer > a : ip4Addresses .entrySet ()) {
683694 String src = a .getKey ();
684695 Integer expected = a .getValue ();
@@ -689,7 +700,6 @@ public void inet_addr() {
689700
690701 @ Test
691702 public void inet_aton () {
692- assumeTrue ("native" .equals (backendName ));
693703 for (Map .Entry <String , Integer > a : ip4Addresses .entrySet ()) {
694704 String src = a .getKey ();
695705 Integer expected = a .getValue ();
@@ -705,7 +715,6 @@ public void inet_aton() {
705715
706716 @ Test
707717 public void inet_ntoa () {
708- assumeTrue ("native" .equals (backendName ));
709718 assertEquals ("0.0.0.0" , p2s (lib .inet_ntoa (posixSupport , 0x00000000 )));
710719 assertEquals ("1.2.3.4" , p2s (lib .inet_ntoa (posixSupport , 0x01020304 )));
711720 assertEquals ("18.52.86.120" , p2s (lib .inet_ntoa (posixSupport , 0x12345678 )));
@@ -714,42 +723,71 @@ public void inet_ntoa() {
714723
715724 @ Test
716725 public void inet_pton () throws PosixException , InvalidAddressException {
717- assumeTrue ("native" .equals (backendName ));
718726 assertArrayEquals (new byte []{1 , 2 , -2 , -1 }, lib .inet_pton (posixSupport , AF_INET .value , s2p ("1.2.254.255" )));
719727 assertArrayEquals (new byte []{0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , -1 }, lib .inet_pton (posixSupport , AF_INET6 .value , s2p ("1::FF" )));
728+ assertArrayEquals (MAPPED_LOOPBACK , lib .inet_pton (posixSupport , AF_INET6 .value , s2p ("::ffff:127.0.0.1" )));
720729 }
721730
722731 @ Test
723732 public void inet_pton_eafnosupport () throws PosixException , InvalidAddressException {
724- assumeTrue ("native" .equals (backendName ));
725733 expectErrno (OSErrorEnum .EAFNOSUPPORT );
726734 lib .inet_pton (posixSupport , AF_UNSPEC .value , s2p ("" ));
727735 }
728736
729737 @ Test
730- public void inet_pton_invalid () throws PosixException , InvalidAddressException {
731- assumeTrue ("native" .equals (backendName ));
738+ public void inet_pton_invalid_inet6 () throws PosixException , InvalidAddressException {
732739 expectedException .expect (InvalidAddressException .class );
733740 lib .inet_pton (posixSupport , AF_INET6 .value , s2p (":" ));
734741 }
735742
743+ @ Test
744+ public void inet_pton_invalid_inet4_as_inet6 () throws PosixException , InvalidAddressException {
745+ expectedException .expect (InvalidAddressException .class );
746+ lib .inet_pton (posixSupport , AF_INET6 .value , s2p ("127.0.0.1" ));
747+ }
748+
749+ @ Test
750+ public void inet_pton_invalid_inet4 () throws PosixException {
751+ String [] addresses = {
752+ "1.2.3.4.5" , // too many bytes
753+ "1.2.65535" , // unlike inet_aton, inet_pton requires exactly four bytes
754+ "1.2.0x10.4" , // hexadecimal is not allowed
755+ "1::FF" , // IPv6 address is not allowed
756+ };
757+ for (String src : addresses ) {
758+ try {
759+ lib .inet_pton (posixSupport , AF_INET .value , s2p (src ));
760+ fail ("inet_pton(AF_INET, \" " + src + "\" ) was expected to fail" );
761+ } catch (InvalidAddressException e ) {
762+ // expected
763+ }
764+ }
765+ }
766+
767+ @ Test
768+ public void inet_pton_inet4_octal () throws PosixException , InvalidAddressException {
769+ // native inet_pton on darwin accepts leading zeroes (but handles them as decimal)
770+ assumeTrue ("java" .equals (backendName ) || runsOnLinux ());
771+ expectedException .expect (InvalidAddressException .class );
772+ lib .inet_pton (posixSupport , AF_INET .value , s2p ("1.2.010.4" ));
773+ }
774+
736775 @ Test
737776 public void inet_ntop () throws PosixException {
738- assumeTrue ("native" .equals (backendName ));
739777 assertEquals ("1.0.255.254" , p2s (lib .inet_ntop (posixSupport , AF_INET .value , new byte []{1 , 0 , -1 , -2 , -3 })));
740- assertEquals ("fdfe:0:ff00::1:203" , p2s (lib .inet_ntop (posixSupport , AF_INET6 .value , new byte []{-3 , -2 , 0 , 0 , -1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 })));
778+ assertThat (p2s (lib .inet_ntop (posixSupport , AF_INET6 .value , new byte []{-3 , -2 , 0 , 0 , -1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 })),
779+ anyOf (equalTo ("fdfe:0:ff00::1:203" ), equalTo ("fdfe:0:ff00:0:0:0:1:203" )));
780+ assertEquals ("::ffff:127.0.0.1" , p2s (lib .inet_ntop (posixSupport , AF_INET6 .value , MAPPED_LOOPBACK )));
741781 }
742782
743783 @ Test
744784 public void inet_ntop_eafnosupport () throws PosixException {
745- assumeTrue ("native" .equals (backendName ));
746785 expectErrno (OSErrorEnum .EAFNOSUPPORT );
747786 lib .inet_ntop (posixSupport , AF_UNSPEC .value , new byte [16 ]);
748787 }
749788
750789 @ Test
751790 public void inet_ntop_len () throws PosixException {
752- assumeTrue ("native" .equals (backendName ));
753791 expectedException .expect (IllegalArgumentException .class );
754792 lib .inet_ntop (posixSupport , AF_INET6 .value , new byte [15 ]);
755793 }
0 commit comments