65
65
import static com .oracle .graal .python .runtime .PosixConstants .SO_PROTOCOL ;
66
66
import static com .oracle .graal .python .runtime .PosixConstants .SO_TYPE ;
67
67
import static com .oracle .graal .python .runtime .PosixConstants .TCP_USER_TIMEOUT ;
68
+ import static org .hamcrest .CoreMatchers .anyOf ;
69
+ import static org .hamcrest .CoreMatchers .equalTo ;
68
70
import static org .junit .Assert .assertArrayEquals ;
69
71
import static org .junit .Assert .assertEquals ;
70
72
import static org .junit .Assert .assertFalse ;
71
73
import static org .junit .Assert .assertNull ;
74
+ import static org .junit .Assert .assertThat ;
72
75
import static org .junit .Assert .assertTrue ;
73
76
import static org .junit .Assert .fail ;
74
77
import static org .junit .Assume .assumeTrue ;
@@ -663,8 +666,16 @@ public void inet4Address() {
663
666
static {
664
667
ip4Addresses .put ("text" , null );
665
668
ip4Addresses .put ("1.2.3." , null );
669
+ ip4Addresses .put (".1.2.3" , null );
666
670
ip4Addresses .put ("1.2.65536" , null );
667
671
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 );
668
679
ip4Addresses .put ("1.2.3.4" , 0x01020304 );
669
680
ip4Addresses .put ("1.2.0x3456" , 0x01023456 );
670
681
ip4Addresses .put ("1.2.0xffff" , 0x0102ffff );
@@ -674,11 +685,11 @@ public void inet4Address() {
674
685
ip4Addresses .put ("0xff.0377.65535" , 0xffffffff );
675
686
ip4Addresses .put ("0xa.012.10.0" , 0x0a0a0a00 );
676
687
ip4Addresses .put ("00.0x00000.0" , 0x00000000 );
688
+ ip4Addresses .put ("00.0x100.0" , null );
677
689
}
678
690
679
691
@ Test
680
692
public void inet_addr () {
681
- assumeTrue ("native" .equals (backendName ));
682
693
for (Map .Entry <String , Integer > a : ip4Addresses .entrySet ()) {
683
694
String src = a .getKey ();
684
695
Integer expected = a .getValue ();
@@ -689,7 +700,6 @@ public void inet_addr() {
689
700
690
701
@ Test
691
702
public void inet_aton () {
692
- assumeTrue ("native" .equals (backendName ));
693
703
for (Map .Entry <String , Integer > a : ip4Addresses .entrySet ()) {
694
704
String src = a .getKey ();
695
705
Integer expected = a .getValue ();
@@ -705,7 +715,6 @@ public void inet_aton() {
705
715
706
716
@ Test
707
717
public void inet_ntoa () {
708
- assumeTrue ("native" .equals (backendName ));
709
718
assertEquals ("0.0.0.0" , p2s (lib .inet_ntoa (posixSupport , 0x00000000 )));
710
719
assertEquals ("1.2.3.4" , p2s (lib .inet_ntoa (posixSupport , 0x01020304 )));
711
720
assertEquals ("18.52.86.120" , p2s (lib .inet_ntoa (posixSupport , 0x12345678 )));
@@ -714,42 +723,71 @@ public void inet_ntoa() {
714
723
715
724
@ Test
716
725
public void inet_pton () throws PosixException , InvalidAddressException {
717
- assumeTrue ("native" .equals (backendName ));
718
726
assertArrayEquals (new byte []{1 , 2 , -2 , -1 }, lib .inet_pton (posixSupport , AF_INET .value , s2p ("1.2.254.255" )));
719
727
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" )));
720
729
}
721
730
722
731
@ Test
723
732
public void inet_pton_eafnosupport () throws PosixException , InvalidAddressException {
724
- assumeTrue ("native" .equals (backendName ));
725
733
expectErrno (OSErrorEnum .EAFNOSUPPORT );
726
734
lib .inet_pton (posixSupport , AF_UNSPEC .value , s2p ("" ));
727
735
}
728
736
729
737
@ 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 {
732
739
expectedException .expect (InvalidAddressException .class );
733
740
lib .inet_pton (posixSupport , AF_INET6 .value , s2p (":" ));
734
741
}
735
742
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
+
736
775
@ Test
737
776
public void inet_ntop () throws PosixException {
738
- assumeTrue ("native" .equals (backendName ));
739
777
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 )));
741
781
}
742
782
743
783
@ Test
744
784
public void inet_ntop_eafnosupport () throws PosixException {
745
- assumeTrue ("native" .equals (backendName ));
746
785
expectErrno (OSErrorEnum .EAFNOSUPPORT );
747
786
lib .inet_ntop (posixSupport , AF_UNSPEC .value , new byte [16 ]);
748
787
}
749
788
750
789
@ Test
751
790
public void inet_ntop_len () throws PosixException {
752
- assumeTrue ("native" .equals (backendName ));
753
791
expectedException .expect (IllegalArgumentException .class );
754
792
lib .inet_ntop (posixSupport , AF_INET6 .value , new byte [15 ]);
755
793
}
0 commit comments