tests: Create a test for threaded SYS_setuid
authorAndreas Schneider <asn@samba.org>
Tue, 27 Oct 2015 09:19:57 +0000 (10:19 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 27 Oct 2015 13:55:41 +0000 (14:55 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
tests/CMakeLists.txt
tests/test_glibc_thread_support.c
tests/test_thread_setuid.c [new file with mode: 0644]

index 3af0695b290416e3d78f8f895daa09194b894679..cf8b830d4669abd08832f12d94ef9b6b1b95ad7a 100644 (file)
@@ -79,6 +79,14 @@ set_property(
         ENVIRONMENT ${TEST_ENVIRONMENT})
 
 if (LINUX OR OSX)
+    add_cmocka_test(test_thread_setuid test_thread_setuid.c ${CMOCKA_LIBRARY})
+    target_link_libraries(test_thread_setuid ${CMAKE_THREAD_LIBS_INIT})
+    set_property(
+        TEST
+            test_thread_setuid
+        PROPERTY
+            ENVIRONMENT ${TEST_ENVIRONMENT};UID_WRAPPER=1;CMOCKA_TEST_ABORT=1)
+
     add_cmocka_test(test_thread_setreuid test_thread_setreuid.c ${CMOCKA_LIBRARY})
     target_link_libraries(test_thread_setreuid ${CMAKE_THREAD_LIBS_INIT})
     set_property(
index 7d61cc2fb9f656b8e98feeb136f960b914ca4999..366131479f966f10f32c6986398b05c2a16a5db2 100644 (file)
@@ -136,72 +136,6 @@ static void test_sync_setgid(void **state)
 }
 
 
-static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
-static void *uwrap_getuid_sync(void *arg)
-{
-       uid_t u;
-
-       (void) arg; /* unused */
-
-       pthread_mutex_unlock(&sleep_mutex);
-       pthread_mutex_lock(&wait_mutex);
-
-       u = getuid();
-       assert_int_equal(u, 888);
-
-       return NULL;
-}
-
-static void *uwrap_setuid_sync(void *arg)
-{
-       int rc;
-
-       (void) arg; /* unused */
-
-       rc = setuid(888);
-       assert_int_equal(rc, 0);
-
-       return NULL;
-}
-
-static void test_real_sync_setuid(void **state)
-{
-       pthread_t threads[2];
-       uid_t u;
-       int rc;
-
-       (void) state; /* unused */
-
-       rc = setuid(222);
-       assert_int_equal(rc, 0);
-
-       pthread_mutex_lock(&wait_mutex);
-       pthread_mutex_lock(&sleep_mutex);
-
-       /* Create thread which will wait for change. */
-       pthread_create(&threads[0],
-                      NULL,
-                      uwrap_getuid_sync,
-                      NULL);
-
-       pthread_mutex_lock(&sleep_mutex);
-
-       pthread_create(&threads[1],
-                      NULL,
-                      uwrap_setuid_sync,
-                      NULL);
-
-       pthread_join(threads[1], NULL);
-
-       pthread_mutex_unlock(&wait_mutex);
-
-       pthread_join(threads[0], NULL);
-
-       u = getuid();
-       assert_int_equal(u, 888);
-}
-
 static void *uwrap_setgroups(void *arg)
 {
        gid_t glist[] = { 100, 200, 300, 400, 500 };
@@ -333,7 +267,6 @@ int main(void) {
        const struct CMUnitTest thread_tests[] = {
                cmocka_unit_test(test_sync_setgid),
                cmocka_unit_test(test_sync_setgid_syscall),
-               cmocka_unit_test(test_real_sync_setuid),
                cmocka_unit_test(test_sync_setgroups),
                cmocka_unit_test(test_thread_create_thread_setgid),
        };
diff --git a/tests/test_thread_setuid.c b/tests/test_thread_setuid.c
new file mode 100644 (file)
index 0000000..8b52c38
--- /dev/null
@@ -0,0 +1,101 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <pthread.h>
+
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.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
+
+#define NUM_THREADS 10
+
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
+
+
+static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void *uwrap_getuid_sync(void *arg)
+{
+       uid_t u;
+
+       (void) arg; /* unused */
+
+       pthread_mutex_unlock(&sleep_mutex);
+       pthread_mutex_lock(&wait_mutex);
+
+       u = getuid();
+       assert_int_equal(u, 888);
+
+       return NULL;
+}
+
+static void *uwrap_setuid_sync(void *arg)
+{
+       int rc;
+
+       (void) arg; /* unused */
+
+       rc = setuid(888);
+       assert_int_equal(rc, 0);
+
+       return NULL;
+}
+
+static void test_real_sync_setuid(void **state)
+{
+       pthread_t threads[2];
+       uid_t u;
+
+       (void) state; /* unused */
+
+       pthread_mutex_lock(&wait_mutex);
+       pthread_mutex_lock(&sleep_mutex);
+
+       /* Create thread which will wait for change. */
+       pthread_create(&threads[0],
+                      NULL,
+                      uwrap_getuid_sync,
+                      NULL);
+
+       pthread_mutex_lock(&sleep_mutex);
+
+       pthread_create(&threads[1],
+                      NULL,
+                      uwrap_setuid_sync,
+                      NULL);
+
+       pthread_join(threads[1], NULL);
+
+       pthread_mutex_unlock(&wait_mutex);
+
+       pthread_join(threads[0], NULL);
+
+       u = getuid();
+       assert_int_equal(u, 888);
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest thread_tests[] = {
+               cmocka_unit_test(test_real_sync_setuid),
+       };
+
+       rc = cmocka_run_group_tests(thread_tests, NULL, NULL);
+
+       return rc;
+}