webmetro/src/lib.rs

125 lines
4 KiB
Rust
Raw Normal View History

2017-01-10 03:40:21 +00:00
extern crate futures;
2017-06-27 06:11:29 +00:00
pub mod ebml;
mod iterator;
pub mod webm;
2017-01-12 05:41:35 +00:00
2017-06-27 06:11:29 +00:00
pub use ebml::{Error, Schema};
2017-01-24 08:20:49 +00:00
2017-01-10 03:40:21 +00:00
#[cfg(test)]
mod tests {
use futures::future::{ok, Future};
2017-01-12 13:44:48 +00:00
use super::*;
use super::Error::{CorruptVarint, UnknownElementId};
2017-01-12 12:37:29 +00:00
use super::Varint::{Unknown, Value};
2017-01-10 03:40:21 +00:00
#[test]
fn hello_futures() {
let my_future = ok::<String, ()>("Hello".into())
.map(|hello| hello + ", Futures!");
let string_result = my_future.wait().unwrap();
assert_eq!(string_result, "Hello, Futures!");
}
2017-01-12 05:41:35 +00:00
#[test]
fn fail_corrupted_varints() {
2017-01-12 13:37:03 +00:00
assert_eq!(decode_varint(&[0]), Err(CorruptVarint));
assert_eq!(decode_varint(&[0, 0, 0]), Err(CorruptVarint));
2017-01-12 05:41:35 +00:00
}
#[test]
fn incomplete_varints() {
assert_eq!(decode_varint(&[]), Ok(None));
assert_eq!(decode_varint(&[0x40]), Ok(None));
assert_eq!(decode_varint(&[0x01, 0, 0]), Ok(None));
}
#[test]
fn parse_varints() {
assert_eq!(decode_varint(&[0xFF]), Ok(Some((Unknown, 1))));
assert_eq!(decode_varint(&[0x7F, 0xFF]), Ok(Some((Unknown, 2))));
assert_eq!(decode_varint(&[0x80]), Ok(Some((Value(0), 1))));
assert_eq!(decode_varint(&[0x81]), Ok(Some((Value(1), 1))));
assert_eq!(decode_varint(&[0x40, 52]), Ok(Some((Value(52), 2))));
// test extra data in buffer
assert_eq!(decode_varint(&[0x83, 0x11]), Ok(Some((Value(3), 1))));
}
2017-01-12 13:44:48 +00:00
#[test]
fn fail_corrupted_tags() {
assert_eq!(decode_tag(&[0]), Err(CorruptVarint));
assert_eq!(decode_tag(&[0x80, 0]), Err(CorruptVarint));
assert_eq!(decode_tag(&[0xFF, 0x80]), Err(UnknownElementId));
assert_eq!(decode_tag(&[0x7F, 0xFF, 0x40, 0]), Err(UnknownElementId));
}
#[test]
fn incomplete_tags() {
assert_eq!(decode_tag(&[]), Ok(None));
assert_eq!(decode_tag(&[0x80]), Ok(None));
assert_eq!(decode_tag(&[0x40, 0, 0x40]), Ok(None));
}
#[test]
fn parse_tags() {
assert_eq!(decode_tag(&[0x80, 0x80]), Ok(Some((0, Value(0), 2))));
assert_eq!(decode_tag(&[0x81, 0x85]), Ok(Some((1, Value(5), 2))));
assert_eq!(decode_tag(&[0x80, 0xFF]), Ok(Some((0, Unknown, 2))));
assert_eq!(decode_tag(&[0x80, 0x7F, 0xFF]), Ok(Some((0, Unknown, 3))));
assert_eq!(decode_tag(&[0x85, 0x40, 52]), Ok(Some((5, Value(52), 3))));
}
const TEST_FILE: &'static [u8] = include_bytes!("data/test1.webm");
2017-01-27 06:24:17 +00:00
struct Dummy;
#[derive(Debug, PartialEq)]
struct GenericElement(u64, usize);
impl<'a> Schema<'a> for Dummy {
type Element = GenericElement;
fn should_unwrap(&self, element_id: u64) -> bool {
match element_id {
_ => false
}
}
fn decode<'b: 'a>(&self, element_id: u64, bytes: &'b[u8]) -> Result<GenericElement, Error> {
match element_id {
_ => Ok(GenericElement(element_id, bytes.len()))
}
}
}
#[test]
fn decode_sanity_test() {
2017-01-27 06:24:17 +00:00
let decoded = Dummy.decode_element(TEST_FILE);
assert_eq!(decoded, Ok(Some((GenericElement(0x0A45DFA3, 31), 43))));
}
2017-01-24 08:20:49 +00:00
#[test]
fn decode_webm_test1() {
2017-04-30 18:35:11 +00:00
let mut iter = Webm.parse(TEST_FILE).into_iter();
2017-01-24 08:20:49 +00:00
// test that we match the structure of the test file
assert_eq!(iter.next(), Some(WebmElement::EbmlHead));
2017-01-27 07:09:09 +00:00
assert_eq!(iter.next(), Some(WebmElement::Segment));
2017-04-16 20:35:48 +00:00
assert_eq!(iter.next(), Some(WebmElement::SeekHead));
assert_eq!(iter.next(), Some(WebmElement::Void));
assert_eq!(iter.next(), Some(WebmElement::Info));
assert_eq!(iter.next(), Some(WebmElement::Tracks(&TEST_FILE[358..421])));
assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[433..13739])));
assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[13751..34814])));
assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[34826..56114])));
assert_eq!(iter.next(), Some(WebmElement::Cues));
assert_eq!(iter.next(), None);
2017-01-24 08:20:49 +00:00
}
2017-01-10 03:40:21 +00:00
}