xref: /aosp_15_r20/tools/netsim/rust/packets/src/llc.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! LLC
16 
17 #![allow(clippy::all)]
18 #![allow(missing_docs)]
19 #![allow(unused)]
20 
21 include!(concat!(env!("OUT_DIR"), "/llc_packets.rs"));
22 
23 impl LlcSnapHeader {
24     // Length of LLC/SNAP headers on data frames
25     pub const LEN: usize = 8;
26 }
27 
28 #[cfg(test)]
29 mod tests {
30     use super::*;
31 
32     #[test]
test_llc_snap_header_len()33     fn test_llc_snap_header_len() {
34         let payload = vec![
35             LlcSap::Snap as u8,
36             LlcSap::Snap as u8,
37             LlcCtrl::UiCmd as u8,
38             // OUI
39             0x00,
40             0x00,
41             0x00,
42             // EtherType
43             0x08,
44             0x00,
45         ];
46 
47         let hdr = LlcSnapHeader::decode_full(&payload).unwrap();
48         assert_eq!(hdr.encoded_len(), LlcSnapHeader::LEN);
49     }
50 
51     #[test]
test_llc_snap_header_valid()52     fn test_llc_snap_header_valid() {
53         let payload = vec![
54             LlcSap::Snap as u8,
55             LlcSap::Snap as u8,
56             LlcCtrl::UiCmd as u8,
57             // OUI
58             0x00,
59             0x00,
60             0x00,
61             // EtherType
62             0x08,
63             0x00,
64         ];
65         let hdr = LlcSnapHeader::decode_full(&payload).unwrap();
66 
67         assert_eq!(hdr.dsap, LlcSap::Snap);
68         assert_eq!(hdr.ssap, LlcSap::Snap);
69         assert_eq!(hdr.ctrl, LlcCtrl::UiCmd);
70         assert_eq!(hdr.ethertype, EtherType::IPv4);
71     }
72 
73     #[test]
test_llc_snap_header_invalid_llc()74     fn test_llc_snap_header_invalid_llc() {
75         #[rustfmt::skip]
76         let payload = vec![
77             // LLC
78             0 as u8, 0 as u8, 0 as u8,
79             // OUI
80             0x00, 0x00, 0x00,
81             // EtherType
82             0x00, 0x00,
83         ];
84         let hdr_result = LlcSnapHeader::decode_full(&payload);
85         assert!(hdr_result.is_err());
86     }
87 
88     #[test]
test_llc_snap_header_invalid_ethertype()89     fn test_llc_snap_header_invalid_ethertype() {
90         let payload = vec![
91             LlcSap::Snap as u8,
92             LlcSap::Snap as u8,
93             LlcCtrl::UiCmd as u8,
94             // OUI
95             0x00,
96             0x00,
97             0x00,
98             // EtherType
99             0x00,
100             0x00,
101         ];
102         let hdr_result = LlcSnapHeader::decode_full(&payload);
103         assert!(hdr_result.is_err());
104     }
105 }
106