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