|
| 1 | +use netlink_packet_utils::{nla::DefaultNla, Emitable, Parseable}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + link::{ |
| 5 | + link_flag::LinkFlags, LinkAttribute, LinkHeader, LinkLayerType, |
| 6 | + LinkMessage, LinkMessageBuffer, LinkXdp, Map, State, Stats, Stats64, |
| 7 | + XdpAttached, |
| 8 | + }, |
| 9 | + AddressFamily, |
| 10 | +}; |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn test_empty_af_spec() { |
| 14 | + let raw = vec![ |
| 15 | + 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, |
| 16 | + 0xff, 0xff, 0xff, 0xff, 0x08, 0x00, 0x0d, 0x00, 0xe8, 0x03, 0x00, 0x00, |
| 17 | + 0x05, 0x00, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00, |
| 18 | + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0xdc, 0x05, 0x00, 0x00, |
| 19 | + 0x08, 0x00, 0x32, 0x00, 0x44, 0x00, 0x00, 0x00, 0x08, 0x00, 0x33, 0x00, |
| 20 | + 0xdc, 0x05, 0x00, 0x00, 0x08, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 21 | + 0x08, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x3d, 0x00, |
| 22 | + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1f, 0x00, 0x04, 0x00, 0x00, 0x00, |
| 23 | + 0x08, 0x00, 0x28, 0x00, 0xff, 0xff, 0x00, 0x00, 0x08, 0x00, 0x29, 0x00, |
| 24 | + 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 25 | + 0x08, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x40, 0x00, |
| 26 | + 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 27 | + 0x08, 0x00, 0x3c, 0x00, 0xff, 0xff, 0x00, 0x00, 0x08, 0x00, 0x20, 0x00, |
| 28 | + 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 29 | + 0x08, 0x00, 0x23, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2f, 0x00, |
| 30 | + 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, 0x30, 0x00, 0x03, 0x00, 0x00, 0x00, |
| 31 | + 0x05, 0x00, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00, |
| 32 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xcc, 0x00, 0x17, 0x00, |
| 33 | + 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, |
| 34 | + 0x00, 0x00, 0x00, 0x00, 0x37, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 35 | + 0x4a, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 36 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 37 | + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, |
| 38 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 39 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 40 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 41 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 42 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 43 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 44 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 46 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 47 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 48 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x07, 0x00, |
| 50 | + 0x26, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x37, 0x10, 0x00, 0x00, |
| 51 | + 0x4a, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 52 | + 0x03, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 53 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 54 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 55 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 56 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 57 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 58 | + 0x0c, 0x00, 0x2b, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 59 | + 0x09, 0x00, 0x06, 0x00, 0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00, |
| 60 | + 0x04, 0x00, 0x1a, 0x00, 0x24, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 61 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 62 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 63 | + 0x00, 0x00, 0x00, 0x00, |
| 64 | + ]; |
| 65 | + |
| 66 | + let expected = LinkMessage { |
| 67 | + header: LinkHeader { |
| 68 | + interface_family: AddressFamily::Unspec, |
| 69 | + index: 4, |
| 70 | + link_layer_type: LinkLayerType::Ether, |
| 71 | + flags: LinkFlags::Broadcast | LinkFlags::Multicast, |
| 72 | + change_mask: LinkFlags::Up |
| 73 | + | LinkFlags::Broadcast |
| 74 | + | LinkFlags::Debug |
| 75 | + | LinkFlags::Loopback |
| 76 | + | LinkFlags::Pointopoint |
| 77 | + | LinkFlags::Notrailers |
| 78 | + | LinkFlags::Running |
| 79 | + | LinkFlags::Noarp |
| 80 | + | LinkFlags::Promisc |
| 81 | + | LinkFlags::Allmulti |
| 82 | + | LinkFlags::Controller |
| 83 | + | LinkFlags::Port |
| 84 | + | LinkFlags::Multicast |
| 85 | + | LinkFlags::Portsel |
| 86 | + | LinkFlags::Automedia |
| 87 | + | LinkFlags::Dynamic |
| 88 | + | LinkFlags::LowerUp |
| 89 | + | LinkFlags::Dormant |
| 90 | + | LinkFlags::Echo |
| 91 | + | LinkFlags::from_bits_retain(0xfff80000), |
| 92 | + }, |
| 93 | + attributes: vec![ |
| 94 | + LinkAttribute::TxQueueLen(1000), |
| 95 | + LinkAttribute::OperState(State::Down), |
| 96 | + LinkAttribute::Mode(0), |
| 97 | + LinkAttribute::Mtu(1500), |
| 98 | + LinkAttribute::MinMtu(68), |
| 99 | + LinkAttribute::MaxMtu(1500), |
| 100 | + LinkAttribute::Group(0), |
| 101 | + LinkAttribute::Promiscuity(0), |
| 102 | + LinkAttribute::Other(DefaultNla::new(61, vec![0, 0, 0, 0])), |
| 103 | + LinkAttribute::NumTxQueues(4), |
| 104 | + LinkAttribute::GsoMaxSegs(65535), |
| 105 | + LinkAttribute::GsoMaxSize(65536), |
| 106 | + LinkAttribute::Other(DefaultNla::new(58, vec![0, 0, 1, 0])), |
| 107 | + LinkAttribute::Other(DefaultNla::new(63, vec![0, 0, 1, 0])), |
| 108 | + LinkAttribute::Other(DefaultNla::new(64, vec![0, 0, 1, 0])), |
| 109 | + LinkAttribute::Other(DefaultNla::new(59, vec![0, 0, 1, 0])), |
| 110 | + LinkAttribute::Other(DefaultNla::new(60, vec![255, 255, 0, 0])), |
| 111 | + LinkAttribute::NumRxQueues(4), |
| 112 | + LinkAttribute::Carrier(0), |
| 113 | + LinkAttribute::CarrierChanges(6), |
| 114 | + LinkAttribute::CarrierUpCount(3), |
| 115 | + LinkAttribute::CarrierDownCount(3), |
| 116 | + LinkAttribute::ProtoDown(0), |
| 117 | + LinkAttribute::Broadcast(vec![255, 255, 255, 255, 255, 255]), |
| 118 | + LinkAttribute::Stats64(Stats64 { |
| 119 | + rx_packets: 38, |
| 120 | + tx_packets: 32, |
| 121 | + rx_bytes: 4151, |
| 122 | + tx_bytes: 3914, |
| 123 | + rx_errors: 0, |
| 124 | + tx_errors: 0, |
| 125 | + rx_dropped: 3, |
| 126 | + tx_dropped: 11, |
| 127 | + multicast: 0, |
| 128 | + collisions: 0, |
| 129 | + rx_length_errors: 0, |
| 130 | + rx_over_errors: 0, |
| 131 | + rx_crc_errors: 0, |
| 132 | + rx_frame_errors: 0, |
| 133 | + rx_fifo_errors: 0, |
| 134 | + rx_missed_errors: 0, |
| 135 | + tx_aborted_errors: 0, |
| 136 | + tx_carrier_errors: 0, |
| 137 | + tx_fifo_errors: 0, |
| 138 | + tx_heartbeat_errors: 0, |
| 139 | + tx_window_errors: 0, |
| 140 | + rx_compressed: 0, |
| 141 | + tx_compressed: 0, |
| 142 | + rx_nohandler: 0, |
| 143 | + rx_otherhost_dropped: 0, |
| 144 | + }), |
| 145 | + LinkAttribute::Stats(Stats { |
| 146 | + rx_packets: 38, |
| 147 | + tx_packets: 32, |
| 148 | + rx_bytes: 4151, |
| 149 | + tx_bytes: 3914, |
| 150 | + rx_errors: 0, |
| 151 | + tx_errors: 0, |
| 152 | + rx_dropped: 3, |
| 153 | + tx_dropped: 11, |
| 154 | + multicast: 0, |
| 155 | + collisions: 0, |
| 156 | + rx_length_errors: 0, |
| 157 | + rx_over_errors: 0, |
| 158 | + rx_crc_errors: 0, |
| 159 | + rx_frame_errors: 0, |
| 160 | + rx_fifo_errors: 0, |
| 161 | + rx_missed_errors: 0, |
| 162 | + tx_aborted_errors: 0, |
| 163 | + tx_carrier_errors: 0, |
| 164 | + tx_fifo_errors: 0, |
| 165 | + tx_heartbeat_errors: 0, |
| 166 | + tx_window_errors: 0, |
| 167 | + rx_compressed: 0, |
| 168 | + tx_compressed: 0, |
| 169 | + rx_nohandler: 0, |
| 170 | + }), |
| 171 | + LinkAttribute::Xdp(vec![LinkXdp::Attached(XdpAttached::None)]), |
| 172 | + LinkAttribute::Qdisc("noop".into()), |
| 173 | + LinkAttribute::AfSpecUnspec(vec![]), // empty |
| 174 | + LinkAttribute::Map(Map { |
| 175 | + memory_start: 0, |
| 176 | + memory_end: 0, |
| 177 | + base_address: 0, |
| 178 | + irq: 0, |
| 179 | + dma: 0, |
| 180 | + port: 0, |
| 181 | + }), |
| 182 | + ], |
| 183 | + }; |
| 184 | + |
| 185 | + assert_eq!( |
| 186 | + expected, |
| 187 | + LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap() |
| 188 | + ); |
| 189 | + |
| 190 | + let mut buf = vec![0; expected.buffer_len()]; |
| 191 | + |
| 192 | + expected.emit(&mut buf); |
| 193 | + |
| 194 | + assert_eq!(buf, raw); |
| 195 | +} |
0 commit comments