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