2 * Copyright (c) 2011 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.
17 * @file security_server_tests_mt.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20 * @ brief This test creates multiple processes that connect to security
21 * server and perform random operations using its API. The purpose
22 * of this test is to check if security-server crashes when under
23 * heavy load. Test succeeds if all processes finish.
26 #include <dpl/log/log.h>
29 #include <security-server.h>
36 const size_t PROC_TOTAL = 1000; // total number of processes to spawn
37 const size_t PROC_MAX = 10; // max number of processes working at the same time
38 const size_t LOOPS = 50; // number of loop repeats
40 std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
42 // common function data
44 char* cookie; // not owned
46 Data(char* c): cookie(c) {}
51 void request_cookie(const Data&) {
53 security_server_request_cookie(cookie, 20);
56 void check_privilege(const Data& d) {
57 int ret = security_server_get_gid("audio");
58 security_server_check_privilege(d.cookie, ret);
61 void check_privilege_by_cookie(const Data& d) {
62 security_server_check_privilege_by_cookie(d.cookie, "label", "rwxat");
65 void get_cookie_pid(const Data& d) {
66 security_server_get_cookie_pid(d.cookie);
69 void get_smack_label(const Data& d) {
70 char* label = security_server_get_smacklabel_cookie(d.cookie);
74 void random_sleep(const Data&) {
75 std::uniform_int_distribution<size_t> distribution(0,100);
76 usleep(distribution(generator));
80 // list of test functions
81 std::vector<std::function<void(const Data&)> > functions = {
85 check_privilege_by_cookie,
92 // randomly calls test functions
93 void security_server_magic() {
95 security_server_request_cookie(cookie, 20);
99 std::uniform_int_distribution<size_t> l_dist(0,LOOPS);
100 size_t loops = l_dist(generator);
102 // random function call
103 std::uniform_int_distribution<size_t> distribution(0,functions.size()-1);
104 auto rnd = std::bind(distribution, generator);
105 for (size_t i=0; i < loops;++i) {
115 if (current >= PROC_MAX || spawned >= PROC_TOTAL) {
117 int ret = wait(&status);
119 // all processes spawned, no more children to wait for
120 if (spawned >= PROC_TOTAL && ret <= 0)
126 // spawn predefined number of processes
127 if (spawned < PROC_TOTAL) {
130 LogDebug("START " << spawned);
131 security_server_magic();
132 LogError("STOP " << spawned);
136 //LogWarning("PID " << pid);