diff options
author | Andreas Schneider <asn@samba.org> | 2015-10-09 13:21:20 +0200 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2015-10-27 14:56:08 +0100 |
commit | 4a0e9d4da9be669755263b68adefba507f5e3ea9 (patch) | |
tree | ad7ddd0b1e892ff359c3049240bf7826e633e7ba | |
parent | 246a94bffd8224baa01d6a4f20cf2b58b5ff5a76 (diff) | |
download | uid_wrapper-4a0e9d4da9be669755263b68adefba507f5e3ea9.tar.gz uid_wrapper-4a0e9d4da9be669755263b68adefba507f5e3ea9.tar.xz uid_wrapper-4a0e9d4da9be669755263b68adefba507f5e3ea9.zip |
tests: Move setresgid test to own executable
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
-rw-r--r-- | tests/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tests/test_gid.c | 23 | ||||
-rw-r--r-- | tests/test_setresgid.c | 198 |
3 files changed, 202 insertions, 23 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 85b6844..cfb45ec 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -45,6 +45,10 @@ if (HAVE_SETREGID) list(APPEND UWRAP_GID_TESTS test_setregid) endif (HAVE_SETREGID) +if (HAVE_SETRESGID) + list(APPEND UWRAP_GID_TESTS test_setresgid) +endif (HAVE_SETRESGID) + set(UWRAP_TESTS ${UWRAP_UID_TESTS} ${UWRAP_GID_TESTS} diff --git a/tests/test_gid.c b/tests/test_gid.c index 222eadf..45d2803 100644 --- a/tests/test_gid.c +++ b/tests/test_gid.c @@ -14,25 +14,6 @@ #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) -#ifdef HAVE_SETRESGID -static void test_uwrap_setresgid(void **state) -{ - int rc; - uid_t u; - - (void) state; /* unused */ - - rc = setresgid(1, 2, -1); - assert_int_equal(rc, 0); - - u = getgid(); - assert_int_equal(u, 1); - - u = getegid(); - assert_int_equal(u, 2); -} -#endif - static void test_uwrap_getgroups(void **state) { gid_t rlist[20] = {0}; @@ -81,10 +62,6 @@ int main(void) { const struct CMUnitTest uwrap_tests[] = { cmocka_unit_test(test_uwrap_getgroups), - -#ifdef HAVE_SETRESGID - cmocka_unit_test(test_uwrap_setresgid), -#endif cmocka_unit_test(test_uwrap_setgroups), }; diff --git a/tests/test_setresgid.c b/tests/test_setresgid.c new file mode 100644 index 0000000..3656e06 --- /dev/null +++ b/tests/test_setresgid.c @@ -0,0 +1,198 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#include <errno.h> +#include <sys/types.h> +#include <unistd.h> +#include <errno.h> + +#include <pwd.h> + +static void test_uwrap_setresgid(void **state) +{ + int rc; + uid_t u; + gid_t g; +#ifdef HAVE_GETRESGID + gid_t cp_rgid, cp_egid, cp_sgid; +#endif + + (void) state; /* unused */ + + u = getuid(); + assert_int_equal(u, 0x0); + u = geteuid(); + assert_int_equal(u, 0x0); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0); + assert_int_equal(cp_egid, 0); + assert_int_equal(cp_sgid, 0); +#endif + + g = getgid(); + assert_int_equal(g, 0x0); + g = getegid(); + assert_int_equal(g, 0x0); + + rc = setresgid(-1, -1, -1); + assert_return_code(rc, errno); + + rc = setresgid(0x4444, 0x5555, -1); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x4444); + assert_int_equal(cp_egid, 0x5555); + assert_int_equal(cp_sgid, 0); +#endif + + g = getgid(); + assert_int_equal(g, 0x4444); + g = getegid(); + assert_int_equal(g, 0x5555); + + /* We can go back cause the sgid is 0 */ + rc = setresgid(0, -1, -1); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0); + assert_int_equal(cp_egid, 0x5555); + assert_int_equal(cp_sgid, 0); +#endif + + u = getgid(); + assert_int_equal(u, 0x0); + g = getegid(); + assert_int_equal(g, 0x5555); + + rc = setresgid(-1, 0, -1); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x0); + assert_int_equal(cp_egid, 0x0); + assert_int_equal(cp_sgid, 0x0); +#endif + + u = getegid(); + assert_int_equal(u, 0); + + rc = setresgid(0, 0x5555, 0x6666); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0); + assert_int_equal(cp_egid, 0x5555); + assert_int_equal(cp_sgid, 0x6666); +#endif + + /* + * The egid needs to be 0 in order to change to an + * unknown value (here 0x4444) + */ + rc = setresgid(0x5555, 0x6666, 0x4444); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x5555); + assert_int_equal(cp_egid, 0x6666); + assert_int_equal(cp_sgid, 0x4444); +#endif + + errno = 0; + rc = setresgid(0x5555, 0x6666, 0); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x5555); + assert_int_equal(cp_egid, 0x6666); + assert_int_equal(cp_sgid, 0); +#endif + + rc = setresgid(0x5555, 0x6666, 0x4444); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x5555); + assert_int_equal(cp_egid, 0x6666); + assert_int_equal(cp_sgid, 0x4444); +#endif + + rc = setresgid(0x5555, 0, 0x6666); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x5555); + assert_int_equal(cp_egid, 0); + assert_int_equal(cp_sgid, 0x6666); +#endif + + rc = setresgid(0x1111, 0x2222, 0x3333); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x1111); + assert_int_equal(cp_egid, 0x2222); + assert_int_equal(cp_sgid, 0x3333); +#endif + + rc = setresgid(0, -1, -1); + assert_return_code(rc, errno); + +#ifdef HAVE_GETRESGID + cp_rgid = cp_egid = cp_sgid = -1; + rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid); + assert_return_code(rc, errno); + assert_int_equal(cp_rgid, 0x0); + assert_int_equal(cp_egid, 0x2222); + assert_int_equal(cp_sgid, 0x3333); +#endif +} + +int main(void) { + int rc; + + const struct CMUnitTest uwrap_tests[] = { + cmocka_unit_test(test_uwrap_setresgid), + }; + + rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL); + + return rc; +} |