2 * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file tests_common.h
19 * @author Lukasz Kostyra (l.kostyra@partner.samsung.com)
21 * @brief Common functions and macros used in security-tests package.
24 #ifndef _TESTS_COMMON_H_
25 #define _TESTS_COMMON_H_
27 #include <sys/smack.h>
28 #include <dpl/test/test_case_extended.h>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/test/test_runner_child.h>
31 #include <dpl/test/test_runner_multiprocess.h>
32 #include <privilege-control.h>
33 #include <sys/smack.h>
39 const uid_t APP_UID = 5000;
40 const gid_t APP_GID = 5000;
41 const uid_t DB_ALARM_UID = 6001;
42 const gid_t DB_ALARM_GID = 6001;
43 const std::string TMP_DIR("/tmp");
45 bool smack_check(void);
46 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
47 void setLabelForSelf(const int line, const char *label);
48 void add_process_group(const char* group_name);
49 void remove_process_group(const char* group_name);
50 std::string formatCstr(const char *cstr);
51 int files_compare(int fd1, int fd2);
52 void mkdirSafe(const std::string &path, mode_t mode);
53 void mktreeSafe(const std::string &path, mode_t mode);
54 void creatSafe(const std::string &path, mode_t mode);
55 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
56 void removeDir(const std::string &path);
59 #define RUNNER_TEST_SMACK(Proc, ...) \
60 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
61 static int Static##Proc##Init() \
64 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
65 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
68 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
69 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
71 #define RUNNER_TEST_NOSMACK(Proc, ...) \
72 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
73 static int Static##Proc##Init() \
76 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
77 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
80 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
81 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
83 #define RUNNER_CHILD_TEST_SMACK(Proc, ...) \
84 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
85 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
86 static int Static##Proc##Init() \
89 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
90 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
93 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
94 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
95 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
97 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
99 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...) \
100 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
101 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
102 static int Static##Proc##Init() \
104 if (!smack_check()) \
105 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
106 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
109 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
110 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
111 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
113 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
115 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...) \
116 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
117 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
118 static int Static##Proc##Init() \
121 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
122 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
125 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
126 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
127 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
129 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
131 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...) \
132 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
133 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
134 static int Static##Proc##Init() \
136 if (!smack_check()) \
137 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
138 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
141 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
142 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
143 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
145 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
153 static int db_result;
156 db_result = perm_begin();
157 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
158 "perm_begin returned: " << db_result);
162 db_result = perm_end();
167 // Database Transaction macros
168 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
169 // They are used to prevent developer from forgetting to close transaction.
170 // Also note that variables defined between these macros will not be visible
174 DB::Transaction db_transaction;
177 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result, \
178 "perm_end returned: " << DB::Transaction::db_result);
180 // Common macros and labels used in tests
181 extern const char *WGT_APP_ID;