Skip to content

Commit 4aae770

Browse files
committed
Add pwd test
1 parent ac71731 commit 4aae770

3 files changed

Lines changed: 372 additions & 0 deletions

File tree

‎tests/unit/out/refcount/pwd.rs‎

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn test_getpwuid_0() {
10+
let pw: Value<Ptr<libcc2rs::Passwd>> = Rc::new(RefCell::new(
11+
match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(
12+
nix::unistd::geteuid().as_raw(),
13+
)) {
14+
Ok(Some(__u)) => Ptr::alloc(Passwd::from_user(&__u)),
15+
Ok(None) => Ptr::null(),
16+
Err(__e) => {
17+
libcc2rs::cpp2rust_errno().write(__e as i32);
18+
Ptr::null()
19+
}
20+
},
21+
));
22+
assert!((((!((*pw.borrow()).is_null())) as i32) != 0));
23+
assert!(
24+
((({
25+
let _lhs = (*(*(*pw.borrow()).upgrade().deref()).pw_uid.borrow());
26+
_lhs == nix::unistd::geteuid().as_raw()
27+
}) as i32)
28+
!= 0)
29+
);
30+
assert!(
31+
((((*(*(*pw.borrow()).upgrade().deref()).pw_name.borrow())
32+
.to_c_string_iterator()
33+
.count()
34+
> 0_usize) as i32)
35+
!= 0)
36+
);
37+
assert!((((!((*(*(*pw.borrow()).upgrade().deref()).pw_dir.borrow()).is_null())) as i32) != 0));
38+
println!(
39+
"{}",
40+
(*(*(*pw.borrow()).upgrade().deref()).pw_name.borrow())
41+
);
42+
}
43+
pub fn test_getpwuid_missing_1() {
44+
libcc2rs::cpp2rust_errno().write(0);
45+
let pw: Value<Ptr<libcc2rs::Passwd>> = Rc::new(RefCell::new(
46+
match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(2147483646_u32)) {
47+
Ok(Some(__u)) => Ptr::alloc(Passwd::from_user(&__u)),
48+
Ok(None) => Ptr::null(),
49+
Err(__e) => {
50+
libcc2rs::cpp2rust_errno().write(__e as i32);
51+
Ptr::null()
52+
}
53+
},
54+
));
55+
assert!(((((*pw.borrow()).is_null()) as i32) != 0));
56+
assert!(((((libcc2rs::cpp2rust_errno().read()) == 0) as i32) != 0));
57+
}
58+
pub fn test_getpwuid_r_2() {
59+
let pw: Value<libcc2rs::Passwd> = Rc::new(RefCell::new(Default::default()));
60+
let buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
61+
(0..4096).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
62+
));
63+
let result: Value<Ptr<libcc2rs::Passwd>> =
64+
Rc::new(RefCell::new(Ptr::<libcc2rs::Passwd>::null()));
65+
assert!(
66+
((({
67+
let __pwbuf = (pw.as_pointer()).clone();
68+
let __buf = (buf.as_pointer() as Ptr<u8>).clone();
69+
let __buflen = ::std::mem::size_of::<[u8; 4096]>();
70+
let __out = (result.as_pointer()).clone();
71+
match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(
72+
nix::unistd::geteuid().as_raw(),
73+
)) {
74+
Ok(Some(__u)) => {
75+
let __strs: [Vec<u8>; 5] = [
76+
__u.name.as_bytes().to_vec(),
77+
__u.passwd.as_bytes().to_vec(),
78+
__u.gecos.as_bytes().to_vec(),
79+
__u.dir.as_os_str().as_encoded_bytes().to_vec(),
80+
__u.shell.as_os_str().as_encoded_bytes().to_vec(),
81+
];
82+
let __needed: usize = __strs.iter().map(|__s| __s.len() + 1).sum();
83+
if __needed > __buflen {
84+
__out.write(Ptr::null());
85+
libcc2rs::cpp2rust_errno().write(::libc::ERANGE);
86+
::libc::ERANGE
87+
} else {
88+
let mut __ptrs: Vec<Ptr<u8>> = Vec::new();
89+
let mut __off: usize = 0;
90+
for __s in &__strs {
91+
__ptrs.push(__buf.offset(__off));
92+
let mut __dst = __buf.offset(__off);
93+
for __b in __s.iter() {
94+
__dst.write(*__b);
95+
__dst += 1;
96+
}
97+
__dst.write(0);
98+
__off += __s.len() + 1;
99+
}
100+
__pwbuf.with_mut(|__pw| *__pw = Passwd::from_user_in(&__u, &__ptrs));
101+
__out.write(__pwbuf.clone());
102+
0
103+
}
104+
}
105+
Ok(None) => {
106+
__out.write(Ptr::null());
107+
0
108+
}
109+
Err(__e) => {
110+
__out.write(Ptr::null());
111+
libcc2rs::cpp2rust_errno().write(__e as i32);
112+
__e as i32
113+
}
114+
}
115+
} == 0) as i32)
116+
!= 0)
117+
);
118+
assert!(
119+
((({
120+
let _lhs = (*result.borrow()).clone();
121+
_lhs == (pw.as_pointer())
122+
}) as i32)
123+
!= 0)
124+
);
125+
assert!(((((*(*pw.borrow()).pw_uid.borrow()) == nix::unistd::geteuid().as_raw()) as i32) != 0));
126+
assert!(
127+
((((*(*pw.borrow()).pw_name.borrow())
128+
.to_c_string_iterator()
129+
.count()
130+
> 0_usize) as i32)
131+
!= 0)
132+
);
133+
let pw2: Value<Ptr<libcc2rs::Passwd>> = Rc::new(RefCell::new(
134+
match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(
135+
nix::unistd::geteuid().as_raw(),
136+
)) {
137+
Ok(Some(__u)) => Ptr::alloc(Passwd::from_user(&__u)),
138+
Ok(None) => Ptr::null(),
139+
Err(__e) => {
140+
libcc2rs::cpp2rust_errno().write(__e as i32);
141+
Ptr::null()
142+
}
143+
},
144+
));
145+
assert!((((!((*pw2.borrow()).is_null())) as i32) != 0));
146+
assert!(
147+
((({
148+
let mut __it1 = (*(*pw.borrow()).pw_name.borrow()).to_c_string_iterator();
149+
let mut __it2 =
150+
(*(*(*pw2.borrow()).upgrade().deref()).pw_name.borrow()).to_c_string_iterator();
151+
loop {
152+
let __c1 = __it1.next();
153+
let __c2 = __it2.next();
154+
if __c1 != __c2 {
155+
break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32);
156+
}
157+
if __c1.is_none() {
158+
break 0;
159+
}
160+
}
161+
} == 0) as i32)
162+
!= 0)
163+
);
164+
println!("{}", (*(*pw.borrow()).pw_name.borrow()));
165+
}
166+
pub fn test_getpwuid_r_erange_3() {
167+
let pw: Value<libcc2rs::Passwd> = Rc::new(RefCell::new(Default::default()));
168+
let tiny: Value<Box<[u8]>> = Rc::new(RefCell::new(
169+
(0..1).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
170+
));
171+
let result: Value<Ptr<libcc2rs::Passwd>> =
172+
Rc::new(RefCell::new(Ptr::<libcc2rs::Passwd>::null()));
173+
assert!(
174+
((({
175+
let __pwbuf = (pw.as_pointer()).clone();
176+
let __buf = (tiny.as_pointer() as Ptr<u8>).clone();
177+
let __buflen = ::std::mem::size_of::<[u8; 1]>();
178+
let __out = (result.as_pointer()).clone();
179+
match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(
180+
nix::unistd::geteuid().as_raw(),
181+
)) {
182+
Ok(Some(__u)) => {
183+
let __strs: [Vec<u8>; 5] = [
184+
__u.name.as_bytes().to_vec(),
185+
__u.passwd.as_bytes().to_vec(),
186+
__u.gecos.as_bytes().to_vec(),
187+
__u.dir.as_os_str().as_encoded_bytes().to_vec(),
188+
__u.shell.as_os_str().as_encoded_bytes().to_vec(),
189+
];
190+
let __needed: usize = __strs.iter().map(|__s| __s.len() + 1).sum();
191+
if __needed > __buflen {
192+
__out.write(Ptr::null());
193+
libcc2rs::cpp2rust_errno().write(::libc::ERANGE);
194+
::libc::ERANGE
195+
} else {
196+
let mut __ptrs: Vec<Ptr<u8>> = Vec::new();
197+
let mut __off: usize = 0;
198+
for __s in &__strs {
199+
__ptrs.push(__buf.offset(__off));
200+
let mut __dst = __buf.offset(__off);
201+
for __b in __s.iter() {
202+
__dst.write(*__b);
203+
__dst += 1;
204+
}
205+
__dst.write(0);
206+
__off += __s.len() + 1;
207+
}
208+
__pwbuf.with_mut(|__pw| *__pw = Passwd::from_user_in(&__u, &__ptrs));
209+
__out.write(__pwbuf.clone());
210+
0
211+
}
212+
}
213+
Ok(None) => {
214+
__out.write(Ptr::null());
215+
0
216+
}
217+
Err(__e) => {
218+
__out.write(Ptr::null());
219+
libcc2rs::cpp2rust_errno().write(__e as i32);
220+
__e as i32
221+
}
222+
}
223+
} == 34) as i32)
224+
!= 0)
225+
);
226+
assert!(((((*result.borrow()).is_null()) as i32) != 0));
227+
}
228+
pub fn main() {
229+
std::process::exit(main_0());
230+
}
231+
fn main_0() -> i32 {
232+
({ test_getpwuid_0() });
233+
({ test_getpwuid_missing_1() });
234+
({ test_getpwuid_r_2() });
235+
({ test_getpwuid_r_erange_3() });
236+
return 0;
237+
}

