Extend owner getter tests 95/323595/8
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 30 Apr 2025 10:52:52 +0000 (12:52 +0200)
committerKrzysztof Małysa <k.malysa@samsung.com>
Wed, 28 May 2025 17:34:48 +0000 (17:34 +0000)
Make them test the API with pid != getpid() as well.

Change-Id: Id0e62e96798f0727af034b5b7661c54bd82d0340

src/security-manager-tests/test_cases.cpp

index b36ceb3d2086c6bd36961d98db9dbd90695cb816..a462dd3d84df59d14ebb41040f79065cd4036f06 100644 (file)
@@ -1192,16 +1192,32 @@ RUNNER_CHILD_TEST(security_manager_26_1a_security_manager_get_app_owner_uid)
         TestUser::createTemporary("sm_test_26_1a_user_name", GUM_USERTYPE_NORMAL);
     AppInstallHelperExt app("sm_test_26_1a", "sm_test_26_1a", testUser.getUid());
     ScopedInstaller appInstall(app);
+    SynchronizationPipe pipe;
+    uid_t owner_uid = 0;
+    pid_t pid;
 
-    ScopedAppLauncher(app, testUser, [&] {
-        uid_t owner_uid = 0;
+    ScopedAppLauncher launched_app(app, testUser, [&] {
+        pipe.claimChildEp();
+        pid = getpid();
+        pipe.post(&pid, sizeof(pid)); // let the parent know that the app is started
         RUNNER_ASSERT_MSG(
-            security_manager_get_app_owner_uid(getpid(), &owner_uid) == SECURITY_MANAGER_SUCCESS,
+            security_manager_get_app_owner_uid(pid, &owner_uid) == SECURITY_MANAGER_SUCCESS,
             "Invalid return from security_manager_get_app_owner_uid()");
 
         RUNNER_ASSERT_MSG(owner_uid == testUser.getUid(), "Invalid uid returned - expected: "
                           << testUser.getUid() << " returned: " << owner_uid);
+        pipe.wait(); // wait until parent checks our owner
     });
+
+    pipe.claimParentEp();
+    pipe.wait(&pid, sizeof(pid)); // wait for the child app to start
+    RUNNER_ASSERT_MSG(
+        security_manager_get_app_owner_uid(pid, &owner_uid) == SECURITY_MANAGER_SUCCESS,
+        "Invalid return from security_manager_get_app_owner_uid()");
+
+    RUNNER_ASSERT_MSG(owner_uid == testUser.getUid(), "Invalid uid returned - expected: "
+                      << testUser.getUid() << " returned: " << owner_uid);
+    pipe.post(); // let the child die
 }
 
 RUNNER_CHILD_TEST(security_manager_26_1b_security_manager_get_app_owner_uid)
@@ -1304,67 +1320,70 @@ RUNNER_CHILD_TEST(security_manager_26_3_security_manager_is_app_from_pid)
     app.checkAfterUninstall();
 }
 
