| 1 |
#include <stdarg.h> |
| 2 |
#include <stddef.h> |
| 3 |
#include <stdint.h> |
| 4 |
#include <setjmp.h> |
| 5 |
#include <cmocka.h> |
| 6 |
#include <unistd.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <grp.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
static void test_nwrap_initgroups(void **state) |
| 12 |
{ |
| 13 |
gid_t gid = 10000; |
| 14 |
int ret, i; |
| 15 |
int ngroups; |
| 16 |
const char *env = getenv("UID_WRAPPER"); |
| 17 |
|
| 18 |
(void)state; /* unused */ |
| 19 |
|
| 20 |
/* initgroups() sets {10000, 1000, 1002, 100010, 100011, 100012} */ |
| 21 |
ret = initgroups("alice", gid); |
| 22 |
assert_return_code(ret, errno); |
| 23 |
|
| 24 |
/* |
| 25 |
* nss_wrapper() in nwrap_initgroups() makes early return if UID_WRAPPER |
| 26 |
* is not present. The reason is that we need privilege for setgroups() |
| 27 |
* called from initgroups() and we get it only with UID_WRAPPER |
| 28 |
*/ |
| 29 |
if (env != NULL && env[0] == '1') { |
| 30 |
gid_t groups1[6] = {10000, 1000, 1002, 100010, 100011, 100012}; |
| 31 |
gid_t *groups2 = malloc(10 * sizeof(gid_t)); |
| 32 |
|
| 33 |
assert_non_null(groups2); |
| 34 |
ngroups = getgroups(10, groups2); /* room for 10, expect 6 */ |
| 35 |
assert_int_equal(ngroups, 6); |
| 36 |
for (i = 0; i < 6; i++) { |
| 37 |
assert_int_equal(groups1[i], groups2[i]); |
| 38 |
} |
| 39 |
free(groups2); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
int main(void) |
| 44 |
{ |
| 45 |
int rc; |
| 46 |
|
| 47 |
const struct CMUnitTest tests[] = { |
| 48 |
cmocka_unit_test(test_nwrap_initgroups) |
| 49 |
}; |
| 50 |
|
| 51 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 52 |
|
| 53 |
return rc; |
| 54 |
} |