1 //! Compare libc's CMSG(3) family of functions against the actual C macros, for 2 //! various inputs. 3 4 extern crate libc; 5 6 #[cfg(unix)] 7 mod t { 8 9 use libc::{self, c_uchar, c_uint, c_void, cmsghdr, msghdr}; 10 use std::mem; 11 12 extern "C" { 13 pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr; 14 pub fn cmsg_nxthdr(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr; 15 pub fn cmsg_space(length: c_uint) -> usize; 16 pub fn cmsg_len(length: c_uint) -> usize; 17 pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar; 18 } 19 20 #[test] 21 fn test_cmsg_data() { 22 for l in 0..128 { 23 let pcmsghdr = l as *const cmsghdr; 24 unsafe { 25 assert_eq!(libc::CMSG_DATA(pcmsghdr), cmsg_data(pcmsghdr)); 26 } 27 } 28 } 29 30 #[test] 31 fn test_cmsg_firsthdr() { 32 let mut mhdr: msghdr = unsafe { mem::zeroed() }; 33 mhdr.msg_control = 0xdeadbeef as *mut c_void; 34 let pmhdr = &mhdr as *const msghdr; 35 for l in 0..128 { 36 mhdr.msg_controllen = l; 37 unsafe { 38 assert_eq!(libc::CMSG_FIRSTHDR(pmhdr), cmsg_firsthdr(pmhdr)); 39 } 40 } 41 } 42 43 #[test] 44 fn test_cmsg_len() { 45 for l in 0..128 { 46 unsafe { 47 assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l)); 48 } 49 } 50 } 51 52 // Skip on sparc64 53 // https://github.com/rust-lang/libc/issues/1239 54 #[cfg(not(target_arch = "sparc64"))] 55 #[test] 56 fn test_cmsg_nxthdr() { 57 use std::ptr; 58 59 let mut buffer = [0u8; 256]; 60 let mut mhdr: msghdr = unsafe { mem::zeroed() }; 61 let pmhdr = &mhdr as *const msghdr; 62 for start_ofs in 0..64 { 63 let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr; 64 mhdr.msg_control = pcmsghdr as *mut c_void; 65 mhdr.msg_controllen = (160 - start_ofs) as _; 66 for cmsg_len in 0..64 { 67 for next_cmsg_len in 0..32 { 68 for i in buffer[start_ofs..].iter_mut() { 69 *i = 0; 70 } 71 unsafe { 72 (*pcmsghdr).cmsg_len = cmsg_len; 73 let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr); 74 let next = cmsg_nxthdr(pmhdr, pcmsghdr); 75 assert_eq!(libc_next, next); 76 77 if libc_next != ptr::null_mut() { 78 (*libc_next).cmsg_len = next_cmsg_len; 79 let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr); 80 let next = cmsg_nxthdr(pmhdr, pcmsghdr); 81 assert_eq!(libc_next, next); 82 } 83 } 84 } 85 } 86 } 87 } 88 89 #[test] 90 fn test_cmsg_space() { 91 unsafe { 92 for l in 0..128 { 93 assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l)); 94 } 95 } 96 } 97 } 98