| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include <stdarg.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include <setjmp.h> |
| 7 |
#include <cmocka.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include <pwd.h> |
| 11 |
|
| 12 |
static void test_nwrap_passwd(void **state) |
| 13 |
{ |
| 14 |
struct passwd *pwd; |
| 15 |
uid_t id = 424242; |
| 16 |
|
| 17 |
(void) state; /* unused */ |
| 18 |
|
| 19 |
pwd = getpwuid(id); |
| 20 |
assert_non_null(pwd); |
| 21 |
|
| 22 |
assert_string_equal(pwd->pw_name, "hanswurst"); |
| 23 |
assert_int_equal(pwd->pw_uid, id); |
| 24 |
assert_int_equal(pwd->pw_gid, id); |
| 25 |
} |
| 26 |
|
| 27 |
static void test_nwrap_passwd_closed_handles(void **state) |
| 28 |
{ |
| 29 |
struct passwd *pwd; |
| 30 |
uid_t id = 424242; |
| 31 |
long maxfd; |
| 32 |
|
| 33 |
(void) state; /* unused */ |
| 34 |
|
| 35 |
pwd = getpwuid(id); |
| 36 |
assert_non_null(pwd); |
| 37 |
|
| 38 |
maxfd = sysconf(_SC_OPEN_MAX); |
| 39 |
if (maxfd < 0) { |
| 40 |
maxfd = 1024; |
| 41 |
} |
| 42 |
for (long fd = 3; fd < maxfd; fd++) { |
| 43 |
close(fd); |
| 44 |
} |
| 45 |
|
| 46 |
pwd = getpwuid(id); |
| 47 |
assert_non_null(pwd); |
| 48 |
} |
| 49 |
|
| 50 |
int main(void) |
| 51 |
{ |
| 52 |
int rc; |
| 53 |
|
| 54 |
const struct CMUnitTest tests[] = { |
| 55 |
cmocka_unit_test(test_nwrap_passwd), |
| 56 |
cmocka_unit_test(test_nwrap_passwd_closed_handles), |
| 57 |
}; |
| 58 |
|
| 59 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 60 |
|
| 61 |
return rc; |
| 62 |
} |