| 1 |
use std::io::{Cursor, Write}; |
| 2 |
use std::iter; |
| 3 |
|
| 4 |
use partial_io::{PartialOp, PartialWrite}; |
| 5 |
|
| 6 |
use crate::stream::decode_all; |
| 7 |
use crate::stream::write::{Decoder, Encoder}; |
| 8 |
|
| 9 |
#[test] |
| 10 |
fn test_cycle() { |
| 11 |
let input = b"Abcdefghabcdefgh"; |
| 12 |
|
| 13 |
let buffer = Cursor::new(Vec::new()); |
| 14 |
let mut encoder = Encoder::new(buffer, 1).unwrap(); |
| 15 |
encoder.write_all(input).unwrap(); |
| 16 |
let encoded = encoder.finish().unwrap().into_inner(); |
| 17 |
|
| 18 |
// println!("Encoded: {:?}", encoded); |
| 19 |
|
| 20 |
let buffer = Cursor::new(Vec::new()); |
| 21 |
let mut decoder = Decoder::new(buffer).unwrap(); |
| 22 |
decoder.write_all(&encoded).unwrap(); |
| 23 |
decoder.flush().unwrap(); |
| 24 |
let decoded = decoder.into_inner().into_inner(); |
| 25 |
|
| 26 |
assert_eq!(input, &decoded[..]); |
| 27 |
} |
| 28 |
|
| 29 |
/// Test that flush after a partial write works successfully without |
| 30 |
/// corrupting the frame. This test is in this module because it checks |
| 31 |
/// internal implementation details. |
| 32 |
#[test] |
| 33 |
fn test_partial_write_flush() { |
| 34 |
let input = vec![b'b'; 128 * 1024]; |
| 35 |
let mut z = setup_partial_write(&input); |
| 36 |
|
| 37 |
// flush shouldn't corrupt the stream |
| 38 |
z.flush().unwrap(); |
| 39 |
|
| 40 |
let buf = z.finish().unwrap().into_inner(); |
| 41 |
assert_eq!(&decode_all(&buf[..]).unwrap(), &input); |
| 42 |
} |
| 43 |
|
| 44 |
/// Test that finish after a partial write works successfully without |
| 45 |
/// corrupting the frame. This test is in this module because it checks |
| 46 |
/// internal implementation details. |
| 47 |
#[test] |
| 48 |
fn test_partial_write_finish() { |
| 49 |
let input = vec![b'b'; 128 * 1024]; |
| 50 |
let z = setup_partial_write(&input); |
| 51 |
|
| 52 |
// finish shouldn't corrupt the stream |
| 53 |
let buf = z.finish().unwrap().into_inner(); |
| 54 |
assert_eq!(&decode_all(&buf[..]).unwrap(), &input); |
| 55 |
} |
| 56 |
|
| 57 |
fn setup_partial_write(input_data: &[u8]) -> Encoder<PartialWrite<Vec<u8>>> { |
| 58 |
let buf = |
| 59 |
PartialWrite::new(Vec::new(), iter::repeat(PartialOp::Limited(1))); |
| 60 |
let mut z = Encoder::new(buf, 1).unwrap(); |
| 61 |
|
| 62 |
// Fill in enough data to make sure the buffer gets written out. |
| 63 |
z.write(input_data).unwrap(); |
| 64 |
|
| 65 |
{ |
| 66 |
let inner = &mut z.writer; |
| 67 |
// At this point, the internal buffer in z should have some data. |
| 68 |
assert_ne!(inner.offset(), inner.buffer().len()); |
| 69 |
} |
| 70 |
|
| 71 |
z |
| 72 |
} |