Add a few additional test cases

This commit is contained in:
Tad Hardesty
2017-05-20 20:38:37 -07:00
parent 43f36f0e7f
commit 7def3a668c
2 changed files with 48 additions and 3 deletions

29
tests/opus-padding.rs Normal file
View File

@@ -0,0 +1,29 @@
// Based on libopus/tests/test_opus_padding.c
/* Check for overflow in reading the padding length.
* http://lists.xiph.org/pipermail/opus/2012-November/001834.html
*/
extern crate opus;
#[test]
fn test_overflow() {
const PACKETSIZE: usize = 16909318;
const CHANNELS: opus::Channels = opus::Channels::Stereo;
const FRAMESIZE: usize = 5760;
let mut input = vec![0xff; PACKETSIZE];
let mut output = vec![0i16; FRAMESIZE * 2];
input[0] = 0xff;
input[1] = 0x41;
*input.last_mut().unwrap() = 0x0b;
let mut decoder = opus::Decoder::new(48000, CHANNELS).unwrap();
let result = decoder.decode(&input[..], &mut output[..], false);
drop(decoder);
drop(input);
drop(output);
assert_eq!(result.unwrap_err().code(), opus::ErrorCode::InvalidPacket);
}

View File

@@ -1,8 +1,24 @@
extern crate opus;
fn check_ascii(s: &str) -> &str {
for &b in s.as_bytes() {
assert!(b < 0x80, "Non-ASCII character in string");
assert!(b > 0x00, "NUL in string")
}
std::str::from_utf8(s.as_bytes()).unwrap()
}
#[test]
fn version_ascii() {
println!("Opus version: {}", opus::version());
fn strings_ascii() {
use opus::ErrorCode::*;
println!("\nVersion: {}", check_ascii(opus::version()));
let codes = [BadArg, BufferTooSmall, InternalError, InvalidPacket,
Unimplemented, InvalidState, AllocFail, Unknown];
for &code in codes.iter() {
println!("{:?}: {}", code, check_ascii(code.description()));
}
}
// 48000Hz * 1 channel * 20 ms / 1000
@@ -11,7 +27,7 @@ const MONO_20MS: usize = 48000 * 1 * 20 / 1000;
#[test]
fn encode_mono() {
let mut encoder = opus::Encoder::new(48000, opus::Channels::Mono, opus::Application::Audio).unwrap();
let mut output = [0; 256];
let len = encoder.encode(&[0_i16; MONO_20MS], &mut output).unwrap();
assert_eq!(&output[..len], &[248, 255, 254]);