Skip to content

Commit

Permalink
chore: coverage for decoding value (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
sacherjj authored and 3Hren committed Jul 17, 2017
1 parent f4184f2 commit d5cfd5a
Showing 1 changed file with 232 additions and 0 deletions.
232 changes: 232 additions & 0 deletions rmpv/tests/decode.rs
Expand Up @@ -22,6 +22,42 @@ fn from_bool_decode_value() {
assert_eq!(Value::Boolean(false), read_value(&mut &buf[1..]).unwrap());
}

#[test]
fn from_bin8_decode_value() {
let buf: &[u8] = &[
0xc4,
0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];

let expected = Value::Binary(vec!(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08));

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_bin16_decode_value() {
let buf: &[u8] = &[
0xc5,
0x00, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];

let expected = Value::Binary(vec!(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08));

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_bin32_decode_value() {
let buf: &[u8] = &[
0xc6,
0x00, 0x00, 0x00, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];

let expected = Value::Binary(vec!(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08));

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_i32_decode_value() {
let buf = [0xd2, 0xff, 0xff, 0xff, 0xff];
Expand Down Expand Up @@ -70,6 +106,53 @@ fn from_fixarray_incomplete_decode_value() {
}
}

#[test]
fn from_fixarray_decode_empty() {
let buf: &[u8] = &[0x90];

assert_eq!(Value::Array(vec![]), read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixarray_of_decode_max_length() {
let buf: &[u8] = &[0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f];

let expected = Value::Array(vec![Value::from(1), Value::from(2), Value::from(3),
Value::from(4), Value::from(5), Value::from(6),
Value::from(7), Value::from(8), Value::from(9),
Value::from(10), Value::from(11), Value::from(12),
Value::from(13), Value::from(14), Value::from(15)]);
assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_array16_decode_value() {
let buf: &[u8] = &[
0xdc,
0x00, 0x06,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06];

let expected = Value::Array(vec![Value::from(1), Value::from(2), Value::from(3),
Value::from(4), Value::from(5), Value::from(6)]);

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}


#[test]
fn from_array32_decode_value() {
let buf: &[u8] = &[
0xdd,
0x00, 0x00, 0x00, 0x06,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06];

let expected = Value::Array(vec![Value::from(1), Value::from(2), Value::from(3),
Value::from(4), Value::from(5), Value::from(6)]);

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixmap_decode_value() {
let buf = [
Expand All @@ -88,12 +171,97 @@ fn from_fixmap_decode_value() {
assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_map16_decode_value() {
let buf = [
0xde,
0x00, 0x02,
0x2a,
0xce, 0x0, 0x1, 0x88, 0x94,
0xa3, 0x6b, 0x65, 0x79,
0xa5, 0x76, 0x61, 0x6c, 0x75, 0x65
];

let expected = Value::Map(vec![
(Value::from(42), Value::from(100500)),
(Value::String("key".into()), Value::String("value".into())),
]);

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_map32_decode_value() {
let buf = [
0xdf,
0x00, 0x00, 0x00, 0x02,
0x2a,
0xce, 0x0, 0x1, 0x88, 0x94,
0xa3, 0x6b, 0x65, 0x79,
0xa5, 0x76, 0x61, 0x6c, 0x75, 0x65
];

let expected = Value::Map(vec![
(Value::from(42), Value::from(100500)),
(Value::String("key".into()), Value::String("value".into())),
]);

assert_eq!(expected, read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixext1_decode_value() {
let buf = [0xd4, 0x01, 0x02];
assert_eq!(Value::Ext(1, vec![2]), read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixext2_decode_value() {
let buf = [0xd5, 0x01, 0x02, 0x03];
assert_eq!(Value::Ext(1, vec![2, 3]), read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixext4_decode_value() {
let buf = [0xd6, 0x01, 0x02, 0x03, 0x04, 0x05];
assert_eq!(Value::Ext(1, vec![2, 3, 4, 5]), read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixext8_decode_value() {
let buf = [0xd7, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
assert_eq!(Value::Ext(1, vec![2, 3, 4, 5, 6, 7, 8, 9]), read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_fixext16_decode_value() {
let buf = [0xd8, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
assert_eq!(Value::Ext(1, vec![2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9]),
read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_ext8_decode_value() {
let buf = [0xc7, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05];
assert_eq!(Value::Ext(1, vec![2, 3, 4, 5]),
read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_ext16_decode_value() {
let buf = [0xc8, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05];
assert_eq!(Value::Ext(1, vec![2, 3, 4, 5]),
read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_ext32_decode_value() {
let buf = [0xc9, 0x00, 0x00, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05];
assert_eq!(Value::Ext(1, vec![2, 3, 4, 5]),
read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_str8_decode_value() {
let buf: &[u8] = &[
Expand Down Expand Up @@ -124,6 +292,70 @@ fn from_str8_invalid_utf8() {
}
}

#[test]
fn from_str16_decode_value() {
let buf: &[u8] = &[
0xda,
0x00,
0x20,
0x42,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x45
];

assert_eq!(Value::String("B123456789012345678901234567890E".into()),
read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_str16_invalid_utf8() {
// Invalid 2 Octet Sequence.
let buf: &[u8] = &[0xda, 0x00, 0x02, 0xc3, 0x28];

match read_value(&mut &buf[..]).unwrap() {
Value::String(s) => {
assert!(s.is_err());
assert_eq!(vec![0xc3, 0x28], s.into_bytes());
}
_ => panic!("wrong type"),
}
}

#[test]
fn from_str32_decode_value() {
let buf: &[u8] = &[
0xdb,
0x00,
0x00,
0x00,
0x20,
0x42,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x45
];

assert_eq!(Value::String("B123456789012345678901234567890E".into()),
read_value(&mut &buf[..]).unwrap());
}

#[test]
fn from_str32_invalid_utf8() {
// Invalid 2 Octet Sequence.
let buf: &[u8] = &[0xdb, 0x00, 0x00, 0x00, 0x02, 0xc3, 0x28];

match read_value(&mut &buf[..]).unwrap() {
Value::String(s) => {
assert!(s.is_err());
assert_eq!(vec![0xc3, 0x28], s.into_bytes());
}
_ => panic!("wrong type"),
}
}

#[test]
fn from_array_of_two_integers() {
let buf: &[u8] = &[0x92, 0x04, 0x2a];
Expand Down

0 comments on commit d5cfd5a

Please sign in to comment.