Skip to content

Commit

Permalink
Test case: Use read_sample instead of open_sample when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mindeng committed Jul 13, 2024
1 parent 0cd94a2 commit d9e4370
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 64 deletions.
27 changes: 5 additions & 22 deletions src/bbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,15 @@ fn parse_cstr(input: &[u8]) -> IResult<&[u8], String> {

#[cfg(test)]
mod tests {
use std::io::Read;

use crate::testkit::open_sample;
use crate::testkit::read_sample;

use super::*;
use nom::error::make_error;
use test_case::test_case;

#[test_case("exif.heic")]
fn travel_heic(path: &str) {
let mut reader = open_sample(path).unwrap();
let mut buf = Vec::new();
reader.read_to_end(buf.as_mut()).unwrap();
let buf = read_sample(path).unwrap();
let mut boxes = Vec::new();

let (remain, bbox) = travel_while(&buf, |bbox| {
Expand Down Expand Up @@ -389,9 +385,7 @@ mod tests {

#[test_case("meta.mov")]
fn travel_mov(path: &str) {
let mut reader = open_sample(path).unwrap();
let mut buf = Vec::new();
reader.read_to_end(buf.as_mut()).unwrap();
let buf = read_sample(path).unwrap();
let mut boxes = Vec::new();

let (remain, bbox) = travel_while(&buf, |bbox| {
Expand Down Expand Up @@ -444,9 +438,7 @@ mod tests {

#[test_case("meta.mp4")]
fn travel_mp4(path: &str) {
let mut reader = open_sample(path).unwrap();
let mut buf = Vec::new();
reader.read_to_end(buf.as_mut()).unwrap();
let buf = read_sample(path).unwrap();
let mut boxes = Vec::new();

let (remain, bbox) = travel_while(&buf, |bbox| {
Expand Down Expand Up @@ -531,16 +523,7 @@ mod tests {
// atom.
#[test_case("meta.mp4")]
fn find_android_gps_box(path: &str) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();

// let (_, bbox) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
// println!("bbox: {:?}", bbox.header);
// let (_, bbox) = travel_while(bbox.body_data(), |b| b.box_type() != "udta").unwrap();
// println!("bbox: {:?}", bbox.header);
// let (_, bbox) = travel_while(bbox.body_data(), |b| b.box_type() != "©xyz").unwrap();

let buf = read_sample(path).unwrap();
let (_, bbox) = find_box(&buf, "moov/udta/©xyz").unwrap();
let bbox = bbox.unwrap();
// println!("bbox: {:?}", bbox.header);
Expand Down
14 changes: 3 additions & 11 deletions src/bbox/ilst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,14 @@ fn parse_value(type_code: u32, data: &[u8]) -> crate::Result<EntryValue> {

#[cfg(test)]
mod tests {
use std::io::Read;

use crate::{bbox::travel_while, testkit::open_sample};
use crate::{bbox::travel_while, testkit::read_sample};

use super::*;
use test_case::test_case;

#[test_case("meta.mov")]
fn ilst_box(path: &str) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();

let buf = read_sample(path).unwrap();
let (_, bbox) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
let bbox = bbox.unwrap();
let (_, bbox) = travel_while(bbox.body_data(), |b| b.box_type() != "meta").unwrap();
Expand Down Expand Up @@ -168,10 +163,7 @@ mod tests {

#[test_case("embedded-in-heic.mov")]
fn heic_mov_ilst(path: &str) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();

let buf = read_sample(path).unwrap();
let (_, moov) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
let moov = moov.unwrap();
let (_, meta) = travel_while(moov.body_data(), |b| b.box_type() != "meta").unwrap();
Expand Down
14 changes: 3 additions & 11 deletions src/bbox/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,17 @@ impl KeyEntry {

#[cfg(test)]
mod tests {
use std::io::Read;

use crate::{
bbox::{travel_while, ParseBox},
testkit::open_sample,
testkit::read_sample,
};

use super::*;
use test_case::test_case;

#[test_case("meta.mov", 4133, 0x01b9, 0xc9)]
fn keys_box(path: &str, moov_size: u64, meta_size: u64, keys_size: u64) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();

let buf = read_sample(path).unwrap();
let (_, moov) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
let moov = moov.unwrap();
let (_, meta) = travel_while(moov.body_data(), |b| b.box_type() != "meta").unwrap();
Expand Down Expand Up @@ -132,10 +127,7 @@ mod tests {

#[test_case("embedded-in-heic.mov", 0x1790, 0x0372, 0x1ce)]
fn heic_mov_keys(path: &str, moov_size: u64, meta_size: u64, keys_size: u64) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();

let buf = read_sample(path).unwrap();
let (_, moov) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
let moov = moov.unwrap();
let (_, meta) = travel_while(moov.body_data(), |b| b.box_type() != "meta").unwrap();
Expand Down
10 changes: 2 additions & 8 deletions src/bbox/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,14 @@ pub struct ItemLocation {

#[cfg(test)]
mod tests {
use std::io::Read;

use crate::{bbox::travel_while, testkit::open_sample};
use crate::{bbox::travel_while, testkit::read_sample};

use super::*;
use test_case::test_case;

#[test_case("exif.heic", 2618)]
fn meta(path: &str, meta_size: usize) {
let mut reader = open_sample(path).unwrap();
let mut buf = Vec::new();
reader.read_to_end(buf.as_mut()).unwrap();
assert_eq!(buf.len() as u64, reader.metadata().unwrap().len());

let buf = read_sample(path).unwrap();
let (_, bbox) = travel_while(&buf, |bbox| {
// println!("got {}", bbox.header.box_type);
bbox.box_type() != "meta"
Expand Down
8 changes: 2 additions & 6 deletions src/bbox/mvhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ impl ParseBody<MvhdBox> for MvhdBox {

#[cfg(test)]
mod tests {
use std::io::Read;

use crate::{
bbox::{travel_while, ParseBox},
testkit::open_sample,
testkit::read_sample,
};

use super::*;
Expand All @@ -106,9 +104,7 @@ mod tests {
1063
)]
fn mvhd_box(path: &str, time_utc: &str, time_east8: &str, milliseconds: u32) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let buf = read_sample(path).unwrap();

let (_, bbox) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
let bbox = bbox.unwrap();
Expand Down
8 changes: 2 additions & 6 deletions src/bbox/tkhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,15 @@ fn find_video_track(input: &[u8]) -> crate::Result<Option<BoxHolder>> {

#[cfg(test)]
mod tests {
use std::io::Read;

use crate::{bbox::travel_while, testkit::open_sample};
use crate::{bbox::travel_while, testkit::read_sample};

use super::*;
use test_case::test_case;

#[test_case("meta.mov", 720, 1280)]
#[test_case("meta.mp4", 1920, 1080)]
fn tkhd_box(path: &str, width: u32, height: u32) {
let mut f = open_sample(path).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let buf = read_sample(path).unwrap();

let (_, bbox) = travel_while(&buf, |b| b.box_type() != "moov").unwrap();
let bbox = bbox.unwrap();
Expand Down

0 comments on commit d9e4370

Please sign in to comment.