tests: test socket_wrapper_syscall_{valid,va} interaction using uwrap_fake_socket_wra...
authorStefan Metzmacher <metze@samba.org>
Mon, 16 Jan 2023 20:41:09 +0000 (21:41 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 17 Jan 2023 13:31:10 +0000 (14:31 +0100)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
tests/CMakeLists.txt
tests/test_syscall_swrap.c [new file with mode: 0644]
tests/uwrap_fake_socket_wrapper.c [new file with mode: 0644]
tests/uwrap_fake_socket_wrapper.h [new file with mode: 0644]

index 1ae3d86de4d2e8a6310a27debca4c8e1040a9474..da804eb2fdeef738ec0d640de6f3ebe377d20a59 100644 (file)
@@ -1,5 +1,14 @@
 project(tests C)
 
+add_library(uwrap_fake_socket_wrapper SHARED uwrap_fake_socket_wrapper.c)
+target_compile_options(uwrap_fake_socket_wrapper
+                       PRIVATE
+                           ${DEFAULT_C_COMPILE_FLAGS}
+                           -D_GNU_SOURCE)
+target_include_directories(uwrap_fake_socket_wrapper
+                           PRIVATE ${CMAKE_BINARY_DIR} ${CMOCKA_INCLUDE_DIR})
+set(UWRAP_FAKE_SOCKET_WRAPPER_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}uwrap_fake_socket_wrapper${CMAKE_SHARED_LIBRARY_SUFFIX}")
+
 function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME)
     if (CMAKE_BUILD_TYPE)
         string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
@@ -19,6 +28,7 @@ function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME)
     if (ASAN_LIBRARY)
         list(APPEND PRELOAD_LIBRARIES ${ASAN_LIBRARY})
     endif()
+    list(APPEND PRELOAD_LIBRARIES ${UWRAP_FAKE_SOCKET_WRAPPER_LOCATION})
     list(APPEND PRELOAD_LIBRARIES ${UID_WRAPPER_LOCATION})
 
     if (OSX)
@@ -95,6 +105,7 @@ set(UWRAP_TESTS
     ${UWRAP_GID_TESTS}
     test_setgroups
     test_syscall
+    test_syscall_swrap
     ${UWRAP_SYSCALL_UID_TESTS}
     test_syscall_gid)
 
diff --git a/tests/test_syscall_swrap.c b/tests/test_syscall_swrap.c
new file mode 100644 (file)
index 0000000..fb3d26b
--- /dev/null
@@ -0,0 +1,50 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdbool.h>
+#include <string.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#endif
+
+#include "uwrap_fake_socket_wrapper.h"
+
+static void test_uwrap_syscall_swrap(void **state)
+{
+       long int rc;
+
+       (void) state; /* unused */
+
+       rc = syscall(__FAKE_SOCKET_WRAPPER_SYSCALL_NO);
+       assert_int_equal(rc, __FAKE_SOCKET_WRAPPER_SYSCALL_RC);
+
+       signal(SIGSYS, SIG_IGN);
+       rc = syscall(__FAKE_SOCKET_WRAPPER_SYSCALL_NO+1);
+       signal(SIGSYS, SIG_DFL);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, ENOSYS);
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest uwrap_tests[] = {
+               cmocka_unit_test(test_uwrap_syscall_swrap),
+       };
+
+       rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);
+
+       return rc;
+}
diff --git a/tests/uwrap_fake_socket_wrapper.c b/tests/uwrap_fake_socket_wrapper.c
new file mode 100644 (file)
index 0000000..657873a
--- /dev/null
@@ -0,0 +1,44 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdbool.h>
+#include <string.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <errno.h>
+
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#endif
+
+#include "uwrap_fake_socket_wrapper.h"
+
+/* simulate socket_wrapper hooks */
+bool socket_wrapper_syscall_valid(long int sysno)
+{
+       if (sysno == __FAKE_SOCKET_WRAPPER_SYSCALL_NO) {
+               return true;
+       }
+
+       return false;
+}
+
+long int socket_wrapper_syscall_va(long int sysno, va_list va)
+{
+       (void) va; /* unused */
+
+       if (sysno == __FAKE_SOCKET_WRAPPER_SYSCALL_NO) {
+               errno = 0;
+               return __FAKE_SOCKET_WRAPPER_SYSCALL_RC;
+       }
+
+       errno = ENOSYS;
+       return -1;
+}
diff --git a/tests/uwrap_fake_socket_wrapper.h b/tests/uwrap_fake_socket_wrapper.h
new file mode 100644 (file)
index 0000000..63e423a
--- /dev/null
@@ -0,0 +1,7 @@
+#include <stdbool.h>
+
+/* simulate socket_wrapper hooks */
+#define __FAKE_SOCKET_WRAPPER_SYSCALL_NO 123456789
+#define __FAKE_SOCKET_WRAPPER_SYSCALL_RC 987654321
+bool socket_wrapper_syscall_valid(long int sysno);
+long int socket_wrapper_syscall_va(long int sysno, va_list va);