1 #[cfg(feature = "std")]
2 use std::str::FromStr;
3 
4 #[cfg(not(feature = "std"))]
5 use core::str::FromStr;
6 
7 use assert_matches::assert_matches;
8 
9 use crate::{MacAddr, MacAddr6, MacAddr8};
10 
11 #[test]
test_parse_v6_upper_case_canonical_format()12 fn test_parse_v6_upper_case_canonical_format() {
13     let addr = MacAddr6::from_str("12-34-56-78-9A-BC");
14 
15     assert!(addr.is_ok());
16     let addr = addr.unwrap();
17 
18     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC], addr.as_bytes());
19 }
20 
21 #[test]
test_parse_v6_lower_case_canonical_format()22 fn test_parse_v6_lower_case_canonical_format() {
23     let addr = MacAddr6::from_str("ab-cd-ef-ab-cd-ef");
24 
25     assert!(addr.is_ok());
26     let addr = addr.unwrap();
27 
28     assert_eq!(&[0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF], addr.as_bytes());
29 }
30 
31 #[test]
test_parse_v6_mixed_case_canonical_format()32 fn test_parse_v6_mixed_case_canonical_format() {
33     let addr = MacAddr6::from_str("AB-cd-Ef-Ab-cD-EF");
34 
35     assert!(addr.is_ok());
36     let addr = addr.unwrap();
37 
38     assert_eq!(&[0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF], addr.as_bytes());
39 }
40 
41 #[test]
test_parse_v6_colon_format()42 fn test_parse_v6_colon_format() {
43     let addr = MacAddr6::from_str("12:34:56:78:9A:BC");
44 
45     assert!(addr.is_ok());
46     let addr = addr.unwrap();
47 
48     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC], addr.as_bytes());
49 }
50 
51 #[test]
test_parse_v6_cisco_format()52 fn test_parse_v6_cisco_format() {
53     let addr = MacAddr6::from_str("1234.5678.9ABC");
54 
55     assert!(addr.is_ok());
56     let addr = addr.unwrap();
57 
58     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC], addr.as_bytes());
59 }
60 
61 #[test]
test_parse_v8_canonical_format()62 fn test_parse_v8_canonical_format() {
63     let addr = MacAddr8::from_str("12-34-56-78-9A-BC-DE-F0");
64 
65     assert!(addr.is_ok());
66     let addr = addr.unwrap();
67 
68     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
69 }
70 
71 #[test]
test_parse_v8_colon_format()72 fn test_parse_v8_colon_format() {
73     let addr = MacAddr8::from_str("12:34:56:78:9A:BC:DE:F0");
74 
75     assert!(addr.is_ok());
76     let addr = addr.unwrap();
77 
78     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
79 }
80 
81 #[test]
test_parse_canonical_format()82 fn test_parse_canonical_format() {
83     let addr = MacAddr::from_str("12-34-56-78-9A-BC-DE-F0");
84 
85     assert!(addr.is_ok());
86     let addr = addr.unwrap();
87     assert_matches!(addr, MacAddr::V8(..));
88     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
89 }
90 
91 #[test]
test_parse_colon_format()92 fn test_parse_colon_format() {
93     let addr = MacAddr::from_str("12:34:56:78:9A:BC:DE:F0");
94 
95     assert!(addr.is_ok());
96     let addr = addr.unwrap();
97     assert_matches!(addr, MacAddr::V8(..));
98     assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
99 }
100 
101 #[test]
test_parse_v6_empty()102 fn test_parse_v6_empty() {
103     let addr = MacAddr6::from_str("");
104 
105     assert!(addr.is_err());
106 }
107 
108 #[test]
test_parse_v8_empty()109 fn test_parse_v8_empty() {
110     let addr = MacAddr8::from_str("");
111 
112     assert!(addr.is_err());
113 }
114 
115 #[test]
test_parse_empty()116 fn test_parse_empty() {
117     let addr = MacAddr::from_str("");
118 
119     assert!(addr.is_err());
120 }
121 
122 #[test]
test_parse_v6_partial_start()123 fn test_parse_v6_partial_start() {
124     let addr = MacAddr6::from_str("b-cd-ef-12-34-56");
125 
126     assert!(addr.is_err());
127 }
128 
129 #[test]
test_parse_v8_partial_start()130 fn test_parse_v8_partial_start() {
131     let addr = MacAddr8::from_str("b-cd-ef-12-34-56-78-9A");
132 
133     assert!(addr.is_err());
134 }
135 
136 #[test]
test_parse_v6_partial_end()137 fn test_parse_v6_partial_end() {
138     let addr = MacAddr6::from_str("ab-cd-ef-12-34-5");
139 
140     assert!(addr.is_err());
141 }
142 
143 #[test]
test_parse_v8_partial_end()144 fn test_parse_v8_partial_end() {
145     let addr = MacAddr8::from_str("ab-cd-ef-12-34-56-78-9");
146 
147     assert!(addr.is_err());
148 }
149 
150 #[test]
test_parse_v6_invalid_char()151 fn test_parse_v6_invalid_char() {
152     let addr = MacAddr6::from_str("ab-Qd-ef-12-34-56");
153 
154     assert!(addr.is_err());
155 }
156 
157 #[test]
test_parse_v8_invalid_char()158 fn test_parse_v8_invalid_char() {
159     let addr = MacAddr8::from_str("ab-Qd-ef-12-34-56-78-9A");
160 
161     assert!(addr.is_err());
162 }
163 
164 #[test]
test_parse_v6_different_delimiters()165 fn test_parse_v6_different_delimiters() {
166     let addr = MacAddr6::from_str("ab-cd:ef-12-34-56");
167 
168     assert!(addr.is_err());
169 }
170 
171 #[test]
test_parse_v8_different_delimiters()172 fn test_parse_v8_different_delimiters() {
173     let addr = MacAddr8::from_str("ab-cd-ef-12-34-56-78:9A");
174 
175     assert!(addr.is_err());
176 }
177