Fix libprivilege-control tests
[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 #include <tzplatform_config.h>
39
40 const uid_t APP_UID = tzplatform_getuid(TZ_USER_NAME);
41 const gid_t APP_GID = tzplatform_getgid(TZ_USER_NAME);
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
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 namespace DB {
149
150     class Transaction
151     {
152     public:
153
154         static int db_result;
155
156         Transaction() {
157             db_result = perm_begin();
158             RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
159                               "perm_begin returned: " << db_result);
160         }
161
162         ~Transaction() {
163             db_result = perm_end();
164         }
165     };
166 } // namespace DB
167
168 // Database Transaction macros
169 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
170 // They are used to prevent developer from forgetting to close transaction.
171 // Also note that variables defined between these macros will not be visible
172 // after DB_END.
173 #define DB_BEGIN                       \
174         {                              \
175         DB::Transaction db_transaction;
176
177 #define DB_END }                                                                   \
178         RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
179         "perm_end returned: " << DB::Transaction::db_result);
180
181 // Common macros and labels used in tests
182 extern const char *WGT_APP_ID;
183
184 #endif