* @file security_server_tests_mt.cpp
* @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
* @version 1.0
- * @ brief This test creates multiple processes that connect to security
- * server and perform random operations using its API. The purpose
- * of this test is to check if security-server crashes when under
- * heavy load. Test succeeds if all processes finish.
+ * @brief This test creates multiple processes that connect to security
+ * server and perform random operations using its API. The purpose
+ * of this test is to check if security-server crashes when under
+ * heavy load. Test succeeds if all processes finish.
*/
#include <dpl/log/log.h>
#include <chrono>
namespace {
-const size_t PROC_TOTAL = 1000; // total number of processes to spawn
-const size_t PROC_MAX = 10; // max number of processes working at the same time
-const size_t LOOPS = 50; // number of loop repeats
+const size_t PROC_TOTAL = 1000; // total number of processes to spawn
+const size_t PROC_MAX = 10; // max number of processes working at the same time
+const size_t LOOPS = 50; // number of loop repeats
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
// common function data
struct Data {
- char* cookie; // not owned
+ char *cookie; // not owned
- Data(char* c): cookie(c) {}
+ Data(char *c) : cookie(c) {}
};
// test functions
-void request_cookie(const Data&) {
- char cookie[20];
- security_server_request_cookie(cookie, 20);
+void request_cookie(const Data&)
+{
+ char cookie[20];
+ security_server_request_cookie(cookie, 20);
}
-void check_privilege(const Data& d) {
- int ret = security_server_get_gid("audio");
- security_server_check_privilege(d.cookie, ret);
+void check_privilege(const Data &d)
+{
+ int ret = security_server_get_gid("audio");
+ security_server_check_privilege(d.cookie, ret);
}
-void check_privilege_by_cookie(const Data& d) {
- security_server_check_privilege_by_cookie(d.cookie, "label", "rwxat");
+void check_privilege_by_cookie(const Data &d)
+{
+ security_server_check_privilege_by_cookie(d.cookie, "label", "rwxat");
}
-void get_cookie_pid(const Data& d) {
- security_server_get_cookie_pid(d.cookie);
+void get_cookie_pid(const Data &d)
+{
+ security_server_get_cookie_pid(d.cookie);
}
-void get_smack_label(const Data& d) {
- char* label = security_server_get_smacklabel_cookie(d.cookie);
- free(label);
+void get_smack_label(const Data &d)
+{
+ char *label = security_server_get_smacklabel_cookie(d.cookie);
+ free(label);
}
-void random_sleep(const Data&) {
- std::uniform_int_distribution<size_t> distribution(0,100);
- usleep(distribution(generator));
+void random_sleep(const Data&)
+{
+ std::uniform_int_distribution<size_t> distribution(0,100);
+ usleep(distribution(generator));
}
// list of test functions
std::vector<std::function<void(const Data&)> > functions = {
- random_sleep,
- request_cookie,
- check_privilege,
- check_privilege_by_cookie,
- get_cookie_pid,
- get_smack_label
+ random_sleep,
+ request_cookie,
+ check_privilege,
+ check_privilege_by_cookie,
+ get_cookie_pid,
+ get_smack_label
};
-
} // namespace
// randomly calls test functions
-void security_server_magic() {
- char cookie[20];
- security_server_request_cookie(cookie, 20);
- Data d(cookie);
-
- // random loop number
- std::uniform_int_distribution<size_t> l_dist(0,LOOPS);
- size_t loops = l_dist(generator);
-
- // random function call
- std::uniform_int_distribution<size_t> distribution(0,functions.size()-1);
- auto rnd = std::bind(distribution, generator);
- for (size_t i=0; i < loops;++i) {
- functions[rnd()](d);
- }
+void security_server_magic()
+{
+ char cookie[20];
+ security_server_request_cookie(cookie, 20);
+ Data d(cookie);
+
+ // random loop number
+ std::uniform_int_distribution<size_t> l_dist(0,LOOPS);
+ size_t loops = l_dist(generator);
+
+ // random function call
+ std::uniform_int_distribution<size_t> distribution(0,functions.size() - 1);
+ auto rnd = std::bind(distribution, generator);
+ for (size_t i = 0; i < loops; ++i) {
+ functions[rnd()](d);
+ }
}
int main()
{
- size_t current = 0;
- size_t spawned = 0;
- for (;;) {
- if (current >= PROC_MAX || spawned >= PROC_TOTAL) {
- int status;
- int ret = wait(&status);
-
- // all processes spawned, no more children to wait for
- if (spawned >= PROC_TOTAL && ret <= 0)
- break;
-
- current--;
- }
-
- // spawn predefined number of processes
- if (spawned < PROC_TOTAL) {
- pid_t pid = fork();
- if (pid == 0) {
- LogDebug("START " << spawned);
- security_server_magic();
- LogError("STOP " << spawned);
- exit(0);
- }
- else {
- //LogWarning("PID " << pid);
- spawned++;
- current++;
- }
- }
- }
- LogInfo("Finished");
+ size_t current = 0;
+ size_t spawned = 0;
+ for (;;) {
+ if (current >= PROC_MAX || spawned >= PROC_TOTAL) {
+ int status;
+ int ret = wait(&status);
+
+ // all processes spawned, no more children to wait for
+ if (spawned >= PROC_TOTAL && ret <= 0)
+ break;
+
+ current--;
+ }
+
+ // spawn predefined number of processes
+ if (spawned < PROC_TOTAL) {
+ pid_t pid = fork();
+ if (pid == 0) {
+ LogDebug("START " << spawned);
+ security_server_magic();
+ LogError("STOP " << spawned);
+ exit(0);
+ }
+ else {
+ //LogWarning("PID " << pid);
+ spawned++;
+ current++;
+ }
+ }
+ }
+ LogInfo("Finished");
return 0;
}