openethereum/util/rlp_compress/tests/compress.rs

67 lines
113 KiB
Rust
Raw Normal View History

// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// 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/>.
extern crate rlp_compress;
use rlp_compress::{compress, decompress, Swapper, snapshot_swapper, blocks_swapper, Compressor, Decompressor};
#[test]
fn invalid_rlp_swapper() {
let to_swap: &[&[u8]] = &[&[0x83, b'c', b'a', b't'], &[0x83, b'd', b'o', b'g']];
let invalid_rlp: &[&[u8]] = &[&[0x81, 0x00], &[0x81, 0x01]];
let swapper = Swapper::new(to_swap, invalid_rlp);
assert_eq!(Some(invalid_rlp[0]), swapper.compressed(&[0x83, b'c', b'a', b't']));
assert_eq!(None, swapper.compressed(&[0x83, b'b', b'a', b't']));
assert_eq!(Some(to_swap[1]), swapper.decompressed(invalid_rlp[1]));
}
#[test]
fn simple_compression() {
let basic_account_rlp = vec![248, 68, 4, 2, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112];
let compressed = compress(&basic_account_rlp, snapshot_swapper());
assert_eq!(compressed.to_vec(), vec![198, 4, 2, 129, 0, 129, 1]);
let decompressed = decompress(&compressed, snapshot_swapper());
assert_eq!(decompressed.to_vec(), basic_account_rlp);
}
#[test]
fn nested_list_rlp() {
let nested_basic_account_rlp = vec![228, 4, 226, 2, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33];
let compressed = compress(&nested_basic_account_rlp, blocks_swapper());
assert_eq!(compressed, vec![197, 4, 195, 2, 129, 0]);
let decompressed = decompress(&compressed, blocks_swapper());
assert_eq!(decompressed.into_vec(), nested_basic_account_rlp);
let compressed = compress(&nested_basic_account_rlp, snapshot_swapper());
assert_eq!(compressed, vec![197, 4, 195, 2, 129, 0]);
let decompressed = decompress(&compressed, snapshot_swapper());
assert_eq!(decompressed.into_vec(), nested_basic_account_rlp);
}
#[test]
fn malformed_rlp() {
let malformed = vec![248, 81, 128, 128, 128, 128, 128, 160, 12, 51, 241, 93, 69, 218, 74, 138, 79, 115, 227, 44, 216, 81, 46, 132, 85, 235, 96, 45, 252, 48, 181, 29, 75, 141, 217, 215, 86, 160, 109, 130, 160, 140, 36, 93, 200, 109, 215, 100, 241, 246, 99, 135, 92, 168, 149, 170, 114, 9, 143, 4, 93, 25, 76, 54, 176, 119, 230, 170, 154, 105, 47, 121, 10, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128];
assert_eq!(decompress(&malformed, blocks_swapper()).into_vec(), malformed);
}
#[test]
fn large_block() {
let block = vec![249, 97, 87, 249, 2, 19, 160, 137, 152, 36, 115, 234, 67, 89, 207, 44, 42, 186, 128, 91, 242, 10, 16, 42, 193, 195, 2, 129, 60, 181, 150, 192, 178, 117, 15, 18, 100, 174, 249, 160, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71, 148, 223, 125, 126, 5, 57, 51, 181, 204, 36, 55, 47, 135, 140, 144, 230, 45, 173, 173, 93, 66, 160, 93, 42, 52, 28, 156, 139, 242, 60, 121, 90, 117, 99, 92, 182, 196, 25, 131, 16, 155, 186, 239, 137, 33, 118, 105, 232, 230, 239, 213, 240, 207, 6, 160, 59, 72, 35, 216, 124, 37, 62, 178, 34, 97, 180, 254, 212, 103, 179, 45, 247, 168, 205, 145, 7, 157, 75, 247, 83, 230, 233, 248, 97, 132, 232, 161, 160, 122, 167, 249, 196, 203, 2, 173, 180, 106, 203, 129, 214, 232, 181, 87, 39, 60, 99, 135, 6, 40, 34, 163, 118, 140, 149, 79, 241, 238, 230, 201, 194, 185, 1, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 64, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 134, 36, 160, 31, 187, 182, 29, 131, 58, 212, 207, 131, 71, 168, 13, 131, 45, 60, 7, 132, 89, 53, 51, 233, 147, 69, 84, 67, 32, 101, 116, 104, 101, 114, 109, 105, 110, 101, 32, 45, 32, 69, 85, 49, 160, 204, 49, 229, 99, 26, 47, 30, 50, 223, 117, 111, 168, 102, 158, 12, 186, 140, 98, 193, 196, 214, 29, 13, 87, 44, 31, 216, 48, 250, 251, 148, 69, 136, 225, 206, 131, 96, 2, 39, 50, 46, 249, 95, 61, 248, 109, 1, 133, 4, 227, 178, 146, 0, 130, 82, 8, 148, 123, 45, 95, 28, 247, 139, 190, 196, 52, 39, 251, 53, 226, 79, 251, 1, 98, 34, 68, 240, 136, 18, 81, 1, 26, 180, 46, 168, 0, 128, 129, 157, 160, 31, 164, 62, 186, 38, 56, 118, 133, 24, 180, 239, 139, 254, 154, 196, 115, 8, 246, 45, 233, 227, 165, 192, 193, 7, 111, 1, 169, 2, 204, 2, 144, 160, 42, 60, 78, 200, 5, 113, 98, 65, 250, 105, 0, 164, 152, 81, 235, 154, 100, 204, 182, 141, 174, 39, 107, 127, 219, 120, 63, 221, 237, 87, 57, 9, 249, 94, 203, 52, 133, 2, 84, 11, 228, 0, 131, 44, 233, 255, 148, 137, 61, 196, 25, 119, 102, 53, 248, 253, 27, 31, 169, 147, 75, 245, 41, 174, 242, 86, 7, 128, 185, 94, 100, 194, 32, 154, 203, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 131, 110, 76, 27, 36, 34, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92, 224, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 248, 249, 1, 245, 160, 89, 97, 73, 3, 84, 63, 105, 163, 169, 7, 237, 254, 220, 243, 13, 27, 0, 45, 215, 39, 235, 232, 237, 22, 162, 83, 99, 164, 247, 240, 48, 66, 160, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71, 148, 137, 61, 196, 25, 119, 102, 53, 248, 253, 27, 31, 169, 147, 75, 245, 41, 174, 242, 86, 7, 160, 243, 100, 253, 32, 58
let compressed = compress(&block, blocks_swapper());
let decompressed = decompress(&compressed, blocks_swapper());
assert_eq!(decompressed.into_vec(), block);
}