2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
extern crate ethcore_bigint as bigint;
|
|
|
|
extern crate rlp;
|
|
|
|
|
2015-12-08 12:59:19 +01:00
|
|
|
use std::{fmt, cmp};
|
2016-12-23 16:53:06 +01:00
|
|
|
use bigint::prelude::U256;
|
2017-03-22 14:41:46 +01:00
|
|
|
use rlp::{Encodable, Decodable, UntrustedRlp, RlpStream, DecoderError};
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_at() {
|
|
|
|
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
{
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
assert!(rlp.is_list());
|
2017-03-22 14:41:46 +01:00
|
|
|
let animals: Vec<String> = rlp.as_list().unwrap();
|
2016-01-19 13:47:30 +01:00
|
|
|
assert_eq!(animals, vec!["cat".to_owned(), "dog".to_owned()]);
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let cat = rlp.at(0).unwrap();
|
|
|
|
assert!(cat.is_data());
|
2016-01-08 15:52:43 +01:00
|
|
|
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);
|
2016-01-19 13:47:30 +01:00
|
|
|
assert_eq!(cat.as_val::<String>().unwrap(), "cat".to_owned());
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let dog = rlp.at(1).unwrap();
|
|
|
|
assert!(dog.is_data());
|
2016-01-08 15:52:43 +01:00
|
|
|
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);
|
2016-01-19 13:47:30 +01:00
|
|
|
assert_eq!(dog.as_val::<String>().unwrap(), "dog".to_owned());
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let cat_again = rlp.at(0).unwrap();
|
|
|
|
assert!(cat_again.is_data());
|
2016-01-08 15:52:43 +01:00
|
|
|
assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']);
|
2016-01-19 13:47:30 +01:00
|
|
|
assert_eq!(cat_again.as_val::<String>().unwrap(), "cat".to_owned());
|
2015-12-08 12:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_at_err() {
|
|
|
|
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o'];
|
|
|
|
{
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
assert!(rlp.is_list());
|
|
|
|
|
|
|
|
let cat_err = rlp.at(0).unwrap_err();
|
2016-09-01 13:36:32 +02:00
|
|
|
assert_eq!(cat_err, DecoderError::RlpIsTooShort);
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let dog_err = rlp.at(1).unwrap_err();
|
2016-09-01 13:36:32 +02:00
|
|
|
assert_eq!(dog_err, DecoderError::RlpIsTooShort);
|
2015-12-08 12:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_iter() {
|
|
|
|
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
{
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
let mut iter = rlp.iter();
|
|
|
|
|
|
|
|
let cat = iter.next().unwrap();
|
|
|
|
assert!(cat.is_data());
|
2016-01-08 15:52:43 +01:00
|
|
|
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let dog = iter.next().unwrap();
|
|
|
|
assert!(dog.is_data());
|
2016-01-08 15:52:43 +01:00
|
|
|
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let none = iter.next();
|
|
|
|
assert!(none.is_none());
|
|
|
|
|
|
|
|
let cat_again = rlp.at(0).unwrap();
|
|
|
|
assert!(cat_again.is_data());
|
2016-01-08 15:52:43 +01:00
|
|
|
assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']);
|
2015-12-08 12:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 13:36:32 +02:00
|
|
|
struct ETestPair<T>(T, Vec<u8>) where T: Encodable;
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
fn run_encode_tests<T>(tests: Vec<ETestPair<T>>)
|
2016-09-01 13:36:32 +02:00
|
|
|
where T: Encodable
|
2015-12-08 12:59:19 +01:00
|
|
|
{
|
|
|
|
for t in &tests {
|
2017-03-22 14:41:46 +01:00
|
|
|
let res = rlp::encode(&t.0);
|
2016-01-27 12:14:57 +01:00
|
|
|
assert_eq!(&res[..], &t.1[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 19:14:29 +01:00
|
|
|
struct VETestPair<T>(Vec<T>, Vec<u8>) where T: Encodable;
|
|
|
|
|
|
|
|
fn run_encode_tests_list<T>(tests: Vec<VETestPair<T>>)
|
|
|
|
where T: Encodable
|
|
|
|
{
|
|
|
|
for t in &tests {
|
2017-03-22 14:41:46 +01:00
|
|
|
let res = rlp::encode_list(&t.0);
|
2017-03-20 19:14:29 +01:00
|
|
|
assert_eq!(&res[..], &t.1[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 12:59:19 +01:00
|
|
|
#[test]
|
|
|
|
fn encode_u16() {
|
|
|
|
let tests = vec![
|
|
|
|
ETestPair(0u16, vec![0x80u8]),
|
|
|
|
ETestPair(0x100, vec![0x82, 0x01, 0x00]),
|
|
|
|
ETestPair(0xffff, vec![0x82, 0xff, 0xff]),
|
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_u32() {
|
|
|
|
let tests = vec![
|
|
|
|
ETestPair(0u32, vec![0x80u8]),
|
|
|
|
ETestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]),
|
|
|
|
ETestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]),
|
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_u64() {
|
|
|
|
let tests = vec![
|
|
|
|
ETestPair(0u64, vec![0x80u8]),
|
|
|
|
ETestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
ETestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_u256() {
|
|
|
|
let tests = vec![ETestPair(U256::from(0u64), vec![0x80u8]),
|
|
|
|
ETestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
ETestPair(U256::from(0xffffffffu64),
|
|
|
|
vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
2017-03-20 19:14:29 +01:00
|
|
|
ETestPair(("8090a0b0c0d0e0f00910203040506077000000000000\
|
|
|
|
000100000000000012f0").into(),
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
|
|
|
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_str() {
|
|
|
|
let tests = vec![ETestPair("cat", vec![0x83, b'c', b'a', b't']),
|
|
|
|
ETestPair("dog", vec![0x83, b'd', b'o', b'g']),
|
|
|
|
ETestPair("Marek", vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
|
|
|
ETestPair("", vec![0x80]),
|
|
|
|
ETestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit",
|
|
|
|
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
|
|
|
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
|
|
|
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
|
|
|
b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c',
|
|
|
|
b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i',
|
|
|
|
b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ',
|
|
|
|
b'e', b'l', b'i', b't'])];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_address() {
|
2016-09-01 13:36:32 +02:00
|
|
|
use bigint::hash::H160;
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let tests = vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
ETestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"),
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
|
|
|
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
|
|
|
0xb3, 0x7d, 0x11, 0x06])
|
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Vec<u8> (Bytes) is treated as a single value
|
|
|
|
#[test]
|
|
|
|
fn encode_vector_u8() {
|
|
|
|
let tests = vec![
|
|
|
|
ETestPair(vec![], vec![0x80]),
|
|
|
|
ETestPair(vec![0u8], vec![0]),
|
|
|
|
ETestPair(vec![0x15], vec![0x15]),
|
|
|
|
ETestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]),
|
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_vector_u64() {
|
|
|
|
let tests = vec![
|
2017-03-20 19:14:29 +01:00
|
|
|
VETestPair(vec![], vec![0xc0]),
|
|
|
|
VETestPair(vec![15u64], vec![0xc1, 0x0f]),
|
|
|
|
VETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
|
|
|
VETestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
2015-12-08 12:59:19 +01:00
|
|
|
];
|
2017-03-20 19:14:29 +01:00
|
|
|
run_encode_tests_list(tests);
|
2015-12-08 12:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_vector_str() {
|
2017-03-20 19:14:29 +01:00
|
|
|
let tests = vec![VETestPair(vec!["cat", "dog"],
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
2017-03-20 19:14:29 +01:00
|
|
|
run_encode_tests_list(tests);
|
2015-12-08 12:59:19 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
struct DTestPair<T>(T, Vec<u8>) where T: Decodable + fmt::Debug + cmp::Eq;
|
|
|
|
|
|
|
|
struct VDTestPair<T>(Vec<T>, Vec<u8>) where T: Decodable + fmt::Debug + cmp::Eq;
|
|
|
|
|
|
|
|
fn run_decode_tests<T>(tests: Vec<DTestPair<T>>) where T: Decodable + fmt::Debug + cmp::Eq {
|
|
|
|
for t in &tests {
|
|
|
|
let res: T = rlp::decode(&t.1);
|
|
|
|
assert_eq!(res, t.0);
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 12:59:19 +01:00
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
fn run_decode_tests_list<T>(tests: Vec<VDTestPair<T>>) where T: Decodable + fmt::Debug + cmp::Eq {
|
2015-12-08 12:59:19 +01:00
|
|
|
for t in &tests {
|
2017-03-22 14:41:46 +01:00
|
|
|
let res: Vec<T> = rlp::decode_list(&t.1);
|
2015-12-08 12:59:19 +01:00
|
|
|
assert_eq!(res, t.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Vec<u8> (Bytes) is treated as a single value
|
|
|
|
#[test]
|
|
|
|
fn decode_vector_u8() {
|
|
|
|
let tests = vec![
|
|
|
|
DTestPair(vec![], vec![0x80]),
|
|
|
|
DTestPair(vec![0u8], vec![0]),
|
|
|
|
DTestPair(vec![0x15], vec![0x15]),
|
|
|
|
DTestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]),
|
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
2016-01-29 13:59:29 +01:00
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_u8() {
|
|
|
|
let tests = vec![
|
|
|
|
DTestPair(0x0u8, vec![0x80]),
|
|
|
|
DTestPair(0x77u8, vec![0x77]),
|
|
|
|
DTestPair(0xccu8, vec![0x81, 0xcc]),
|
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
2015-12-08 12:59:19 +01:00
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_u16() {
|
|
|
|
let tests = vec![
|
2016-01-28 20:13:05 +01:00
|
|
|
DTestPair(0x100u16, vec![0x82, 0x01, 0x00]),
|
|
|
|
DTestPair(0xffffu16, vec![0x82, 0xff, 0xff]),
|
2015-12-08 12:59:19 +01:00
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_u32() {
|
|
|
|
let tests = vec![
|
2016-01-28 20:13:05 +01:00
|
|
|
DTestPair(0x10000u32, vec![0x83, 0x01, 0x00, 0x00]),
|
|
|
|
DTestPair(0xffffffu32, vec![0x83, 0xff, 0xff, 0xff]),
|
2015-12-08 12:59:19 +01:00
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_u64() {
|
|
|
|
let tests = vec![
|
2016-01-28 20:13:05 +01:00
|
|
|
DTestPair(0x1000000u64, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
DTestPair(0xFFFFFFFFu64, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
2015-12-08 12:59:19 +01:00
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_u256() {
|
|
|
|
let tests = vec![DTestPair(U256::from(0u64), vec![0x80u8]),
|
|
|
|
DTestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
DTestPair(U256::from(0xffffffffu64),
|
|
|
|
vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
2017-03-20 19:14:29 +01:00
|
|
|
DTestPair(("8090a0b0c0d0e0f00910203040506077000000000000\
|
|
|
|
000100000000000012f0").into(),
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
|
|
|
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_str() {
|
2016-01-19 13:47:30 +01:00
|
|
|
let tests = vec![DTestPair("cat".to_owned(), vec![0x83, b'c', b'a', b't']),
|
|
|
|
DTestPair("dog".to_owned(), vec![0x83, b'd', b'o', b'g']),
|
|
|
|
DTestPair("Marek".to_owned(),
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
2016-01-19 13:47:30 +01:00
|
|
|
DTestPair("".to_owned(), vec![0x80]),
|
2015-12-08 12:59:19 +01:00
|
|
|
DTestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit"
|
2016-01-19 13:47:30 +01:00
|
|
|
.to_owned(),
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
|
|
|
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
|
|
|
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
|
|
|
b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c',
|
|
|
|
b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i',
|
|
|
|
b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ',
|
|
|
|
b'e', b'l', b'i', b't'])];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_address() {
|
2016-09-01 13:36:32 +02:00
|
|
|
use bigint::hash::H160;
|
2015-12-08 12:59:19 +01:00
|
|
|
|
|
|
|
let tests = vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
DTestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"),
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
|
|
|
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
|
|
|
0xb3, 0x7d, 0x11, 0x06])
|
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_vector_u64() {
|
|
|
|
let tests = vec![
|
2017-03-22 14:41:46 +01:00
|
|
|
VDTestPair(vec![], vec![0xc0]),
|
|
|
|
VDTestPair(vec![15u64], vec![0xc1, 0x0f]),
|
|
|
|
VDTestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
|
|
|
VDTestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
2015-12-08 12:59:19 +01:00
|
|
|
];
|
2017-03-22 14:41:46 +01:00
|
|
|
run_decode_tests_list(tests);
|
2015-12-08 12:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decode_untrusted_vector_str() {
|
2017-03-22 14:41:46 +01:00
|
|
|
let tests = vec![VDTestPair(vec!["cat".to_owned(), "dog".to_owned()],
|
2015-12-08 12:59:19 +01:00
|
|
|
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
2017-03-22 14:41:46 +01:00
|
|
|
run_decode_tests_list(tests);
|
2015-12-14 11:56:11 +01:00
|
|
|
}
|
2016-01-25 13:23:05 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rlp_data_length_check()
|
|
|
|
{
|
|
|
|
let data = vec![0x84, b'c', b'a', b't'];
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
|
2016-01-25 14:27:11 +01:00
|
|
|
let as_val: Result<String, DecoderError> = rlp.as_val();
|
|
|
|
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rlp_long_data_length_check()
|
|
|
|
{
|
|
|
|
let mut data: Vec<u8> = vec![0xb8, 255];
|
|
|
|
for _ in 0..253 {
|
|
|
|
data.push(b'c');
|
|
|
|
}
|
|
|
|
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
|
2016-01-25 13:23:05 +01:00
|
|
|
let as_val: Result<String, DecoderError> = rlp.as_val();
|
|
|
|
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
|
2016-01-25 15:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_the_exact_long_string()
|
|
|
|
{
|
|
|
|
let mut data: Vec<u8> = vec![0xb8, 255];
|
|
|
|
for _ in 0..255 {
|
|
|
|
data.push(b'c');
|
|
|
|
}
|
|
|
|
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
|
|
|
|
let as_val: Result<String, DecoderError> = rlp.as_val();
|
|
|
|
assert!(as_val.is_ok());
|
2016-01-25 16:13:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rlp_2bytes_data_length_check()
|
|
|
|
{
|
|
|
|
let mut data: Vec<u8> = vec![0xb9, 2, 255]; // 512+255
|
|
|
|
for _ in 0..700 {
|
|
|
|
data.push(b'c');
|
|
|
|
}
|
|
|
|
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
|
|
|
|
let as_val: Result<String, DecoderError> = rlp.as_val();
|
|
|
|
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
|
|
|
|
}
|
|
|
|
|
2016-02-01 01:06:21 +01:00
|
|
|
#[test]
|
|
|
|
fn test_rlp_nested_empty_list_encode() {
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
2017-03-20 19:14:29 +01:00
|
|
|
stream.append_list(&(Vec::new() as Vec<u32>));
|
2016-02-01 01:06:21 +01:00
|
|
|
stream.append(&40u32);
|
|
|
|
assert_eq!(stream.drain()[..], [0xc2u8, 0xc0u8, 40u8][..]);
|
|
|
|
}
|
|
|
|
|
2016-06-15 00:58:08 +02:00
|
|
|
#[test]
|
|
|
|
fn test_rlp_list_length_overflow() {
|
|
|
|
let data: Vec<u8> = vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00];
|
|
|
|
let rlp = UntrustedRlp::new(&data);
|
|
|
|
let as_val: Result<String, DecoderError> = rlp.val_at(0);
|
|
|
|
assert_eq!(Err(DecoderError::RlpIsTooShort), as_val);
|
|
|
|
}
|