@@ -749,6 +749,145 @@ fn split() -> Result {
749
749
sim. run ( )
750
750
}
751
751
752
+ #[ test]
753
+ fn peek_empty_buffer ( ) -> Result {
754
+ let mut sim = Builder :: new ( ) . build ( ) ;
755
+
756
+ sim. client ( "server" , async move {
757
+ let listener = bind ( ) . await ?;
758
+ let _ = listener. accept ( ) . await ?;
759
+ Ok ( ( ) )
760
+ } ) ;
761
+
762
+ sim. client ( "client" , async move {
763
+ let mut s = TcpStream :: connect ( ( "server" , PORT ) ) . await ?;
764
+
765
+ // no-op peek with empty buffer
766
+ let mut buf = [ 0 ; 0 ] ;
767
+ let n = s. peek ( & mut buf) . await ?;
768
+ assert_eq ! ( 0 , n) ;
769
+
770
+ Ok ( ( ) )
771
+ } ) ;
772
+
773
+ sim. run ( )
774
+ }
775
+
776
+ #[ test]
777
+ fn peek_then_read ( ) -> Result {
778
+ let mut sim = Builder :: new ( ) . build ( ) ;
779
+
780
+ sim. client ( "server" , async move {
781
+ let listener = bind ( ) . await ?;
782
+ let ( mut s, _) = listener. accept ( ) . await ?;
783
+
784
+ s. write_u64 ( 1234 ) . await ?;
785
+ Ok ( ( ) )
786
+ } ) ;
787
+
788
+ sim. client ( "client" , async move {
789
+ let mut s = TcpStream :: connect ( ( "server" , PORT ) ) . await ?;
790
+
791
+ // peek full message
792
+ let mut peek_buf = [ 0 ; 8 ] ;
793
+ assert_eq ! ( 8 , s. peek( & mut peek_buf) . await ?) ;
794
+ assert_eq ! ( 1234u64 , u64 :: from_be_bytes( peek_buf) ) ;
795
+
796
+ // peek again should see same data
797
+ let mut peek_buf2 = [ 0 ; 8 ] ;
798
+ assert_eq ! ( 8 , s. peek( & mut peek_buf2) . await ?) ;
799
+ assert_eq ! ( 1234u64 , u64 :: from_be_bytes( peek_buf2) ) ;
800
+
801
+ // read should consume the data
802
+ assert_eq ! ( 1234 , s. read_u64( ) . await ?) ;
803
+ let mut buf = [ 0 ; 8 ] ;
804
+ assert ! ( matches!( s. read( & mut buf) . await , Ok ( 0 ) ) ) ;
805
+
806
+ Ok ( ( ) )
807
+ } ) ;
808
+
809
+ sim. run ( )
810
+ }
811
+
812
+ #[ test]
813
+ fn peek_partial ( ) -> Result {
814
+ let mut sim = Builder :: new ( ) . build ( ) ;
815
+
816
+ sim. client ( "server" , async move {
817
+ let listener = bind ( ) . await ?;
818
+ let ( mut s, _) = listener. accept ( ) . await ?;
819
+
820
+ s. write_all ( & [ 0 , 0 , 1 , 1 ] ) . await ?;
821
+ Ok ( ( ) )
822
+ } ) ;
823
+
824
+ sim. client ( "client" , async move {
825
+ let mut s = TcpStream :: connect ( ( "server" , PORT ) ) . await ?;
826
+
827
+ // peek with smaller buffer
828
+ let mut peek_buf = [ 0 ; 2 ] ;
829
+ assert_eq ! ( 2 , s. peek( & mut peek_buf) . await ?) ;
830
+ assert_eq ! ( [ 0 , 0 ] , peek_buf) ;
831
+
832
+ // peek with larger buffer should still see all data
833
+ let mut peek_buf2 = [ 0 ; 4 ] ;
834
+ assert_eq ! ( 4 , s. peek( & mut peek_buf2) . await ?) ;
835
+ assert_eq ! ( [ 0 , 0 , 1 , 1 ] , peek_buf2) ;
836
+
837
+ // read partial
838
+ let mut read_buf = [ 0 ; 2 ] ;
839
+ assert_eq ! ( 2 , s. read( & mut read_buf) . await ?) ;
840
+ assert_eq ! ( [ 0 , 0 ] , read_buf) ;
841
+
842
+ // peek remaining
843
+ let mut peek_buf3 = [ 0 ; 2 ] ;
844
+ assert_eq ! ( 2 , s. peek( & mut peek_buf3) . await ?) ;
845
+ assert_eq ! ( [ 1 , 1 ] , peek_buf3) ;
846
+
847
+ Ok ( ( ) )
848
+ } ) ;
849
+
850
+ sim. run ( )
851
+ }
852
+
853
+ #[ test]
854
+ fn peek_multiple_messages ( ) -> Result {
855
+ let mut sim = Builder :: new ( ) . build ( ) ;
856
+
857
+ sim. client ( "server" , async move {
858
+ let listener = bind ( ) . await ?;
859
+ let ( mut s, _) = listener. accept ( ) . await ?;
860
+
861
+ s. write_u64 ( 1234 ) . await ?;
862
+ s. write_u64 ( 5678 ) . await ?;
863
+ Ok ( ( ) )
864
+ } ) ;
865
+
866
+ sim. client ( "client" , async move {
867
+ let mut s = TcpStream :: connect ( ( "server" , PORT ) ) . await ?;
868
+
869
+ // peek first message
870
+ let mut peek_buf = [ 0 ; 8 ] ;
871
+ assert_eq ! ( 8 , s. peek( & mut peek_buf) . await ?) ;
872
+ assert_eq ! ( 1234u64 , u64 :: from_be_bytes( peek_buf) ) ;
873
+
874
+ // read first message
875
+ assert_eq ! ( 1234 , s. read_u64( ) . await ?) ;
876
+
877
+ // peek second message
878
+ let mut peek_buf2 = [ 0 ; 8 ] ;
879
+ assert_eq ! ( 8 , s. peek( & mut peek_buf2) . await ?) ;
880
+ assert_eq ! ( 5678u64 , u64 :: from_be_bytes( peek_buf2) ) ;
881
+
882
+ // read second message
883
+ assert_eq ! ( 5678 , s. read_u64( ) . await ?) ;
884
+
885
+ Ok ( ( ) )
886
+ } ) ;
887
+
888
+ sim. run ( )
889
+ }
890
+
752
891
// # IpVersion specific tests
753
892
754
893
#[ test]
0 commit comments