-RUNNER_CHILD_TEST(security_manager_26_4a_security_manager_get_app_full_credentials_from_pid)
+namespace {
+void getAppFullCredentialsTest(bool hybrid)
 {
-    TestUser testUser = TestUser::createTemporary("sm_test_26_4a_user_name", GUM_USERTYPE_NORMAL);
-
-    AppInstallHelperExt app("sm_test_26_4a", "sm_test_26_4a", testUser.getUid());
+    SynchronizationPipe pipe;
+    auto testUser = TestUser::createTemporary("sm_app_full_creds_test_user_name",
+                                              GUM_USERTYPE_NORMAL);
+    AppInstallHelperExt app("sm_app_full_creds_test", "sm_app_full_creds_test", testUser.getUid());
+    if (hybrid)
+        app.setHybrid();
     ScopedInstaller appInstall(app);
-    ScopedAppLauncher(app, testUser, [&]{
+
+    auto credentialsCheck = [&](pid_t pid){
         uid_t owner_uid = 0;
         char* pkgId = nullptr;
         char* appId = nullptr;
+        int ret = security_manager_get_app_full_credentials_from_pid(
+                pid,
+                &owner_uid,
+                &pkgId,
+                &appId);
         RUNNER_ASSERT_MSG(
-            security_manager_get_app_full_credentials_from_pid(getpid(), &owner_uid, &pkgId, &appId) == SECURITY_MANAGER_SUCCESS,
-            "Invalid return from ecurity_manager_get_app_full_credentials_from_pid()");
-
-        CStringPtr pkgPtr(pkgId);
-        CStringPtr appPtr(appId);
+                ret == SECURITY_MANAGER_SUCCESS,
+                "Invalid return from security_manager_get_app_full_credentials_from_pid()");
 
         RUNNER_ASSERT_MSG(owner_uid == testUser.getUid(), "Invalid uid returned - expected: "
                           << testUser.getUid() << " returned: " << owner_uid);
 
+        CStringPtr pkgPtr(pkgId);
+        CStringPtr appPtr(appId);
+
         RUNNER_ASSERT_MSG(
-            pkgId != nullptr && app.getPkgId() == pkgId,
+            pkgId != nullptr && pkgId == app.getPkgId(),
             "Invalid pkgId returned from security_manager_get_app_full_credentials_from_pid"
         );
-        // non-hybrid app
+
         RUNNER_ASSERT_MSG(
-            appId == nullptr,
+            hybrid ? appId != nullptr && appId == app.getAppId() : appId == nullptr,
             "Invalid appId returned from security_manager_get_app_full_credentials_from_pid"
         );
+    };
+
+    pid_t pid;
+    ScopedAppLauncher launched_app(app, testUser, [&]{
+        pipe.claimChildEp();
+        // child - the actual application
+        pid = getpid();
+        pipe.post(&pid, sizeof(pid)); // let the parent know that the app is started
+        credentialsCheck(pid);
+        pipe.wait(); // wait until parent checks our creds
     });
+
+    pipe.claimParentEp();
+    pipe.wait(&pid, sizeof(pid)); // wait for the child app to start
+    credentialsCheck(pid);
+    pipe.post(); // let the child die
 }
+} // namespace
 
-RUNNER_CHILD_TEST(security_manager_26_4b_security_manager_get_app_full_credentials_from_pid)
+RUNNER_CHILD_TEST(security_manager_26_4a_security_manager_get_app_full_credentials_from_pid)
 {
-    TestUser testUser = TestUser::createTemporary("sm_test_26_4b_user_name", GUM_USERTYPE_NORMAL);
-
-    AppInstallHelperExt app("sm_test_26_4b", "sm_test_26_4b", testUser.getUid());
-    app.setHybrid();
-    ScopedInstaller appInstall(app);
-    ScopedAppLauncher(app, testUser, [&]{
-        uid_t owner_uid = 0;
-        char* pkgId = nullptr;
-        char* appId = nullptr;
-        RUNNER_ASSERT_MSG(
-            security_manager_get_app_full_credentials_from_pid(getpid(), &owner_uid, &pkgId, &appId) == SECURITY_MANAGER_SUCCESS,
-            "Invalid return from ecurity_manager_get_app_full_credentials_from_pid()");
-
-        CStringPtr pkgPtr(pkgId);
-        CStringPtr appPtr(appId);
-
-        RUNNER_ASSERT_MSG(owner_uid == testUser.getUid(), "Invalid uid returned - expected: "
-                          << testUser.getUid() << " returned: " << owner_uid);
+    getAppFullCredentialsTest(false);
+}
 
-        RUNNER_ASSERT_MSG(
-            pkgId != nullptr && app.getPkgId() == pkgId,
-            "Invalid pkgId returned from security_manager_get_app_full_credentials_from_pid"
-        );
-        // hybrid app
-        RUNNER_ASSERT_MSG(
-            appId != nullptr && app.getAppId() == appId,
-            "Invalid appId returned from security_manager_get_app_full_credentials_from_pid"
-        );
-    });
+RUNNER_CHILD_TEST(security_manager_26_4b_security_manager_get_app_full_credentials_from_pid)
+{
+    getAppFullCredentialsTest(true);
 }