tests: Move setreuid test to own executable
authorAndreas Schneider <asn@samba.org>
Tue, 22 Sep 2015 12:29:49 +0000 (14:29 +0200)
committerAndreas Schneider <asn@samba.org>
Tue, 27 Oct 2015 13:55:15 +0000 (14:55 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
tests/CMakeLists.txt
tests/test_setreuid.c [new file with mode: 0644]
tests/test_uid.c

index 9216d83dfc5258ce95b10a3ebc3614e11285a2dc..d0a1c5a705b19b13cc49c102e2e5b51271da590a 100644 (file)
@@ -17,9 +17,16 @@ endif ()
 
 set(TESTSUITE_LIBRARIES ${UWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
 
+set(UWRAP_UID_TESTS
+     test_setuid
+     test_seteuid)
+
+if (HAVE_SETREUID)
+    list(APPEND UWRAP_UID_TESTS test_setreuid)
+endif (HAVE_SETREUID)
+
 set(UWRAP_TESTS
-    test_setuid
-    test_seteuid
+    ${UWRAP_UID_TESTS}
     test_uid
     test_gid
     test_syscall
diff --git a/tests/test_setreuid.c b/tests/test_setreuid.c
new file mode 100644 (file)
index 0000000..8b24931
--- /dev/null
@@ -0,0 +1,99 @@
+#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 <pwd.h>
+
+static void test_uwrap_setreuid(void **state)
+{
+       int rc;
+       uid_t u;
+#ifdef HAVE_GETRESUID
+       uid_t cp_ruid, cp_euid, cp_suid;
+#endif
+
+       (void) state; /* unused */
+
+#ifdef HAVE_GETRESUID
+       cp_ruid = cp_euid = cp_suid = -1;
+       rc = getresuid(&cp_ruid, &cp_euid, &cp_suid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_ruid, 0);
+       assert_int_equal(cp_euid, 0);
+       assert_int_equal(cp_suid, 0);
+#endif
+       u = geteuid();
+       assert_int_equal(u, 0x0);
+
+       rc = setreuid(-1, -1);
+       assert_int_equal(rc, 0);
+
+       rc = setreuid(0x4444, 0x5555);
+       assert_int_equal(rc, 0);
+
+#ifdef HAVE_GETRESUID
+       cp_ruid = cp_euid = cp_suid = -1;
+       rc = getresuid(&cp_ruid, &cp_euid, &cp_suid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_ruid, 0x4444);
+       assert_int_equal(cp_euid, 0x5555);
+       assert_int_equal(cp_suid, 0x5555);
+#endif
+
+       u = getuid();
+       assert_int_equal(u, 0x4444);
+
+       u = geteuid();
+       assert_int_equal(u, 0x5555);
+
+       rc = setreuid(0, -1);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+#ifdef HAVE_GETRESUID
+       cp_ruid = cp_euid = cp_suid = -1;
+       rc = getresuid(&cp_ruid, &cp_euid, &cp_suid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_ruid, 0x4444);
+       assert_int_equal(cp_euid, 0x5555);
+       assert_int_equal(cp_suid, 0x5555);
+#endif
+       u = getuid();
+       assert_int_equal(u, 0x4444);
+
+       u = geteuid();
+       assert_int_equal(u, 0x5555);
+
+
+       rc = setreuid(-1, 0);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+#ifdef HAVE_GETRESUID
+       cp_ruid = cp_euid = cp_suid = -1;
+       rc = getresuid(&cp_ruid, &cp_euid, &cp_suid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_ruid, 0x4444);
+       assert_int_equal(cp_euid, 0x5555);
+       assert_int_equal(cp_suid, 0x5555);
+#endif
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest uwrap_tests[] = {
+               cmocka_unit_test(test_uwrap_setreuid),
+       };
+
+       rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);
+
+       return rc;
+}
index 575cd7499c40577d1fc6d8bb18b4ee8f53572015..bb73ea7602d7a08ea12d7d1643ae278b7a442c63 100644 (file)
 
 #include <pwd.h>
 
-#ifdef HAVE_SETREUID
-static void test_uwrap_setreuid(void **state)
-{
-       int rc;
-       uid_t u;
-
-       (void) state; /* unused */
-
-       rc = setreuid(1, 2);
-       assert_int_equal(rc, 0);
-
-       u = getuid();
-       assert_int_equal(u, 1);
-
-       u = geteuid();
-       assert_int_equal(u, 2);
-}
-#endif
-
 #ifdef HAVE_SETRESUID
 static void test_uwrap_setresuid(void **state)
 {
@@ -75,9 +56,6 @@ int main(void) {
        int rc;
 
        const struct CMUnitTest uwrap_tests[] = {
-#ifdef HAVE_SETREUID
-               cmocka_unit_test(test_uwrap_setreuid),
-#endif
 #ifdef HAVE_SETRESUID
                cmocka_unit_test(test_uwrap_setresuid),
 #endif