2021-02-18 00:01:57 +00:00
|
|
|
use crate::types::{Bandwidth, Mid, Pid, Prio, Promises, Sid};
|
2021-02-14 17:45:12 +00:00
|
|
|
use bytes::{Buf, BufMut, Bytes, BytesMut};
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
// const FRAME_RESERVED_1: u8 = 0;
|
|
|
|
const FRAME_HANDSHAKE: u8 = 1;
|
|
|
|
const FRAME_INIT: u8 = 2;
|
|
|
|
const FRAME_SHUTDOWN: u8 = 3;
|
|
|
|
const FRAME_OPEN_STREAM: u8 = 4;
|
|
|
|
const FRAME_CLOSE_STREAM: u8 = 5;
|
|
|
|
const FRAME_DATA_HEADER: u8 = 6;
|
|
|
|
const FRAME_DATA: u8 = 7;
|
|
|
|
const FRAME_RAW: u8 = 8;
|
|
|
|
//const FRAME_RESERVED_2: u8 = 10;
|
|
|
|
//const FRAME_RESERVED_3: u8 = 13;
|
|
|
|
|
|
|
|
/// Used for Communication between Channel <----(TCP/UDP)----> Channel
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2021-02-14 17:45:12 +00:00
|
|
|
pub enum InitFrame {
|
2021-01-22 16:09:20 +00:00
|
|
|
Handshake {
|
|
|
|
magic_number: [u8; 7],
|
|
|
|
version: [u32; 3],
|
|
|
|
},
|
|
|
|
Init {
|
|
|
|
pid: Pid,
|
|
|
|
secret: u128,
|
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
/// WARNING: sending RAW is only for debug purposes and will drop the
|
|
|
|
/// connection
|
2021-01-22 16:09:20 +00:00
|
|
|
Raw(Vec<u8>),
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
/// Used for OUT TCP Communication between Channel --(TCP)--> Channel
|
2021-01-22 16:09:20 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2021-02-14 17:45:12 +00:00
|
|
|
pub enum OTFrame {
|
2021-01-22 16:09:20 +00:00
|
|
|
Shutdown, /* Shutdown this channel gracefully, if all channels are shutdown (gracefully),
|
|
|
|
* Participant is deleted */
|
|
|
|
OpenStream {
|
|
|
|
sid: Sid,
|
|
|
|
prio: Prio,
|
|
|
|
promises: Promises,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth: Bandwidth,
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
CloseStream {
|
|
|
|
sid: Sid,
|
|
|
|
},
|
|
|
|
DataHeader {
|
|
|
|
mid: Mid,
|
|
|
|
sid: Sid,
|
|
|
|
length: u64,
|
|
|
|
},
|
|
|
|
Data {
|
|
|
|
mid: Mid,
|
2021-02-14 17:45:12 +00:00
|
|
|
data: Bytes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used for IN TCP Communication between Channel <--(TCP)-- Channel
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum ITFrame {
|
|
|
|
Shutdown, /* Shutdown this channel gracefully, if all channels are shutdown (gracefully),
|
|
|
|
* Participant is deleted */
|
|
|
|
OpenStream {
|
|
|
|
sid: Sid,
|
|
|
|
prio: Prio,
|
|
|
|
promises: Promises,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth: Bandwidth,
|
2021-02-14 17:45:12 +00:00
|
|
|
},
|
|
|
|
CloseStream {
|
|
|
|
sid: Sid,
|
|
|
|
},
|
|
|
|
DataHeader {
|
|
|
|
mid: Mid,
|
|
|
|
sid: Sid,
|
|
|
|
length: u64,
|
|
|
|
},
|
|
|
|
Data {
|
|
|
|
mid: Mid,
|
|
|
|
data: BytesMut,
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InitFrame {
|
|
|
|
// Size WITHOUT the 1rst indicating byte
|
|
|
|
pub(crate) const HANDSHAKE_CNS: usize = 19;
|
|
|
|
pub(crate) const INIT_CNS: usize = 32;
|
|
|
|
/// const part of the RAW frame, actual size is variable
|
|
|
|
pub(crate) const RAW_CNS: usize = 2;
|
|
|
|
|
|
|
|
//provide an appropriate buffer size. > 1500
|
2021-02-14 17:45:12 +00:00
|
|
|
pub(crate) fn write_bytes(self, bytes: &mut BytesMut) {
|
2021-01-22 16:09:20 +00:00
|
|
|
match self {
|
|
|
|
InitFrame::Handshake {
|
|
|
|
magic_number,
|
|
|
|
version,
|
|
|
|
} => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_HANDSHAKE);
|
|
|
|
bytes.put_slice(&magic_number);
|
|
|
|
bytes.put_u32_le(version[0]);
|
|
|
|
bytes.put_u32_le(version[1]);
|
|
|
|
bytes.put_u32_le(version[2]);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
InitFrame::Init { pid, secret } => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_INIT);
|
|
|
|
pid.to_bytes(bytes);
|
|
|
|
bytes.put_u128_le(secret);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
InitFrame::Raw(data) => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_RAW);
|
|
|
|
bytes.put_u16_le(data.len() as u16);
|
|
|
|
bytes.put_slice(&data);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
pub(crate) fn read_frame(bytes: &mut BytesMut) -> Option<Self> {
|
2021-01-22 16:09:20 +00:00
|
|
|
let frame_no = match bytes.get(0) {
|
|
|
|
Some(&f) => f,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
let frame = match frame_no {
|
|
|
|
FRAME_HANDSHAKE => {
|
|
|
|
if bytes.len() < Self::HANDSHAKE_CNS + 1 {
|
|
|
|
return None;
|
|
|
|
}
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.advance(1);
|
|
|
|
let mut magic_number_bytes = bytes.copy_to_bytes(7);
|
|
|
|
let mut magic_number = [0u8; 7];
|
|
|
|
magic_number_bytes.copy_to_slice(&mut magic_number);
|
|
|
|
InitFrame::Handshake {
|
|
|
|
magic_number,
|
|
|
|
version: [bytes.get_u32_le(), bytes.get_u32_le(), bytes.get_u32_le()],
|
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
FRAME_INIT => {
|
|
|
|
if bytes.len() < Self::INIT_CNS + 1 {
|
|
|
|
return None;
|
|
|
|
}
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.advance(1);
|
|
|
|
InitFrame::Init {
|
|
|
|
pid: Pid::from_bytes(bytes),
|
|
|
|
secret: bytes.get_u128_le(),
|
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
FRAME_RAW => {
|
|
|
|
if bytes.len() < Self::RAW_CNS + 1 {
|
|
|
|
return None;
|
|
|
|
}
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.advance(1);
|
|
|
|
let length = bytes.get_u16_le() as usize;
|
|
|
|
// lower length is allowed
|
|
|
|
let max_length = length.min(bytes.len());
|
|
|
|
let mut data = vec![0; max_length];
|
|
|
|
data.copy_from_slice(&bytes[..max_length]);
|
2021-01-22 16:09:20 +00:00
|
|
|
InitFrame::Raw(data)
|
|
|
|
},
|
2021-02-10 10:37:42 +00:00
|
|
|
_ => InitFrame::Raw(bytes.to_vec()),
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
Some(frame)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
pub(crate) const TCP_CLOSE_STREAM_CNS: usize = 8;
|
|
|
|
/// const part of the DATA frame, actual size is variable
|
2021-02-18 00:01:57 +00:00
|
|
|
pub(crate) const TCP_DATA_CNS: usize = 10;
|
2021-02-14 17:45:12 +00:00
|
|
|
pub(crate) const TCP_DATA_HEADER_CNS: usize = 24;
|
2021-02-18 00:01:57 +00:00
|
|
|
pub(crate) const TCP_OPEN_STREAM_CNS: usize = 18;
|
2021-02-14 17:45:12 +00:00
|
|
|
// Size WITHOUT the 1rst indicating byte
|
|
|
|
pub(crate) const TCP_SHUTDOWN_CNS: usize = 0;
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
impl OTFrame {
|
|
|
|
pub fn write_bytes(self, bytes: &mut BytesMut) {
|
2021-01-22 16:09:20 +00:00
|
|
|
match self {
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::Shutdown => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_SHUTDOWN);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::OpenStream {
|
2021-01-22 16:09:20 +00:00
|
|
|
sid,
|
|
|
|
prio,
|
|
|
|
promises,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth,
|
2021-01-22 16:09:20 +00:00
|
|
|
} => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_OPEN_STREAM);
|
2021-02-14 17:45:12 +00:00
|
|
|
sid.to_bytes(bytes);
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(prio);
|
|
|
|
bytes.put_u8(promises.to_le_bytes()[0]);
|
2021-02-18 00:01:57 +00:00
|
|
|
bytes.put_u64_le(guaranteed_bandwidth);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::CloseStream { sid } => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_CLOSE_STREAM);
|
2021-02-14 17:45:12 +00:00
|
|
|
sid.to_bytes(bytes);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::DataHeader { mid, sid, length } => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_DATA_HEADER);
|
|
|
|
bytes.put_u64_le(mid);
|
2021-02-14 17:45:12 +00:00
|
|
|
sid.to_bytes(bytes);
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u64_le(length);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-18 00:01:57 +00:00
|
|
|
Self::Data { mid, data } => {
|
2021-02-10 10:37:42 +00:00
|
|
|
bytes.put_u8(FRAME_DATA);
|
|
|
|
bytes.put_u64_le(mid);
|
|
|
|
bytes.put_u16_le(data.len() as u16);
|
|
|
|
bytes.put_slice(&data);
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 17:45:12 +00:00
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
impl ITFrame {
|
2021-04-07 17:11:16 +00:00
|
|
|
/// Err => cannot recover
|
|
|
|
/// Ok(None) => waiting for more data
|
|
|
|
pub(crate) fn read_frame(bytes: &mut BytesMut) -> Result<Option<Self>, ()> {
|
2021-02-10 10:37:42 +00:00
|
|
|
let frame_no = match bytes.first() {
|
2021-01-22 16:09:20 +00:00
|
|
|
Some(&f) => f,
|
2021-04-07 17:11:16 +00:00
|
|
|
None => return Ok(None),
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
let size = match frame_no {
|
2021-02-14 17:45:12 +00:00
|
|
|
FRAME_SHUTDOWN => TCP_SHUTDOWN_CNS,
|
|
|
|
FRAME_OPEN_STREAM => TCP_OPEN_STREAM_CNS,
|
|
|
|
FRAME_CLOSE_STREAM => TCP_CLOSE_STREAM_CNS,
|
|
|
|
FRAME_DATA_HEADER => TCP_DATA_HEADER_CNS,
|
2021-01-22 16:09:20 +00:00
|
|
|
FRAME_DATA => {
|
2021-02-18 00:01:57 +00:00
|
|
|
if bytes.len() < 9 + 1 + 1 {
|
2021-04-07 17:11:16 +00:00
|
|
|
return Ok(None);
|
2021-02-10 10:37:42 +00:00
|
|
|
}
|
2021-02-18 00:01:57 +00:00
|
|
|
u16::from_le_bytes([bytes[8 + 1], bytes[9 + 1]]) as usize + TCP_DATA_CNS
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-04-07 17:11:16 +00:00
|
|
|
_ => return Err(()),
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if bytes.len() < size + 1 {
|
2021-04-07 17:11:16 +00:00
|
|
|
return Ok(None);
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let frame = match frame_no {
|
|
|
|
FRAME_SHUTDOWN => {
|
2021-02-10 10:37:42 +00:00
|
|
|
let _ = bytes.split_to(size + 1);
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::Shutdown
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
FRAME_OPEN_STREAM => {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut bytes = bytes.split_to(size + 1);
|
|
|
|
bytes.advance(1);
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::OpenStream {
|
|
|
|
sid: Sid::from_bytes(&mut bytes),
|
2021-02-10 10:37:42 +00:00
|
|
|
prio: bytes.get_u8(),
|
|
|
|
promises: Promises::from_bits_truncate(bytes.get_u8()),
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth: bytes.get_u64_le(),
|
2021-02-10 10:37:42 +00:00
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
FRAME_CLOSE_STREAM => {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut bytes = bytes.split_to(size + 1);
|
|
|
|
bytes.advance(1);
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::CloseStream {
|
|
|
|
sid: Sid::from_bytes(&mut bytes),
|
2021-02-10 10:37:42 +00:00
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
FRAME_DATA_HEADER => {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut bytes = bytes.split_to(size + 1);
|
|
|
|
bytes.advance(1);
|
2021-02-14 17:45:12 +00:00
|
|
|
Self::DataHeader {
|
2021-02-10 10:37:42 +00:00
|
|
|
mid: bytes.get_u64_le(),
|
2021-02-14 17:45:12 +00:00
|
|
|
sid: Sid::from_bytes(&mut bytes),
|
2021-02-10 10:37:42 +00:00
|
|
|
length: bytes.get_u64_le(),
|
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
FRAME_DATA => {
|
2021-02-14 17:45:12 +00:00
|
|
|
bytes.advance(1);
|
|
|
|
let mid = bytes.get_u64_le();
|
|
|
|
let length = bytes.get_u16_le();
|
|
|
|
debug_assert_eq!(length as usize, size - TCP_DATA_CNS);
|
2021-02-10 10:37:42 +00:00
|
|
|
let data = bytes.split_to(length as usize);
|
2021-02-18 00:01:57 +00:00
|
|
|
Self::Data { mid, data }
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
|
|
|
_ => unreachable!("Frame::to_frame should be handled before!"),
|
|
|
|
};
|
2021-04-07 17:11:16 +00:00
|
|
|
Ok(Some(frame))
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
impl PartialEq<ITFrame> for OTFrame {
|
|
|
|
fn eq(&self, other: &ITFrame) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Shutdown => matches!(other, ITFrame::Shutdown),
|
|
|
|
Self::OpenStream {
|
|
|
|
sid,
|
|
|
|
prio,
|
|
|
|
promises,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth,
|
2021-02-14 17:45:12 +00:00
|
|
|
} => matches!(other, ITFrame::OpenStream {
|
|
|
|
sid,
|
|
|
|
prio,
|
2021-02-18 00:01:57 +00:00
|
|
|
promises,
|
|
|
|
guaranteed_bandwidth,
|
2021-02-14 17:45:12 +00:00
|
|
|
}),
|
|
|
|
Self::CloseStream { sid } => matches!(other, ITFrame::CloseStream { sid }),
|
|
|
|
Self::DataHeader { mid, sid, length } => {
|
|
|
|
matches!(other, ITFrame::DataHeader { mid, sid, length })
|
|
|
|
},
|
2021-02-18 00:01:57 +00:00
|
|
|
Self::Data { mid, data } => matches!(other, ITFrame::Data { mid, data }),
|
2021-02-14 17:45:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:09:20 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::types::{VELOREN_MAGIC_NUMBER, VELOREN_NETWORK_VERSION};
|
|
|
|
|
|
|
|
fn get_initframes() -> Vec<InitFrame> {
|
|
|
|
vec![
|
|
|
|
InitFrame::Handshake {
|
|
|
|
magic_number: VELOREN_MAGIC_NUMBER,
|
|
|
|
version: VELOREN_NETWORK_VERSION,
|
|
|
|
},
|
|
|
|
InitFrame::Init {
|
|
|
|
pid: Pid::fake(0),
|
|
|
|
secret: 0u128,
|
|
|
|
},
|
|
|
|
InitFrame::Raw(vec![1, 2, 3]),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
fn get_otframes() -> Vec<OTFrame> {
|
2021-01-22 16:09:20 +00:00
|
|
|
vec![
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::OpenStream {
|
2021-01-22 16:09:20 +00:00
|
|
|
sid: Sid::new(1337),
|
|
|
|
prio: 14,
|
|
|
|
promises: Promises::GUARANTEED_DELIVERY,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth: 1_000_000,
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::DataHeader {
|
2021-01-22 16:09:20 +00:00
|
|
|
sid: Sid::new(1337),
|
|
|
|
mid: 0,
|
|
|
|
length: 36,
|
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::Data {
|
2021-01-22 16:09:20 +00:00
|
|
|
mid: 0,
|
2021-02-14 17:45:12 +00:00
|
|
|
data: Bytes::from(&[77u8; 20][..]),
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::Data {
|
2021-01-22 16:09:20 +00:00
|
|
|
mid: 0,
|
2021-02-14 17:45:12 +00:00
|
|
|
data: Bytes::from(&[42u8; 16][..]),
|
2021-01-22 16:09:20 +00:00
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::CloseStream {
|
2021-01-22 16:09:20 +00:00
|
|
|
sid: Sid::new(1337),
|
|
|
|
},
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::Shutdown,
|
2021-01-22 16:09:20 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_individual() {
|
|
|
|
let dupl = |frame: InitFrame| {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(1500);
|
2021-02-14 17:45:12 +00:00
|
|
|
InitFrame::write_bytes(frame, &mut buffer);
|
|
|
|
InitFrame::read_frame(&mut buffer)
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for frame in get_initframes() {
|
|
|
|
println!("initframe: {:?}", &frame);
|
|
|
|
assert_eq!(Some(frame.clone()), dupl(frame));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_multiple() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(3000);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
let mut frames = get_initframes();
|
|
|
|
// to string
|
2021-02-10 10:37:42 +00:00
|
|
|
for f in &frames {
|
2021-02-14 17:45:12 +00:00
|
|
|
InitFrame::write_bytes(f.clone(), &mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
// from string
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut framesd = frames
|
2021-01-22 16:09:20 +00:00
|
|
|
.iter()
|
2021-02-14 17:45:12 +00:00
|
|
|
.map(|&_| InitFrame::read_frame(&mut buffer))
|
2021-01-22 16:09:20 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// compare
|
|
|
|
for (f, fd) in frames.drain(..).zip(framesd.drain(..)) {
|
|
|
|
println!("initframe: {:?}", &f);
|
|
|
|
assert_eq!(Some(f), fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_individual() {
|
2021-02-14 17:45:12 +00:00
|
|
|
let dupl = |frame: OTFrame| {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(1500);
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::write_bytes(frame, &mut buffer);
|
|
|
|
ITFrame::read_frame(&mut buffer)
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
for frame in get_otframes() {
|
2021-01-22 16:09:20 +00:00
|
|
|
println!("frame: {:?}", &frame);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(frame.clone(), dupl(frame).expect("ERR").expect("NONE"));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_multiple() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(3000);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
let mut frames = get_otframes();
|
2021-01-22 16:09:20 +00:00
|
|
|
// to string
|
2021-02-10 10:37:42 +00:00
|
|
|
for f in &frames {
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::write_bytes(f.clone(), &mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
}
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
// from string
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut framesd = frames
|
2021-01-22 16:09:20 +00:00
|
|
|
.iter()
|
2021-02-14 17:45:12 +00:00
|
|
|
.map(|&_| ITFrame::read_frame(&mut buffer))
|
2021-01-22 16:09:20 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// compare
|
|
|
|
for (f, fd) in frames.drain(..).zip(framesd.drain(..)) {
|
|
|
|
println!("frame: {:?}", &f);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(f, fd.expect("ERR").expect("NONE"));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_exact_size() {
|
2021-02-14 17:45:12 +00:00
|
|
|
const SIZE: usize = TCP_CLOSE_STREAM_CNS+1/*first byte*/;
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(SIZE);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1 = OTFrame::CloseStream { sid: Sid::new(2) };
|
|
|
|
OTFrame::write_bytes(frame1.clone(), &mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
assert_eq!(buffer.len(), SIZE);
|
2021-02-14 17:45:12 +00:00
|
|
|
let mut deque = buffer.iter().copied().collect();
|
|
|
|
let frame2 = ITFrame::read_frame(&mut deque);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(frame1, frame2.expect("ERR").expect("NONE"));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_too_short_buffer() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(10);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
let frame1 = InitFrame::Handshake {
|
|
|
|
magic_number: VELOREN_MAGIC_NUMBER,
|
|
|
|
version: VELOREN_NETWORK_VERSION,
|
|
|
|
};
|
2021-02-14 17:45:12 +00:00
|
|
|
InitFrame::write_bytes(frame1, &mut buffer);
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_too_less_data() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(20);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
let frame1 = InitFrame::Handshake {
|
|
|
|
magic_number: VELOREN_MAGIC_NUMBER,
|
|
|
|
version: VELOREN_NETWORK_VERSION,
|
|
|
|
};
|
2021-02-14 17:45:12 +00:00
|
|
|
let _ = InitFrame::write_bytes(frame1, &mut buffer);
|
2021-01-22 16:09:20 +00:00
|
|
|
buffer.truncate(6); // simulate partial retrieve
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1d = InitFrame::read_frame(&mut buffer);
|
2021-01-22 16:09:20 +00:00
|
|
|
assert_eq!(frame1d, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_rubish() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::from(&b"dtrgwcser"[..]);
|
2021-01-22 16:09:20 +00:00
|
|
|
assert_eq!(
|
2021-02-14 17:45:12 +00:00
|
|
|
InitFrame::read_frame(&mut buffer),
|
2021-01-22 16:09:20 +00:00
|
|
|
Some(InitFrame::Raw(b"dtrgwcser".to_vec()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_attack_too_much_length() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(50);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
let frame1 = InitFrame::Raw(b"foobar".to_vec());
|
2021-02-14 17:45:12 +00:00
|
|
|
let _ = InitFrame::write_bytes(frame1.clone(), &mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
buffer[1] = 255;
|
2021-02-14 17:45:12 +00:00
|
|
|
let framed = InitFrame::read_frame(&mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
assert_eq!(framed, Some(frame1));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn initframe_attack_too_low_length() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(50);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
|
|
|
let frame1 = InitFrame::Raw(b"foobar".to_vec());
|
2021-02-14 17:45:12 +00:00
|
|
|
let _ = InitFrame::write_bytes(frame1, &mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
buffer[1] = 3;
|
2021-02-14 17:45:12 +00:00
|
|
|
let framed = InitFrame::read_frame(&mut buffer);
|
2021-02-10 10:37:42 +00:00
|
|
|
// we accept a different frame here, as it's RAW and debug only!
|
|
|
|
assert_eq!(framed, Some(InitFrame::Raw(b"foo".to_vec())));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_too_short_buffer() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(10);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1 = OTFrame::OpenStream {
|
2021-01-22 16:09:20 +00:00
|
|
|
sid: Sid::new(88),
|
|
|
|
promises: Promises::ENCRYPTED,
|
|
|
|
prio: 88,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth: 1_000_000,
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::write_bytes(frame1, &mut buffer);
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_too_less_data() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(20);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1 = OTFrame::OpenStream {
|
2021-01-22 16:09:20 +00:00
|
|
|
sid: Sid::new(88),
|
|
|
|
promises: Promises::ENCRYPTED,
|
|
|
|
prio: 88,
|
2021-02-18 00:01:57 +00:00
|
|
|
guaranteed_bandwidth: 1_000_000,
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::write_bytes(frame1, &mut buffer);
|
2021-01-22 16:09:20 +00:00
|
|
|
buffer.truncate(6); // simulate partial retrieve
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1d = ITFrame::read_frame(&mut buffer);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(frame1d, Ok(None));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_rubish() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::from(&b"dtrgwcser"[..]);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(ITFrame::read_frame(&mut buffer), Err(()));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_attack_too_much_length() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(50);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1 = OTFrame::Data {
|
2021-01-22 16:09:20 +00:00
|
|
|
mid: 7u64,
|
2021-02-14 17:45:12 +00:00
|
|
|
data: Bytes::from(&b"foobar"[..]),
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::write_bytes(frame1, &mut buffer);
|
2021-02-18 00:01:57 +00:00
|
|
|
buffer[9] = 255;
|
2021-02-14 17:45:12 +00:00
|
|
|
let framed = ITFrame::read_frame(&mut buffer);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(framed, Ok(None));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn frame_attack_too_low_length() {
|
2021-02-10 10:37:42 +00:00
|
|
|
let mut buffer = BytesMut::with_capacity(50);
|
2021-01-22 16:09:20 +00:00
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
let frame1 = OTFrame::Data {
|
2021-01-22 16:09:20 +00:00
|
|
|
mid: 7u64,
|
2021-02-14 17:45:12 +00:00
|
|
|
data: Bytes::from(&b"foobar"[..]),
|
2021-01-22 16:09:20 +00:00
|
|
|
};
|
|
|
|
|
2021-02-14 17:45:12 +00:00
|
|
|
OTFrame::write_bytes(frame1, &mut buffer);
|
2021-02-18 00:01:57 +00:00
|
|
|
buffer[9] = 3;
|
2021-02-14 17:45:12 +00:00
|
|
|
let framed = ITFrame::read_frame(&mut buffer);
|
2021-01-22 16:09:20 +00:00
|
|
|
assert_eq!(
|
|
|
|
framed,
|
2021-04-07 17:11:16 +00:00
|
|
|
Ok(Some(ITFrame::Data {
|
2021-01-22 16:09:20 +00:00
|
|
|
mid: 7u64,
|
2021-02-14 17:45:12 +00:00
|
|
|
data: BytesMut::from(&b"foo"[..]),
|
2021-04-07 17:11:16 +00:00
|
|
|
}))
|
2021-01-22 16:09:20 +00:00
|
|
|
);
|
|
|
|
//next = Invalid => Empty
|
2021-02-14 17:45:12 +00:00
|
|
|
let framed = ITFrame::read_frame(&mut buffer);
|
2021-04-07 17:11:16 +00:00
|
|
|
assert_eq!(framed, Err(()));
|
2021-01-22 16:09:20 +00:00
|
|
|
}
|
|
|
|
}
|