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