diff --git a/haggis-454c3e482188.core b/haggis-454c3e482188.core deleted file mode 100644 index b4c0672..0000000 Binary files a/haggis-454c3e482188.core and /dev/null differ diff --git a/src/checksum.rs b/src/checksum.rs index ec03276..bc3acbc 100644 --- a/src/checksum.rs +++ b/src/checksum.rs @@ -87,3 +87,57 @@ impl Checksum { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + + const MD5BUF: [u8; 16] = [ + 102, 95, 170, 95, 1, 29, 157, 44, 184, 244, 181, 209, 47, 30, 223, 21, + ]; + + const SHA1BUF: [u8; 20] = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + ]; + + #[test] + fn load_store_md5() { + { + let mut fd = File::options() + .create(true) + .truncate(true) + .write(true) + .open("test/md5") + .unwrap(); + let cksum = Checksum::Md5(MD5BUF); + cksum.write(&mut fd).unwrap(); + } + let mut fd = File::open("test/md5").unwrap(); + let cksum = Checksum::read(&mut fd).unwrap(); + match cksum { + Checksum::Md5(sum) => assert_eq!(sum, MD5BUF), + _ => unreachable!(), + } + } + + #[test] + fn load_store_sha1() { + { + let mut fd = File::options() + .create(true) + .truncate(true) + .write(true) + .open("test/sha1") + .unwrap(); + let cksum = Checksum::Sha1(SHA1BUF); + cksum.write(&mut fd).unwrap(); + } + let mut fd = File::open("test/sha1").unwrap(); + let cksum = Checksum::read(&mut fd).unwrap(); + match cksum { + Checksum::Sha1(sum) => assert_eq!(sum, SHA1BUF), + _ => unreachable!(), + } + } +} diff --git a/src/file.rs b/src/file.rs index 289702c..cb8f228 100644 --- a/src/file.rs +++ b/src/file.rs @@ -84,3 +84,35 @@ impl File { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File as Fd; + + const SHA1BUF: [u8; 20] = [ + 155, 243, 229, 181, 239, 210, 47, 147, 46, 16, 11, 134, 200, 52, 130, 120, 126, 130, 166, + 130, + ]; + + #[test] + fn load_store() { + let file = File { + len: 1005, + checksum: Checksum::Sha1(SHA1BUF), + data: include_bytes!("../test/li.txt").to_vec(), + }; + { + let mut fd = Fd::create("test/file").unwrap(); + file.write(&mut fd).unwrap(); + } + let mut fd = Fd::open("test/file").unwrap(); + let res = File::read(&mut fd).unwrap(); + assert_eq!(file.len, res.len); + assert_eq!(&file.data, &res.data); + match res.checksum { + Checksum::Sha1(sum) => assert_eq!(sum, SHA1BUF), + _ => unreachable!(), + } + } +} diff --git a/src/filetype.rs b/src/filetype.rs index 1253042..8b0f50b 100644 --- a/src/filetype.rs +++ b/src/filetype.rs @@ -4,6 +4,7 @@ use { }; #[repr(u8)] +#[derive(Clone, Copy)] pub(crate) enum Flag { Normal = 0, HardLink = 1, @@ -28,7 +29,7 @@ impl TryFrom for Flag { 5 => Ok(Self::Block), 6 => Ok(Self::Fifo), 7 => Ok(Self::Eof), - 8 => Err(Error::UnknownFileType), + _ => Err(Error::UnknownFileType), } } } @@ -119,33 +120,14 @@ impl FileType { pub(crate) fn write(&self, writer: &mut T) -> Result<(), Error> { match self { - Self::Normal(f) => { - writer.write_all(&[0])?; - f.write(writer)?; - } - Self::HardLink(s) => { - writer.write_all(&[1])?; + Self::Normal(f) => f.write(writer)?, + Self::HardLink(s) | Self::SoftLink(s) => { let len = s.len() as u64; writer.write_all(&len.to_le_bytes())?; writer.write_all(s.as_bytes())?; } - Self::SoftLink(s) => { - writer.write_all(&[2])?; - let len = s.len() as u64; - writer.write_all(&len.to_le_bytes())?; - writer.write_all(s.as_bytes())?; - } - Self::Directory => writer.write_all(&[3])?, - Self::Character(s) => { - writer.write_all(&[4])?; - s.write(writer)?; - } - Self::Block(s) => { - writer.write_all(&[5])?; - s.write(writer)?; - } - Self::Fifo => writer.write_all(&[6])?, - Self::Eof => {} + Self::Character(s) | Self::Block(s) => s.write(writer)?, + _ => {} } Ok(()) } diff --git a/src/node.rs b/src/node.rs index fb05b86..2877c17 100644 --- a/src/node.rs +++ b/src/node.rs @@ -108,9 +108,8 @@ impl Node { let len: u16 = self.name.len().try_into()?; writer.write_all(&len.to_le_bytes())?; writer.write_all(self.name.as_bytes())?; - [self.uid, self.gid] - .iter() - .try_for_each(|f| writer.write_all(&f.to_le_bytes()))?; + writer.write_all(&self.uid.to_le_bytes())?; + writer.write_all(&self.gid.to_le_bytes())?; writer.write_all(&self.mtime.to_le_bytes())?; let mode = Flag::from(&self.filetype).append_mode(self.mode); writer.write_all(&mode.to_le_bytes())?; @@ -207,10 +206,10 @@ impl Node { }; Ok(Self { name, - mode, uid, gid, mtime, + mode: (mode & !(0o111 << 13)).try_into()?, filetype, }) } @@ -246,19 +245,19 @@ impl Node { match self.filetype { FileType::Eof => {} FileType::Fifo => { - nix::mkfifo(&path, self.mode)?; + nix::mkfifo(&path, self.mode.into())?; if euid == 0 { nix::chown(&path, self.uid, self.gid)?; } } FileType::Block(ref b) => { if euid == 0 { - nix::mknod(&path, self.mode, b.major, b.minor)?; + nix::mknod(&path, self.mode.into(), b.major, b.minor)?; } } FileType::Character(ref c) => { if euid == 0 { - nix::mknod(&path, self.mode, c.major, c.minor)?; + nix::mknod(&path, self.mode.into(), c.major, c.minor)?; } } FileType::Normal(ref n) => { @@ -274,7 +273,7 @@ impl Node { if euid == 0 { nix::chown(&path, self.uid, self.gid)?; } - nix::chmod(&path, self.mode)?; + nix::chmod(&path, self.mode.into())?; } FileType::HardLink(ref t) => { let target = if let Some(prefix) = prefix { @@ -303,7 +302,7 @@ impl Node { } nix::chmod( dir.to_str().ok_or(Error::BadPath)?, - self.mode & 0o7777 | 0o100, + (self.mode & 0o7777 | 0o100).into(), )?; Ok(()) } @@ -334,16 +333,14 @@ mod tests { } #[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() { + fn load_store() { + { + 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(); + } let fd = std::fs::File::open("test/li.node").unwrap(); let mut reader = BufReader::new(fd); let node = Node::read(&mut reader).unwrap(); diff --git a/src/special.rs b/src/special.rs index dcf0c4a..c4c4d9d 100644 --- a/src/special.rs +++ b/src/special.rs @@ -49,3 +49,30 @@ impl Special { } } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + + #[test] + fn load_store() { + { + let mut fd = File::options() + .write(true) + .create(true) + .truncate(true) + .open("test/device") + .unwrap(); + let dev = Special { + major: 42, + minor: 69, + }; + dev.write(&mut fd).unwrap(); + } + let mut fd = File::open("test/device").unwrap(); + let dev = Special::read(&mut fd).unwrap(); + assert_eq!(dev.major, 42); + assert_eq!(dev.minor, 69); + } +} diff --git a/test/device b/test/device new file mode 100644 index 0000000..698e4c6 Binary files /dev/null and b/test/device differ diff --git a/test/file b/test/file new file mode 100644 index 0000000..0c1ee0b Binary files /dev/null and b/test/file differ diff --git a/test/li.node b/test/li.node index 1cdd2c8..ae67c6c 100644 Binary files a/test/li.node and b/test/li.node differ diff --git a/test/md5 b/test/md5 new file mode 100644 index 0000000..d8848b1 Binary files /dev/null and b/test/md5 differ diff --git a/test/sha1 b/test/sha1 new file mode 100644 index 0000000..2a1bdc4 Binary files /dev/null and b/test/sha1 differ