Add tests of concurrent prepare_app and app_install/uninstall 05/319405/3
authorTomasz Swierczek <t.swierczek@samsung.com>
Tue, 22 Oct 2024 15:30:25 +0000 (17:30 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Wed, 23 Oct 2024 07:32:07 +0000 (09:32 +0200)
These tests should properly stress the two-threads implementation
of the daemon that has dedicated thread just for prepare_app call.

The test takes more time so its run as RUNNER_TEST (not CHILD test)
as CHILD tests do  have a timeout in the testing framework internals.

Change-Id: Iad094acfc3d86d9b1d15c79a6b9095b733adda93

src/security-manager-tests/test_cases_prepare_app.cpp

index edde37bca797027819ed3d0a8a86758f791fd3e0..1f69117f918a540f1fb44f59dcaf664c844ed37c 100644 (file)
@@ -645,3 +645,93 @@ RUNNER_CHILD_TEST(security_manager_300_prepare_app_recursive_threads)
         Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
     }
 }
+
+RUNNER_CHILD_TEST(security_manager_400_prepare_app_with_concurrent_install)
+{
+    std::srand(std::time(nullptr)); // use current time as seed for random generator
+    TemporaryTestUser tmpUser(APP_TEST_USER, GUM_USERTYPE_NORMAL, false);
+    tmpUser.create();
+
+    // few iterations of the test...
+    for (int i = 0; i < 5; ++i) {
+        AppInstallHelper app("app400", tmpUser.getUid());
+        app.addPrivileges({PRIV_EXTERNALSTORAGE, PRIV_MEDIASTORAGE});
+        ScopedInstaller appInstall(app);
+
+        pid_t pid = fork();
+        RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "Fork failed");
+        if (pid == 0) {
+            RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(tmpUser) == 0, "launcher failed");
+
+            // wait a bit for the other process to start running install/deinstall loop
+            std::this_thread::sleep_for(std::chrono::milliseconds( 10 * (std::rand() % 200) )); // max 2 seconds
+
+            Api::prepareAppCandidate();
+            Api::prepareApp(app.getAppId());
+
+            exit(0);
+        } else {
+            // in a loop, install & uninstall a temporary app
+            std::time_t begin = std::time(nullptr);
+            while(1) {
+                {
+                    AppInstallHelper app2("app400_2", tmpUser.getUid());
+                    app2.addPrivileges({PRIV_EXTERNALSTORAGE, PRIV_MEDIASTORAGE});
+                    ScopedInstaller appInstall(app2);
+                }
+                std::time_t now = std::time(nullptr);
+                if (now - begin >= 3) // wait at most 3 seconds
+                    break;
+            }
+            waitPid(pid);
+            Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
+        }
+    }
+}
+
+RUNNER_TEST(security_manager_400_prepare_app_series_with_concurrent_install_stress)
+{
+    std::srand(std::time(nullptr)); // use current time as seed for random generator
+    TemporaryTestUser tmpUser(APP_TEST_USER, GUM_USERTYPE_NORMAL, false);
+    tmpUser.create();
+
+    pid_t pid = fork();
+    RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "Fork failed");
+    if (pid == 0) {
+        // install an app, and in a loop - fork + launch it
+        AppInstallHelper app("app400", tmpUser.getUid());
+        ScopedInstaller appInstall(app);
+
+        std::time_t begin = std::time(nullptr);
+        while(1) {
+            pid_t pid2 = fork();
+            RUNNER_ASSERT_ERRNO_MSG(pid2 >= 0, "Fork failed");
+            if (pid2 == 0) {
+                RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(tmpUser) == 0, "launcher failed");
+                Api::prepareAppCandidate();
+                Api::prepareApp(app.getAppId());
+                exit(0);
+            } else {
+                waitPid(pid2);
+            }
+            std::time_t now = std::time(nullptr);
+            if (now - begin >= 120) // wait 2 mins
+                break;
+        }
+        exit(0);
+    } else {
+        // in a loop, install & uninstall a temporary app
+        std::time_t begin = std::time(nullptr);
+        while(1) {
+            {
+                AppInstallHelper app2("app400_2", tmpUser.getUid());
+                app2.addPrivileges({PRIV_EXTERNALSTORAGE, PRIV_MEDIASTORAGE});
+                ScopedInstaller appInstall(app2);
+            }
+            std::time_t now = std::time(nullptr);
+            if (now - begin >= 120) // wait 2 mins
+                break;
+        }
+        waitPid(pid);
+    }
+}