Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dholroyd committed Apr 10, 2021
1 parent 4a7caca commit 7a478a0
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ log = "0.4"
smptera-format-identifiers-rust = "0.3.0"

[dev-dependencies]
matches = "0.1.6"
assert_matches = "1.5.0"
bitstream-io = "0.9"
criterion = "0.3"
hex-literal = "0.2.1"
Expand Down
30 changes: 21 additions & 9 deletions src/descriptor/avcvideo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! usage of certain AVC stream features.

use super::DescriptorError;
use crate::descriptor::descriptor_len;
use std::fmt;

/// Descriptor holding copies of properties from the AVC metadata such as AVC 'profile' and 'level'.
Expand All @@ -15,11 +16,8 @@ impl<'buf> AvcVideoDescriptor<'buf> {
/// slice.
pub fn new(tag: u8, buf: &'buf [u8]) -> Result<AvcVideoDescriptor<'buf>, DescriptorError> {
assert_eq!(tag, Self::TAG);
if buf.len() < 4 {
Err(DescriptorError::BufferTooShort { buflen: buf.len() })
} else {
Ok(AvcVideoDescriptor { buf })
}
descriptor_len(buf, tag, 4)?;
Ok(AvcVideoDescriptor { buf })
}

/// The AVC _profile_ used in this stream will be equal to, or lower than, this value
Expand Down Expand Up @@ -98,20 +96,34 @@ impl fmt::Debug for AvcVideoDescriptor<'_> {
#[cfg(test)]
mod test {
use super::super::{CoreDescriptors, Descriptor};
use assert_matches::assert_matches;
use hex_literal::*;

#[test]
fn descriptor() {
let data = hex!("280442c01e3f");
let desc = CoreDescriptors::from_bytes(&data[..]).unwrap();
if let CoreDescriptors::AvcVideo(avc_video) = desc {
assert_matches!(desc, CoreDescriptors::AvcVideo(avc_video) => {
assert_eq!(avc_video.level_idc(), 30);
assert_eq!(avc_video.constraint_set0_flag(), true);
assert_eq!(avc_video.constraint_set1_flag(), true);
assert_eq!(avc_video.constraint_set3_flag(), false);
assert_eq!(avc_video.constraint_set4_flag(), false);
assert_eq!(avc_video.constraint_set5_flag(), false);
assert_eq!(avc_video.avc_compatible_flags(), 0);
assert_eq!(avc_video.profile_idc(), 66);
assert_eq!(avc_video.avc_still_present(), false);
assert_eq!(avc_video.avc_24_hour_picture_flag(), false);
assert_eq!(avc_video.frame_packing_sei_not_present_flag(), true);
} else {
panic!("unexpected {:?}", desc);
}
})
}

#[test]
fn debug() {
let data = hex!("280442c01e3f");
let desc = CoreDescriptors::from_bytes(&data[..]).unwrap();
assert_matches!(desc, CoreDescriptors::AvcVideo(avc_video) => {
assert!(!format!("{:?}", avc_video).is_empty());
});
}
}
28 changes: 24 additions & 4 deletions src/descriptor/iso_639_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,42 @@ impl<'buf> fmt::Debug for Iso639LanguageDescriptor<'buf> {
mod test {
use super::super::{CoreDescriptors, Descriptor};
use super::*;
use assert_matches::assert_matches;
use encoding;
use hex_literal::*;
use matches::assert_matches;

#[test]
fn descriptor() {
let data = hex!("0a04656e6700");
let desc = CoreDescriptors::from_bytes(&data).unwrap();
if let CoreDescriptors::ISO639Language(iso_639_language) = desc {
assert_matches!(desc, CoreDescriptors::ISO639Language(iso_639_language) => {
let mut langs = iso_639_language.languages();
let first = langs.next().unwrap();
assert_eq!("eng", first.code(encoding::DecoderTrap::Strict).unwrap());
assert_eq!(AudioType::Undefined, first.audio_type());
assert_matches!(langs.next(), None);
} else {
panic!("wrong descriptor type {:?}", desc);
});
}

#[test]
fn debug() {
let data = hex!("0a04656e6700");
let desc = CoreDescriptors::from_bytes(&data).unwrap();
assert_matches!(desc, CoreDescriptors::ISO639Language(iso_639_language) => {
assert!(!format!("{:?}", iso_639_language).is_empty());
});
}

/*
TODO: we need to adjust the iterator to return Result<Language,?>
#[test]
fn too_short() {
let data = hex!("0a03656e67");
let desc = CoreDescriptors::from_bytes(&data).unwrap();
if let CoreDescriptors::ISO639Language(iso_639_language) = desc {
let mut langs = iso_639_language.languages();
langs.next();
}
}
*/
}
17 changes: 7 additions & 10 deletions src/descriptor/max_bitrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! May be attached an an elementary stream, indicating the max bitrate of that elementary stream,
//! or to the program as a whole. In both cases it appears in the PMT.

use super::descriptor_len;
use super::DescriptorError;
use std::fmt;

Expand All @@ -20,13 +21,9 @@ impl<'buf> MaximumBitrateDescriptor<'buf> {
tag: u8,
buf: &'buf [u8],
) -> Result<MaximumBitrateDescriptor<'buf>, DescriptorError> {
println!("{:x?}", buf);
assert_eq!(tag, Self::TAG);
if buf.len() < 3 {
Err(DescriptorError::BufferTooShort { buflen: buf.len() })
} else {
Ok(MaximumBitrateDescriptor { buf })
}
descriptor_len(buf, tag, 3)?;
Ok(MaximumBitrateDescriptor { buf })
}

/// The maximum bitrate expressed in units of 50 bytes per second
Expand Down Expand Up @@ -54,17 +51,17 @@ impl fmt::Debug for MaximumBitrateDescriptor<'_> {
#[cfg(test)]
mod test {
use super::super::{CoreDescriptors, Descriptor};
use assert_matches::assert_matches;
use hex_literal::*;

#[test]
fn descriptor() {
let data = hex!("0e03c00184");
let desc = CoreDescriptors::from_bytes(&data[..]).unwrap();
if let CoreDescriptors::MaximumBitrate(max_bitrate) = desc {
assert_matches!(desc, CoreDescriptors::MaximumBitrate(max_bitrate) => {
assert_eq!(max_bitrate.maximum_bitrate(), 388);
assert_eq!(max_bitrate.maximum_bits_per_second(), 155200);
} else {
panic!("unexpected {:?}", desc);
}
assert!(!format!("{:?}", max_bitrate).is_empty());
});
}
}
43 changes: 43 additions & 0 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,46 @@ pub enum DescriptorError {
/// There is no mapping defined of the given descriptor tag value to a `Descriptor` value.
UnhandledTagValue(u8),
}

pub(crate) fn descriptor_len(buf: &[u8], tag: u8, len: usize) -> Result<(), DescriptorError> {
if buf.len() < len {
Err(DescriptorError::NotEnoughData {
tag,
actual: buf.len(),
expected: len,
})
} else {
Ok(())
}
}

#[cfg(test)]
mod test {
use crate::descriptor::{descriptor_len, CoreDescriptors, DescriptorError, DescriptorIter};
use assert_matches::assert_matches;
use hex_literal::*;

#[test]
fn core() {
let data = hex!("000100");
let mut iter = DescriptorIter::<CoreDescriptors<'_>>::new(&data);
let desc = iter.next().unwrap();
assert!(!format!("{:?}", desc).is_empty());
assert_matches!(desc, Ok(CoreDescriptors::Reserved(d)) => {
assert_eq!(d.tag, 0);
assert_eq!(d.payload, &[0]);
});
}

#[test]
fn not_enough_data() {
assert_matches!(
descriptor_len(b"", 0, 1),
Err(DescriptorError::NotEnoughData {
tag: 0,
actual: 0,
expected: 1,
})
);
}
}
27 changes: 11 additions & 16 deletions src/descriptor/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! stream will be following

use super::DescriptorError;
use crate::descriptor::descriptor_len;
use smptera_format_identifiers_rust::FormatIdentifier;
use std::fmt;

Expand All @@ -15,16 +16,9 @@ impl<'buf> RegistrationDescriptor<'buf> {
pub const TAG: u8 = 5;
/// Construct a `RegistrationDescriptor` instance that will parse the data from the given
/// slice.
pub fn new(_tag: u8, buf: &'buf [u8]) -> Result<RegistrationDescriptor<'buf>, DescriptorError> {
if buf.len() < 4 {
Err(DescriptorError::NotEnoughData {
tag: Self::TAG,
actual: buf.len(),
expected: 4,
})
} else {
Ok(RegistrationDescriptor { buf })
}
pub fn new(tag: u8, buf: &'buf [u8]) -> Result<RegistrationDescriptor<'buf>, DescriptorError> {
descriptor_len(buf, tag, 4)?;
Ok(RegistrationDescriptor { buf })
}

/// Format identifier value assigned by a _Registration Authority_.
Expand Down Expand Up @@ -71,17 +65,18 @@ impl<'buf> fmt::Debug for RegistrationDescriptor<'buf> {
#[cfg(test)]
mod test {
use super::super::{CoreDescriptors, Descriptor};
use super::*;
use assert_matches::assert_matches;
use hex_literal::*;
use matches::assert_matches;

#[test]
fn descriptor() {
let data = hex!("050443554549");
let desc = CoreDescriptors::from_bytes(&data[..]).unwrap();
assert_matches!(
desc,
CoreDescriptors::Registration(RegistrationDescriptor { buf: b"CUEI" })
);
assert_matches!(desc, CoreDescriptors::Registration(reg) => {
let expected = smptera_format_identifiers_rust::FormatIdentifier::from(&b"CUEI"[..]);
assert_eq!(reg.format_identifier(), expected);
assert!(reg.is_format(expected));
assert!(!format!("{:?}", reg).is_empty())
});
}
}
2 changes: 1 addition & 1 deletion src/pes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,10 @@ mod test {
use crate::demultiplex::PacketFilter;
use crate::packet;
use crate::pes;
use assert_matches::assert_matches;
use bitstream_io::BigEndian;
use bitstream_io::{BitWriter, BE};
use hex_literal::*;
use matches::assert_matches;
use std;
use std::io;

Expand Down

0 comments on commit 7a478a0

Please sign in to comment.