From 3c660acca83b7f29b490aa1e80c0d6a8d7ad1e5c Mon Sep 17 00:00:00 2001
From: Mara Bos <m-ou.se@m-ou.se>
Date: Fri, 23 Nov 2018 16:29:17 +0100
Subject: [PATCH] Add unit test for dynamically loading modules on posix.

---
 Tools/sitl_run.sh                                |  6 ++++++
 cmake/configs/posix_sitl_default.cmake           |  1 +
 platforms/posix/cmake/sitl_tests.cmake           | 16 +++++++++++++++-
 posix-configs/SITL/init/test/test_dyn_hello      |  8 ++++++++
 .../posix/tests/dyn_hello/CMakeLists.txt         |  8 ++++++++
 src/platforms/posix/tests/dyn_hello/hello.cpp    | 16 ++++++++++++++++
 6 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 posix-configs/SITL/init/test/test_dyn_hello
 create mode 100644 src/platforms/posix/tests/dyn_hello/CMakeLists.txt
 create mode 100644 src/platforms/posix/tests/dyn_hello/hello.cpp

diff --git a/Tools/sitl_run.sh b/Tools/sitl_run.sh
index 65b5943c92..83e7d1706c 100755
--- a/Tools/sitl_run.sh
+++ b/Tools/sitl_run.sh
@@ -8,6 +8,7 @@ program="$3"
 model="$4"
 src_path="$5"
 build_path="$6"
+# The rest of the arguments are files to copy into the working dir.
 
 echo SITL ARGS
 
@@ -58,6 +59,11 @@ pkill -x px4_$model || true
 cp $src_path/Tools/posix_lldbinit $rootfs/.lldbinit
 cp $src_path/Tools/posix.gdbinit $rootfs/.gdbinit
 
+shift 6
+for file in "$@"; do
+	cp "$file" $rootfs/
+done
+
 SIM_PID=0
 
 if [ "$program" == "jmavsim" ] && [ ! -n "$no_sim" ]
diff --git a/cmake/configs/posix_sitl_default.cmake b/cmake/configs/posix_sitl_default.cmake
index a8a5cdbb96..7fcd64b75a 100644
--- a/cmake/configs/posix_sitl_default.cmake
+++ b/cmake/configs/posix_sitl_default.cmake
@@ -55,6 +55,7 @@ set(config_module_list
 	systemcmds/tests
 
 	platforms/posix/tests/hello
+	platforms/posix/tests/dyn_hello
 	platforms/posix/tests/hrt_test
 	platforms/posix/tests/vcdev_test
 
diff --git a/platforms/posix/cmake/sitl_tests.cmake b/platforms/posix/cmake/sitl_tests.cmake
index 0b36456e56..2ef5a9722d 100644
--- a/platforms/posix/cmake/sitl_tests.cmake
+++ b/platforms/posix/cmake/sitl_tests.cmake
@@ -99,6 +99,20 @@ add_test(NAME shutdown
 set_tests_properties(shutdown PROPERTIES PASS_REGULAR_EXPRESSION "Shutting down")
 sanitizer_fail_test_on_error(shutdown)
 
+# Dynamic module loading test
+add_test(NAME dyn
+	COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh
+		$<TARGET_FILE:px4>
+		none
+		none
+		test_dyn_hello
+		${PX4_SOURCE_DIR}
+		${PX4_BINARY_DIR}
+		$<TARGET_FILE:platforms__posix__tests__dyn_hello>
+	WORKING_DIRECTORY ${SITL_WORKING_DIR})
+set_tests_properties(dyn PROPERTIES PASS_REGULAR_EXPRESSION "1: PASSED")
+sanitizer_fail_test_on_error(dyn)
+
 # run arbitrary commands
 set(test_cmds
 	hello
@@ -127,7 +141,7 @@ endforeach()
 
 add_custom_target(test_results
 		COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test
-		DEPENDS px4
+		DEPENDS px4 platforms__posix__tests__dyn_hello
 		USES_TERMINAL
 		COMMENT "Running tests in sitl"
 		WORKING_DIRECTORY ${PX4_BINARY_DIR})
diff --git a/posix-configs/SITL/init/test/test_dyn_hello b/posix-configs/SITL/init/test/test_dyn_hello
new file mode 100644
index 0000000000..e323fa95c2
--- /dev/null
+++ b/posix-configs/SITL/init/test/test_dyn_hello
@@ -0,0 +1,8 @@
+#!/bin/sh
+# PX4 commands need the 'px4-' prefix in bash.
+# (px4-alias.sh is expected to be in the PATH)
+. px4-alias.sh
+
+dyn ./platforms__posix__tests__dyn_hello.px4mod PASSED
+
+shutdown
diff --git a/src/platforms/posix/tests/dyn_hello/CMakeLists.txt b/src/platforms/posix/tests/dyn_hello/CMakeLists.txt
new file mode 100644
index 0000000000..fd1577b8db
--- /dev/null
+++ b/src/platforms/posix/tests/dyn_hello/CMakeLists.txt
@@ -0,0 +1,8 @@
+px4_add_module(
+	MODULE platforms__posix__tests__dyn_hello
+	MAIN hello
+	SRCS
+		hello.cpp
+	DEPENDS
+	DYNAMIC
+	)
diff --git a/src/platforms/posix/tests/dyn_hello/hello.cpp b/src/platforms/posix/tests/dyn_hello/hello.cpp
new file mode 100644
index 0000000000..067e28e951
--- /dev/null
+++ b/src/platforms/posix/tests/dyn_hello/hello.cpp
@@ -0,0 +1,16 @@
+#include <px4_log.h>
+#include <px4_app.h>
+
+extern "C" __EXPORT int hello_main(int argc, char *argv[]);
+int hello_main(int argc, char *argv[])
+{
+	PX4_INFO("Hello, I am a dynamically loaded module.");
+
+	PX4_INFO("Argv:");
+
+	for (int i = 0; i < argc; ++i) {
+		PX4_INFO("  %d: %s", i, argv[i]);
+	}
+
+	return 0;
+}
-- 
GitLab