tests: Move syscall setgid32 test to own executable
authorAndreas Schneider <asn@samba.org>
Wed, 28 Oct 2015 07:36:53 +0000 (08:36 +0100)
committerAndreas Schneider <asn@samba.org>
Wed, 28 Oct 2015 09:25:05 +0000 (10:25 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
tests/CMakeLists.txt
tests/test_syscall_gid32.c
tests/test_syscall_setgid32.c [new file with mode: 0644]

index 41b1ce22304cbcce6289048206873d4da32f72db..82b4eab1799043c701e929aa5d3d19173df624a1 100644 (file)
@@ -71,6 +71,7 @@ if (HAVE_LINUX_32BIT_SYSCALLS)
         test_syscall_setuid32
         test_syscall_setreuid32
         test_syscall_setresuid32
+        test_syscall_setgid32
         test_syscall_gid32)
 endif (HAVE_LINUX_32BIT_SYSCALLS)
 
index 04b8c9e25168a8589a4b09ba75c776cfbde6aff1..a810cb09aa25f2feca690d5ce7c672ad04ec5829 100644 (file)
 
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
 
-static void test_uwrap_syscall_setgid32(void **state)
-{
-       long int rc;
-       gid_t g;
-
-       (void) state; /* unused */
-
-       rc = syscall(SYS_setgid32, 1);
-       assert_int_equal(rc, 0);
-
-       g = getgid();
-       assert_int_equal(g, 1);
-       assert_int_equal(g, syscall(SYS_getgid32));
-}
-
 static void test_uwrap_syscall_setregid32(void **state)
 {
        long int rc;
@@ -115,7 +100,6 @@ int main(void) {
        int rc;
 
        const struct CMUnitTest uwrap_tests[] = {
-               cmocka_unit_test(test_uwrap_syscall_setgid32),
                cmocka_unit_test(test_uwrap_syscall_setregid32),
                cmocka_unit_test(test_uwrap_syscall_setresgid32),
                cmocka_unit_test(test_uwrap_syscall_setgroups32),
diff --git a/tests/test_syscall_setgid32.c b/tests/test_syscall_setgid32.c
new file mode 100644 (file)
index 0000000..e01a2b4
--- /dev/null
@@ -0,0 +1,158 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <grp.h>
+
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#endif
+
+static void test_uwrap_syscall_setgid32_root(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 = syscall(SYS_setgid32, -1);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EINVAL);
+
+#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
+
+       rc = syscall(SYS_setgid32, 0x5555);
+       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, 0x5555);
+       assert_int_equal(cp_sgid, 0x5555);
+#endif
+
+       g = getgid();
+       assert_int_equal(g, 0x5555);
+
+       g = getegid();
+       assert_int_equal(g, 0x5555);
+
+       rc = setgid(0);
+       assert_return_code(rc, errno);
+}
+
+static void test_uwrap_syscall_setgid32_user(void **state)
+{
+       int rc;
+       uid_t u;
+#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
+
+       setuid(0x5555);
+       assert_return_code(rc, errno);
+
+       u = getuid();
+       assert_int_equal(u, 0x5555);
+       u = geteuid();
+       assert_int_equal(u, 0x5555);
+
+#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
+
+       rc = syscall(SYS_setgid32, 0x5555);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+#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
+
+       rc = syscall(SYS_setgid32, 0x0);
+       assert_return_code(rc, errno);
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest uwrap_tests[] = {
+               cmocka_unit_test(test_uwrap_syscall_setgid32_root),
+               cmocka_unit_test(test_uwrap_syscall_setgid32_user),
+       };
+
+       rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);
+
+       return rc;
+}
+