Skip to content

Commit f54e6e8

Browse files
committed
some more tests
1 parent cda7102 commit f54e6e8

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ ciborium = "0.2.2"
1919
[dev-dependencies]
2020
candid = "0.10.14"
2121
pocket-ic = "9.0.2"
22+

src/fs_tests.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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") {

src/runtime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pub(crate) mod dir;
22
pub(crate) mod fd;
33
pub(crate) mod file;
44
pub(crate) mod structure_helpers;
5+
56
pub mod types;

src/test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub fn write_text_file(
9393

9494
let file_fd = fs.open(parent_fd, path, FdStat::default(), OpenFlags::CREATE, 0)?;
9595

96-
write_text_fd(fs, file_fd, content, times)
96+
write_text_fd(fs, file_fd, content, times)?;
97+
98+
fs.close(file_fd)
9799
}
98100

99101
#[cfg(test)]

0 commit comments

Comments
 (0)