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&)
54 security_server_request_cookie(cookie, 20);
57 void check_privilege(const Data &d)
59 int ret = security_server_get_gid("audio");
60 security_server_check_privilege(d.cookie, ret);
63 void check_privilege_by_cookie(const Data &d)
65 security_server_check_privilege_by_cookie(d.cookie, "label", "rwxat");
68 void get_cookie_pid(const Data &d)
70 security_server_get_cookie_pid(d.cookie);
73 void get_smack_label(const Data &d)
75 char *label = security_server_get_smacklabel_cookie(d.cookie);
79 void random_sleep(const Data&)
81 std::uniform_int_distribution<size_t> distribution(0,100);
82 usleep(distribution(generator));
86 // list of test functions
87 std::vector<std::function<void(const Data&)> > functions = {
91 check_privilege_by_cookie,
97 // randomly calls test functions
98 void security_server_magic()
101 security_server_request_cookie(cookie, 20);
104 // random loop number
105 std::uniform_int_distribution<size_t> l_dist(0,LOOPS);
106 size_t loops = l_dist(generator);
108 // random function call
109 std::uniform_int_distribution<size_t> distribution(0,functions.size() - 1);
110 auto rnd = std::bind(distribution, generator);
111 for (size_t i = 0; i < loops; ++i) {
121 if (current >= PROC_MAX || spawned >= PROC_TOTAL) {
123 int ret = wait(&status);
125 // all processes spawned, no more children to wait for
126 if (spawned >= PROC_TOTAL && ret <= 0)
132 // spawn predefined number of processes
133 if (spawned < PROC_TOTAL) {
136 LogDebug("START " << spawned);
137 security_server_magic();
138 LogError("STOP " << spawned);
142 //LogWarning("PID " << pid);