Merge branch 'tizen' into cynara
[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 <privilege-control.h>
33 #include <sys/smack.h>
34 #include <string>
35 #include <tuple>
36 #include <errno.h>
37 #include <string.h>
38
39 const uid_t APP_UID     = 5000;
40 const gid_t APP_GID     = 5000;
41 const uid_t APP_UID_2   = 5200;
42 const gid_t APP_GID_2   = 5200;
43 const uid_t DB_ALARM_UID = 6001;
44 const gid_t DB_ALARM_GID = 6001;
45 const std::string TMP_DIR("/tmp");
46
47 bool smack_check(void);
48 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
49 void setLabelForSelf(const int line, const char *label);
50 void add_process_group(const char* group_name);
51 void remove_process_group(const char* group_name);
52 std::string formatCstr(const char *cstr);
53 int files_compare(int fd1, int fd2);
54 void mkdirSafe(const std::string &path, mode_t mode);
55 void mktreeSafe(const std::string &path, mode_t mode);
56 void creatSafe(const std::string &path, mode_t mode);
57 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
58 void removeDir(const std::string &path);
59
60
61 #define RUNNER_TEST_SMACK(Proc, ...)                                                        \
62     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
63     static int Static##Proc##Init()                                                         \
64     {                                                                                       \
65         if (smack_check())                                                                  \
66             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
67                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
68         return 0;                                                                           \
69     }                                                                                       \
70     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
71     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
72
73 #define RUNNER_TEST_NOSMACK(Proc, ...)                                                      \
74     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
75     static int Static##Proc##Init()                                                         \
76     {                                                                                       \
77         if (!smack_check())                                                                 \
78             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
79                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
80         return 0;                                                                           \
81     }                                                                                       \
82     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
83     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
84
85 #define RUNNER_CHILD_TEST_SMACK(Proc, ...)                                                  \
86     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
87     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
88     static int Static##Proc##Init()                                                         \
89     {                                                                                       \
90         if (smack_check())                                                                  \
91             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
92                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
93         return 0;                                                                           \
94     }                                                                                       \
95     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
96     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
97         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
98     }                                                                                       \
99     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
100
101 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...)                                                \
102     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
103     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
104     static int Static##Proc##Init()                                                         \
105     {                                                                                       \
106         if (!smack_check())                                                                 \
107             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
108                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
109         return 0;                                                                           \
110     }                                                                                       \
111     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
112     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
113         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));                 \
114     }                                                                                       \
115     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
116
117 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...)                                           \
118     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
119     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
120     static int Static##Proc##Init()                                                         \
121     {                                                                                       \
122         if (smack_check())                                                                  \
123             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
124                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
125         return 0;                                                                           \
126     }                                                                                       \
127     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
128     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
129         DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
130     }                                                                                       \
131     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
132
133 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...)                                         \
134     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
135     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple);                           \
136     static int Static##Proc##Init()                                                         \
137     {                                                                                       \
138         if (!smack_check())                                                                 \
139             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
140                 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
141         return 0;                                                                           \
142     }                                                                                       \
143     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                      \
144     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                                 \
145         DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple));                 \
146     }                                                                                       \
147     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
148
149 namespace DB {
150
151     class Transaction
152     {
153     public:
154
155         static int db_result;
156
157         Transaction() {
158             db_result = perm_begin();
159             RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
160                               "perm_begin returned: " << db_result);
161         }
162
163         ~Transaction() {
164             db_result = perm_end();
165         }
166     };
167 } // namespace DB
168
169 // Database Transaction macros
170 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
171 // They are used to prevent developer from forgetting to close transaction.
172 // Also note that variables defined between these macros will not be visible
173 // after DB_END.
174 #define DB_BEGIN                       \
175         {                              \
176         DB::Transaction db_transaction;
177
178 #define DB_END }                                                                   \
179         RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
180         "perm_end returned: " << DB::Transaction::db_result);
181
182 // Common macros and labels used in tests
183 extern const char *WGT_APP_ID;
184
185 #endif