1 | pub mod bufread; |
2 | pub mod read; |
3 | pub mod write; |
4 | |
5 | #[cfg (test)] |
6 | mod tests { |
7 | use std::io; |
8 | use std::io::prelude::*; |
9 | |
10 | use rand::{thread_rng, Rng}; |
11 | |
12 | use crate::zlib::{read, write}; |
13 | use crate::Compression; |
14 | |
15 | #[test ] |
16 | fn roundtrip() { |
17 | let mut real = Vec::new(); |
18 | let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); |
19 | let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); |
20 | for _ in 0..200 { |
21 | let to_write = &v[..thread_rng().gen_range(0..v.len())]; |
22 | real.extend(to_write.iter().copied()); |
23 | w.write_all(to_write).unwrap(); |
24 | } |
25 | let result = w.finish().unwrap(); |
26 | let mut r = read::ZlibDecoder::new(&result[..]); |
27 | let mut ret = Vec::new(); |
28 | r.read_to_end(&mut ret).unwrap(); |
29 | assert_eq!(ret, real); |
30 | } |
31 | |
32 | #[test ] |
33 | fn drop_writes() { |
34 | let mut data = Vec::new(); |
35 | write::ZlibEncoder::new(&mut data, Compression::default()) |
36 | .write_all(b"foo" ) |
37 | .unwrap(); |
38 | let mut r = read::ZlibDecoder::new(&data[..]); |
39 | let mut ret = Vec::new(); |
40 | r.read_to_end(&mut ret).unwrap(); |
41 | assert_eq!(ret, b"foo" ); |
42 | } |
43 | |
44 | #[test ] |
45 | fn total_in() { |
46 | let mut real = Vec::new(); |
47 | let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); |
48 | let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); |
49 | for _ in 0..200 { |
50 | let to_write = &v[..thread_rng().gen_range(0..v.len())]; |
51 | real.extend(to_write.iter().copied()); |
52 | w.write_all(to_write).unwrap(); |
53 | } |
54 | let mut result = w.finish().unwrap(); |
55 | |
56 | let result_len = result.len(); |
57 | |
58 | for _ in 0..200 { |
59 | result.extend(v.iter().copied()); |
60 | } |
61 | |
62 | let mut r = read::ZlibDecoder::new(&result[..]); |
63 | let mut ret = Vec::new(); |
64 | r.read_to_end(&mut ret).unwrap(); |
65 | assert_eq!(ret, real); |
66 | assert_eq!(r.total_in(), result_len as u64); |
67 | } |
68 | |
69 | #[test ] |
70 | fn roundtrip2() { |
71 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
72 | let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Compression::default())); |
73 | let mut ret = Vec::new(); |
74 | r.read_to_end(&mut ret).unwrap(); |
75 | assert_eq!(ret, v); |
76 | } |
77 | |
78 | #[test ] |
79 | fn roundtrip3() { |
80 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
81 | let mut w = |
82 | write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Compression::default()); |
83 | w.write_all(&v).unwrap(); |
84 | let w = w.finish().unwrap().finish().unwrap(); |
85 | assert_eq!(w, v); |
86 | } |
87 | |
88 | #[test ] |
89 | fn reset_decoder() { |
90 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
91 | let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); |
92 | w.write_all(&v).unwrap(); |
93 | let data = w.finish().unwrap(); |
94 | |
95 | { |
96 | let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); |
97 | let mut r = read::ZlibDecoder::new(&data[..]); |
98 | r.read_to_end(&mut a).unwrap(); |
99 | r.reset(&data); |
100 | r.read_to_end(&mut b).unwrap(); |
101 | |
102 | let mut r = read::ZlibDecoder::new(&data[..]); |
103 | r.read_to_end(&mut c).unwrap(); |
104 | assert!(a == b && b == c && c == v); |
105 | } |
106 | |
107 | { |
108 | let mut w = write::ZlibDecoder::new(Vec::new()); |
109 | w.write_all(&data).unwrap(); |
110 | let a = w.reset(Vec::new()).unwrap(); |
111 | w.write_all(&data).unwrap(); |
112 | let b = w.finish().unwrap(); |
113 | |
114 | let mut w = write::ZlibDecoder::new(Vec::new()); |
115 | w.write_all(&data).unwrap(); |
116 | let c = w.finish().unwrap(); |
117 | assert!(a == b && b == c && c == v); |
118 | } |
119 | } |
120 | |
121 | #[test ] |
122 | fn bad_input() { |
123 | // regress tests: previously caused a panic on drop |
124 | let mut out: Vec<u8> = Vec::new(); |
125 | let data: Vec<u8> = (0..255).cycle().take(1024).collect(); |
126 | let mut w = write::ZlibDecoder::new(&mut out); |
127 | match w.write_all(&data[..]) { |
128 | Ok(_) => panic!("Expected an error to be returned!" ), |
129 | Err(e) => assert_eq!(e.kind(), io::ErrorKind::InvalidInput), |
130 | } |
131 | } |
132 | |
133 | #[test ] |
134 | fn qc_reader() { |
135 | ::quickcheck::quickcheck(test as fn(_) -> _); |
136 | |
137 | fn test(v: Vec<u8>) -> bool { |
138 | let mut r = |
139 | read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Compression::default())); |
140 | let mut v2 = Vec::new(); |
141 | r.read_to_end(&mut v2).unwrap(); |
142 | v == v2 |
143 | } |
144 | } |
145 | |
146 | #[test ] |
147 | fn qc_writer() { |
148 | ::quickcheck::quickcheck(test as fn(_) -> _); |
149 | |
150 | fn test(v: Vec<u8>) -> bool { |
151 | let mut w = write::ZlibEncoder::new( |
152 | write::ZlibDecoder::new(Vec::new()), |
153 | Compression::default(), |
154 | ); |
155 | w.write_all(&v).unwrap(); |
156 | v == w.finish().unwrap().finish().unwrap() |
157 | } |
158 | } |
159 | } |
160 | |