File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -721,4 +721,31 @@ mod tests {
721
721
722
722
assert_eq ! ( cstr_hash, cstring_hash) ;
723
723
}
724
+
725
+ #[ test]
726
+ fn from_bytes_with_nul ( ) {
727
+ let data = b"123\0 " ;
728
+ let cstr = CStr :: from_bytes_with_nul ( data) ;
729
+ assert_eq ! ( cstr. map( CStr :: to_bytes) , Some ( & b"123" [ ..] ) ) ;
730
+ assert_eq ! ( cstr. map( CStr :: to_bytes_with_nul) , Some ( & b"123\0 " [ ..] ) ) ;
731
+
732
+ unsafe {
733
+ let cstr_unchecked = CStr :: from_bytes_with_nul_unchecked ( data) ;
734
+ assert_eq ! ( cstr, Some ( cstr_unchecked) ) ;
735
+ }
736
+ }
737
+
738
+ #[ test]
739
+ fn from_bytes_with_nul_unterminated ( ) {
740
+ let data = b"123" ;
741
+ let cstr = CStr :: from_bytes_with_nul ( data) ;
742
+ assert ! ( cstr. is_none( ) ) ;
743
+ }
744
+
745
+ #[ test]
746
+ fn from_bytes_with_nul_interior ( ) {
747
+ let data = b"1\0 23\0 " ;
748
+ let cstr = CStr :: from_bytes_with_nul ( data) ;
749
+ assert ! ( cstr. is_none( ) ) ;
750
+ }
724
751
}
You can’t perform that action at this time.
0 commit comments