Test piece-hashing edge cases

- Uneven last piece
- Even last piece
- Piece that spans multiple files
- Multiple pieces in one file

type: testing
This commit is contained in:
Casey Rodarmor 2020-03-07 19:45:26 -08:00
parent 2b19a62134
commit 027b229df1
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
2 changed files with 143 additions and 0 deletions

View File

@ -21,6 +21,11 @@ impl Md5Digest {
Self { bytes } Self { bytes }
} }
#[cfg(test)]
pub(crate) fn from_data(data: impl AsRef<[u8]>) -> Self {
md5::compute(data).into()
}
} }
impl From<md5::Digest> for Md5Digest { impl From<md5::Digest> for Md5Digest {

View File

@ -864,6 +864,144 @@ mod tests {
assert_eq!(metainfo.creation_date, None); assert_eq!(metainfo.creation_date, None);
} }
#[test]
fn uneven_last_piece() {
let mut env = test_env! {
args: [
"torrent",
"create",
"--input",
"foo",
"--announce",
"http://bar",
"--allow",
"small-piece-length",
"--piece-length",
"4",
],
tree: {
foo: "123",
},
};
env.run().unwrap();
let metainfo = env.load_metainfo("foo.torrent");
assert_eq!(metainfo.info.pieces, PieceList::from_pieces(&["123"]));
assert_eq!(
metainfo.info.mode,
Mode::Single {
length: Bytes(3),
md5sum: None,
}
)
}
#[test]
fn even_last_piece() {
let mut env = test_env! {
args: [
"torrent",
"create",
"--input",
"foo",
"--announce",
"http://bar",
"--allow",
"small-piece-length",
"--piece-length",
"4",
],
tree: {
foo: "1234",
},
};
env.run().unwrap();
let metainfo = env.load_metainfo("foo.torrent");
assert_eq!(metainfo.info.pieces, PieceList::from_pieces(&["1234"]));
assert_eq!(
metainfo.info.mode,
Mode::Single {
length: Bytes(4),
md5sum: None,
}
)
}
#[test]
fn multi_piece_file() {
let mut env = test_env! {
args: [
"torrent",
"create",
"--input",
"foo",
"--announce",
"http://bar",
"--allow",
"small-piece-length",
"--piece-length",
"2",
],
tree: {
foo: "1234",
},
};
env.run().unwrap();
let metainfo = env.load_metainfo("foo.torrent");
assert_eq!(metainfo.info.pieces, PieceList::from_pieces(&["12", "34"]));
assert_eq!(
metainfo.info.mode,
Mode::Single {
length: Bytes(4),
md5sum: None,
}
)
}
#[test]
fn multi_file_piece() {
let mut env = test_env! {
args: [
"torrent",
"create",
"--input",
"dir",
"--announce",
"http://bar",
"--allow",
"small-piece-length",
"--piece-length",
"8",
"--md5sum",
],
tree: {
dir: {
foo: "1234",
bar: "5678",
},
},
};
env.run().unwrap();
let metainfo = env.load_metainfo("dir.torrent");
assert_eq!(metainfo.info.pieces, PieceList::from_pieces(&["56781234"]));
assert_eq!(
metainfo.info.mode,
Mode::Multiple {
files: vec![
FileInfo {
path: FilePath::from_components(&["bar"]),
length: Bytes(4),
md5sum: Some(Md5Digest::from_data("5678")),
},
FileInfo {
path: FilePath::from_components(&["foo"]),
length: Bytes(4),
md5sum: Some(Md5Digest::from_data("1234")),
},
],
}
)
}
#[test] #[test]
fn single_small() { fn single_small() {
let mut env = test_env! { let mut env = test_env! {