@@ -18,7 +18,8 @@ test_includes_internal = [
]
libtest = static_library('libtest', libtest_sources,
- dependencies : libcamera_dep,
+ dependencies : [ libcamera_dep, libtest_dep ],
+ cpp_args : libtest_cpp_args,
include_directories : test_includes_internal)
test_libraries = [libtest]
@@ -5,10 +5,40 @@
* test.cpp - libcamera test base class
*/
-#include <stdlib.h>
+#include <stdarg.h>
#include "test.h"
+TestStatus
+Test::testCapabilities(int nCapabilities, ...)
+{
+#ifdef HAVE_LIBLOG
+ va_list args;
+
+ va_start(args, nCapabilities);
+
+ cap_t caps = cap_get_proc();
+ if (caps == NULL)
+ return TestFail;
+
+ cap_value_t v;
+ cap_flag_t f;
+ cap_flag_value_t fv;
+ while (nCapabilities-- > 0) {
+ v = (cap_value_t)va_arg(args, int);
+ f = (cap_flag_t)va_arg(args, int);
+ if ((cap_get_flag(caps, v, f, &fv) < 0) || (fv != CAP_SET))
+ break;
+ }
+
+ cap_free(caps);
+
+ return nCapabilities >= 0 ? TestFail : TestPass;
+#else
+ return TestSkip;
+#endif
+}
+
Test::Test()
{
}
@@ -7,6 +7,10 @@
#ifndef __TEST_TEST_H__
#define __TEST_TEST_H__
+#ifdef HAVE_LIBLOG
+#include <sys/capability.h>
+#endif
+
#include <sstream>
enum TestStatus {
@@ -23,6 +27,8 @@ public:
int execute();
+ static TestStatus testCapabilities(int nCapabilities, ...);
+
protected:
virtual int init() { return 0; }
virtual int run() = 0;
@@ -55,13 +55,24 @@ class LogProcessTest : public Test
protected:
int init()
{
+#ifdef HAVE_LIBLOG
+ /* Check required permissions: CAP_SYS_ADMIN: unshare */
+ int ret = testCapabilities(1, CAP_SYS_ADMIN, CAP_EFFECTIVE);
+ if (ret != TestPass) {
+ char *cap = cap_to_name(CAP_SYS_ADMIN);
+ cerr << "Insufficient capability: " << cap << endl;
+ cap_free(cap);
+ return TestSkip;
+ }
+#endif
+
random_device random;
num_ = random();
logPath_ = "/tmp/libcamera.worker.test." +
to_string(num_) + ".log";
proc_.finished.connect(this, &LogProcessTest::procFinished);
- return 0;
+ return TestPass;
}
int run()
@@ -7,7 +7,8 @@ log_test = [
foreach t : log_test
exe = executable(t[0], t[1],
- dependencies : libcamera_dep,
+ dependencies : [ libcamera_dep, libtest_dep ],
+ cpp_args : libtest_cpp_args,
link_with : test_libraries,
include_directories : test_includes_internal)
@@ -1,5 +1,14 @@
# SPDX-License-Identifier: CC0-1.0
+libtest_dep = []
+libtest_cpp_args = []
+
+libcap = dependency('libcap')
+if libcap.found()
+ libtest_dep += [ libcap ]
+ libtest_cpp_args += [ '-DHAVE_LIBLOG' ]
+endif
+
subdir('libtest')
subdir('camera')
@@ -46,7 +55,8 @@ internal_tests = [
foreach t : public_tests
exe = executable(t[0], t[1],
- dependencies : libcamera_dep,
+ dependencies : [ libcamera_dep, libtest_dep ],
+ cpp_args : libtest_cpp_args,
link_with : test_libraries,
include_directories : test_includes_public)
@@ -55,7 +65,8 @@ endforeach
foreach t : internal_tests
exe = executable(t[0], t[1],
- dependencies : libcamera_dep,
+ dependencies : [ libcamera_dep, libtest_dep ],
+ cpp_args : libtest_cpp_args,
link_with : test_libraries,
include_directories : test_includes_internal)
@@ -6,7 +6,8 @@ process_tests = [
foreach t : process_tests
exe = executable(t[0], t[1],
- dependencies : libcamera_dep,
+ dependencies : [ libcamera_dep, libtest_dep ],
+ cpp_args : libtest_cpp_args,
link_with : test_libraries,
include_directories : test_includes_internal)
@@ -41,6 +41,21 @@ public:
}
protected:
+ int init()
+ {
+#ifdef HAVE_LIBLOG
+ /* Check required permissions: CAP_SYS_ADMIN: unshare */
+ int ret = testCapabilities(1, CAP_SYS_ADMIN, CAP_EFFECTIVE);
+ if (ret != TestPass) {
+ char *cap = cap_to_name(CAP_SYS_ADMIN);
+ cerr << "Insufficient capability: " << cap << endl;
+ cap_free(cap);
+ return TestSkip;
+ }
+#endif
+ return TestPass;
+ }
+
int run()
{
EventDispatcher *dispatcher = Thread::current()->eventDispatcher();
While these tests may be executed as normal user at build time, unshare() call will fail and so are tests log_process and process_test. This change checks if one is granted with necessary capabilities so that we don't fail the build unexpectedly. Signed-off-by: You-Sheng Yang <vicamo.yang@canonical.com> --- test/libtest/meson.build | 3 ++- test/libtest/test.cpp | 32 +++++++++++++++++++++++++++++++- test/libtest/test.h | 6 ++++++ test/log/log_process.cpp | 13 ++++++++++++- test/log/meson.build | 3 ++- test/meson.build | 15 +++++++++++++-- test/process/meson.build | 3 ++- test/process/process_test.cpp | 15 +++++++++++++++ 8 files changed, 83 insertions(+), 7 deletions(-)