Skip to content

Commit

Permalink
Test piece-hashing edge cases
Browse files Browse the repository at this point in the history
- Uneven last piece
- Even last piece
- Piece that spans multiple files
- Multiple pieces in one file

type: testing
  • Loading branch information
casey committed Apr 8, 2020
1 parent 2b19a62 commit 027b229
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/md5_digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ impl Md5Digest {

Self { bytes }
}

#[cfg(test)]
pub(crate) fn from_data(data: impl AsRef<[u8]>) -> Self {
md5::compute(data).into()
}
}

impl From<md5::Digest> for Md5Digest {
Expand Down
138 changes: 138 additions & 0 deletions src/subcommand/torrent/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,144 @@ mod tests {
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]
fn single_small() {
let mut env = test_env! {
Expand Down

0 comments on commit 027b229

Please sign in to comment.