Merge branch 'cynara' into tizen
[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 #include <sys/smack.h>
33 #include <string>
34 #include <tuple>
35 #include <errno.h>
36 #include <string.h>
37
38 const uid_t APP_UID     = 5000;
39 const gid_t APP_GID     = 5000;
40 const uid_t APP_UID_2   = 5200;
41 const gid_t APP_GID_2   = 5200;
42 const uid_t DB_ALARM_UID = 6001;
43 const gid_t DB_ALARM_GID = 6001;
44 const std::string TMP_DIR("/tmp");
45
46 bool smack_check(void);
47 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
48 void setLabelForSelf(const int line, const char *label);
49 void add_process_group(const char* group_name);
50 void remove_process_group(const char* group_name);
51 std::string formatCstr(const char *cstr);
52 int files_compare(int fd1, int fd2);
53 void mkdirSafe(const std::string &path, mode_t mode);
54 void mktreeSafe(const std::string &path, mode_t mode);
55 void creatSafe(const std::string &path, mode_t mode);
56 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
57 void removeDir(const std::string &path);
58 void change_label(const char* label);
59
60 #define RUNNER_TEST_SMACK(Proc, ...)                                                        \
61     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
62     static int Static##Proc##Init()                                                         \
63     {                                                                                       \
64         if (smack_check())                                                                  \
65             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
66                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
67         return 0;                                                                           \
68     }                                                                                       \
69     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
70     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
71
72 #define RUNNER_TEST_NOSMACK(Proc, ...)                                                      \
73     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
74     static int Static##Proc##Init()                                                         \
75     {                                                                                       \
76         if (!smack_check())                                                                 \
77             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
78                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
79         return 0;                                                                           \
80     }                                                                                       \
81     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
82     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
83
84 #define RUNNER_CHILD_TEST_SMACK(Proc, ...)                                                  \
85     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
86     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
87     static int Static##Proc##Init()                                                         \
88     {                                                                                       \
89         if (smack_check())                                                                  \
90             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
91                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
92         return 0;                                                                           \
93     }                                                                                       \
94     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
95     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
96         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
97     }                                                                                       \
98     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
99
100 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...)                                                \
101     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
102     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
103     static int Static##Proc##Init()                                                         \
104     {                                                                                       \
105         if (!smack_check())                                                                 \
106             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
107                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
108         return 0;                                                                           \
109     }                                                                                       \
110     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
111     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
112         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
113     }                                                                                       \
114     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
115
116 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...)                                           \
117     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
118     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
119     static int Static##Proc##Init()                                                         \
120     {                                                                                       \
121         if (smack_check())                                                                  \
122             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
123                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
124         return 0;                                                                           \
125     }                                                                                       \
126     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
127     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
128         DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
129     }                                                                                       \
130     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
131
132 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...)                                         \
133     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
134     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
135     static int Static##Proc##Init()                                                         \
136     {                                                                                       \
137         if (!smack_check())                                                                 \
138             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
139                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
140         return 0;                                                                           \
141     }                                                                                       \
142     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
143     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
144         DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
145     }                                                                                       \
146     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
147
148 #endif