diff --git a/src/node.rs b/src/node.rs index ab120c4..934be9d 100644 --- a/src/node.rs +++ b/src/node.rs @@ -373,53 +373,6 @@ mod tests { assert_eq!(kind, Kind::Char); } - #[test] - fn from_file_path() { - let links = Mutex::new(HashMap::new()); - let node = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap(); - let FileType::Normal(f) = node.filetype else { - panic!() - }; - assert_eq!(f.len, 1005); - let Checksum::Sha256(sum) = f.checksum else { - panic!() - }; - let mut s = String::new(); - for c in &sum { - write!(s, "{c:02x}").unwrap(); - } - assert_eq!( - s, - "5f1b6e6e31682fb6683db2e78db11e624527c897618f1a5b0a0b5256f557c22d" - ); - } - - #[test] - fn from_symlink_path() { - let _res = remove_file("test/lilnk.txt"); - symlink("li.txt", "test/lilnk.txt").unwrap(); - let links = Mutex::new(HashMap::new()); - let node = Node::from_path("test/lilnk.txt", Algorithm::Skip, &links).unwrap(); - let FileType::SoftLink(tgt) = node.filetype else { - eprintln!("Incorrect filetype: {:?}", node.filetype); - panic!(); - }; - assert_eq!(tgt, "li.txt"); - } - - #[test] - fn from_hardlink_path() { - let _res = remove_file("test/lorem.txt"); - fs::hard_link("test/li.txt", "test/lorem.txt").unwrap(); - let links = Mutex::new(HashMap::new()); - let _node0 = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap(); - let node1 = Node::from_path("test/lorem.txt", Algorithm::Sha256, &links).unwrap(); - match node1.filetype { - FileType::HardLink(s) => assert_eq!(s, "test/li.txt"), - _ => panic!(), - } - } - #[test] fn from_fifo_path() { let _res = remove_file("test/fifo"); @@ -433,16 +386,28 @@ mod tests { } #[test] - fn load_store_file() { + fn file_node() { { let fd = fs::File::create("test/li.node").unwrap(); let mut writer = BufWriter::new(fd); let links = Mutex::new(HashMap::new()); - let node = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap(); - let FileType::Normal(_) = node.filetype else { + let node = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap(); + let FileType::Normal(ref f) = node.filetype else { eprintln!("Created wrong filetype: {:?}", node.filetype); panic!(); }; + assert_eq!(f.len, 1005); + let Checksum::Sha256(sum) = f.checksum else { + panic!() + }; + let mut s = String::new(); + for c in &sum { + write!(s, "{c:02x}").unwrap(); + } + assert_eq!( + s, + "5f1b6e6e31682fb6683db2e78db11e624527c897618f1a5b0a0b5256f557c22d" + ); node.write(&mut writer).unwrap(); } let meta = fs::metadata("test/li.txt").unwrap(); @@ -453,27 +418,35 @@ mod tests { assert_eq!(meta.uid(), node.uid); assert_eq!(meta.gid(), node.gid); assert_eq!(meta.mtime(), node.mtime as i64); - let FileType::Normal(f) = node.filetype else { + let FileType::Normal(ref f) = node.filetype else { eprintln!("Read incorrect filetype: {:?}", node.filetype); panic!() }; - let Checksum::Sha1(sum) = f.checksum else { + let Checksum::Sha256(sum) = f.checksum else { panic!() }; let mut s = String::new(); for c in &sum { write!(s, "{c:02x}").unwrap(); } - assert_eq!(s, "9bf3e5b5efd22f932e100b86c83482787e82a682"); + assert_eq!( + s, + "5f1b6e6e31682fb6683db2e78db11e624527c897618f1a5b0a0b5256f557c22d" + ); assert_eq!(LI, f.data); + node.extract(Some("test/output")).unwrap(); + let f = fs::read_to_string("test/output/test/li.txt").unwrap(); + assert_eq!(f.as_bytes(), LI); } #[test] - fn load_store_symlink() { + fn symlink_node() { { let _res = remove_file("test/lilnk.txt"); symlink("li.txt", "test/lilnk.txt").unwrap(); - let _res = remove_file("test/lilnk.node"); + if PathBuf::from("test/lilnk.node").exists() { + let _res = remove_file("test/lilnk.node"); + } let fd = fs::File::create("test/lilnk.node").unwrap(); let mut writer = BufWriter::new(fd); let links = Mutex::new(HashMap::new()); @@ -493,40 +466,60 @@ mod tests { panic!(); }; assert_eq!(tgt, "li.txt"); + node.extract(Some("test/output")).unwrap(); + let tgt = fs::read_link("test/output/test/lilnk.txt").unwrap(); + assert_eq!(tgt, PathBuf::from("li.txt")); } #[test] - fn load_store_hardlink() { + fn hardlink_node() { { - let _res = remove_file("test/lihlnk.txt"); - fs::hard_link("test/li.txt", "test/lihlnk.txt").unwrap(); - let _res = remove_file("test/lihlnk.node"); - let fd = fs::File::create("test/lihlnk.node").unwrap(); - let mut writer = BufWriter::new(fd); + if PathBuf::from("test/lorem.txt").exists() { + let _res = remove_file("test/lorem.txt"); + } + fs::hard_link("test/li.txt", "test/lorem.txt").unwrap(); + if PathBuf::from("test/lorem.node").exists() { + let _res = remove_file("test/lorem.node"); + } + if PathBuf::from("test/li.node").exists() { + let _res = remove_file("test/li.node"); + } let links = Mutex::new(HashMap::new()); - let _node = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap(); - let node = Node::from_path("test/lihlnk.txt", Algorithm::Sha1, &links).unwrap(); - let FileType::HardLink(ref tgt) = node.filetype else { - eprintln!("Created wrong filetype: {:?}", node.filetype); + let node0 = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap(); + let node1 = Node::from_path("test/lorem.txt", Algorithm::Sha1, &links).unwrap(); + let FileType::HardLink(ref tgt) = node1.filetype else { + eprintln!("Created wrong filetype: {:?}", node1.filetype); panic!(); }; assert_eq!(tgt, "test/li.txt"); - node.write(&mut writer).unwrap(); + let fd = fs::File::create("test/li.node").unwrap(); + let mut writer = BufWriter::new(fd); + node0.write(&mut writer).unwrap(); + let fd = fs::File::create("test/lorem.node").unwrap(); + let mut writer = BufWriter::new(fd); + node1.write(&mut writer).unwrap(); } - let fd = fs::File::open("test/lihlnk.node").unwrap(); + let fd = fs::File::open("test/lorem.node").unwrap(); let mut reader = BufReader::new(fd); - let node = Node::read(&mut reader).unwrap(); - let FileType::HardLink(ref tgt) = node.filetype else { - eprintln!("Read incorrect filetype: {:?}", node.filetype); + let node1 = Node::read(&mut reader).unwrap(); + let FileType::HardLink(ref tgt) = node1.filetype else { + eprintln!("Read incorrect filetype: {:?}", node1.filetype); panic!(); }; assert_eq!(tgt, "test/li.txt"); + let fd = fs::File::open("test/li.node").unwrap(); + let mut reader = BufReader::new(fd); + let node0 = Node::read(&mut reader).unwrap(); + node1.extract(Some("test/output")).unwrap(); + node0.extract(Some("test/output")).unwrap(); } #[test] fn load_store_fifo() { { - let _res = remove_file("test/fifo"); + if PathBuf::from("test/fifo").exists() { + let _res = remove_file("test/fifo"); + } nix::mkfifo("test/fifo", 0o644).unwrap(); let links = Mutex::new(HashMap::new()); let node = Node::from_path("test/fifo", Algorithm::Skip, &links).unwrap(); @@ -534,7 +527,9 @@ mod tests { eprintln!("Incorrect filetype: {:?}", node.filetype); panic!(); }; - let _res = remove_file("test/fifo.node"); + if PathBuf::from("test/fifo.node").exists() { + let _res = remove_file("test/fifo.node"); + } let fd = fs::File::create("test/fifo.node").unwrap(); let mut writer = BufWriter::new(fd); node.write(&mut writer).unwrap(); @@ -547,45 +542,4 @@ mod tests { panic!(); }; } - - #[test] - fn extract_file() { - let links = Mutex::new(HashMap::new()); - let node = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap(); - node.extract(Some("test/output")).unwrap(); - let f = fs::read_to_string("test/output/test/li.txt").unwrap(); - assert_eq!(f.as_bytes(), LI); - } - - #[test] - fn extract_symlink() { - let links = Mutex::new(HashMap::new()); - let _res = remove_file("test/lilnk.txt"); - symlink("li.txt", "test/lilnk.txt").unwrap(); - let node = Node::from_path("test/lilnk.txt", Algorithm::Md5, &links).unwrap(); - node.extract(Some("test/output")).unwrap(); - let tgt = fs::read_link("test/output/test/lilnk.txt").unwrap(); - assert_eq!(tgt, PathBuf::from("li.txt")); - } - - #[test] - fn extract_hardlink() { - let links = Mutex::new(HashMap::new()); - if !PathBuf::from("test/lihlnk.txt").exists() { - fs::hard_link("test/li.txt", "test/lihlnk.txt").unwrap(); - } - let node0 = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap(); - let FileType::Normal(_) = node0.filetype else { - eprintln!("Created wrong filetype for node0: {:?}", node0.filetype); - panic!(); - }; - let node1 = Node::from_path("test/lihlnk.txt", Algorithm::Sha256, &links).unwrap(); - let FileType::HardLink(ref tgt) = node1.filetype else { - eprintln!("Created wrong filetype for node1: {:?}", node1.filetype); - panic!(); - }; - assert_eq!(tgt, "test/li.txt"); - node1.extract(Some("test/output")).unwrap(); - node0.extract(Some("test/output")).unwrap(); - } }