Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / security-server-tests / security_server_tests_mt.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file       security_server_tests_mt.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
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.
24  */
25
26 #include <dpl/log/log.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <security-server.h>
30 #include <sys/wait.h>
31 #include <random>
32 #include <functional>
33 #include <chrono>
34
35 namespace {
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
39
40 std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
41
42 // common function data
43 struct Data {
44     char *cookie;   // not owned
45
46     Data(char *c) : cookie(c) {}
47 };
48
49
50 // test functions
51 void request_cookie(const Data&)
52 {
53     char cookie[20];
54     security_server_request_cookie(cookie, 20);
55 }
56
57 void check_privilege(const Data &d)
58 {
59     int ret = security_server_get_gid("audio");
60     security_server_check_privilege(d.cookie, ret);
61 }
62
63 void check_privilege_by_cookie(const Data &d)
64 {
65     security_server_check_privilege_by_cookie(d.cookie, "label", "rwxat");
66 }
67
68 void get_cookie_pid(const Data &d)
69 {
70     security_server_get_cookie_pid(d.cookie);
71 }
72
73 void get_smack_label(const Data &d)
74 {
75     char *label = security_server_get_smacklabel_cookie(d.cookie);
76     free(label);
77 }
78
79 void random_sleep(const Data&)
80 {
81     std::uniform_int_distribution<size_t> distribution(0,100);
82     usleep(distribution(generator));
83 }
84
85
86 // list of test functions
87 std::vector<std::function<void(const Data&)> > functions = {
88     random_sleep,
89     request_cookie,
90     check_privilege,
91     check_privilege_by_cookie,
92     get_cookie_pid,
93     get_smack_label
94 };
95 } // namespace
96
97 // randomly calls test functions
98 void security_server_magic()
99 {
100     char cookie[20];
101     security_server_request_cookie(cookie, 20);
102     Data d(cookie);
103
104     // random loop number
105     std::uniform_int_distribution<size_t> l_dist(0,LOOPS);
106     size_t loops = l_dist(generator);
107
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) {
112         functions[rnd()](d);
113     }
114 }
115
116 int main()
117 {
118     size_t current = 0;
119     size_t spawned = 0;
120     for (;;) {
121         if (current >= PROC_MAX || spawned >= PROC_TOTAL) {
122             int status;
123             int ret = wait(&status);
124
125             // all processes spawned, no more children to wait for
126             if (spawned >= PROC_TOTAL && ret <= 0)
127                 break;
128
129             current--;
130         }
131
132         // spawn predefined number of processes
133         if (spawned < PROC_TOTAL) {
134             pid_t pid = fork();
135             if (pid == 0) {
136                 LogDebug("START " << spawned);
137                 security_server_magic();
138                 LogError("STOP " << spawned);
139                 exit(0);
140             }
141             else {
142                 //LogWarning("PID " << pid);
143                 spawned++;
144                 current++;
145             }
146         }
147     }
148     LogInfo("Finished");
149     return 0;
150 }