mirror of
https://github.com/rustdesk/magnum-opus.git
synced 2026-04-03 06:16:06 +00:00
Add a few additional test cases
This commit is contained in:
29
tests/opus-padding.rs
Normal file
29
tests/opus-padding.rs
Normal 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);
|
||||
}
|
||||
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user