| 1 |
#![warn(rust_2018_idioms)] |
| 2 |
|
| 3 |
use bytes::buf::UninitSlice; |
| 4 |
use bytes::{BufMut, BytesMut}; |
| 5 |
use core::fmt::Write; |
| 6 |
use core::mem::MaybeUninit; |
| 7 |
|
| 8 |
#[test] |
| 9 |
fn test_vec_as_mut_buf() { |
| 10 |
let mut buf = Vec::with_capacity(64); |
| 11 |
|
| 12 |
assert_eq!(buf.remaining_mut(), isize::MAX as usize); |
| 13 |
|
| 14 |
assert!(buf.chunk_mut().len() >= 64); |
| 15 |
|
| 16 |
buf.put(&b"zomg"[..]); |
| 17 |
|
| 18 |
assert_eq!(&buf, b"zomg"); |
| 19 |
|
| 20 |
assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4); |
| 21 |
assert_eq!(buf.capacity(), 64); |
| 22 |
|
| 23 |
for _ in 0..16 { |
| 24 |
buf.put(&b"zomg"[..]); |
| 25 |
} |
| 26 |
|
| 27 |
assert_eq!(buf.len(), 68); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_vec_put_bytes() { |
| 32 |
let mut buf = Vec::new(); |
| 33 |
buf.push(17); |
| 34 |
buf.put_bytes(19, 2); |
| 35 |
assert_eq!([17, 19, 19], &buf[..]); |
| 36 |
} |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn test_put_u8() { |
| 40 |
let mut buf = Vec::with_capacity(8); |
| 41 |
buf.put_u8(33); |
| 42 |
assert_eq!(b"\x21", &buf[..]); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_put_u16() { |
| 47 |
let mut buf = Vec::with_capacity(8); |
| 48 |
buf.put_u16(8532); |
| 49 |
assert_eq!(b"\x21\x54", &buf[..]); |
| 50 |
|
| 51 |
buf.clear(); |
| 52 |
buf.put_u16_le(8532); |
| 53 |
assert_eq!(b"\x54\x21", &buf[..]); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn test_put_int() { |
| 58 |
let mut buf = Vec::with_capacity(8); |
| 59 |
buf.put_int(0x1020304050607080, 3); |
| 60 |
assert_eq!(b"\x60\x70\x80", &buf[..]); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
#[should_panic] |
| 65 |
fn test_put_int_nbytes_overflow() { |
| 66 |
let mut buf = Vec::with_capacity(8); |
| 67 |
buf.put_int(0x1020304050607080, 9); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_put_int_le() { |
| 72 |
let mut buf = Vec::with_capacity(8); |
| 73 |
buf.put_int_le(0x1020304050607080, 3); |
| 74 |
assert_eq!(b"\x80\x70\x60", &buf[..]); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
#[should_panic] |
| 79 |
fn test_put_int_le_nbytes_overflow() { |
| 80 |
let mut buf = Vec::with_capacity(8); |
| 81 |
buf.put_int_le(0x1020304050607080, 9); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
#[should_panic(expected = "advance out of bounds: the len is 8 but advancing by 12")] |
| 86 |
fn test_vec_advance_mut() { |
| 87 |
// Verify fix for #354 |
| 88 |
let mut buf = Vec::with_capacity(8); |
| 89 |
unsafe { |
| 90 |
buf.advance_mut(12); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn test_clone() { |
| 96 |
let mut buf = BytesMut::with_capacity(100); |
| 97 |
buf.write_str("this is a test").unwrap(); |
| 98 |
let buf2 = buf.clone(); |
| 99 |
|
| 100 |
buf.write_str(" of our emergency broadcast system").unwrap(); |
| 101 |
assert!(buf != buf2); |
| 102 |
} |
| 103 |
|
| 104 |
fn do_test_slice_small<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T) |
| 105 |
where |
| 106 |
for<'r> &'r mut T: BufMut, |
| 107 |
{ |
| 108 |
let mut buf = [b'X'; 8]; |
| 109 |
|
| 110 |
let mut slice = make(&mut buf[..]); |
| 111 |
slice.put_bytes(b'A', 2); |
| 112 |
slice.put_u8(b'B'); |
| 113 |
slice.put_slice(b"BCC"); |
| 114 |
assert_eq!(2, slice.remaining_mut()); |
| 115 |
assert_eq!(b"AABBCCXX", &buf[..]); |
| 116 |
|
| 117 |
let mut slice = make(&mut buf[..]); |
| 118 |
slice.put_u32(0x61626364); |
| 119 |
assert_eq!(4, slice.remaining_mut()); |
| 120 |
assert_eq!(b"abcdCCXX", &buf[..]); |
| 121 |
|
| 122 |
let mut slice = make(&mut buf[..]); |
| 123 |
slice.put_u32_le(0x30313233); |
| 124 |
assert_eq!(4, slice.remaining_mut()); |
| 125 |
assert_eq!(b"3210CCXX", &buf[..]); |
| 126 |
} |
| 127 |
|
| 128 |
fn do_test_slice_large<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T) |
| 129 |
where |
| 130 |
for<'r> &'r mut T: BufMut, |
| 131 |
{ |
| 132 |
const LEN: usize = 100; |
| 133 |
const FILL: [u8; LEN] = [b'Y'; LEN]; |
| 134 |
|
| 135 |
let test = |fill: &dyn Fn(&mut &mut T, usize)| { |
| 136 |
for buf_len in 0..LEN { |
| 137 |
let mut buf = [b'X'; LEN]; |
| 138 |
for fill_len in 0..=buf_len { |
| 139 |
let mut slice = make(&mut buf[..buf_len]); |
| 140 |
fill(&mut slice, fill_len); |
| 141 |
assert_eq!(buf_len - fill_len, slice.remaining_mut()); |
| 142 |
let (head, tail) = buf.split_at(fill_len); |
| 143 |
assert_eq!(&FILL[..fill_len], head); |
| 144 |
assert!(tail.iter().all(|b| *b == b'X')); |
| 145 |
} |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
test(&|slice, fill_len| slice.put_slice(&FILL[..fill_len])); |
| 150 |
test(&|slice, fill_len| slice.put_bytes(FILL[0], fill_len)); |
| 151 |
} |
| 152 |
|
| 153 |
fn do_test_slice_put_slice_panics<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T) |
| 154 |
where |
| 155 |
for<'r> &'r mut T: BufMut, |
| 156 |
{ |
| 157 |
let mut buf = [b'X'; 4]; |
| 158 |
let mut slice = make(&mut buf[..]); |
| 159 |
slice.put_slice(b"12345"); |
| 160 |
} |
| 161 |
|
| 162 |
fn do_test_slice_put_bytes_panics<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T) |
| 163 |
where |
| 164 |
for<'r> &'r mut T: BufMut, |
| 165 |
{ |
| 166 |
let mut buf = [b'X'; 4]; |
| 167 |
let mut slice = make(&mut buf[..]); |
| 168 |
slice.put_bytes(b'1', 5); |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn test_slice_buf_mut_small() { |
| 173 |
do_test_slice_small(|x| x); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn test_slice_buf_mut_large() { |
| 178 |
do_test_slice_large(|x| x); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
#[should_panic] |
| 183 |
fn test_slice_buf_mut_put_slice_overflow() { |
| 184 |
do_test_slice_put_slice_panics(|x| x); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
#[should_panic] |
| 189 |
fn test_slice_buf_mut_put_bytes_overflow() { |
| 190 |
do_test_slice_put_bytes_panics(|x| x); |
| 191 |
} |
| 192 |
|
| 193 |
fn make_maybe_uninit_slice(slice: &mut [u8]) -> &mut [MaybeUninit<u8>] { |
| 194 |
// SAFETY: [u8] has the same layout as [MaybeUninit<u8>]. |
| 195 |
unsafe { core::mem::transmute(slice) } |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn test_maybe_uninit_buf_mut_small() { |
| 200 |
do_test_slice_small(make_maybe_uninit_slice); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn test_maybe_uninit_buf_mut_large() { |
| 205 |
do_test_slice_large(make_maybe_uninit_slice); |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
#[should_panic] |
| 210 |
fn test_maybe_uninit_buf_mut_put_slice_overflow() { |
| 211 |
do_test_slice_put_slice_panics(make_maybe_uninit_slice); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
#[should_panic] |
| 216 |
fn test_maybe_uninit_buf_mut_put_bytes_overflow() { |
| 217 |
do_test_slice_put_bytes_panics(make_maybe_uninit_slice); |
| 218 |
} |
| 219 |
|
| 220 |
#[allow(unused_allocation)] // This is intentional. |
| 221 |
#[test] |
| 222 |
fn test_deref_bufmut_forwards() { |
| 223 |
struct Special; |
| 224 |
|
| 225 |
unsafe impl BufMut for Special { |
| 226 |
fn remaining_mut(&self) -> usize { |
| 227 |
unreachable!("remaining_mut"); |
| 228 |
} |
| 229 |
|
| 230 |
fn chunk_mut(&mut self) -> &mut UninitSlice { |
| 231 |
unreachable!("chunk_mut"); |
| 232 |
} |
| 233 |
|
| 234 |
unsafe fn advance_mut(&mut self, _: usize) { |
| 235 |
unreachable!("advance"); |
| 236 |
} |
| 237 |
|
| 238 |
fn put_u8(&mut self, _: u8) { |
| 239 |
// specialized! |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// these should all use the specialized method |
| 244 |
Special.put_u8(b'x'); |
| 245 |
(&mut Special as &mut dyn BufMut).put_u8(b'x'); |
| 246 |
(Box::new(Special) as Box<dyn BufMut>).put_u8(b'x'); |
| 247 |
Box::new(Special).put_u8(b'x'); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
#[should_panic] |
| 252 |
fn write_byte_panics_if_out_of_bounds() { |
| 253 |
let mut data = [b'b', b'a', b'r']; |
| 254 |
|
| 255 |
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; |
| 256 |
slice.write_byte(4, b'f'); |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
#[should_panic] |
| 261 |
fn copy_from_slice_panics_if_different_length_1() { |
| 262 |
let mut data = [b'b', b'a', b'r']; |
| 263 |
|
| 264 |
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; |
| 265 |
slice.copy_from_slice(b"a"); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
#[should_panic] |
| 270 |
fn copy_from_slice_panics_if_different_length_2() { |
| 271 |
let mut data = [b'b', b'a', b'r']; |
| 272 |
|
| 273 |
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; |
| 274 |
slice.copy_from_slice(b"abcd"); |
| 275 |
} |
| 276 |
|
| 277 |
/// Test if with zero capacity BytesMut does not infinitely recurse in put from Buf |
| 278 |
#[test] |
| 279 |
fn test_bytes_mut_reuse() { |
| 280 |
let mut buf = BytesMut::new(); |
| 281 |
buf.put(&[] as &[u8]); |
| 282 |
let mut buf = BytesMut::new(); |
| 283 |
buf.put(&[1u8, 2, 3] as &[u8]); |
| 284 |
} |