1 use crate::err::ValueTooBigError; 2 3 /// Error while calculating the checksum in a transport header. 4 #[derive(Clone, Debug, Eq, PartialEq, Hash)] 5 pub enum TransportChecksumError { 6 /// Error if the length of the payload is too 7 /// big to be representable by the length fields. 8 PayloadLen(ValueTooBigError<usize>), 9 10 /// Error when an Icmpv6 payload is found in an IPv4 packet. 11 Icmpv6InIpv4, 12 } 13 14 impl core::fmt::Display for TransportChecksumError { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result15 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 16 use TransportChecksumError::*; 17 match self { 18 PayloadLen(err) => err.fmt(f), 19 Icmpv6InIpv4 => write!(f, "Error: ICMPv6 can not be combined with an IPv4 headers (checksum can not be calculated)."), 20 } 21 } 22 } 23 24 #[cfg(feature = "std")] 25 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 26 impl std::error::Error for TransportChecksumError { source(&self) -> Option<&(dyn std::error::Error + 'static)>27 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 28 use TransportChecksumError::*; 29 match self { 30 PayloadLen(err) => Some(err), 31 Icmpv6InIpv4 => None, 32 } 33 } 34 } 35 36 #[cfg(test)] 37 mod tests { 38 use super::{TransportChecksumError::*, *}; 39 use crate::err::ValueType; 40 use alloc::format; 41 use std::{ 42 collections::hash_map::DefaultHasher, 43 error::Error, 44 hash::{Hash, Hasher}, 45 }; 46 47 #[test] debug()48 fn debug() { 49 assert_eq!("Icmpv6InIpv4", format!("{:?}", Icmpv6InIpv4)); 50 } 51 52 #[test] clone_eq_hash()53 fn clone_eq_hash() { 54 let err = Icmpv6InIpv4; 55 assert_eq!(err, err.clone()); 56 let hash_a = { 57 let mut hasher = DefaultHasher::new(); 58 err.hash(&mut hasher); 59 hasher.finish() 60 }; 61 let hash_b = { 62 let mut hasher = DefaultHasher::new(); 63 err.clone().hash(&mut hasher); 64 hasher.finish() 65 }; 66 assert_eq!(hash_a, hash_b); 67 } 68 69 #[test] fmt()70 fn fmt() { 71 // PayloadLen 72 { 73 let err = ValueTooBigError { 74 actual: 1, 75 max_allowed: 2, 76 value_type: ValueType::TcpPayloadLengthIpv6, 77 }; 78 assert_eq!(format!("{}", &err), format!("{}", PayloadLen(err))); 79 } 80 81 // Icmpv6InIpv4 82 assert_eq!( 83 format!("{}", Icmpv6InIpv4), 84 "Error: ICMPv6 can not be combined with an IPv4 headers (checksum can not be calculated)." 85 ); 86 } 87 88 #[cfg(feature = "std")] 89 #[test] source()90 fn source() { 91 // Len 92 { 93 let err = ValueTooBigError { 94 actual: 1, 95 max_allowed: 2, 96 value_type: ValueType::TcpPayloadLengthIpv6, 97 }; 98 assert!(PayloadLen(err).source().is_some()); 99 } 100 101 // IpHeader 102 assert!(Icmpv6InIpv4.source().is_none()); 103 } 104 } 105