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

index 72a46c79a30e3368121ac7832becb4f5482b9688..9216d83dfc5258ce95b10a3ebc3614e11285a2dc 100644 (file)
@@ -19,6 +19,7 @@ set(TESTSUITE_LIBRARIES ${UWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
 
 set(UWRAP_TESTS
     test_setuid
+    test_seteuid
     test_uid
     test_gid
     test_syscall
diff --git a/tests/test_seteuid.c b/tests/test_seteuid.c
new file mode 100644 (file)
index 0000000..b4f170c
--- /dev/null
@@ -0,0 +1,119 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <pwd.h>
+
+static void test_uwrap_seteuid(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 = seteuid(-1);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EINVAL);
+
+       u = geteuid();
+       assert_int_equal(u, 0x0);
+
+       rc = seteuid(0x4444);
+       assert_return_code(rc, errno);
+
+#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, 0x4444);
+       assert_int_equal(cp_suid, 0);
+#endif
+
+       u = geteuid();
+       assert_int_equal(u, 0x4444);
+
+       /*
+        * The euid needs to be 0 in order to change to an
+        * unknown value (here 0x5555).
+        */
+       rc = seteuid(0x5555);
+       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, 0);
+       assert_int_equal(cp_euid, 0x4444);
+       assert_int_equal(cp_suid, 0);
+#endif
+
+       u = geteuid();
+       assert_int_equal(u, 0x4444);
+
+       rc = seteuid(0);
+       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, 0);
+       assert_int_equal(cp_euid, 0);
+       assert_int_equal(cp_suid, 0);
+#endif
+
+       u = geteuid();
+       assert_int_equal(u, 0);
+
+       rc = seteuid(0x5555);
+       assert_return_code(rc, errno);
+
+#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, 0x5555);
+       assert_int_equal(cp_suid, 0);
+#endif
+
+       u = geteuid();
+       assert_int_equal(u, 0x5555);
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest uwrap_tests[] = {
+               cmocka_unit_test(test_uwrap_seteuid),
+       };
+
+       rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);
+
+       return rc;
+}
index e96caa6a044ade2b629b8de51920f8a520ec12ce..575cd7499c40577d1fc6d8bb18b4ee8f53572015 100644 (file)
 
 #include <pwd.h>
 
-static void test_uwrap_seteuid(void **state)
-{
-       int rc;
-       uid_t u;
-
-       (void) state; /* unused */
-
-       rc = seteuid(-1);
-       assert_int_equal(rc, -1);
-
-       rc = seteuid(0);
-       assert_int_equal(rc, 0);
-
-       u = geteuid();
-       assert_int_equal(u, 0);
-
-       rc = setuid(42);
-       assert_int_equal(rc, 0);
-
-       u = getuid();
-       assert_int_equal(u, 42);
-}
-
 #ifdef HAVE_SETREUID
 static void test_uwrap_setreuid(void **state)
 {
@@ -98,7 +75,6 @@ int main(void) {
        int rc;
 
        const struct CMUnitTest uwrap_tests[] = {
-               cmocka_unit_test(test_uwrap_seteuid),
 #ifdef HAVE_SETREUID
                cmocka_unit_test(test_uwrap_setreuid),
 #endif