From patchwork Sat Aug 15 15:10:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: You-Sheng Yang X-Patchwork-Id: 9330 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id DB6EDBD87C for ; Sat, 15 Aug 2020 15:10:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7E3CA617BD; Sat, 15 Aug 2020 17:10:43 +0200 (CEST) Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7977060386 for ; Sat, 15 Aug 2020 17:10:42 +0200 (CEST) Received: by mail-pg1-f174.google.com with SMTP id h12so5902983pgm.7 for ; Sat, 15 Aug 2020 08:10:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=DPAtEG4MbvUZOCqGMhxRyy/PvkJM4FtI6lBz8Ixu6P4=; b=rUuUMRbVC3G2gooDRKkvdUkVxgs0QQX4CvSWz9tOVIu+E3NRdX7vB5FzG2EYwZmvr4 yoIrWyXPrEOByfRgdyOssoBcaKpEZYdXhMOr2QOk6h0UfHvZfwgeHk6z0yau5tA6dxQE 22yG8AU4KEXafWQuB2R2lAFWm0BjTdmFg2S1KkGstokX56KhkU/r7xBytl30gvtgc6ZR BlyGCVhPcmGe6pLnwmH+0p7pyFkW3WiCx1TvaKRcYH6Vf9fQ/mCl40Yo2oxsaRC1SXds lrqIE8x/V22AweX5gUjoVAgEdE/2Dt9VbW3YGn2ASg+t1iE8fP/SVPVqhNHEj/Gfwxbk wdpg== X-Gm-Message-State: AOAM532aLqn8dIreh0PJX7INISCjLNpWFyxK1+SI79DLDtp2DgH03UMk 0G9haGKjCUiJMqQZRBczzsnEsTxnIC3tjA== X-Google-Smtp-Source: ABdhPJwlWisknB4A/zv/Gk+YhlevMvp7T+YfHEmX6NdRl/qhIQJMJXmtkgeEtLXflcuQrc23IUap7g== X-Received: by 2002:a63:e24d:: with SMTP id y13mr5026290pgj.248.1597504239854; Sat, 15 Aug 2020 08:10:39 -0700 (PDT) Received: from localhost (36-228-82-217.dynamic-ip.hinet.net. [36.228.82.217]) by smtp.gmail.com with ESMTPSA id q5sm10755165pgv.1.2020.08.15.08.10.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Aug 2020 08:10:38 -0700 (PDT) From: You-Sheng Yang To: libcamera-devel@lists.libcamera.org Date: Sat, 15 Aug 2020 23:10:36 +0800 Message-Id: <20200815151036.509376-1-vicamo.yang@canonical.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3] test: log/process: check CAP_SYS_ADMIN in test init X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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. v2: - add a shared capability testing util func - drop merge process kill(-1) patch - drop patch for removed list-camera test v3: - mark libcap dependency as optional Signed-off-by: You-Sheng Yang --- 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(-) diff --git a/test/libtest/meson.build b/test/libtest/meson.build index 542335e..9e7c603 100644 --- a/test/libtest/meson.build +++ b/test/libtest/meson.build @@ -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] diff --git a/test/libtest/test.cpp b/test/libtest/test.cpp index fd9f3d7..564e0bc 100644 --- a/test/libtest/test.cpp +++ b/test/libtest/test.cpp @@ -5,10 +5,40 @@ * test.cpp - libcamera test base class */ -#include +#include #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() { } diff --git a/test/libtest/test.h b/test/libtest/test.h index 26d4b94..da64179 100644 --- a/test/libtest/test.h +++ b/test/libtest/test.h @@ -7,6 +7,10 @@ #ifndef __TEST_TEST_H__ #define __TEST_TEST_H__ +#ifdef HAVE_LIBLOG +#include +#endif + #include enum TestStatus { @@ -23,6 +27,8 @@ public: int execute(); + static TestStatus testCapabilities(int nCapabilities, ...); + protected: virtual int init() { return 0; } virtual int run() = 0; diff --git a/test/log/log_process.cpp b/test/log/log_process.cpp index d46d5e3..0db7e20 100644 --- a/test/log/log_process.cpp +++ b/test/log/log_process.cpp @@ -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() diff --git a/test/log/meson.build b/test/log/meson.build index 8cd664e..d4745a1 100644 --- a/test/log/meson.build +++ b/test/log/meson.build @@ -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) diff --git a/test/meson.build b/test/meson.build index 0a1d434..ea48b32 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,5 +1,14 @@ # SPDX-License-Identifier: CC0-1.0 +libtest_dep = [] +libtest_cpp_args = [] + +libcap = dependency('libcap', required : false) +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) diff --git a/test/process/meson.build b/test/process/meson.build index c215fa7..c141042 100644 --- a/test/process/meson.build +++ b/test/process/meson.build @@ -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) diff --git a/test/process/process_test.cpp b/test/process/process_test.cpp index 721a7c9..6386826 100644 --- a/test/process/process_test.cpp +++ b/test/process/process_test.cpp @@ -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();