#![allow(clippy::unusual_byte_groupings)]
use crate::frame::{Frame, Opcode};
use crate::tests::mock_stream::MockStream;
#[rustfmt::skip]
pub const FRAME_1_BYTES: [u8; 12] = [
0b0000_0001, 0b1_0000110, 0x69, 0x69, 0x69, 0x69, 1, 12, 5, 5, 6, 73 ];
#[rustfmt::skip]
pub const FRAME_2_BYTES: [u8; 11] = [
0b1000_0000, 0b1_0000101, 0x69, 0x69, 0x69, 0x69, 30, 6, 27, 5, 13 ];
#[rustfmt::skip]
pub const STANDALONE_FRAME_BYTES: [u8; 11] = [
0b1000_0001, 0b1_0000101, 0x69, 0x69, 0x69, 0x69, 1, 12, 5, 5, 6 ];
#[rustfmt::skip]
pub const UNMASKED_BYTES: [u8; 13] = [
0b1000_0001, 0b0_0001011, b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd' ];
#[rustfmt::skip]
pub const MEDIUM_FRAME_BYTES: [u8; 8] = [
0b1000_0001, 0b1_1111110, 0x01, 0x00, 0x69, 0x69, 0x69, 0x69, ];
#[rustfmt::skip]
pub const LONG_FRAME_BYTES: [u8; 14] = [
0b1000_0001, 0b1_1111111, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x69, 0x69, 0x69, 0x69, ];
#[test]
fn test_initial_frame() {
let mut bytes = Vec::with_capacity(23);
bytes.extend(FRAME_1_BYTES);
bytes.extend(FRAME_2_BYTES);
let mut stream = MockStream::with_data(bytes);
let frame = Frame::from_stream(&mut stream).unwrap();
let expected_frame = Frame {
fin: false,
rsv: [false; 3],
opcode: Opcode::Text,
mask: true,
masking_key: [0x69; 4],
length: 6,
payload: b"hello ".to_vec(),
};
assert_eq!(frame, expected_frame);
}
#[test]
fn test_continuation_frame() {
let mut stream = MockStream::with_data(FRAME_2_BYTES.to_vec());
let frame = Frame::from_stream(&mut stream).unwrap();
let expected_frame = Frame {
fin: true,
rsv: [false; 3],
opcode: Opcode::Continuation,
mask: true,
masking_key: [0x69; 4],
length: 5,
payload: b"world".to_vec(),
};
assert_eq!(frame, expected_frame);
}
#[test]
fn test_standalone_frame() {
let mut stream = MockStream::with_data(STANDALONE_FRAME_BYTES.to_vec());
let frame = Frame::from_stream(&mut stream).unwrap();
let expected_frame = Frame {
fin: true,
rsv: [false; 3],
opcode: Opcode::Text,
mask: true,
masking_key: [0x69; 4],
length: 5,
payload: b"hello".to_vec(),
};
assert_eq!(frame, expected_frame);
}
#[test]
fn test_medium_frame() {
let mut bytes = Vec::with_capacity(264);
bytes.extend(MEDIUM_FRAME_BYTES);
bytes.extend(vec![b'x' ^ 0x69; 256]);
let mut stream = MockStream::with_data(bytes);
let frame = Frame::from_stream(&mut stream).unwrap();
let expected_frame = Frame {
fin: true,
rsv: [false; 3],
opcode: Opcode::Text,
mask: true,
masking_key: [0x69; 4],
length: 256,
payload: vec![b'x'; 256],
};
assert_eq!(frame, expected_frame);
}
#[test]
fn test_long_frame() {
let mut bytes = Vec::with_capacity(65550);
bytes.extend(LONG_FRAME_BYTES);
bytes.extend(vec![b'x' ^ 0x69; 65536]);
let mut stream = MockStream::with_data(bytes);
let frame = Frame::from_stream(&mut stream).unwrap();
let expected_frame = Frame {
fin: true,
rsv: [false; 3],
opcode: Opcode::Text,
mask: true,
masking_key: [0x69; 4],
length: 65536,
payload: vec![b'x'; 65536],
};
assert_eq!(frame, expected_frame);
}
#[test]
fn test_write() {
let frame = Frame {
fin: true,
rsv: [false; 3],
opcode: Opcode::Text,
mask: false,
masking_key: [0; 4],
length: 11,
payload: b"hello world".to_vec(),
};
let bytes: Vec<u8> = frame.into();
assert_eq!(bytes, UNMASKED_BYTES.to_vec());
}