[go: up one dir, main page]

rmp 0.3.1

Pure Rust MessagePack serialization implementation
use std::io::Cursor;

use msgpack::Marker;
use msgpack::decode::*;

#[test]
fn from_bin8_min_read_len() {
    let buf: &[u8] = &[0xc4, 0x00];
    let mut cur = Cursor::new(buf);

    assert_eq!(0, read_bin_len(&mut cur).unwrap());
    assert_eq!(2, cur.position());
}

#[test]
fn from_bin8_max_read_len() {
    let buf: &[u8] = &[0xc4, 0xff];
    let mut cur = Cursor::new(buf);

    assert_eq!(255, read_bin_len(&mut cur).unwrap());
    assert_eq!(2, cur.position());
}

#[test]
fn from_bin8_eof_read_len() {
    let buf: &[u8] = &[0xc4];
    let mut cur = Cursor::new(buf);

    match read_bin_len(&mut cur) {
        Err(ValueReadError::InvalidDataRead(ReadError::UnexpectedEOF)) => (),
        other => panic!("unexpected result: {:?}", other)
    }
    assert_eq!(1, cur.position());
}

#[test]
fn from_null_read_len() {
    let buf: &[u8] = &[0xc0];
    let mut cur = Cursor::new(buf);

    match read_bin_len(&mut cur) {
        Err(ValueReadError::TypeMismatch(Marker::Null)) => (),
        other => panic!("unexpected result: {:?}", other)
    }
    assert_eq!(1, cur.position());
}

#[test]
fn from_bin16_max_read_len() {
    let buf: &[u8] = &[0xc5, 0xff, 0xff];
    let mut cur = Cursor::new(buf);

    assert_eq!(65535, read_bin_len(&mut cur).unwrap());
    assert_eq!(3, cur.position());
}

#[test]
fn from_bin32_max_read_len() {
    let buf: &[u8] = &[0xc6, 0xff, 0xff, 0xff, 0xff];
    let mut cur = Cursor::new(buf);

    assert_eq!(4294967295, read_bin_len(&mut cur).unwrap());
    assert_eq!(5, cur.position());
}

#[test]
fn from_bin8_read_zero_copy() {
    let buf = [0xc4, 0x05, 0x00, 0x00, 0x2a, 0x00, 0x00];

    assert_eq!([0x00, 0x00, 0x2a, 0x00, 0x00], read_bin_borrow(&mut &buf[..]).unwrap());
}

#[test]
fn from_bin8_read_zero_copy_insufficient_bytes() {
    let buf = [0xc4, 0x05, 0x00, 0x00, 0x2a, 0x00];

    match read_bin_borrow(&mut &buf[..]) {
        Err(ValueReadError::InvalidDataRead(ReadError::UnexpectedEOF)) => (),
        other => panic!("unexpected result: {:?}", other)
    }
}