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);
+ }
+}