Merge branch 'nether' into tizen
[platform/core/test/security-tests.git] / src / common / tests_common.h
index 2fb249b..bfb46d9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2013 - 2019 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.
 #define _TESTS_COMMON_H_
 
 #include <sys/smack.h>
+#include <dpl/test/test_case_extended.h>
 #include <dpl/test/test_runner.h>
 #include <dpl/test/test_runner_child.h>
 #include <dpl/test/test_runner_multiprocess.h>
-#include <privilege-control.h>
-#include <sys/smack.h>
+
+#include <algorithm>
+#include <sys/types.h>
 #include <string>
+#include <tuple>
 #include <errno.h>
 #include <string.h>
 
-const uid_t APP_UID = 5000;
-const gid_t APP_GID = 5000;
+const uid_t APP_UID     = 5001;
+const gid_t APP_GID     = 100;
+const uid_t APP_UID_2   = 5200;
+const gid_t APP_GID_2   = 5200;
 const uid_t DB_ALARM_UID = 6001;
 const gid_t DB_ALARM_GID = 6001;
 const std::string TMP_DIR("/tmp");
 
 bool smack_check(void);
 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
-void setLabelForSelf(const int line, const char *label);
 void add_process_group(const char* group_name);
 void remove_process_group(const char* group_name);
 std::string formatCstr(const char *cstr);
 int files_compare(int fd1, int fd2);
-
-#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()
-
-#define RUNNER_MULTIPROCESS_TEST_SMACK(Proc)                                         \
-    void Proc();                                                                     \
-    void Proc##Multi();                                                              \
-    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::RunMultiProc(&Proc##Multi);                                   \
-    }                                                                                \
-    void Proc##Multi()
-
-#define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc)                                       \
-    void Proc();                                                                     \
-    void Proc##Multi();                                                              \
-    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::RunMultiProc(&Proc##Multi);                                   \
-    }                                                                                \
-    void Proc##Multi()
-
-namespace DB {
-
-    class Transaction
-    {
-    public:
-
-        static int db_result;
-
-        Transaction() {
-            db_result = perm_begin();
-            RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
-                              "perm_begin returned: " << db_result);
-        }
-
-        ~Transaction() {
-            db_result = perm_end();
-        }
-    };
-} // namespace DB
-
-// Database Transaction macros
-// PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
-// They are used to prevent developer from forgetting to close transaction.
-// Also note that variables defined between these macros will not be visible
-// after DB_END.
-#define DB_BEGIN                       \
-        {                              \
-        DB::Transaction db_transaction;
-
-#define DB_END }                                                                   \
-        RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
-        "perm_end returned: " << DB::Transaction::db_result);
-
-// Common macros and labels used in tests
-extern const char *WGT_APP_ID;
+void mkdirSafe(const std::string &path, mode_t mode);
+void mktreeSafe(const std::string &path, mode_t mode);
+void creatSafe(const std::string &path, mode_t mode);
+void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
+void removeDir(const std::string &path);
+void waitPid(pid_t pid);
+
+pid_t runInChild(const std::function<void(void)> &process);
+void runInChildParentWait(const std::function<void(void)> &process);
+
+#define RUNNER_TEST_SMACK(Proc, ...)                                                        \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
+    static int Static##Proc##Init()                                                         \
+    {                                                                                       \
+        if (smack_check())                                                                  \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
+                new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
+        return 0;                                                                           \
+    }                                                                                       \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
+
+#define RUNNER_TEST_NOSMACK(Proc, ...)                                                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
+    static int Static##Proc##Init()                                                         \
+    {                                                                                       \
+        if (!smack_check())                                                                 \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
+                new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
+        return 0;                                                                           \
+    }                                                                                       \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
+
+#define RUNNER_CHILD_TEST_SMACK(Proc, ...)                                                  \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
+    void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
+    static int Static##Proc##Init()                                                         \
+    {                                                                                       \
+        if (smack_check())                                                                  \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
+                new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
+        return 0;                                                                           \
+    }                                                                                       \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
+        DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
+    }                                                                                       \
+    void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
+
+#define RUNNER_CHILD_TEST_NOSMACK(Proc, ...)                                                \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
+    void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
+    static int Static##Proc##Init()                                                         \
+    {                                                                                       \
+        if (!smack_check())                                                                 \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
+                new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
+        return 0;                                                                           \
+    }                                                                                       \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
+        DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
+    }                                                                                       \
+    void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
+
+#define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...)                                           \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
+    void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
+    static int Static##Proc##Init()                                                         \
+    {                                                                                       \
+        if (smack_check())                                                                  \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
+                new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
+        return 0;                                                                           \
+    }                                                                                       \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
+        DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
+    }                                                                                       \
+    void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
+
+#define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...)                                         \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
+    void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
+    static int Static##Proc##Init()                                                         \
+    {                                                                                       \
+        if (!smack_check())                                                                 \
+            DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
+                new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
+        return 0;                                                                           \
+    }                                                                                       \
+    const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
+    void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
+        DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
+    }                                                                                       \
+    void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
 
 #endif