diff --git a/haggis-454c3e482188.core b/haggis-454c3e482188.core new file mode 100644 index 0000000..b4c0672 Binary files /dev/null and b/haggis-454c3e482188.core differ diff --git a/src/checksum.rs b/src/checksum.rs index 556e3b7..f54f86c 100644 --- a/src/checksum.rs +++ b/src/checksum.rs @@ -68,18 +68,18 @@ impl Checksum { pub(crate) fn write(&self, writer: &mut T) -> Result<(), Error> { match self { Self::Md5(sum) => { - writer.write_all(&[b'0'])?; + writer.write_all(&[0])?; writer.write_all(&sum[..])?; } Self::Sha1(sum) => { - writer.write_all(&[b'1'])?; + writer.write_all(&[1])?; writer.write_all(&sum[..])?; } Self::Sha256(sum) => { - writer.write_all(&[b'2'])?; + writer.write_all(&[2])?; writer.write_all(&sum[..])?; } - Self::Skip => writer.write_all(&[b'3'])?, + Self::Skip => writer.write_all(&[3])?, } Ok(()) } diff --git a/src/file.rs b/src/file.rs index 7fd120b..289702c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -25,7 +25,7 @@ impl File { let checksum = Checksum::read(reader)?; let mut data = Vec::with_capacity(len.try_into()?); let mut handle = reader.take(len); - handle.read_exact(&mut data)?; + handle.read_to_end(&mut data)?; match checksum { Checksum::Md5(sum) => { let mut hasher = Md5::new(); @@ -78,7 +78,7 @@ impl File { } pub(crate) fn write(&self, writer: &mut T) -> Result<(), Error> { - writer.write_all(&self.len.to_be_bytes())?; + writer.write_all(&self.len.to_le_bytes())?; Checksum::write(&self.checksum, writer)?; writer.write_all(&self.data)?; Ok(()) diff --git a/src/node.rs b/src/node.rs index f0a6ab9..d3d7eff 100644 --- a/src/node.rs +++ b/src/node.rs @@ -77,7 +77,7 @@ impl Node { } let mut name = Vec::with_capacity(len.try_into()?); let mut handle = reader.take(len); - handle.read_exact(&mut name)?; + handle.read_to_end(&mut name)?; let mut buf = [0; 20]; reader.read_exact(&mut buf)?; let mode: [u8; 4] = buf[..4].try_into()?; @@ -291,3 +291,49 @@ impl Node { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fmt::Write; + + static LI: &[u8] = include_bytes!("../test/li.txt"); + + #[test] + fn from_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 write() { + let fd = std::fs::File::create("test/li.node").unwrap(); + let mut writer = std::io::BufWriter::new(fd); + let links = Mutex::new(HashMap::new()); + let node = Node::from_path("test/li.txt", Algorithm::Sha1, &links).unwrap(); + node.write(&mut writer).unwrap(); + } + + #[test] + fn read() { + let fd = std::fs::File::open("test/li.node").unwrap(); + let mut reader = BufReader::new(fd); + let node = Node::read(&mut reader).unwrap(); + let FileType::Normal(f) = node.filetype else { panic!() }; + let Checksum::Sha1(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!(LI, f.data); + } +} diff --git a/test/li.node b/test/li.node new file mode 100644 index 0000000..1cdd2c8 Binary files /dev/null and b/test/li.node differ diff --git a/test/li.txt b/test/li.txt new file mode 100644 index 0000000..a70445c --- /dev/null +++ b/test/li.txt @@ -0,0 +1,13 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ac neque sit amet +leo rutrum placerat non in justo. Donec sed velit non ante fermentum scelerisque +ut eu ligula. Etiam maximus tellus sapien, eget placerat mi maximus vehicula. +Phasellus sit amet sapien at mauris tincidunt rutrum. Nunc scelerisque accumsan +felis, egestas commodo ligula. Nulla mauris lacus, porttitor eu laoreet dignissim, +consectetur sit amet sapien. Pellentesque habitant morbi tristique senectus et +netus et malesuada fames ac turpis egestas. Etiam laoreet, mauris ut euismod +convallis, purus nunc faucibus dui, in malesuada purus massa sit amet velit. +Aenean neque enim, luctus ut urna a, laoreet varius sem. Aliquam interdum +ultricies metus, in dignissim augue sagittis ut. Aliquam tempus felis sed lacinia +fringilla. Curabitur dictum, eros eget hendrerit aliquet, ante tellus consectetur +massa, quis dignissim libero metus ut nibh. Mauris egestas quis lorem nec volutpat. +In et euismod elit, vestibulum convallis nulla.