Skip to content

Commit 06938ad

Browse files
committed
Add test for SO_EE_OFFENDER
Modelled after the cmsg tests, this wraps the C macro into a function, and then compares the results to the Rust implementation in libc.
1 parent a9a6ef1 commit 06938ad

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

libc-test/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ harness = false
5252
name = "cmsg"
5353
path = "test/cmsg.rs"
5454
harness = true
55+
56+
[[test]]
57+
name = "errqueue"
58+
path = "test/errqueue.rs"
59+
harness = true

libc-test/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ fn do_cc() {
1010
if cfg!(unix) && !target.contains("wasi") {
1111
cc::Build::new().file("src/cmsg.c").compile("cmsg");
1212
}
13+
if target.contains("android") || target.contains("linux") {
14+
cc::Build::new().file("src/errqueue.c").compile("errqueue");
15+
}
1316
}
1417

1518
fn do_ctest() {

libc-test/src/errqueue.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <time.h>
2+
#include <linux/errqueue.h>
3+
4+
// SO_EE_OFFENDER is defined as a macro in linux/errqueue.h. This file wraps
5+
// that macro in a function so we can test the reimplementation in this package
6+
// is equivalent.
7+
8+
struct sockaddr *so_ee_offender(struct sock_extended_err *ee) {
9+
return SO_EE_OFFENDER(ee);
10+
}

libc-test/test/errqueue.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Compare libc's SO_EE_OFFENDER function against the actual C macro
2+
3+
extern crate libc;
4+
5+
#[cfg(any(target_os = "linux", target_os = "android"))]
6+
mod t {
7+
use libc::{self, sock_extended_err, sockaddr};
8+
9+
extern "C" {
10+
pub fn so_ee_offender(ee: *const sock_extended_err) -> *mut sockaddr;
11+
}
12+
13+
#[test]
14+
fn test_cmsg_data() {
15+
for l in 0..128 {
16+
let ee = l as *const sock_extended_err;
17+
unsafe {
18+
assert_eq!(libc::SO_EE_OFFENDER(ee), so_ee_offender(ee));
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)