1use crate::protocol::*;
2use crate::errors::*;
3use std::convert::TryInto;
4use std::collections::HashMap;
5use std::hash::Hash;
6use num_enum::TryFromPrimitive;
7use std::convert::TryFrom;
8
9#[derive(Debug)]
12pub struct IceSize {
13 pub size: i32
14}
15
16pub trait ToBytes {
19 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>>;
20}
21
22pub trait FromBytes {
25 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> where Self: Sized;
26}
27
28pub trait OptionalType {
29 fn optional_type() -> u8;
30}
31
32impl OptionalType for i32 {
33 fn optional_type() -> u8 {
34 2
35 }
36}
37
38impl OptionalType for String {
39 fn optional_type() -> u8 {
40 5
41 }
42}
43
44pub struct OptionalWrapper<T> {
45 flag: OptionalFlag,
46 value: Option<T>
47}
48
49impl<T: OptionalType> OptionalWrapper<T> {
50 pub fn new(tag: u8, value: Option<T>) -> OptionalWrapper<T> {
51 OptionalWrapper {
52 flag: OptionalFlag::new(tag, T::optional_type()),
53 value
54 }
55 }
56}
57
58impl<T> Into<Option<T>> for OptionalWrapper<T> {
59 fn into(self) -> Option<T> {
60 self.value
61 }
62}
63
64#[derive(Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
65#[repr(u8)]
66pub enum SliceFlagsTypeEncoding {
67 NoTypeId,
68 StringTypeId,
69 IndexTypeId,
70 CompactTypeId,
71}
72
73#[derive(Debug)]
74pub struct SliceFlags {
75 pub type_id: SliceFlagsTypeEncoding,
76 pub optional_members: bool,
77 pub indirection_table: bool,
78 pub slice_size: bool,
79 pub last_slice: bool,
80}
81
82#[derive(Debug)]
83pub struct OptionalFlag {
84 pub tag: u8,
85 pub r#type: u8
86}
87
88impl OptionalFlag {
89 pub fn new(tag: u8, r#type: u8) -> OptionalFlag {
90 OptionalFlag {
91 tag: tag,
92 r#type: r#type
93 }
94 }
95}
96
97impl ToBytes for SliceFlags {
99 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
100 let mut byte = self.type_id.clone() as u8 & 0b11;
101 if self.optional_members {
102 byte = byte | 0b100;
103 }
104 if self.indirection_table {
105 byte = byte | 0b1000;
106 }
107 if self.slice_size {
108 byte = byte | 0b10000;
109 }
110 if self.last_slice {
111 byte = byte | 0b100000;
112 }
113
114 Ok(vec![byte])
115 }
116}
117
118impl FromBytes for SliceFlags {
119 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
120 where Self: Sized {
121 if bytes.len() < 1 {
122 return Err(Box::new(ProtocolError::new("Not enough bytes to read SliceFlags")));
123 } else {
124 let byte = bytes[0];
125 *read_bytes = *read_bytes + 1;
126 Ok(SliceFlags {
127 type_id: SliceFlagsTypeEncoding::try_from(byte & 0b11)?,
128 optional_members: byte & 0b100 > 0,
129 indirection_table: byte & 0b1000 > 0,
130 slice_size: byte & 0b10000 > 0,
131 last_slice: byte & 0b100000 > 0,
132 })
133 }
134 }
135}
136
137impl ToBytes for OptionalFlag {
138 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
139 let tag_bits = self.r#type & 7;
140 let type_bits = self.tag << 3;
141 Ok(vec![tag_bits | type_bits])
142 }
143}
144
145impl FromBytes for OptionalFlag {
146 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
147 where Self: Sized {
148 if bytes.len() < 1 {
149 return Err(Box::new(ProtocolError::new("Not enough bytes to read OptionalFlag")));
150 } else {
151 let byte = bytes[0];
152 let tag = byte >> 3;
153 let r#type = byte & 7;
154 *read_bytes = *read_bytes + 1;
155 Ok(OptionalFlag::new(tag, r#type))
156 }
157 }
158}
159
160impl ToBytes for IceSize {
161 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
162 if self.size < 255 {
163 Ok(vec![self.size as u8])
164 } else {
165 let mut bytes = vec![255];
166 bytes.extend(self.size.to_bytes()?);
167 Ok(bytes)
168 }
169 }
170}
171
172impl FromBytes for IceSize {
173 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
174 where Self: Sized {
175 if bytes.len() < 1 {
176 return Err(Box::new(ProtocolError::new("Not enough bytes to read IceSize")));
177 }
178 else if bytes[0] == 255 {
179 if bytes.len() < 5 {
180 return Err(Box::new(ProtocolError::new("Not enough bytes to read IceSize")));
181 } else {
182 *read_bytes = 1;
183 Ok(IceSize {
184 size: i32::from_bytes(&bytes[1..5], read_bytes)?
185 })
186 }
187 } else {
188 Ok(IceSize {
189 size: u8::from_bytes(bytes, read_bytes)? as i32
190 })
191 }
192 }
193}
194
195impl ToBytes for str {
196 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
197 let mut bytes = IceSize{size: self.len() as i32}.to_bytes()?;
198 bytes.extend(self.as_bytes());
199 Ok(bytes)
200 }
201}
202
203impl ToBytes for String {
204 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
205 let mut bytes = IceSize{size: self.len() as i32}.to_bytes()?;
206 bytes.extend(self.as_bytes());
207 Ok(bytes)
208 }
209}
210
211impl FromBytes for String {
212 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
213 where Self: Sized {
214 let mut read = 0;
215 let size = IceSize::from_bytes(bytes, &mut read)?.size;
216 let s = String::from_utf8(bytes[read as usize..read as usize + size as usize].to_vec())?;
217 *read_bytes = *read_bytes + read + size;
218 Ok(s)
219 }
220}
221
222
223impl<T: ToBytes, U: ToBytes> ToBytes for HashMap<T, U> {
224 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
225 let mut bytes = IceSize{size: self.len() as i32}.to_bytes()?;
226 for (key, value) in self {
227 bytes.extend(key.to_bytes()?);
228 bytes.extend(value.to_bytes()?);
229 }
230 Ok(bytes)
231 }
232}
233
234impl<T: FromBytes + Eq + Hash, U: FromBytes> FromBytes for HashMap<T, U> {
235 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
236 where Self: Sized {
237 let mut read = 0;
238 let size = IceSize::from_bytes(bytes, &mut read)?.size;
239 let mut dict: HashMap<T, U> = HashMap::new();
240
241 for _i in 0..size {
242 let key = T::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
243 let value = U::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
244 dict.insert(key, value);
245 }
246 *read_bytes = *read_bytes + read;
247 Ok(dict)
248 }
249}
250
251impl<T: ToBytes> ToBytes for Vec<T>
252{
253 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
254 let mut bytes = IceSize{size: self.len() as i32}.to_bytes()?;
255 for item in self {
256 bytes.extend(item.to_bytes()?);
257 }
258 Ok(bytes)
259 }
260}
261
262impl<T: FromBytes> FromBytes for Vec<T>
263{
264 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
265 where Self: Sized {
266 let mut read = 0;
267 let size = IceSize::from_bytes(bytes, &mut read)?.size;
268 let mut seq: Vec<T> = vec![];
269
270 for _i in 0..size {
271 seq.push(T::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?);
272 }
273 *read_bytes = *read_bytes + read;
274 Ok(seq)
275 }
276}
277
278impl ToBytes for u8 {
279 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
280 Ok(vec![*self])
281 }
282}
283
284impl FromBytes for u8 {
285 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
286 where Self: Sized {
287 *read_bytes = *read_bytes + 1;
288 Ok(bytes[0])
289 }
290}
291
292impl ToBytes for i16 {
293 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
294 Ok(self.to_le_bytes().to_vec())
295 }
296}
297
298impl FromBytes for i16 {
299 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
300 where Self: Sized {
301 let size = std::mem::size_of::<i16>();
302 if bytes.len() < size {
303 return Err(Box::new(ProtocolError::new("Not enough bytes to read i16")));
304 }
305 match bytes[0..size].try_into() {
306 Ok(barray) => {
307 *read_bytes = *read_bytes + size as i32;
308 Ok(i16::from_le_bytes(barray))
309 },
310 _ => Err(Box::new(ProtocolError::new("Error reading i16")))
311 }
312 }
313}
314
315impl ToBytes for i32 {
316 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
317 Ok(self.to_le_bytes().to_vec())
318 }
319}
320
321impl FromBytes for i32 {
322 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
323 where Self: Sized {
324 let size = std::mem::size_of::<i32>();
325 if bytes.len() < size {
326 return Err(Box::new(ProtocolError::new("Not enough bytes to read i32")));
327 }
328 match bytes[0..size].try_into() {
329 Ok(barray) => {
330 *read_bytes = *read_bytes + size as i32;
331 Ok(i32::from_le_bytes(barray))
332 },
333 _ => Err(Box::new(ProtocolError::new("Error reading i32")))
334 }
335 }
336}
337
338impl ToBytes for i64 {
339 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
340 Ok(self.to_le_bytes().to_vec())
341 }
342}
343
344impl FromBytes for i64 {
345 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
346 where Self: Sized {
347 let size = std::mem::size_of::<i64>();
348 if bytes.len() < size {
349 return Err(Box::new(ProtocolError::new("Not enough bytes to read i64")));
350 }
351 match bytes[0..size].try_into() {
352 Ok(barray) => {
353 *read_bytes = *read_bytes + size as i32;
354 Ok(i64::from_le_bytes(barray))
355 },
356 _ => Err(Box::new(ProtocolError::new("Error reading i64")))
357 }
358 }
359}
360
361impl ToBytes for f32 {
362 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
363 Ok(self.to_le_bytes().to_vec())
364 }
365}
366
367impl FromBytes for f32 {
368 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
369 where Self: Sized {
370 let size = std::mem::size_of::<f32>();
371 if bytes.len() < size {
372 return Err(Box::new(ProtocolError::new("Not enough bytes to read f32")));
373 }
374 match bytes[0..size].try_into() {
375 Ok(barray) => {
376 *read_bytes = *read_bytes + size as i32;
377 Ok(f32::from_le_bytes(barray))
378 },
379 _ => Err(Box::new(ProtocolError::new("Error reading f32")))
380 }
381 }
382}
383
384impl ToBytes for f64 {
385 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
386 Ok(self.to_le_bytes().to_vec())
387 }
388}
389
390impl FromBytes for f64 {
391 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
392 where Self: Sized {
393 let size = std::mem::size_of::<f64>();
394 if bytes.len() < size {
395 return Err(Box::new(ProtocolError::new("Not enough bytes to read f64")));
396 }
397 match bytes[0..size].try_into() {
398 Ok(barray) => {
399 *read_bytes = *read_bytes + size as i32;
400 Ok(f64::from_le_bytes(barray))
401 },
402 _ => Err(Box::new(ProtocolError::new("Error reading f64")))
403 }
404 }
405}
406
407impl ToBytes for bool {
408 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
409 Ok(vec![if *self { 1 } else { 0 }])
410 }
411}
412
413impl ToBytes for () {
414 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
415 Ok(vec![])
416 }
417}
418
419impl FromBytes for bool {
420 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>>
421 where Self: Sized {
422 if bytes.len() < 1 {
423 return Err(Box::new(ProtocolError::new("Not enough bytes to read bool")));
424 }
425 *read_bytes = *read_bytes + 1;
426 Ok(bytes[0] != 0)
427 }
428}
429
430
431impl ToBytes for Encapsulation {
432 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>>
433 {
434 let mut buffer: Vec<u8> = Vec::new();
435 buffer.extend(&self.size.to_le_bytes());
436 buffer.push(self.major);
437 buffer.push(self.minor);
438 if self.data.len() > 0 {
439 buffer.extend(&self.data);
440 }
441 Ok(buffer)
442 }
443}
444
445impl FromBytes for Encapsulation {
446 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
447 let mut read: i32 = 0;
448 if bytes.len() < 6 {
449 return Err(Box::new(ProtocolError::new("Not enough bytes to read Encapsulation")));
450 }
451
452 let size = i32::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
453 let major = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
454 let minor = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
455 *read_bytes = *read_bytes + read + (bytes.len() as i32 - read);
456
457 Ok(Encapsulation {
458 size: size,
459 major: major,
460 minor: minor,
461 data: bytes[read as usize..bytes.len()].to_vec()
462 })
463 }
464}
465
466impl FromBytes for ReplyData {
467 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
468 let mut read: i32 = 0;
469 if bytes.len() < 11 {
470 return Err(Box::new(ProtocolError::new("Not enough bytes to read ReplyData")));
471 }
472
473 let request_id = i32::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
474 let status = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
475 match status {
476 0 | 1 => {
477 let encapsulation = Encapsulation::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
478 *read_bytes = *read_bytes + read;
479 Ok(ReplyData {
480 request_id: request_id,
481 status: status,
482 body: encapsulation
483 })
484 }
485 7 => {
489 Err(Box::new(
490 RemoteException {
491 cause: String::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?
492 }
493 ))
494 }
495 _ => Err(Box::new(ProtocolError::new(&format!("Unsupported ReplyData status: {}", status))))
496 }
497 }
498}
499
500impl ToBytes for Header {
501 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>>
502 {
503 let mut buffer: Vec<u8> = Vec::new();
504 buffer.extend(self.magic.as_bytes());
505 buffer.extend(self.protocol_major.to_bytes()?);
506 buffer.extend(self.protocol_minor.to_bytes()?);
507 buffer.extend(self.encoding_major.to_bytes()?);
508 buffer.extend(self.encoding_minor.to_bytes()?);
509 buffer.extend(self.message_type.to_bytes()?);
510 buffer.extend(self.compression_status.to_bytes()?);
511 buffer.extend(self.message_size.to_bytes()?);
512
513 Ok(buffer)
514 }
515}
516
517impl FromBytes for Header {
518 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
519 if bytes.len() < 14 {
520 return Err(Box::new(ProtocolError::new("Not enough bytes to read Header")));
521 }
522
523 let magic = String::from_utf8(bytes[0..4].to_vec())?;
524 if magic != "IceP" {
525 return Err(Box::new(ProtocolError::new(&format!("Wrong magic! Expected IceP but found {}", magic))));
526 }
527 let mut read: i32 = 4;
528 let protocol_major = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
529 let protocol_minor = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
530 let encoding_major = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
531 let encoding_minor = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
532 let message_type = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
533 let comression_status = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
534 let message_size = i32::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
535 *read_bytes = *read_bytes + read;
536
537 Ok(Header {
538 magic: magic,
539 protocol_major: protocol_major,
540 protocol_minor: protocol_minor,
541 encoding_major: encoding_major,
542 encoding_minor: encoding_minor,
543 message_type: message_type,
544 compression_status: comression_status,
545 message_size: message_size
546 })
547 }
548}
549
550impl<T: ToBytes> ToBytes for Option<T> {
551 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
552 let mut bytes = Vec::new();
553 match self {
554 Some(value) => {
555 bytes.extend((11 as u8).to_bytes()?);
557 bytes.extend(value.to_bytes()?);
558 }
559 None => {}
560 }
561 Ok(bytes)
562 }
563}
564
565impl<T: FromBytes> FromBytes for Option<T> {
566 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
567 if bytes.len() > 0 {
568 let mut read: i32 = 0;
569 let _flag = u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
570 let result = Some(T::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?);
571 *read_bytes = *read_bytes + read;
572 Ok(result)
573 } else {
574 Ok(None)
575 }
576 }
577}
578
579impl<T: ToBytes> ToBytes for OptionalWrapper<T> {
580 fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>> {
581 let mut bytes = Vec::new();
582 match &self.value {
583 Some(value) => {
584 bytes.extend(self.flag.to_bytes()?);
585 bytes.extend(value.to_bytes()?);
586 }
587 None => {}
588 }
589 Ok(bytes)
590 }
591}
592
593impl<T: FromBytes> FromBytes for OptionalWrapper<T> {
594 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
595 if bytes.len() > 0 {
596 let mut read: i32 = 0;
597 let result = OptionalWrapper {
598 flag: OptionalFlag::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?,
599 value: Some(T::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?)
600 };
601 *read_bytes = *read_bytes + read;
602 Ok(result)
603 } else {
604 Ok(OptionalWrapper {
605 flag: OptionalFlag::new(0, 0),
606 value: None
607 })
608 }
609 }
610}
611
612impl FromBytes for LocatorResult {
613 fn from_bytes(bytes: &[u8], read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
614 let mut read = 0;
615
616 let proxy_data = ProxyData::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
617 let size = IceSize::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
618
619 if size.size == 0 {
620 return Err(Box::new(ProtocolError::new("Error reading LocatorResult")));
621 }
622
623 match u8::from_bytes(&bytes[read as usize..bytes.len()], &mut read)? {
624 1 => {
625 let _unsued = i16::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
626 let encapsulation = Encapsulation::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
627 let tcp = EndpointData::from_bytes(&encapsulation.data[0..encapsulation.data.len()], &mut read)?;
628 *read_bytes = *read_bytes + read;
629 Ok(LocatorResult {
630 proxy_data: proxy_data,
631 size: size,
632 endpoint: EndPointType::TCP(tcp)
633 })
634 },
635 2 => {
636 let _unsued = i16::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
637 let encapsulation = Encapsulation::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
638 let ssl = EndpointData::from_bytes(&encapsulation.data[0..encapsulation.data.len()], &mut read)?;
639 *read_bytes = *read_bytes + read;
640 Ok(LocatorResult {
641 proxy_data: proxy_data,
642 size: size,
643 endpoint: EndPointType::SSL(ssl)
644 })
645 }
646 0 => {
647 let object = String::from_bytes(&bytes[read as usize..bytes.len()], &mut read)?;
648 *read_bytes = *read_bytes + read;
649 Ok(LocatorResult {
650 proxy_data: proxy_data,
651 size: size,
652 endpoint: EndPointType::WellKnownObject(object)
653 })
654 }
655 _ => {
656 Err(Box::new(ProtocolError::new("Unsupported endpoint type")))
657 }
658 }
659 }
660}
661
662
663
664#[cfg(test)]
665mod test {
666 use super::*;
667
668 #[test]
669 fn test_size_encoding() {
670 let mut read_bytes = 0;
671 let encoded = IceSize{size: 10}.to_bytes().expect("Could not encode size");
672 let decoded = IceSize::from_bytes(&encoded, &mut read_bytes).expect("Could not decode size").size;
673 assert_eq!(10, decoded);
674 assert_eq!(1, read_bytes);
675
676 read_bytes = 0;
677 let encoded = IceSize{size: 500}.to_bytes().expect("Could not encode size");
678 let decoded = IceSize::from_bytes(&encoded, &mut read_bytes).expect("Could not decode size").size;
679 assert_eq!(500, decoded);
680 assert_eq!(5, read_bytes);
681 }
682
683 #[test]
684 fn test_string_encoding() {
685 let mut read_bytes = 0;
686 let encoded = "Hello".to_bytes().expect("Cannot necode test string");
687 let decoded = String::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test string");
688 assert_eq!("Hello", decoded);
689 assert_eq!(6, read_bytes);
690 }
691
692 #[test]
693 fn test_dict_encoding() {
694 let mut read_bytes = 0;
695 let mut dict = HashMap::new();
696 dict.insert(String::from("Hello"), String::from("World"));
697
698 let encoded = dict.to_bytes().expect("Cannot encode test dict");
699 let decoded: HashMap<String, String> = HashMap::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test dict");
700 assert!(decoded.contains_key("Hello"));
701 assert_eq!("World", decoded.get("Hello").unwrap_or(&String::from("")));
702 }
703
704 #[test]
705 fn test_string_seq_encoding() {
706 let mut read_bytes = 0;
707 let seq = vec![String::from("Hello"), String::from("World")];
708 let encoded = seq.to_bytes().expect("Cannot encode test dict");
709 let decoded: Vec<String> = Vec::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test dict");
710 assert_eq!(2, decoded.len());
711 assert_eq!(seq, decoded);
712 }
713
714 #[test]
715 fn test_short_encoding() {
716 let mut read_bytes = 0;
717 let value: i16 = 3;
718 let encoded = value.to_bytes().expect("Cannot encode test short");
719 let decoded = i16::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test short");
720 assert_eq!(value, decoded);
721 }
722
723 #[test]
724 fn test_int_encoding() {
725 let mut read_bytes = 0;
726 let value: i32 = 3;
727 let encoded = value.to_bytes().expect("Cannot encode test int");
728 let decoded = i32::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test int");
729 assert_eq!(value, decoded);
730 }
731
732 #[test]
733 fn test_long_encoding() {
734 let mut read_bytes = 0;
735 let value: i64 = 3;
736 let encoded = value.to_bytes().expect("Cannot encode test long");
737 let decoded = i64::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test long");
738 assert_eq!(value, decoded);
739 }
740
741 #[test]
742 fn test_float_encoding() {
743 let mut read_bytes = 0;
744 let value: f32 = 3.14;
745 let encoded = value.to_bytes().expect("Cannot encode test float");
746 let decoded = f32::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test float");
747 assert_eq!(value, decoded);
748 }
749
750 #[test]
751 fn test_double_encoding() {
752 let mut read_bytes = 0;
753 let value: f64 = 3.14;
754 let encoded = value.to_bytes().expect("Cannot encode test double");
755 let decoded = f64::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode double long");
756 assert_eq!(value, decoded);
757 }
758
759 #[test]
760 fn test_bool_encoding() {
761 let mut read_bytes = 0;
762 let value = true;
763 let encoded = value.to_bytes().expect("Cannot encode test bool");
764 let decoded = bool::from_bytes(&encoded, &mut read_bytes).expect("Cannot decode test bool");
765 assert_eq!(value, decoded);
766 }
767
768 #[test]
769 fn test_identity_ecoding() {
770 let mut read_bytes = 0;
771 let id = Identity {
772 name: String::from("Hello"),
773 category: String::from(""),
774 };
775 let bytes = id.to_bytes().expect("Cannot encode test identity");
776 let decoded = Identity::from_bytes(&bytes, &mut read_bytes).expect("Cannot decode test identity");
777 assert_eq!(7, read_bytes);
778 assert_eq!(id.name, decoded.name);
779 assert_eq!(id.category, decoded.category);
780 }
781
782 #[test]
783 fn test_header_ecoding() {
784 let mut read_bytes = 0;
785 let header = Header::new(0, 14);
786 let bytes = header.to_bytes().expect("Cannot encode test header");
787 let decoded = Header::from_bytes(&bytes, &mut read_bytes).expect("Cannot decode test header");
788 assert_eq!(14, read_bytes);
789 assert_eq!(header.magic, decoded.magic);
790 assert_eq!(header.message_size, decoded.message_size);
791 assert_eq!(header.message_type, decoded.message_type);
792 assert_eq!(header.magic, decoded.magic);
793 }
794
795 #[test]
796 fn test_request_ecoding() {
797 let mut read_bytes = 0;
798 let request = RequestData {
799 request_id: 1,
800 id: Identity {
801 name: String::from("Test"),
802 category: String::from(""),
803 },
804 facet: vec![],
805 operation: String::from("Op"),
806 mode: 0,
807 context: HashMap::new(),
808 params: Encapsulation::empty()
809 };
810 let bytes = request.to_bytes().expect("Cannot encode test request");
811 let decoded = RequestData::from_bytes(&bytes, &mut read_bytes).expect("Cannot decode test request");
812 assert_eq!(22, read_bytes);
813 assert_eq!(request.request_id, decoded.request_id);
814 assert_eq!(request.id.name, decoded.id.name);
815 assert_eq!(request.facet, decoded.facet);
816 assert_eq!(request.operation, decoded.operation);
817 assert_eq!(request.mode, decoded.mode);
818 assert_eq!(request.context, decoded.context);
819 }
820
821 #[test]
822 fn test_reply_encoding() {
823 let mut read_bytes = 0;
824 let reply = ReplyData {
825 request_id: 1,
826 status: 0,
827 body: Encapsulation::empty()
828 };
829 let bytes = reply.to_bytes().expect("Cannot encode test reply");
830 let decoded = ReplyData::from_bytes(&bytes, &mut read_bytes).expect("Cannot decode test reply");
831 assert_eq!(11, read_bytes);
832 assert_eq!(reply.request_id, decoded.request_id);
833 assert_eq!(reply.status, decoded.status);
834 }
835}