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 <sys/smack.h>
33 #include <sys/types.h>
39 const uid_t APP_UID = 5000;
40 const gid_t APP_GID = 5000;
41 const uid_t APP_UID_2 = 5200;
42 const gid_t APP_GID_2 = 5200;
43 const uid_t DB_ALARM_UID = 6001;
44 const gid_t DB_ALARM_GID = 6001;
45 const std::string TMP_DIR("/tmp");
47 bool 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 void mkdirSafe(const std::string &path, mode_t mode);
55 void mktreeSafe(const std::string &path, mode_t mode);
56 void creatSafe(const std::string &path, mode_t mode);
57 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
58 void removeDir(const std::string &path);
59 void waitPid(pid_t pid);
60 void change_label(const char* label);
62 #define RUNNER_TEST_SMACK(Proc, ...) \
63 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
64 static int Static##Proc##Init() \
67 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
68 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
71 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
72 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
74 #define RUNNER_TEST_NOSMACK(Proc, ...) \
75 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
76 static int Static##Proc##Init() \
79 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
80 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
83 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
84 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
86 #define RUNNER_CHILD_TEST_SMACK(Proc, ...) \
87 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
88 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
89 static int Static##Proc##Init() \
92 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
93 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
96 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
97 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
98 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
100 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
102 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...) \
103 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
104 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
105 static int Static##Proc##Init() \
107 if (!smack_check()) \
108 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
109 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
112 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
113 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
114 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
116 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
118 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...) \
119 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
120 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
121 static int Static##Proc##Init() \
124 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
125 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
128 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
129 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
130 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
132 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
134 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...) \
135 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
136 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
137 static int Static##Proc##Init() \
139 if (!smack_check()) \
140 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
141 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
144 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
145 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
146 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
148 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)