‎tests/unit/out/unsafe/pwd.rs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_getpwuid_0() {
10+
let mut pw: *mut ::libc::passwd = libc::getpwuid(libc::geteuid());
11+
assert!((((!((pw).is_null())) as i32) != 0));
12+
assert!((((((*pw).pw_uid) == (libc::geteuid())) as i32) != 0));
13+
assert!(((((libc::strlen(((*pw).pw_name).cast_const())) > (0_usize)) as i32) != 0));
14+
assert!((((!(((*pw).pw_dir).is_null())) as i32) != 0));
15+
printf(
16+
(c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8,
17+
(*pw).pw_name,
18+
);
19+
}
20+
pub unsafe fn test_getpwuid_missing_1() {
21+
(*libcc2rs::cpp2rust_errno_unsafe()) = 0;
22+
let mut pw: *mut ::libc::passwd = libc::getpwuid(2147483646_u32);
23+
assert!(((((pw).is_null()) as i32) != 0));
24+
assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (0)) as i32) != 0));
25+
}
26+
pub unsafe fn test_getpwuid_r_2() {
27+
let mut pw: ::libc::passwd = unsafe { std::mem::zeroed() };
28+
let mut buf: [libc::c_char; 4096] = [(0 as libc::c_char); 4096];
29+
let mut result: *mut ::libc::passwd = std::ptr::null_mut();
30+
assert!(
31+
((((libc::getpwuid_r(
32+
libc::geteuid(),
33+
(&mut pw as *mut ::libc::passwd),
34+
buf.as_mut_ptr(),
35+
::std::mem::size_of::<[libc::c_char; 4096]>(),
36+
(&mut result as *mut *mut ::libc::passwd)
37+
)) == (0)) as i32)
38+
!= 0)
39+
);
40+
assert!(((((result) == (&mut pw as *mut ::libc::passwd)) as i32) != 0));
41+
assert!(((((pw.pw_uid) == (libc::geteuid())) as i32) != 0));
42+
assert!(((((libc::strlen((pw.pw_name).cast_const())) > (0_usize)) as i32) != 0));
43+
let mut pw2: *mut ::libc::passwd = libc::getpwuid(libc::geteuid());
44+
assert!((((!((pw2).is_null())) as i32) != 0));
45+
assert!(
46+
((((libc::strcmp((pw.pw_name).cast_const(), ((*pw2).pw_name).cast_const())) == (0))
47+
as i32)
48+
!= 0)
49+
);
50+
printf(
51+
(c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8,
52+
pw.pw_name,
53+
);
54+
}
55+
pub unsafe fn test_getpwuid_r_erange_3() {
56+
let mut pw: ::libc::passwd = unsafe { std::mem::zeroed() };
57+
let mut tiny: [libc::c_char; 1] = [(0 as libc::c_char); 1];
58+
let mut result: *mut ::libc::passwd = std::ptr::null_mut();
59+
assert!(
60+
((((libc::getpwuid_r(
61+
libc::geteuid(),
62+
(&mut pw as *mut ::libc::passwd),
63+
tiny.as_mut_ptr(),
64+
::std::mem::size_of::<[libc::c_char; 1]>(),
65+
(&mut result as *mut *mut ::libc::passwd)
66+
)) == (34)) as i32)
67+
!= 0)
68+
);
69+
assert!(((((result).is_null()) as i32) != 0));
70+
}
71+
pub fn main() {
72+
unsafe {
73+
std::process::exit(main_0() as i32);
74+
}
75+
}
76+
unsafe fn main_0() -> i32 {
77+
(unsafe { test_getpwuid_0() });
78+
(unsafe { test_getpwuid_missing_1() });
79+
(unsafe { test_getpwuid_r_2() });
80+
(unsafe { test_getpwuid_r_erange_3() });
81+
return 0;
82+
}

‎tests/unit/pwd.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <assert.h>
2+
#include <errno.h>
3+
#include <pwd.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include <sys/types.h>
7+
#include <unistd.h>
8+
9+
static void test_getpwuid(void) {
10+
struct passwd *pw = getpwuid(geteuid());
11+
assert(pw != NULL);
12+
assert(pw->pw_uid == geteuid());
13+
assert(strlen(pw->pw_name) > 0);
14+
assert(pw->pw_dir != NULL);
15+
printf("%s\n", pw->pw_name);
16+
}
17+
18+
static void test_getpwuid_missing(void) {
19+
errno = 0;
20+
struct passwd *pw = getpwuid(0x7ffffffe);
21+
assert(pw == NULL);
22+
assert(errno == 0);
23+
}
24+
25+
static void test_getpwuid_r(void) {
26+
struct passwd pw;
27+
char buf[4096];
28+
struct passwd *result = NULL;
29+
assert(getpwuid_r(geteuid(), &pw, buf, sizeof(buf), &result) == 0);
30+
assert(result == &pw);
31+
assert(pw.pw_uid == geteuid());
32+
assert(strlen(pw.pw_name) > 0);
33+
struct passwd *pw2 = getpwuid(geteuid());
34+
assert(pw2 != NULL);
35+
assert(strcmp(pw.pw_name, pw2->pw_name) == 0);
36+
printf("%s\n", pw.pw_name);
37+
}
38+
39+
static void test_getpwuid_r_erange(void) {
40+
struct passwd pw;
41+
char tiny[1];
42+
struct passwd *result = NULL;
43+
assert(getpwuid_r(geteuid(), &pw, tiny, sizeof(tiny), &result) == ERANGE);
44+
assert(result == NULL);
45+
}
46+
47+
int main(void) {
48+
test_getpwuid();
49+
test_getpwuid_missing();
50+
test_getpwuid_r();
51+
test_getpwuid_r_erange();
52+
return 0;
53+
}

0 commit comments

Comments
 (0)