Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / 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 // separate DB-space for async tests
38 // gives independence: other test sets can modify the password per will
39 // without risk of making async tests fail
40 const uid_t APP_UID = 5003;
41 const gid_t APP_GID = 5003;
42 const uid_t DB_ALARM_UID = 6001;
43 const gid_t DB_ALARM_GID = 6001;
44 const std::string TMP_DIR("/tmp");
45
46 int smack_runtime_check(void);
47 int smack_check(void);
48 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
49 void setLabelForSelf(const int line, const char *label);
50 void add_process_group(const char* group_name);
51 void remove_process_group(const char* group_name);
52 std::string formatCstr(const char *cstr);
53 int files_compare(int fd1, int fd2);
54
55 #define RUNNER_TEST_SMACK(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_TEST_NOSMACK(Proc)                                                   \
68     void Proc();                                                                    \
69     static int Static##Proc##Init()                                                 \
70     {                                                                               \
71         if(!(smack_check()))                                                        \
72             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
73                                                                                     \
74         return 0;                                                                   \
75     }                                                                               \
76     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
77     void Proc()
78
79 #define RUNNER_CHILD_TEST_SMACK(Proc)                                                \
80     void Proc();                                                                     \
81     void Proc##Child();                                                              \
82     static int Static##Proc##Init()                                                  \
83     {                                                                                \
84         if(smack_check())                                                            \
85             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
86         return 0;                                                                    \
87     }                                                                                \
88     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
89     void Proc(){                                                                     \
90             DPL::Test::RunChildProc(&Proc##Child);                                   \
91     }                                                                                \
92     void Proc##Child()
93
94 #define RUNNER_CHILD_TEST_NOSMACK(Proc)                                              \
95     void Proc();                                                                     \
96     void Proc##Child();                                                              \
97     static int Static##Proc##Init()                                                  \
98     {                                                                                \
99         if(!(smack_check()))                                                         \
100             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
101         return 0;                                                                    \
102     }                                                                                \
103     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
104     void Proc(){                                                                     \
105             DPL::Test::RunChildProc(&Proc##Child);                                   \
106     }                                                                                \
107     void Proc##Child()
108
109 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc)                                         \
110     void Proc();                                                                     \
111     void Proc##Multi();                                                              \
112     static int Static##Proc##Init()                                                  \
113     {                                                                                \
114         if(smack_check())                                                            \
115             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
116         return 0;                                                                    \
117     }                                                                                \
118     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
119     void Proc(){                                                                     \
120             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
121     }                                                                                \
122     void Proc##Multi()
123
124 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc)                                       \
125     void Proc();                                                                     \
126     void Proc##Multi();                                                              \
127     static int Static##Proc##Init()                                                  \
128     {                                                                                \
129         if(!(smack_check()))                                                         \
130             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
131         return 0;                                                                    \
132     }                                                                                \
133     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
134     void Proc(){                                                                     \
135             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
136     }                                                                                \
137     void Proc##Multi()
138
139 namespace DB {
140
141     class Transaction
142     {
143     public:
144
145         static int db_result;
146
147         Transaction() {
148             db_result = perm_begin();
149             RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
150                               "perm_begin returned: " << db_result);
151         }
152
153         ~Transaction() {
154             db_result = perm_end();
155         }
156     };
157 } // namespace DB
158
159 // Database Transaction macros
160 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
161 // They are used to prevent developer from forgetting to close transaction.
162 // Also note that variables defined between these macros will not be visible
163 // after DB_END.
164 #define DB_BEGIN                       \
165         {                              \
166         DB::Transaction db_transaction;
167
168 #define DB_END }                                                                   \
169         RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
170         "perm_end returned: " << DB::Transaction::db_result);
171
172 // Common macros and labels used in tests
173 extern const char *WGT_APP_ID;
174
175 #endif