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