Fix bug caused by writing big endian bytes instead of little endian for

file sizes; Fix bug caused by writing ascii characters instead of
integers for enum tags;
This commit is contained in:
Nathan Fisher 2023-07-10 01:02:04 -04:00
parent f1ca04d706
commit 17c25c54bc
6 changed files with 66 additions and 7 deletions

BIN
haggis-454c3e482188.core Normal file

Binary file not shown.

View File

@ -68,18 +68,18 @@ impl Checksum {
pub(crate) fn write<T: 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(())
}

View File

@ -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<T: 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(())

View File

@ -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);
}
}

BIN
test/li.node Normal file

Binary file not shown.

13
test/li.txt Normal file
View File

@ -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.