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