Multiple security server clients stress test added.
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 9 May 2013 13:26:43 +0000 (15:26 +0200)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 23 Jan 2014 13:32:20 +0000 (14:32 +0100)
[Issue#] SSDWSSP-237
[Problem] N/A
[Cause] N/A
[Solution] Stress test added.

[Verification] Run security-server-tests-mt --output=text

Change-Id: If15ebf15431557cb5c53ccf244e65963f3eba37b

packaging/security-tests.spec
tests/security-server-tests/CMakeLists.txt
tests/security-server-tests/security_server_tests_mt.cpp [new file with mode: 0644]

index 6cc4019..1a9c3a1 100644 (file)
@@ -61,6 +61,7 @@ echo "security-tests postinst done ..."
 /etc/smack/test_smack_rules2
 /etc/smack/test_smack_rules3
 /etc/smack/test_smack_rules4
+/usr/bin/security-server-tests-mt
 /etc/smack/test_smack_rules
 /etc/smack/test_smack_rules_lnk
 /usr/share/privilege-control/*
index 6f3e2fe..b3bcef4 100644 (file)
@@ -36,6 +36,7 @@ SET(TARGET_SEC_SRV_LABEL_TESTS "security-server-tests-label")
 SET(TARGET_SEC_SRV_PID_TESTS "security-server-tests-pid-reuser")
 SET(TARGET_SEC_SRV_TC_SERVER_TESTS "security-server-tests-server")
 SET(TARGET_SEC_SRV_PWD_TESTS "security-server-tests-password")
+SET(TARGET_SEC_SRV_MT_TESTS "security-server-tests-mt")
 
 # Sources definition
 
@@ -60,6 +61,10 @@ SET(SEC_SRV_PWD_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/security-server-tests/security_server_tests_password.cpp
    )
 
+SET(SEC_SRV_MT_SOURCES
+    ${PROJECT_SOURCE_DIR}/tests/security-server-tests/security_server_tests_mt.cpp
+   )
+
 
 INCLUDE_DIRECTORIES(
     ${SEC_SRV_TESTS_DEP_INCLUDE_DIRS}
@@ -72,6 +77,8 @@ ADD_EXECUTABLE(${TARGET_SEC_SRV_LABEL_TESTS} ${SEC_SRV_LABEL_SOURCES})
 ADD_EXECUTABLE(${TARGET_SEC_SRV_PID_TESTS} ${SEC_SRV_PID_SOURCES})
 ADD_EXECUTABLE(${TARGET_SEC_SRV_TC_SERVER_TESTS} ${SEC_SRV_TC_SERVER_SOURCES})
 ADD_EXECUTABLE(${TARGET_SEC_SRV_PWD_TESTS} ${SEC_SRV_PWD_SOURCES})
+ADD_EXECUTABLE(${TARGET_SEC_SRV_MT_TESTS} ${SEC_SRV_MT_SOURCES})
+
 
 
 TARGET_LINK_LIBRARIES(${TARGET_SEC_SRV_CLIENT_SMACK_TESTS}
@@ -89,6 +96,9 @@ TARGET_LINK_LIBRARIES(${TARGET_SEC_SRV_TC_SERVER_TESTS}
 TARGET_LINK_LIBRARIES(${TARGET_SEC_SRV_PWD_TESTS}
     ${SEC_SRV_TESTS_DEP_LIBRARIES})
 
+TARGET_LINK_LIBRARIES(${TARGET_SEC_SRV_MT_TESTS}
+    ${SEC_SRV_TESTS_DEP_LIBRARIES})
+
 # Installation
 
 INSTALL(TARGETS ${TARGET_SEC_SRV_CLIENT_SMACK_TESTS} DESTINATION /usr/bin)
@@ -96,3 +106,4 @@ INSTALL(TARGETS ${TARGET_SEC_SRV_LABEL_TESTS} DESTINATION /usr/bin)
 INSTALL(TARGETS ${TARGET_SEC_SRV_PID_TESTS} DESTINATION /usr/bin)
 INSTALL(TARGETS ${TARGET_SEC_SRV_TC_SERVER_TESTS} DESTINATION /usr/bin)
 INSTALL(TARGETS ${TARGET_SEC_SRV_PWD_TESTS} DESTINATION /usr/bin)
+INSTALL(TARGETS ${TARGET_SEC_SRV_MT_TESTS} DESTINATION /usr/bin)
diff --git a/tests/security-server-tests/security_server_tests_mt.cpp b/tests/security-server-tests/security_server_tests_mt.cpp
new file mode 100644 (file)
index 0000000..1717e71
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @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.
+ */
+
+#include <dpl/log/log.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <security-server.h>
+#include <sys/wait.h>
+#include <random>
+#include <functional>
+#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
+
+std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
+
+// common function data
+struct Data {
+       char* cookie;   // not owned
+
+       Data(char* c): cookie(c) {}
+};
+
+
+// test functions
+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_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_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));
+}
+
+
+// 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
+};
+
+} // 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);
+       }
+}
+
+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");
+    return 0;
+}