forked from rust-lang/libc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an integration test for the cmsg(3) functions.
Since these are defined in C as macros, they must be reimplemented in libc as Rust functions. They're hard to get exactly right, and they vary from platform to platform. The test builds custom C code that uses the real macros, and compares its output to the Rust versions' output for various inputs. Skip the CMSG_NXTHDR test on sparc64 linux because it hits a Bus Error. Issue rust-lang#1239 Skip the entire cmsg test program on s390x because it dumps core seemingly before the kernel finishes booting. Issue rust-lang#1240
- Loading branch information
Showing
9 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <sys/param.h> | ||
#include <sys/socket.h> | ||
|
||
// Since the cmsg(3) macros are macros instead of functions, they aren't | ||
// available to FFI. libc must reimplement them, which is error-prone. This | ||
// file provides FFI access to the actual macros so they can be tested against | ||
// the Rust reimplementations. | ||
|
||
struct cmsghdr *cmsg_firsthdr(struct msghdr *msgh) { | ||
return CMSG_FIRSTHDR(msgh); | ||
} | ||
|
||
struct cmsghdr *cmsg_nxthdr(struct msghdr *msgh, struct cmsghdr *cmsg) { | ||
return CMSG_NXTHDR(msgh, cmsg); | ||
} | ||
|
||
size_t cmsg_space(size_t length) { | ||
return CMSG_SPACE(length); | ||
} | ||
|
||
size_t cmsg_len(size_t length) { | ||
return CMSG_LEN(length); | ||
} | ||
|
||
unsigned char *cmsg_data(struct cmsghdr *cmsg) { | ||
return CMSG_DATA(cmsg); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//! Compare libc's CMSG(3) family of functions against the actual C macros, for | ||
//! various inputs. | ||
|
||
extern crate libc; | ||
|
||
#[cfg(unix)] | ||
mod t { | ||
|
||
use libc::{self, c_uchar, c_uint, c_void, cmsghdr, msghdr}; | ||
use std::mem; | ||
|
||
extern { | ||
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr; | ||
pub fn cmsg_nxthdr(mhdr: *const msghdr, | ||
cmsg: *const cmsghdr) -> *mut cmsghdr; | ||
pub fn cmsg_space(length: c_uint) -> usize; | ||
pub fn cmsg_len(length: c_uint) -> usize; | ||
pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar; | ||
} | ||
|
||
#[test] | ||
fn test_cmsg_data() { | ||
for l in 0..128 { | ||
let pcmsghdr = l as *const cmsghdr; | ||
unsafe { | ||
assert_eq!(libc::CMSG_DATA(pcmsghdr), cmsg_data(pcmsghdr)); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_cmsg_firsthdr() { | ||
let mut mhdr: msghdr = unsafe{mem::zeroed()}; | ||
mhdr.msg_control = 0xdeadbeef as *mut c_void; | ||
let pmhdr = &mhdr as *const msghdr; | ||
for l in 0..128 { | ||
mhdr.msg_controllen = l; | ||
unsafe { | ||
assert_eq!(libc::CMSG_FIRSTHDR(pmhdr), cmsg_firsthdr(pmhdr)); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_cmsg_len() { | ||
for l in 0..128 { | ||
unsafe { | ||
assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l)); | ||
} | ||
} | ||
} | ||
|
||
// Skip on sparc64 | ||
// https://github.com/rust-lang/libc/issues/1239 | ||
#[cfg(not(target_arch = "sparc64"))] | ||
#[test] | ||
fn test_cmsg_nxthdr() { | ||
use std::ptr; | ||
|
||
let mut buffer = [0u8; 256]; | ||
let mut mhdr: msghdr = unsafe{mem::zeroed()}; | ||
let pmhdr = &mhdr as *const msghdr; | ||
for start_ofs in 0..64 { | ||
let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr; | ||
mhdr.msg_control = pcmsghdr as *mut c_void; | ||
mhdr.msg_controllen = (160 - start_ofs) as _; | ||
for cmsg_len in 0..64 { | ||
for next_cmsg_len in 0..32 { | ||
for i in buffer[start_ofs..].iter_mut() { | ||
*i = 0; | ||
} | ||
unsafe { | ||
(*pcmsghdr).cmsg_len = cmsg_len; | ||
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr); | ||
let next = cmsg_nxthdr(pmhdr, pcmsghdr); | ||
assert_eq!(libc_next, next); | ||
|
||
if libc_next != ptr::null_mut() { | ||
(*libc_next).cmsg_len = next_cmsg_len; | ||
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr); | ||
let next = cmsg_nxthdr(pmhdr, pcmsghdr); | ||
assert_eq!(libc_next, next); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_cmsg_space() { | ||
unsafe { | ||
for l in 0..128 { | ||
assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l)); | ||
} | ||
} | ||
} | ||
|
||
} |