diff options
| author | Max Audron <audron@cocaine.farm> | 2020-01-17 10:47:50 +0100 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2020-01-17 10:48:44 +0100 |
| commit | de973723312c56a58651f12146668500697543c0 (patch) | |
| tree | f7cc1e5f9039101bc199e611901b162aa4ed13b1 /src/tests | |
| parent | refactor parse impl (diff) | |
finish main parsing
Diffstat (limited to 'src/tests')
| -rw-r--r-- | src/tests/base_types.rs | 33 | ||||
| -rw-r--r-- | src/tests/handshake_types.rs | 63 |
2 files changed, 80 insertions, 16 deletions
diff --git a/src/tests/base_types.rs b/src/tests/base_types.rs index a35f243..99e6cd5 100644 --- a/src/tests/base_types.rs +++ b/src/tests/base_types.rs @@ -1,5 +1,6 @@ use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; +use crate::protocol::primitive::qread::QRead; use crate::protocol::primitive::*; @@ -18,6 +19,21 @@ pub fn serialize_string_utf8() { } #[test] +pub fn read_string() { + use std::io::Cursor; + + let test_bytes: Vec<u8> = vec![0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1]; + + let mut buf: Vec<u8> = [0; 24].to_vec(); + let len = String::read(&mut Cursor::new(&test_bytes), &mut buf); + + assert_eq!(len, 24); + + let result_bytes: Vec<u8> = vec![0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100]; + assert_eq!(buf, result_bytes); +} + +#[test] pub fn deserialize_string() { let test_bytes: &[u8] = &[0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1]; let (len, res) = String::parse(test_bytes); @@ -44,8 +60,23 @@ pub fn serialize_string_list() { } #[test] +pub fn read_string_list() { + use std::io::Cursor; + + let test_bytes: Vec<u8> = vec![0, 0, 0, 1, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1]; + + let mut buf: Vec<u8> = [0; 28].to_vec(); + let len = StringList::read(&mut Cursor::new(&test_bytes), &mut buf); + + assert_eq!(len, 28); + + let result_bytes: Vec<u8> = vec![0, 0, 0, 1, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100]; + assert_eq!(buf, result_bytes); +} + +#[test] pub fn deserialize_string_list() { - let test_bytes: &[u8] = &[0, 0, 0, 24, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1]; + let test_bytes: &[u8] = &[0, 0, 0, 1, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1]; let mut test_list = StringList::new(); test_list.push("Configured".to_string()); println!("aaaaa"); diff --git a/src/tests/handshake_types.rs b/src/tests/handshake_types.rs index 99fc1ed..dd3387f 100644 --- a/src/tests/handshake_types.rs +++ b/src/tests/handshake_types.rs @@ -1,11 +1,11 @@ -use crate::protocol::message::handshake::{VariantMap, HandshakeSerialize, HandshakeDeserialize}; +use crate::protocol::message::handshake::{VariantMap, HandshakeSerialize, HandshakeDeserialize, HandshakeQRead}; use crate::protocol::primitive::{Variant}; #[test] pub fn serialize_variantmap() { let mut test_variantmap = VariantMap::new(); test_variantmap.insert("Configured".to_string(), Variant::bool(true)); - let bytes = [0, 0, 0, 43, 0, 0, 0, 10, 0, 0, 0, 10, 0, + let bytes = [0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1, 0, 1].to_vec(); assert_eq!( @@ -14,16 +14,49 @@ pub fn serialize_variantmap() { ); } -// #[test] -// pub fn deserialize_variantmap() { -// let test_bytes: &[u8] = &[0, 0, 0, 43, 0, 0, 0, 10, 0, 0, 0, 10, 0, -// 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, -// 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]; -// let mut test_variantmap = VariantMap::new(); -// test_variantmap.insert("Configured".to_string(), Variant::bool(true)); -// -// let (len, res) = VariantMap::parse(test_bytes); -// -// assert_eq!(len, 43); -// assert_eq!(res, test_variantmap); -// } +#[test] +pub fn read_variantmap() { + use std::io::Cursor; + + let test_bytes: Vec<u8> = vec![0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0, + 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, + 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]; + + let mut buf: Vec<u8> = [0; 43].to_vec(); + let len = VariantMap::read(&mut Cursor::new(&test_bytes), &mut buf); + + assert_eq!(len, 43); + + let result_bytes: Vec<u8> = vec![0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0, + 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, + 0, 0, 0, 1, 0, 1]; + assert_eq!(buf, result_bytes); +} + +#[test] +pub fn deserialize_variantmap() { + let test_bytes: &[u8] = &[0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0, + 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, + 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]; + let mut test_variantmap = VariantMap::new(); + test_variantmap.insert("Configured".to_string(), Variant::bool(true)); + + let (len, res) = VariantMap::parse(test_bytes); + + assert_eq!(len, 43); + assert_eq!(res, test_variantmap); +} + +#[test] +pub fn deserialize_variantmap_utf8() { + let test_bytes: &[u8] = &[0, 0, 0, 29, 0, 0, 0, 10, 0, 0, 0, 12, 0, + 0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100, + 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]; + let mut test_variantmap = VariantMap::new(); + test_variantmap.insert("Configured".to_string(), Variant::bool(true)); + + let (len, res) = VariantMap::parse(test_bytes); + + assert_eq!(len, 33); + assert_eq!(res, test_variantmap); +} |
