Tests for new Security Server security_server_open_for function.
[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
33 int smack_runtime_check(void);
34 int smack_check(void);
35 int drop_root_privileges(void);
36
37 const uid_t APP_UID = 5000;
38 const gid_t APP_GID = 5000;
39
40 const uid_t DB_ALARM_UID = 6001;
41 const gid_t DB_ALARM_GID = 6001;
42
43 #define RUNNER_TEST_SMACK(Proc)                                                     \
44     void Proc();                                                                    \
45     static int Static##Proc##Init()                                                 \
46     {                                                                               \
47         if(smack_check())                                                           \
48             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
49                                                                                     \
50         return 0;                                                                   \
51     }                                                                               \
52     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
53     void Proc()
54
55 #define RUNNER_TEST_NOSMACK(Proc)                                                   \
56     void Proc();                                                                    \
57     static int Static##Proc##Init()                                                 \
58     {                                                                               \
59         if(!(smack_check()))                                                        \
60             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
61                                                                                     \
62         return 0;                                                                   \
63     }                                                                               \
64     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
65     void Proc()
66
67 #define RUNNER_CHILD_TEST_SMACK(Proc)                                                \
68     void Proc();                                                                     \
69     void Proc##Child();                                                              \
70     static int Static##Proc##Init()                                                  \
71     {                                                                                \
72         if(smack_check())                                                            \
73             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
74         return 0;                                                                    \
75     }                                                                                \
76     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
77     void Proc(){                                                                     \
78             DPL::Test::RunChildProc(&Proc##Child);                                   \
79     }                                                                                \
80     void Proc##Child()
81
82 #define RUNNER_CHILD_TEST_NOSMACK(Proc)                                              \
83     void Proc();                                                                     \
84     void Proc##Child();                                                              \
85     static int Static##Proc##Init()                                                  \
86     {                                                                                \
87         if(!(smack_check()))                                                         \
88             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
89         return 0;                                                                    \
90     }                                                                                \
91     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
92     void Proc(){                                                                     \
93             DPL::Test::RunChildProc(&Proc##Child);                                   \
94     }                                                                                \
95     void Proc##Child()
96
97 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc)                                         \
98     void Proc();                                                                     \
99     void Proc##Multi();                                                              \
100     static int Static##Proc##Init()                                                  \
101     {                                                                                \
102         if(smack_check())                                                            \
103             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
104         return 0;                                                                    \
105     }                                                                                \
106     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
107     void Proc(){                                                                     \
108             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
109     }                                                                                \
110     void Proc##Multi()
111
112 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc)                                       \
113     void Proc();                                                                     \
114     void Proc##Multi();                                                              \
115     static int Static##Proc##Init()                                                  \
116     {                                                                                \
117         if(!(smack_check()))                                                         \
118             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
119         return 0;                                                                    \
120     }                                                                                \
121     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
122     void Proc(){                                                                     \
123             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
124     }                                                                                \
125     void Proc##Multi()
126
127 namespace DB {
128
129     class Transaction
130     {
131     public:
132
133         static int db_result;
134
135         Transaction() {
136             db_result = perm_begin();
137             RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
138                               "perm_begin returned: " << db_result);
139         }
140
141         ~Transaction() {
142             db_result = perm_end();
143         }
144     };
145 } // namespace DB
146
147 // Database Transaction macros
148 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
149 // They are used to prevent developer from forgetting to close transaction.
150 // Also note that variables defined between these macros will not be visible
151 // after DB_END.
152 #define DB_BEGIN                       \
153         {                              \
154         DB::Transaction db_transaction;
155
156 #define DB_END }                                                                   \
157         RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
158         "perm_end returned: " << DB::Transaction::db_result);
159
160 // Common typedefs
161 typedef std::unique_ptr<smack_accesses,std::function<void (smack_accesses*)> > SmackUniquePtr;
162
163 // Common macros and labels used in tests
164 extern const char *WGT_APP_ID;
165
166 #endif