@@ -375,13 +375,35 @@ mod tests {
375375 assert_eq ! ( nla_align!( get_len( ) - 3 ) , usize :: MAX ) ;
376376 }
377377
378- // compile-time test, should not result in compiler complaints regarding
379- // lifetimes or returning values allegedly owned by this function
380- #[ allow( dead_code) ]
381- fn nla_buffer_outlives_value ( nlas : & [ u8 ] ) -> Option < & [ u8 ] > {
382- for nla in NlasIterator :: new ( nlas) {
383- return Some ( nla. unwrap ( ) . value ( ) )
384- }
385- None
378+ // compile-time test: it should be possible to pass &[u8] through
379+ // NlasIterator and return one of its output &[u8]s without facing
380+ // compiler error about lifetimes and returning borrows from something
381+ // that this funciton owns
382+ fn last_nla_from_buffer ( nlas : & [ u8 ] ) -> Option < Result < & [ u8 ] , DecodeError > > {
383+ NlasIterator :: new ( nlas) . last ( )
384+ . map ( |nla| nla. map ( |nla| nla. value ( ) ) )
385+ }
386+
387+ #[ test]
388+ fn test_nlas_iterator ( ) {
389+ // sample NFTA_LIST_ELEM from nftables, with nested nlas at the end
390+ static NESTED_NLAS : & [ u8 ] = & [
391+ 0x0c , 0x00 , 0x01 , 0x00 ,
392+ 0x63 , 0x6f , 0x75 , 0x6e , 0x74 , 0x65 , 0x72 , 0x00 ,
393+ 0x1c , 0x00 , 0x02 , 0x00 ,
394+ 0x0c , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 0x47 , 0xda , 0x7a , 0x03 ,
395+ 0x1b , 0x0c , 0x00 , 0x02 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x8e , 0x14 ,
396+ 0x56 , 0x39
397+ ] ;
398+ let mut iter = NlasIterator :: new ( NESTED_NLAS ) ;
399+ // DecodeError does not implement PartialEq, hence
400+ // unwrap() and is_none()
401+ assert_eq ! ( iter. next( ) . unwrap( ) . unwrap( ) . value( ) , & NESTED_NLAS [ 4 ..12 ] ) ;
402+ assert_eq ! ( iter. next( ) . unwrap( ) . unwrap( ) . value( ) , & NESTED_NLAS [ 16 ..] ) ;
403+ assert ! ( iter. next( ) . is_none( ) ) ;
404+
405+ // this sholud be an Err()
406+ let truncated = & NESTED_NLAS [ .. NESTED_NLAS . len ( ) -1 ] ;
407+ assert ! ( last_nla_from_buffer( truncated) . unwrap( ) . is_err( ) ) ;
386408 }
387409}
0 commit comments