@@ -930,10 +930,10 @@ mod tests {
930930
931931 fs. unmount_memory_file ( file_name) . unwrap ( ) ;
932932
933+ // init new memory into a file
933934 fs. mount_memory_file ( file_name, Box :: new ( memory2. clone ( ) ) )
934935 . unwrap ( ) ;
935936
936- // init new memory into a file
937937 fs. init_memory_file ( file_name) . unwrap ( ) ;
938938
939939 let mut buf1 = [ 0u8 ; 10 ] ;
@@ -948,6 +948,66 @@ mod tests {
948948 }
949949 }
950950
951+ #[ test]
952+ fn test_mounts ( ) {
953+ let dir_fd = 3 ;
954+
955+ for mut fs in test_fs_setups ( "" ) {
956+ let memory = new_vector_memory ( ) ;
957+ let file_name = "file.txt" ;
958+ let hello_message = "Hello host" . to_string ( ) ;
959+ let hello_message2 = "1234Hello from regular file" . to_string ( ) ;
960+
961+ fs. mount_memory_file ( file_name, Box :: new ( memory. clone ( ) ) )
962+ . unwrap ( ) ;
963+
964+ // write something into a host memory file
965+ write_text_file ( & mut fs, dir_fd, file_name, & hello_message, 1 ) . unwrap ( ) ;
966+
967+ // the memory should contain the file now
968+ let v: Vec < u8 > = memory. borrow ( ) . clone ( ) ;
969+ assert_eq ! ( & v[ 0 ..hello_message. len( ) ] , hello_message. as_bytes( ) ) ;
970+
971+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
972+ assert_eq ! ( str , hello_message) ;
973+
974+ // unmount file, the file.txt should become empty
975+ fs. unmount_memory_file ( file_name) . unwrap ( ) ;
976+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
977+ assert_eq ! ( str , "" . to_string( ) ) ;
978+
979+ // mount again, the old content should recover
980+ fs. mount_memory_file ( file_name, Box :: new ( memory. clone ( ) ) )
981+ . unwrap ( ) ;
982+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
983+ assert_eq ! ( str , hello_message) ;
984+
985+ // store mounted contents into the host file, check the host file content is renewed
986+ fs. store_memory_file ( file_name) . unwrap ( ) ;
987+ fs. unmount_memory_file ( file_name) . unwrap ( ) ;
988+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
989+ assert_eq ! ( str , hello_message) ;
990+
991+ // write some other content message,
992+ // check there is a new content now
993+ write_text_file ( & mut fs, dir_fd, file_name, & hello_message2, 1 ) . unwrap ( ) ;
994+
995+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
996+ assert_eq ! ( str , hello_message2) ;
997+
998+ // after mounting, we still have the old content
999+ fs. mount_memory_file ( file_name, Box :: new ( memory. clone ( ) ) )
1000+ . unwrap ( ) ;
1001+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
1002+ assert_eq ! ( str , hello_message) ;
1003+
1004+ // initializing should recover the data from the host file to the mounted memory
1005+ fs. init_memory_file ( file_name) . unwrap ( ) ;
1006+ let str = read_text_file ( & mut fs, dir_fd, file_name, 0 , 1000 ) ;
1007+ assert_eq ! ( str , hello_message2) ;
1008+ }
1009+ }
1010+
9511011 #[ test]
9521012 fn writing_from_different_file_descriptors ( ) {
9531013 for mut fs in test_fs_setups ( "f1/f2/text.txt" ) {
0 commit comments