From 0cadc915eaf431b381c77533a3a6853997cd3a8e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 13:55:58 +0100 Subject: [PATCH 1/6] Restructure tests to avoid future conflicts --- tests/unit/fd_io.c | 14 --- tests/unit/isatty.c | 13 ++ tests/unit/lseek_ftruncate.c | 22 ++++ tests/unit/out/refcount/fd_io.rs | 124 ------------------- tests/unit/out/refcount/isatty.rs | 49 ++++++++ tests/unit/out/refcount/lseek_ftruncate.rs | 110 ++++++++++++++++ tests/unit/out/refcount/pipe_io.rs | 109 ++++++++++++++++ tests/unit/out/refcount/socket_listen.rs | 44 +++++++ tests/unit/out/refcount/socket_open_close.rs | 43 +++++++ tests/unit/out/unsafe/fd_io.rs | 46 ------- tests/unit/out/unsafe/isatty.rs | 29 +++++ tests/unit/out/unsafe/lseek_ftruncate.rs | 65 ++++++++++ tests/unit/out/unsafe/pipe_io.rs | 59 +++++++++ tests/unit/out/unsafe/socket_listen.rs | 20 +++ tests/unit/out/unsafe/socket_open_close.rs | 19 +++ tests/unit/pipe_io.c | 17 +++ tests/unit/socket_listen.c | 11 ++ tests/unit/socket_open_close.c | 10 ++ 18 files changed, 620 insertions(+), 184 deletions(-) create mode 100644 tests/unit/isatty.c create mode 100644 tests/unit/lseek_ftruncate.c create mode 100644 tests/unit/out/refcount/isatty.rs create mode 100644 tests/unit/out/refcount/lseek_ftruncate.rs create mode 100644 tests/unit/out/refcount/pipe_io.rs create mode 100644 tests/unit/out/refcount/socket_listen.rs create mode 100644 tests/unit/out/refcount/socket_open_close.rs create mode 100644 tests/unit/out/unsafe/isatty.rs create mode 100644 tests/unit/out/unsafe/lseek_ftruncate.rs create mode 100644 tests/unit/out/unsafe/pipe_io.rs create mode 100644 tests/unit/out/unsafe/socket_listen.rs create mode 100644 tests/unit/out/unsafe/socket_open_close.rs create mode 100644 tests/unit/pipe_io.c create mode 100644 tests/unit/socket_listen.c create mode 100644 tests/unit/socket_open_close.c diff --git a/tests/unit/fd_io.c b/tests/unit/fd_io.c index 7dcefe46..4f1fbeca 100644 --- a/tests/unit/fd_io.c +++ b/tests/unit/fd_io.c @@ -1,7 +1,6 @@ #include #include #include -#include #include int main(void) { @@ -19,18 +18,5 @@ int main(void) { assert(read(fd, buf, sizeof(buf)) == 0); assert(close(fd) == 0); assert(unlink(path) == 0); - int fds[2]; - assert(pipe(fds) == 0); - assert(write(fds[1], "ab", 2) == 2); - char buf2[4]; - memset(buf2, 0, sizeof(buf2)); - assert(read(fds[0], buf2, sizeof(buf2)) == 2); - assert(strcmp(buf2, "ab") == 0); - assert(close(fds[1]) == 0); - assert(read(fds[0], buf2, sizeof(buf2)) == 0); - assert(close(fds[0]) == 0); - int s = socket(AF_INET, SOCK_STREAM, 0); - assert(s >= 0); - assert(close(s) == 0); return 0; } diff --git a/tests/unit/isatty.c b/tests/unit/isatty.c new file mode 100644 index 00000000..e849816a --- /dev/null +++ b/tests/unit/isatty.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(void) { + const char *path = "/tmp/cpp2rust_isatty_test.tmp"; + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + assert(fd >= 0); + assert(isatty(fd) == 0); + assert(close(fd) == 0); + assert(unlink(path) == 0); + return 0; +} diff --git a/tests/unit/lseek_ftruncate.c b/tests/unit/lseek_ftruncate.c new file mode 100644 index 00000000..bd6a988a --- /dev/null +++ b/tests/unit/lseek_ftruncate.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main(void) { + const char *path = "/tmp/cpp2rust_lseek_ftruncate_test.tmp"; + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + assert(fd >= 0); + assert(write(fd, "hello world", 11) == 11); + assert(lseek(fd, 0, SEEK_END) == 11); + assert(lseek(fd, 6, SEEK_SET) == 6); + char buf[16]; + memset(buf, 0, sizeof(buf)); + assert(read(fd, buf, sizeof(buf)) == 5); + assert(strcmp(buf, "world") == 0); + assert(ftruncate(fd, 5) == 0); + assert(lseek(fd, 0, SEEK_END) == 5); + assert(close(fd) == 0); + assert(unlink(path) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index 1bc80d3a..a8be4c06 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -138,129 +138,5 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"ab") - .to_any() - .reinterpret_cast::() - .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - let buf2: Value> = Rc::new(RefCell::new( - (0..4).map(|_| ::default()).collect::>(), - )); - { - ((buf2.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - ((buf2.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf2.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf2.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf2.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0_isize) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - let s: Value = Rc::new(RefCell::new({ - let __family = match libc::AF_INET { - ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, - ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, - ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, - __d => panic!("socket: unsupported domain {__d}"), - }; - let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); - let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { - ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, - ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, - __t => panic!("socket: unsupported type {__t}"), - }; - let __proto = match 0 { - 0 => None, - ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), - ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), - __p => panic!("socket: unsupported protocol {__p}"), - }; - match nix::sys::socket::socket(__family, __ty, __flags, __proto) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*s.borrow()) >= 0) as i32) != 0)); - assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); return 0; } diff --git a/tests/unit/out/refcount/isatty.rs b/tests/unit/out/refcount/isatty.rs new file mode 100644 index 00000000..e9828c72 --- /dev/null +++ b/tests/unit/out/refcount/isatty.rs @@ -0,0 +1,49 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_isatty_test.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!((((libc::isatty((*fd.borrow())) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs new file mode 100644 index 00000000..7883bf55 --- /dev/null +++ b/tests/unit/out/refcount/lseek_ftruncate.rs @@ -0,0 +1,110 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_lseek_ftruncate_test.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello world") + .to_any() + .reinterpret_cast::() + .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!((((libc::lseek((*fd.borrow()), 0_i64, 2) == 11_i64) as i32) != 0)); + assert!((((libc::lseek((*fd.borrow()), 6_i64, 0) == 6_i64) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new( + (0..16).map(|_| ::default()).collect::>(), + )); + { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 5_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"world").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((libc::ftruncate((*fd.borrow()), 5_i64) == 0) as i32) != 0)); + assert!((((libc::lseek((*fd.borrow()), 0_i64, 2) == 5_i64) as i32) != 0)); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/out/refcount/pipe_io.rs b/tests/unit/out/refcount/pipe_io.rs new file mode 100644 index 00000000..5f51cf01 --- /dev/null +++ b/tests/unit/out/refcount/pipe_io.rs @@ -0,0 +1,109 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"ab") + .to_any() + .reinterpret_cast::() + .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0_isize) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/refcount/socket_listen.rs b/tests/unit/out/refcount/socket_listen.rs new file mode 100644 index 00000000..05ca09fe --- /dev/null +++ b/tests/unit/out/refcount/socket_listen.rs @@ -0,0 +1,44 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + assert!((((libc::listen((*s.borrow()), 5) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/refcount/socket_open_close.rs b/tests/unit/out/refcount/socket_open_close.rs new file mode 100644 index 00000000..7cb8a701 --- /dev/null +++ b/tests/unit/out/refcount/socket_open_close.rs @@ -0,0 +1,43 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 92306d37..29c8057d 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -66,51 +66,5 @@ unsafe fn main_0() -> i32 { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 2_usize - )) == (2_isize)) as i32) - != 0) - ); - let mut buf2: [libc::c_char; 4] = [(0 as libc::c_char); 4]; - { - let byte_0 = (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fds[(0) as usize], - (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (2_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf2.as_mut_ptr()).cast_const(), - (c"ab".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); - assert!( - ((((libc::read( - fds[(0) as usize], - (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (0_isize)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); return 0; } diff --git a/tests/unit/out/unsafe/isatty.rs b/tests/unit/out/unsafe/isatty.rs new file mode 100644 index 00000000..3856611f --- /dev/null +++ b/tests/unit/out/unsafe/isatty.rs @@ -0,0 +1,29 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_isatty_test.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!(((((libc::isatty(fd)) == (0)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/lseek_ftruncate.rs b/tests/unit/out/unsafe/lseek_ftruncate.rs new file mode 100644 index 00000000..579c1b54 --- /dev/null +++ b/tests/unit/out/unsafe/lseek_ftruncate.rs @@ -0,0 +1,65 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut path: *const libc::c_char = (c"/tmp/cpp2rust_lseek_ftruncate_test.tmp" + .as_ptr() + .cast_mut()) + .cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 11_usize + )) == (11_isize)) as i32) + != 0) + ); + assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); + let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16]; + { + let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fd, + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 16]>() + )) == (5_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"world".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::ftruncate(fd, 5_i64)) == (0)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/pipe_io.rs b/tests/unit/out/unsafe/pipe_io.rs new file mode 100644 index 00000000..2455e40d --- /dev/null +++ b/tests/unit/out/unsafe/pipe_io.rs @@ -0,0 +1,59 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 2_usize + )) == (2_isize)) as i32) + != 0) + ); + let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4]; + { + let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fds[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (2_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"ab".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); + assert!( + ((((libc::read( + fds[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (0_isize)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/socket_listen.rs b/tests/unit/out/unsafe/socket_listen.rs new file mode 100644 index 00000000..ca55cbf7 --- /dev/null +++ b/tests/unit/out/unsafe/socket_listen.rs @@ -0,0 +1,20 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/socket_open_close.rs b/tests/unit/out/unsafe/socket_open_close.rs new file mode 100644 index 00000000..6f8ccf39 --- /dev/null +++ b/tests/unit/out/unsafe/socket_open_close.rs @@ -0,0 +1,19 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/pipe_io.c b/tests/unit/pipe_io.c new file mode 100644 index 00000000..4d5cfa99 --- /dev/null +++ b/tests/unit/pipe_io.c @@ -0,0 +1,17 @@ +#include +#include +#include + +int main(void) { + int fds[2]; + assert(pipe(fds) == 0); + assert(write(fds[1], "ab", 2) == 2); + char buf[4]; + memset(buf, 0, sizeof(buf)); + assert(read(fds[0], buf, sizeof(buf)) == 2); + assert(strcmp(buf, "ab") == 0); + assert(close(fds[1]) == 0); + assert(read(fds[0], buf, sizeof(buf)) == 0); + assert(close(fds[0]) == 0); + return 0; +} diff --git a/tests/unit/socket_listen.c b/tests/unit/socket_listen.c new file mode 100644 index 00000000..f90d1895 --- /dev/null +++ b/tests/unit/socket_listen.c @@ -0,0 +1,11 @@ +#include +#include +#include + +int main(void) { + int s = socket(AF_INET, SOCK_STREAM, 0); + assert(s >= 0); + assert(listen(s, 5) == 0); + assert(close(s) == 0); + return 0; +} diff --git a/tests/unit/socket_open_close.c b/tests/unit/socket_open_close.c new file mode 100644 index 00000000..e9a8ddae --- /dev/null +++ b/tests/unit/socket_open_close.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main(void) { + int s = socket(AF_INET, SOCK_STREAM, 0); + assert(s >= 0); + assert(close(s) == 0); + return 0; +} From 23a43adb9ac33a764d6d55c02ebc304d61ec524b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 20 Jul 2026 17:55:21 +0100 Subject: [PATCH 2/6] Add fstat and termios safe rules --- libcc2rs/src/libc_shims/termios.rs | 62 ++++++++++++++++++++++++++++++ rules/stat/tgt_refcount.rs | 13 +++++++ rules/termios/tgt_refcount.rs | 32 +++++++++++++++ 3 files changed, 107 insertions(+) diff --git a/libcc2rs/src/libc_shims/termios.rs b/libcc2rs/src/libc_shims/termios.rs index 66e3d9db..5d96f474 100644 --- a/libcc2rs/src/libc_shims/termios.rs +++ b/libcc2rs/src/libc_shims/termios.rs @@ -48,6 +48,68 @@ impl Clone for Termios { impl ByteRepr for Termios {} +impl Termios { + #[allow(clippy::unnecessary_cast)] + pub fn from_libc(t: &::libc::termios) -> Self { + let s = Self::default(); + *s.c_iflag.borrow_mut() = t.c_iflag as u32; + *s.c_oflag.borrow_mut() = t.c_oflag as u32; + *s.c_cflag.borrow_mut() = t.c_cflag as u32; + *s.c_lflag.borrow_mut() = t.c_lflag as u32; + #[cfg(target_os = "linux")] + { + *s.c_line.borrow_mut() = t.c_line; + } + { + let mut cc = s.c_cc.borrow_mut(); + let n = t.c_cc.len().min(cc.len()); + cc[..n].copy_from_slice(&t.c_cc[..n]); + } + *s.c_ispeed.borrow_mut() = t.c_ispeed as u32; + *s.c_ospeed.borrow_mut() = t.c_ospeed as u32; + s + } + + #[cfg(target_os = "linux")] + pub fn to_libc(&self) -> ::libc::termios { + ::libc::termios { + c_iflag: *self.c_iflag.borrow(), + c_oflag: *self.c_oflag.borrow(), + c_cflag: *self.c_cflag.borrow(), + c_lflag: *self.c_lflag.borrow(), + c_line: *self.c_line.borrow(), + c_cc: { + let mut cc = [0u8; 32]; + let src = self.c_cc.borrow(); + let n = src.len().min(cc.len()); + cc[..n].copy_from_slice(&src[..n]); + cc + }, + c_ispeed: *self.c_ispeed.borrow(), + c_ospeed: *self.c_ospeed.borrow(), + } + } + + #[cfg(target_os = "macos")] + pub fn to_libc(&self) -> ::libc::termios { + ::libc::termios { + c_iflag: *self.c_iflag.borrow() as u64, + c_oflag: *self.c_oflag.borrow() as u64, + c_cflag: *self.c_cflag.borrow() as u64, + c_lflag: *self.c_lflag.borrow() as u64, + c_cc: { + let mut cc = [0u8; 20]; + let src = self.c_cc.borrow(); + let n = src.len().min(cc.len()); + cc[..n].copy_from_slice(&src[..n]); + cc + }, + c_ispeed: *self.c_ispeed.borrow() as u64, + c_ospeed: *self.c_ospeed.borrow() as u64, + } + } +} + #[derive(Default)] pub struct Winsize { pub ws_row: Value, diff --git a/rules/stat/tgt_refcount.rs b/rules/stat/tgt_refcount.rs index bf577f5c..636e87c0 100644 --- a/rules/stat/tgt_refcount.rs +++ b/rules/stat/tgt_refcount.rs @@ -20,6 +20,19 @@ fn f1(a0: Ptr, a1: Ptr) -> i32 { } } +fn f2(a0: i32, a1: Ptr) -> i32 { + match FdRegistry::with_fd(a0, |__fd| nix::sys::stat::fstat(__fd)) { + Ok(__s) => { + a1.with_mut(|__st| *__st = Stat::from_libc(&__s)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + fn f3(a0: Ptr, a1: ::libc::mode_t) -> i32 { match nix::unistd::mkdir( a0.to_rust_string().as_str(), diff --git a/rules/termios/tgt_refcount.rs b/rules/termios/tgt_refcount.rs index 1c5f47f6..79140e21 100644 --- a/rules/termios/tgt_refcount.rs +++ b/rules/termios/tgt_refcount.rs @@ -10,3 +10,35 @@ fn t1() -> libcc2rs::Termios { fn t2() -> libcc2rs::Winsize { Default::default() } + +fn f1(a0: i32, a1: i32, a2: Ptr) -> i32 { + let __action = match a1 { + 0 => nix::sys::termios::SetArg::TCSANOW, + 1 => nix::sys::termios::SetArg::TCSADRAIN, + 2 => nix::sys::termios::SetArg::TCSAFLUSH, + __a => panic!("tcsetattr: unsupported action {__a}"), + }; + let __t = nix::sys::termios::Termios::from(a2.with(|__src| __src.to_libc())); + match FdRegistry::with_fd(a0, |__fd| { + nix::sys::termios::tcsetattr(__fd, __action, &__t) + }) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f2(a0: i32, a1: Ptr) -> i32 { + match FdRegistry::with_fd(a0, |__fd| nix::sys::termios::tcgetattr(__fd)) { + Ok(__t) => { + a1.with_mut(|__dst| *__dst = Termios::from_libc(&__t.into())); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} From 006ed074d40287f7599d701a1578ced10907e2c4 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 14:12:25 +0100 Subject: [PATCH 3/6] Revert "Restructure tests to avoid future conflicts" This reverts commit 0cadc915eaf431b381c77533a3a6853997cd3a8e. --- tests/unit/fd_io.c | 14 +++ tests/unit/isatty.c | 13 -- tests/unit/lseek_ftruncate.c | 22 ---- tests/unit/out/refcount/fd_io.rs | 124 +++++++++++++++++++ tests/unit/out/refcount/isatty.rs | 49 -------- tests/unit/out/refcount/lseek_ftruncate.rs | 110 ---------------- tests/unit/out/refcount/pipe_io.rs | 109 ---------------- tests/unit/out/refcount/socket_listen.rs | 44 ------- tests/unit/out/refcount/socket_open_close.rs | 43 ------- tests/unit/out/unsafe/fd_io.rs | 46 +++++++ tests/unit/out/unsafe/isatty.rs | 29 ----- tests/unit/out/unsafe/lseek_ftruncate.rs | 65 ---------- tests/unit/out/unsafe/pipe_io.rs | 59 --------- tests/unit/out/unsafe/socket_listen.rs | 20 --- tests/unit/out/unsafe/socket_open_close.rs | 19 --- tests/unit/pipe_io.c | 17 --- tests/unit/socket_listen.c | 11 -- tests/unit/socket_open_close.c | 10 -- 18 files changed, 184 insertions(+), 620 deletions(-) delete mode 100644 tests/unit/isatty.c delete mode 100644 tests/unit/lseek_ftruncate.c delete mode 100644 tests/unit/out/refcount/isatty.rs delete mode 100644 tests/unit/out/refcount/lseek_ftruncate.rs delete mode 100644 tests/unit/out/refcount/pipe_io.rs delete mode 100644 tests/unit/out/refcount/socket_listen.rs delete mode 100644 tests/unit/out/refcount/socket_open_close.rs delete mode 100644 tests/unit/out/unsafe/isatty.rs delete mode 100644 tests/unit/out/unsafe/lseek_ftruncate.rs delete mode 100644 tests/unit/out/unsafe/pipe_io.rs delete mode 100644 tests/unit/out/unsafe/socket_listen.rs delete mode 100644 tests/unit/out/unsafe/socket_open_close.rs delete mode 100644 tests/unit/pipe_io.c delete mode 100644 tests/unit/socket_listen.c delete mode 100644 tests/unit/socket_open_close.c diff --git a/tests/unit/fd_io.c b/tests/unit/fd_io.c index 4f1fbeca..7dcefe46 100644 --- a/tests/unit/fd_io.c +++ b/tests/unit/fd_io.c @@ -1,6 +1,7 @@ #include #include #include +#include #include int main(void) { @@ -18,5 +19,18 @@ int main(void) { assert(read(fd, buf, sizeof(buf)) == 0); assert(close(fd) == 0); assert(unlink(path) == 0); + int fds[2]; + assert(pipe(fds) == 0); + assert(write(fds[1], "ab", 2) == 2); + char buf2[4]; + memset(buf2, 0, sizeof(buf2)); + assert(read(fds[0], buf2, sizeof(buf2)) == 2); + assert(strcmp(buf2, "ab") == 0); + assert(close(fds[1]) == 0); + assert(read(fds[0], buf2, sizeof(buf2)) == 0); + assert(close(fds[0]) == 0); + int s = socket(AF_INET, SOCK_STREAM, 0); + assert(s >= 0); + assert(close(s) == 0); return 0; } diff --git a/tests/unit/isatty.c b/tests/unit/isatty.c deleted file mode 100644 index e849816a..00000000 --- a/tests/unit/isatty.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include - -int main(void) { - const char *path = "/tmp/cpp2rust_isatty_test.tmp"; - int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); - assert(fd >= 0); - assert(isatty(fd) == 0); - assert(close(fd) == 0); - assert(unlink(path) == 0); - return 0; -} diff --git a/tests/unit/lseek_ftruncate.c b/tests/unit/lseek_ftruncate.c deleted file mode 100644 index bd6a988a..00000000 --- a/tests/unit/lseek_ftruncate.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include -#include - -int main(void) { - const char *path = "/tmp/cpp2rust_lseek_ftruncate_test.tmp"; - int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); - assert(fd >= 0); - assert(write(fd, "hello world", 11) == 11); - assert(lseek(fd, 0, SEEK_END) == 11); - assert(lseek(fd, 6, SEEK_SET) == 6); - char buf[16]; - memset(buf, 0, sizeof(buf)); - assert(read(fd, buf, sizeof(buf)) == 5); - assert(strcmp(buf, "world") == 0); - assert(ftruncate(fd, 5) == 0); - assert(lseek(fd, 0, SEEK_END) == 5); - assert(close(fd) == 0); - assert(unlink(path) == 0); - return 0; -} diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index a8be4c06..1bc80d3a 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -138,5 +138,129 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"ab") + .to_any() + .reinterpret_cast::() + .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + let buf2: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + { + ((buf2.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); + ((buf2.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf2.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf2.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf2.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0_isize) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + let s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); return 0; } diff --git a/tests/unit/out/refcount/isatty.rs b/tests/unit/out/refcount/isatty.rs deleted file mode 100644 index e9828c72..00000000 --- a/tests/unit/out/refcount/isatty.rs +++ /dev/null @@ -1,49 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_isatty_test.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!((((libc::isatty((*fd.borrow())) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - return 0; -} diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs deleted file mode 100644 index 7883bf55..00000000 --- a/tests/unit/out/refcount/lseek_ftruncate.rs +++ /dev/null @@ -1,110 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_lseek_ftruncate_test.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello world") - .to_any() - .reinterpret_cast::() - .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 11_isize) as i32) - != 0) - ); - assert!((((libc::lseek((*fd.borrow()), 0_i64, 2) == 11_i64) as i32) != 0)); - assert!((((libc::lseek((*fd.borrow()), 6_i64, 0) == 6_i64) as i32) != 0)); - let buf: Value> = Rc::new(RefCell::new( - (0..16).map(|_| ::default()).collect::>(), - )); - { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 5_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"world").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((libc::ftruncate((*fd.borrow()), 5_i64) == 0) as i32) != 0)); - assert!((((libc::lseek((*fd.borrow()), 0_i64, 2) == 5_i64) as i32) != 0)); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - return 0; -} diff --git a/tests/unit/out/refcount/pipe_io.rs b/tests/unit/out/refcount/pipe_io.rs deleted file mode 100644 index 5f51cf01..00000000 --- a/tests/unit/out/refcount/pipe_io.rs +++ /dev/null @@ -1,109 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"ab") - .to_any() - .reinterpret_cast::() - .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new( - (0..4).map(|_| ::default()).collect::>(), - )); - { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0_isize) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/refcount/socket_listen.rs b/tests/unit/out/refcount/socket_listen.rs deleted file mode 100644 index 05ca09fe..00000000 --- a/tests/unit/out/refcount/socket_listen.rs +++ /dev/null @@ -1,44 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new({ - let __family = match libc::AF_INET { - ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, - ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, - ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, - __d => panic!("socket: unsupported domain {__d}"), - }; - let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); - let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { - ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, - ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, - __t => panic!("socket: unsupported type {__t}"), - }; - let __proto = match 0 { - 0 => None, - ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), - ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), - __p => panic!("socket: unsupported protocol {__p}"), - }; - match nix::sys::socket::socket(__family, __ty, __flags, __proto) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*s.borrow()) >= 0) as i32) != 0)); - assert!((((libc::listen((*s.borrow()), 5) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/refcount/socket_open_close.rs b/tests/unit/out/refcount/socket_open_close.rs deleted file mode 100644 index 7cb8a701..00000000 --- a/tests/unit/out/refcount/socket_open_close.rs +++ /dev/null @@ -1,43 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new({ - let __family = match libc::AF_INET { - ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, - ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, - ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, - __d => panic!("socket: unsupported domain {__d}"), - }; - let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); - let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { - ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, - ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, - __t => panic!("socket: unsupported type {__t}"), - }; - let __proto = match 0 { - 0 => None, - ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), - ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), - __p => panic!("socket: unsupported protocol {__p}"), - }; - match nix::sys::socket::socket(__family, __ty, __flags, __proto) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*s.borrow()) >= 0) as i32) != 0)); - assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 29c8057d..92306d37 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -66,5 +66,51 @@ unsafe fn main_0() -> i32 { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 2_usize + )) == (2_isize)) as i32) + != 0) + ); + let mut buf2: [libc::c_char; 4] = [(0 as libc::c_char); 4]; + { + let byte_0 = (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fds[(0) as usize], + (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (2_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf2.as_mut_ptr()).cast_const(), + (c"ab".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); + assert!( + ((((libc::read( + fds[(0) as usize], + (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (0_isize)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); return 0; } diff --git a/tests/unit/out/unsafe/isatty.rs b/tests/unit/out/unsafe/isatty.rs deleted file mode 100644 index 3856611f..00000000 --- a/tests/unit/out/unsafe/isatty.rs +++ /dev/null @@ -1,29 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_isatty_test.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!(((((libc::isatty(fd)) == (0)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/unsafe/lseek_ftruncate.rs b/tests/unit/out/unsafe/lseek_ftruncate.rs deleted file mode 100644 index 579c1b54..00000000 --- a/tests/unit/out/unsafe/lseek_ftruncate.rs +++ /dev/null @@ -1,65 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut path: *const libc::c_char = (c"/tmp/cpp2rust_lseek_ftruncate_test.tmp" - .as_ptr() - .cast_mut()) - .cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 11_usize - )) == (11_isize)) as i32) - != 0) - ); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); - assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); - let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16]; - { - let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fd, - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 16]>() - )) == (5_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf.as_mut_ptr()).cast_const(), - (c"world".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::ftruncate(fd, 5_i64)) == (0)) as i32) != 0)); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/unsafe/pipe_io.rs b/tests/unit/out/unsafe/pipe_io.rs deleted file mode 100644 index 2455e40d..00000000 --- a/tests/unit/out/unsafe/pipe_io.rs +++ /dev/null @@ -1,59 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 2_usize - )) == (2_isize)) as i32) - != 0) - ); - let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4]; - { - let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fds[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (2_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf.as_mut_ptr()).cast_const(), - (c"ab".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); - assert!( - ((((libc::read( - fds[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (0_isize)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/unsafe/socket_listen.rs b/tests/unit/out/unsafe/socket_listen.rs deleted file mode 100644 index ca55cbf7..00000000 --- a/tests/unit/out/unsafe/socket_listen.rs +++ /dev/null @@ -1,20 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); - return 0; -} diff --git a/tests/unit/out/unsafe/socket_open_close.rs b/tests/unit/out/unsafe/socket_open_close.rs deleted file mode 100644 index 6f8ccf39..00000000 --- a/tests/unit/out/unsafe/socket_open_close.rs +++ /dev/null @@ -1,19 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); - return 0; -} diff --git a/tests/unit/pipe_io.c b/tests/unit/pipe_io.c deleted file mode 100644 index 4d5cfa99..00000000 --- a/tests/unit/pipe_io.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include - -int main(void) { - int fds[2]; - assert(pipe(fds) == 0); - assert(write(fds[1], "ab", 2) == 2); - char buf[4]; - memset(buf, 0, sizeof(buf)); - assert(read(fds[0], buf, sizeof(buf)) == 2); - assert(strcmp(buf, "ab") == 0); - assert(close(fds[1]) == 0); - assert(read(fds[0], buf, sizeof(buf)) == 0); - assert(close(fds[0]) == 0); - return 0; -} diff --git a/tests/unit/socket_listen.c b/tests/unit/socket_listen.c deleted file mode 100644 index f90d1895..00000000 --- a/tests/unit/socket_listen.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include -#include - -int main(void) { - int s = socket(AF_INET, SOCK_STREAM, 0); - assert(s >= 0); - assert(listen(s, 5) == 0); - assert(close(s) == 0); - return 0; -} diff --git a/tests/unit/socket_open_close.c b/tests/unit/socket_open_close.c deleted file mode 100644 index e9a8ddae..00000000 --- a/tests/unit/socket_open_close.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include - -int main(void) { - int s = socket(AF_INET, SOCK_STREAM, 0); - assert(s >= 0); - assert(close(s) == 0); - return 0; -} From 96e2f22c2036218919f3aba1f977b53fa208c5ee Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 14:15:01 +0100 Subject: [PATCH 4/6] Add fstat and termios tests --- tests/unit/fstat.c | 18 +++++++ tests/unit/out/refcount/fstat.rs | 79 ++++++++++++++++++++++++++++++ tests/unit/out/refcount/termios.rs | 62 +++++++++++++++++++++++ tests/unit/out/unsafe/fstat.rs | 40 +++++++++++++++ tests/unit/out/unsafe/termios.rs | 32 ++++++++++++ tests/unit/termios.c | 15 ++++++ 6 files changed, 246 insertions(+) create mode 100644 tests/unit/fstat.c create mode 100644 tests/unit/out/refcount/fstat.rs create mode 100644 tests/unit/out/refcount/termios.rs create mode 100644 tests/unit/out/unsafe/fstat.rs create mode 100644 tests/unit/out/unsafe/termios.rs create mode 100644 tests/unit/termios.c diff --git a/tests/unit/fstat.c b/tests/unit/fstat.c new file mode 100644 index 00000000..6ba1aebd --- /dev/null +++ b/tests/unit/fstat.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +int main(void) { + const char *path = "/tmp/cpp2rust_fstat_test.tmp"; + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + assert(fd >= 0); + assert(write(fd, "hello", 5) == 5); + struct stat st; + assert(fstat(fd, &st) == 0); + assert(st.st_size == 5); + assert((st.st_mode & S_IFMT) == S_IFREG); + assert(close(fd) == 0); + assert(unlink(path) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/fstat.rs b/tests/unit/out/refcount/fstat.rs new file mode 100644 index 00000000..2b13f553 --- /dev/null +++ b/tests/unit/out/refcount/fstat.rs @@ -0,0 +1,79 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fstat_test.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello") + .to_any() + .reinterpret_cast::() + .with_slice(5_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 5_isize) as i32) + != 0) + ); + let st: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::stat::fstat(__fd)) { + Ok(__s) => { + (st.as_pointer()).with_mut(|__st| *__st = Stat::from_libc(&__s)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); + assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0)); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/out/refcount/termios.rs b/tests/unit/out/refcount/termios.rs new file mode 100644 index 00000000..f783b015 --- /dev/null +++ b/tests/unit/out/refcount/termios.rs @@ -0,0 +1,62 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_termios_test.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + let tio: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::termios::tcgetattr(__fd)) { + Ok(__t) => { + (tio.as_pointer()).with_mut(|__dst| *__dst = Termios::from_libc(&__t.into())); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == -1_i32) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/out/unsafe/fstat.rs b/tests/unit/out/unsafe/fstat.rs new file mode 100644 index 00000000..be56dab1 --- /dev/null +++ b/tests/unit/out/unsafe/fstat.rs @@ -0,0 +1,40 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fstat_test.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 5_usize + )) == (5_isize)) as i32) + != 0) + ); + let mut st: ::libc::stat = unsafe { std::mem::zeroed() }; + assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0)); + assert!(((((st.st_size) == (5_i64)) as i32) != 0)); + assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/termios.rs b/tests/unit/out/unsafe/termios.rs new file mode 100644 index 00000000..0b29e438 --- /dev/null +++ b/tests/unit/out/unsafe/termios.rs @@ -0,0 +1,32 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_termios_test.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + let mut tio: ::libc::termios = unsafe { std::mem::zeroed() }; + assert!( + ((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0) + ); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/termios.c b/tests/unit/termios.c new file mode 100644 index 00000000..1f4ded43 --- /dev/null +++ b/tests/unit/termios.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +int main(void) { + const char *path = "/tmp/cpp2rust_termios_test.tmp"; + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + assert(fd >= 0); + struct termios tio; + assert(tcgetattr(fd, &tio) == -1); + assert(close(fd) == 0); + assert(unlink(path) == 0); + return 0; +} From 669418d5162f0d7e4dc261024efa7e4779f14b88 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 14:18:46 +0100 Subject: [PATCH 5/6] Add tcsetattr call in test --- tests/unit/out/refcount/termios.rs | 22 ++++++++++++++++++++++ tests/unit/out/unsafe/termios.rs | 5 +++++ tests/unit/termios.c | 1 + 3 files changed, 28 insertions(+) diff --git a/tests/unit/out/refcount/termios.rs b/tests/unit/out/refcount/termios.rs index f783b015..5371707f 100644 --- a/tests/unit/out/refcount/termios.rs +++ b/tests/unit/out/refcount/termios.rs @@ -47,6 +47,28 @@ fn main_0() -> i32 { } == -1_i32) as i32) != 0) ); + assert!( + ((({ + let __action = match 0 { + 0 => nix::sys::termios::SetArg::TCSANOW, + 1 => nix::sys::termios::SetArg::TCSADRAIN, + 2 => nix::sys::termios::SetArg::TCSAFLUSH, + __a => panic!("tcsetattr: unsupported action {__a}"), + }; + let __t = + nix::sys::termios::Termios::from((tio.as_pointer()).with(|__src| __src.to_libc())); + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::sys::termios::tcsetattr(__fd, __action, &__t) + }) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == -1_i32) as i32) + != 0) + ); assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); assert!( (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { diff --git a/tests/unit/out/unsafe/termios.rs b/tests/unit/out/unsafe/termios.rs index 0b29e438..a0659541 100644 --- a/tests/unit/out/unsafe/termios.rs +++ b/tests/unit/out/unsafe/termios.rs @@ -26,6 +26,11 @@ unsafe fn main_0() -> i32 { assert!( ((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0) ); + assert!( + ((((libc::tcsetattr(fd, 0, (&mut tio as *mut ::libc::termios).cast_const())) == (-1_i32)) + as i32) + != 0) + ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); return 0; diff --git a/tests/unit/termios.c b/tests/unit/termios.c index 1f4ded43..79bd869e 100644 --- a/tests/unit/termios.c +++ b/tests/unit/termios.c @@ -9,6 +9,7 @@ int main(void) { assert(fd >= 0); struct termios tio; assert(tcgetattr(fd, &tio) == -1); + assert(tcsetattr(fd, TCSANOW, &tio) == -1); assert(close(fd) == 0); assert(unlink(path) == 0); return 0; From 153bf07d1bad61523456888862d74495196fa794 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 14:34:04 +0100 Subject: [PATCH 6/6] Delete unportable test part --- tests/unit/fstat.c | 1 - tests/unit/out/refcount/fstat.rs | 1 - tests/unit/out/unsafe/fstat.rs | 1 - 3 files changed, 3 deletions(-) diff --git a/tests/unit/fstat.c b/tests/unit/fstat.c index 6ba1aebd..a879e16d 100644 --- a/tests/unit/fstat.c +++ b/tests/unit/fstat.c @@ -11,7 +11,6 @@ int main(void) { struct stat st; assert(fstat(fd, &st) == 0); assert(st.st_size == 5); - assert((st.st_mode & S_IFMT) == S_IFREG); assert(close(fd) == 0); assert(unlink(path) == 0); return 0; diff --git a/tests/unit/out/refcount/fstat.rs b/tests/unit/out/refcount/fstat.rs index 2b13f553..207cfb95 100644 --- a/tests/unit/out/refcount/fstat.rs +++ b/tests/unit/out/refcount/fstat.rs @@ -63,7 +63,6 @@ fn main_0() -> i32 { != 0) ); assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); - assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0)); assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); assert!( (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { diff --git a/tests/unit/out/unsafe/fstat.rs b/tests/unit/out/unsafe/fstat.rs index be56dab1..9a6ba69c 100644 --- a/tests/unit/out/unsafe/fstat.rs +++ b/tests/unit/out/unsafe/fstat.rs @@ -33,7 +33,6 @@ unsafe fn main_0() -> i32 { let mut st: ::libc::stat = unsafe { std::mem::zeroed() }; assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0)); assert!(((((st.st_size) == (5_i64)) as i32) != 0)); - assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0)); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); return 0;