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