|
| 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 { |
| 13 | + pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr; |
| 14 | + pub fn cmsg_nxthdr(mhdr: *const msghdr, |
| 15 | + 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 | + |
| 60 | + let mut buffer = [0u8; 256]; |
| 61 | + let mut mhdr: msghdr = unsafe{mem::zeroed()}; |
| 62 | + let pmhdr = &mhdr as *const msghdr; |
| 63 | + for start_ofs in 0..64 { |
| 64 | + let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr; |
| 65 | + mhdr.msg_control = pcmsghdr as *mut c_void; |
| 66 | + mhdr.msg_controllen = (160 - start_ofs) as _; |
| 67 | + for cmsg_len in 0..64 { |
| 68 | + for next_cmsg_len in 0..32 { |
| 69 | + for i in buffer[start_ofs..].iter_mut() { |
| 70 | + *i = 0; |
| 71 | + } |
| 72 | + unsafe { |
| 73 | + (*pcmsghdr).cmsg_len = cmsg_len; |
| 74 | + let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr); |
| 75 | + let next = cmsg_nxthdr(pmhdr, pcmsghdr); |
| 76 | + assert_eq!(libc_next, next); |
| 77 | + |
| 78 | + if libc_next != ptr::null_mut() { |
| 79 | + (*libc_next).cmsg_len = next_cmsg_len; |
| 80 | + let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr); |
| 81 | + let next = cmsg_nxthdr(pmhdr, pcmsghdr); |
| 82 | + assert_eq!(libc_next, next); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn test_cmsg_space() { |
| 92 | + unsafe { |
| 93 | + for l in 0..128 { |
| 94 | + assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l)); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +} |
0 commit comments