1 /// The header of an ICMPv4 Parameter Problems (contents up to
2 /// the offending ip header).
3 #[derive(Clone, Debug, PartialEq, Eq)]
4 pub enum ParameterProblemHeader {
5     /// Identifies the octet where an error was detected.
6     ///
7     /// The value is the pointer pointing to the offending octet in
8     /// the offending packet.
9     PointerIndicatesError(u8),
10     /// Missing a Required Option
11     MissingRequiredOption,
12     /// Bad Length
13     BadLength,
14 }
15 
16 impl ParameterProblemHeader {
17     /// Tries to convert the code [`u8`] value and pointer to a [`ParameterProblemHeader`] value.
18     ///
19     /// Returns [`None`] in case the code value is not known as a parameter problem code.
from_values(code_u8: u8, pointer: u8) -> Option<ParameterProblemHeader>20     pub fn from_values(code_u8: u8, pointer: u8) -> Option<ParameterProblemHeader> {
21         use super::{ParameterProblemHeader::*, *};
22         match code_u8 {
23             CODE_PARAMETER_PROBLEM_POINTER_INDICATES_ERROR => Some(PointerIndicatesError(pointer)),
24             CODE_PARAMETER_PROBLEM_MISSING_REQUIRED_OPTION => Some(MissingRequiredOption),
25             CODE_PARAMETER_PROBLEM_BAD_LENGTH => Some(BadLength),
26             _ => None,
27         }
28     }
29 }
30 
31 #[cfg(test)]
32 mod test {
33     use crate::icmpv4::{ParameterProblemHeader::*, *};
34     use alloc::format;
35     use proptest::prelude::*;
36 
37     proptest! {
38         #[test]
39         fn from_values(pointer in any::<u8>()) {
40             {
41                 let tests = [
42                     (CODE_PARAMETER_PROBLEM_POINTER_INDICATES_ERROR, PointerIndicatesError(pointer)),
43                     (CODE_PARAMETER_PROBLEM_MISSING_REQUIRED_OPTION, MissingRequiredOption),
44                     (CODE_PARAMETER_PROBLEM_BAD_LENGTH, BadLength),
45                 ];
46                 for t in tests {
47                     assert_eq!(Some(t.1), ParameterProblemHeader::from_values(t.0, pointer));
48                 }
49             }
50             for code_u8 in 3..=u8::MAX {
51                 assert_eq!(None, ParameterProblemHeader::from_values(code_u8, pointer));
52             }
53         }
54     }
55 
56     #[test]
clone_eq()57     fn clone_eq() {
58         let tests = [PointerIndicatesError(0), MissingRequiredOption, BadLength];
59         for t in tests {
60             assert_eq!(t.clone(), t);
61         }
62     }
63 
64     #[test]
debug()65     fn debug() {
66         let tests = [
67             ("PointerIndicatesError(0)", PointerIndicatesError(0)),
68             ("MissingRequiredOption", MissingRequiredOption),
69             ("BadLength", BadLength),
70         ];
71         for t in tests {
72             assert_eq!(t.0, format!("{:?}", t.1));
73         }
74     }
75 }
76