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