Add test case macros for different environments
authorLukasz Kostyra <l.kostyra@partner.samsung.com>
Fri, 19 Jul 2013 09:51:51 +0000 (11:51 +0200)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 23 Jan 2014 14:07:35 +0000 (15:07 +0100)
[Issue#]        SSDWSSP-415
[Feature]       Adds test case macros - RUNNER_TEST_SMACK, RUNNER_TEST_NOSMACK,
                RUNNER_CHILD_TEST_SMACK and RUNNER_CHILD_TEST_NOSMACK for different testing
                environments.
[Cause]         Tests that fail when launched on NOSMACK environment
[Solution]      Create macros that will launch tests only on SMACK or NOSMACK environment.
[Verification]  Run libprivilege-control-test. Tests 24a and 24c should appear on SMACK environment,
                tests 24b and 24d should appear on NOSMACK environment.

Change-Id: I2cb9a5bd6aa9634e019726eb536744f3c9e43288

CMakeLists.txt
tests/CMakeLists.txt
tests/common/CMakeLists.txt [new file with mode: 0644]
tests/common/tests_common.cpp [new file with mode: 0644]
tests/common/tests_common.h [new file with mode: 0644]
tests/libprivilege-control-tests/CMakeLists.txt
tests/libprivilege-control-tests/test_cases.cpp

index ce54dbe..4003ffd 100644 (file)
@@ -42,7 +42,7 @@ SET(CMAKE_CXX_FLAGS_DEBUG      "-O0 -ggdb")
 SET(CMAKE_C_FLAGS_RELEASE      "-O2")
 SET(CMAKE_CXX_FLAGS_RELEASE    "-O2")
 
-#SET(SMACK_ENABLE ON)
+SET(SMACK_ENABLE ON)
 
 OPTION(DPL_LOG "DPL logs status" ON)
 IF(DPL_LOG)
index a4bba5f..9aba980 100644 (file)
@@ -48,6 +48,7 @@ INSTALL(FILES
                 WORLD_EXECUTE
     )
 
+ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(libprivilege-control-tests)
 ADD_SUBDIRECTORY(libsmack-tests)
 ADD_SUBDIRECTORY(security-server-tests)
diff --git a/tests/common/CMakeLists.txt b/tests/common/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7665d38
--- /dev/null
@@ -0,0 +1,22 @@
+INCLUDE(FindPkgConfig)
+SET(COMMON_TARGET_TEST "tests-common")
+
+#dependencies
+PKG_CHECK_MODULES(COMMON_TARGET_DEP
+    libsmack
+    dpl-test-efl
+    REQUIRED
+    )
+
+#files to compile
+SET(COMMON_TARGET_TEST_SOURCES
+    ${PROJECT_SOURCE_DIR}/tests/common/tests_common.cpp
+    )
+
+#header directories
+INCLUDE_DIRECTORIES(
+    ${COMMON_TARGET_DEP_INCLUDE_DIRS}
+    )
+
+#output OBJECT format
+ADD_LIBRARY(${COMMON_TARGET_TEST} ${COMMON_TARGET_TEST_SOURCES})
diff --git a/tests/common/tests_common.cpp b/tests/common/tests_common.cpp
new file mode 100644 (file)
index 0000000..34e7da5
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+/*
+ * @file        tests_common.cpp
+ * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
+ * @version     1.0
+ * @brief       Common functions and macros used in security-tests package.
+ */
+
+#include "tests_common.h"
+
+int smack_runtime_check(void)
+{
+    static int smack_present = -1;
+    if (-1 == smack_present) {
+        if (smack_smackfs_path()) {
+            smack_present = 1;
+        } else {
+            smack_present = 0;
+        }
+    }
+    return smack_present;
+}
+
+int smack_check(void)
+{
+#ifndef WRT_SMACK_ENABLED
+    return 0;
+#else
+    return smack_runtime_check();
+#endif
+}
diff --git a/tests/common/tests_common.h b/tests/common/tests_common.h
new file mode 100644 (file)
index 0000000..5073c3f
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+/*
+ * @file        tests_common.h
+ * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
+ * @version     1.0
+ * @brief       Common functions and macros used in security-tests package.
+ */
+
+#ifndef _TESTS_COMMON_H_
+#define _TESTS_COMMON_H_
+
+#include <sys/smack.h>
+#include <dpl/test/test_runner.h>
+#include <dpl/test/test_runner_child.h>
+
+int smack_runtime_check(void);
+int smack_check(void);
+
+#define RUNNER_TEST_SMACK(Proc)                                                     \
+    void Proc();                                                                    \
+    static int Static##Proc##Init()                                                 \
+    {                                                                               \
+        if(smack_check())                                                           \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
+                                                                                    \
+        return 0;                                                                   \
+    }                                                                               \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
+    void Proc()
+
+#define RUNNER_TEST_NOSMACK(Proc)                                                   \
+    void Proc();                                                                    \
+    static int Static##Proc##Init()                                                 \
+    {                                                                               \
+        if(!(smack_check()))                                                        \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
+                                                                                    \
+        return 0;                                                                   \
+    }                                                                               \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
+    void Proc()
+
+#define RUNNER_CHILD_TEST_SMACK(Proc)                                                \
+    void Proc();                                                                     \
+    void Proc##Child();                                                              \
+    static int Static##Proc##Init()                                                  \
+    {                                                                                \
+        if(smack_check())                                                            \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
+        return 0;                                                                    \
+    }                                                                                \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
+    void Proc(){                                                                     \
+            DPL::Test::RunChildProc(&Proc##Child);                                   \
+    }                                                                                \
+    void Proc##Child()
+
+#define RUNNER_CHILD_TEST_NOSMACK(Proc)                                              \
+    void Proc();                                                                     \
+    void Proc##Child();                                                              \
+    static int Static##Proc##Init()                                                  \
+    {                                                                                \
+        if(!(smack_check()))                                                         \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
+        return 0;                                                                    \
+    }                                                                                \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
+    void Proc(){                                                                     \
+            DPL::Test::RunChildProc(&Proc##Child);                                   \
+    }                                                                                \
+    void Proc##Child()
+
+#endif
index 3716eed..c382fc1 100644 (file)
@@ -38,6 +38,7 @@ SET(LPC_TARGET_TEST_SOURCES
 #header directories
 INCLUDE_DIRECTORIES(
     ${LPC_TARGET_DEP_INCLUDE_DIRS}
+    ${PROJECT_SOURCE_DIR}/tests/common/
     )
 
 #preprocessor definitions
@@ -49,6 +50,7 @@ ADD_EXECUTABLE(${LPC_TARGET_TEST} ${LPC_TARGET_TEST_SOURCES})
 #linker directories
 TARGET_LINK_LIBRARIES(${LPC_TARGET_TEST}
     ${LPC_TARGET_DEP_LIBRARIES}
+    tests-common
     )
 
 #place for output file
@@ -154,4 +156,4 @@ INSTALL(FILES
 INSTALL(FILES
     ${PROJECT_SOURCE_DIR}/tests/libprivilege-control-tests/EFL_test_privilege_control_rules_efl.smack
     DESTINATION /usr/share/privilege-control/
-  )
\ No newline at end of file
+  )
index 237fa4a..2aef3b8 100644 (file)
@@ -47,6 +47,7 @@
 #include <fstream>
 #include <sstream>
 #include <sys/stat.h>
+#include <tests_common.h>
 
 #define SMACK_RULES_DIR          "/opt/etc/smack-app/accesses.d/"
 #define SMACK_STARTUP_RULES_FILE "/opt/etc/smack-app-early/accesses.d/rules"
@@ -2565,3 +2566,25 @@ RUNNER_TEST(privilege_control20_early_rules)
        RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
        RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " found");
 }
+
+RUNNER_TEST_SMACK(privilege_control24a_test_whether_macro_works_smack)
+{
+    RUNNER_IGNORED_MSG("STUB: I should be called on SMACK environment only.");
+}
+
+RUNNER_TEST_NOSMACK(privilege_control24b_test_whether_macro_works_nosmack)
+{
+    RUNNER_IGNORED_MSG("STUB: I should be called on NOSMACK environment only.");
+}
+
+RUNNER_CHILD_TEST_SMACK(privilege_control24c_test_whether_macro_works_child_smack)
+{
+       //Until a fix for RUNNER_IGNORED_MSG comes up to wrt-commons, this must be commented out.
+       //RUNNER_IGNORED_MSG("STUB: I should be called on SMACK environment only.");
+}
+
+RUNNER_CHILD_TEST_NOSMACK(privilege_control24d_test_whether_macro_works_child_nosmack)
+{
+       //Until a fix for RUNNER_IGNORED_MSG comes up to wrt-commons, this must be commented out.
+       //RUNNER_IGNORED_MSG("STUB: I should be called on NOSMACK environment only.");
+}