Fix multiple definitions of runInChild
[platform/core/test/security-tests.git] / src / common / tests_common.h
1 /*
2  * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 /*
18  * @file        tests_common.h
19  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
20  * @version     1.0
21  * @brief       Common functions and macros used in security-tests package.
22  */
23
24 #ifndef _TESTS_COMMON_H_
25 #define _TESTS_COMMON_H_
26
27 #include <sys/smack.h>
28 #include <dpl/test/test_case_extended.h>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/test/test_runner_child.h>
31 #include <dpl/test/test_runner_multiprocess.h>
32
33 #include <algorithm>
34 #include <sys/types.h>
35 #include <string>
36 #include <tuple>
37 #include <errno.h>
38 #include <string.h>
39
40 const uid_t APP_UID     = 5000;
41 const gid_t APP_GID     = 5000;
42 const uid_t APP_UID_2   = 5200;
43 const gid_t APP_GID_2   = 5200;
44 const uid_t DB_ALARM_UID = 6001;
45 const gid_t DB_ALARM_GID = 6001;
46 const std::string TMP_DIR("/tmp");
47
48 bool smack_check(void);
49 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
50 void setLabelForSelf(const int line, const char *label);
51 void add_process_group(const char* group_name);
52 void remove_process_group(const char* group_name);
53 std::string formatCstr(const char *cstr);
54 int files_compare(int fd1, int fd2);
55 void mkdirSafe(const std::string &path, mode_t mode);
56 void mktreeSafe(const std::string &path, mode_t mode);
57 void creatSafe(const std::string &path, mode_t mode);
58 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
59 void removeDir(const std::string &path);
60 void waitPid(pid_t pid);
61 void change_label(const char* label);
62
63 pid_t runInChild(const std::function<void(void)> &process);
64 void runInChildParentWait(const std::function<void(void)> &process);
65
66 #define RUNNER_TEST_SMACK(Proc, ...)                                                        \
67     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
68     static int Static##Proc##Init()                                                         \
69     {                                                                                       \
70         if (smack_check())                                                                  \
71             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
72                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
73         return 0;                                                                           \
74     }                                                                                       \
75     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
76     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
77
78 #define RUNNER_TEST_NOSMACK(Proc, ...)                                                      \
79     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
80     static int Static##Proc##Init()                                                         \
81     {                                                                                       \
82         if (!smack_check())                                                                 \
83             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
84                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
85         return 0;                                                                           \
86     }                                                                                       \
87     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
88     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
89
90 #define RUNNER_CHILD_TEST_SMACK(Proc, ...)                                                  \
91     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
92     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
93     static int Static##Proc##Init()                                                         \
94     {                                                                                       \
95         if (smack_check())                                                                  \
96             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
97                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
98         return 0;                                                                           \
99     }                                                                                       \
100     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
101     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
102         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
103     }                                                                                       \
104     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
105
106 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...)                                                \
107     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
108     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
109     static int Static##Proc##Init()                                                         \
110     {                                                                                       \
111         if (!smack_check())                                                                 \
112             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
113                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
114         return 0;                                                                           \
115     }                                                                                       \
116     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
117     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
118         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
119     }                                                                                       \
120     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
121
122 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...)                                           \
123     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
124     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
125     static int Static##Proc##Init()                                                         \
126     {                                                                                       \
127         if (smack_check())                                                                  \
128             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
129                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
130         return 0;                                                                           \
131     }                                                                                       \
132     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
133     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
134         DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
135     }                                                                                       \
136     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
137
138 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...)                                         \
139     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
140     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
141     static int Static##Proc##Init()                                                         \
142     {                                                                                       \
143         if (!smack_check())                                                                 \
144             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
145                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
146         return 0;                                                                           \
147     }                                                                                       \
148     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
149     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
150         DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
151     }                                                                                       \
152     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
153
154 #endif