cynara: Add tests for PID credentials API 74/316574/4 cynara
authorZofia Abramowska <z.abramowska@samsung.com>
Thu, 12 Dec 2024 14:08:45 +0000 (15:08 +0100)
committerZofia Abramowska <z.abramowska@samsung.com>
Thu, 2 Jan 2025 09:44:11 +0000 (09:44 +0000)
Change-Id: Idb6acd9f73f869bae4eb05c49f020f624bb16ed7

src/cynara-tests/CMakeLists.txt
src/cynara-tests/test_cases_helpers.cpp

index 63ff2674757e74176261430048f96478090083f9..8916e809dc1346dfcbb0d34b270e43ef4501fdc7 100644 (file)
@@ -26,6 +26,7 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
     cynara-client-async
     cynara-creds-dbus
     cynara-creds-gdbus
+    cynara-creds-pid
     cynara-creds-sd-bus
     cynara-creds-self
     cynara-creds-socket
index 00ae296e798871c12f1a06d96c898728ae8caf78..0ff8f15f1846db631a6a325671971887a8e70a73 100644 (file)
@@ -47,6 +47,7 @@
 #include <cynara-creds-dbus.h>
 #include <cynara-creds-gdbus.h>
 #include <cynara-creds-self.h>
+#include <cynara-creds-pid.h>
 #include <scoped_process_label.h>
 
 
@@ -1431,3 +1432,136 @@ RUNNER_CHILD_TEST_SMACK(tccsl06_self_credentials_user_default) {
         RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value " << method);
     }
 }
+
+//================================Pid=================================================
+
+void childProcess(SynchronizationPipe &pipe, const struct ProcessCredentials &childCreds) {
+    AppContext ctx(childCreds.label());
+    ctx.apply(childCreds.uid(), childCreds.gid());
+    pipe.claimChildEp();
+
+    pipe.post();
+    pipe.wait();
+}
+
+typedef std::function<void(pid_t, const ProcessCredentials &)> PidAssertionFn;
+
+void pidTestTemplate(PidAssertionFn assertion) {
+    const ProcessCredentials childCreds;
+
+    SynchronizationPipe pipe;
+
+    pid_t pid = runInChild(std::bind(childProcess, std::ref(pipe), std::cref(childCreds)));
+
+    pipe.claimParentEp();
+    pipe.wait();
+
+    assertion(pid, childCreds);
+    pipe.post();
+}
+
+RUNNER_TEST_GROUP_INIT(cynara_creds_pid)
+
+void testPidClientPid(cynara_client_creds method) {
+    pidTestTemplate([method](pid_t pid, const ProcessCredentials &) {
+        char *client;
+        int ret = cynara_creds_pid_get_client(pid, method, &client);
+        RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_pid_get_client failed with "
+                          << ret);
+        const std::string expected = std::to_string(pid);
+        RUNNER_ASSERT_MSG(expected == client,
+                          "client doesn't match, expected: " << expected
+                          << ", got: " << client);
+    });
+}
+
+void testPidClientLabel(cynara_client_creds method) {
+    pidTestTemplate([method](pid_t pid, const ProcessCredentials &childCreds) {
+        char *client;
+        int ret = cynara_creds_pid_get_client(pid, method, &client);
+        RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_pid_get_client failed with "
+                          << ret);
+        const std::string expected = childCreds.label();
+        RUNNER_ASSERT_MSG(expected == client,
+                          "client doesn't match, expected: " << expected
+                          << ", got: " << client);
+    });
+}
+
+RUNNER_MULTIPROCESS_TEST(tccp01_pid_credentials_client_pid)
+{
+    testPidClientPid(CLIENT_METHOD_PID);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccp02_pid_credentials_client_label)
+{
+    testPidClientLabel(CLIENT_METHOD_SMACK);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccp03_pid_credentials_client_default)
+{
+    auto method = getClientDefaultMethod();
+    switch(method) {
+    case CLIENT_METHOD_PID:
+        testPidClientPid(CLIENT_METHOD_DEFAULT);
+        break;
+    case CLIENT_METHOD_SMACK:
+        testPidClientLabel(CLIENT_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
+                        << method);
+    }
+}
+
+void testPidUserUid(cynara_user_creds method) {
+    pidTestTemplate([method](pid_t pid, const ProcessCredentials &childCreds) {
+        char *client;
+        int ret = cynara_creds_pid_get_user(pid, method, &client);
+        RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_pid_get_client failed with "
+                          << ret);
+        const std::string expected = std::to_string(childCreds.uid());
+        RUNNER_ASSERT_MSG(expected == client,
+                          "user doesn't match, expected: " << expected
+                          << ", got: " << client);
+    });
+}
+
+void testPidUserGid(cynara_user_creds method) {
+    pidTestTemplate([method](pid_t pid, const ProcessCredentials &childCreds) {
+        char *client;
+        int ret = cynara_creds_pid_get_user(pid, method, &client);
+        RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_pid_get_client failed with "
+                          << ret);
+        const std::string expected = std::to_string(childCreds.gid());
+        RUNNER_ASSERT_MSG(expected == client,
+                          "user doesn't match, expected: " << expected
+                          << ", got: " << client);
+    });
+}
+
+RUNNER_MULTIPROCESS_TEST(tccp04_pid_credentials_user_uid)
+{
+    testPidUserUid(USER_METHOD_UID);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccp05_pid_credentials_user_gid)
+{
+    testPidUserGid(USER_METHOD_GID);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccp06_pid_credentials_user_default)
+{
+    auto method = getUserDefaultMethod();
+    switch(method) {
+    case USER_METHOD_UID:
+        testPidUserUid(USER_METHOD_DEFAULT);
+        break;
+    case USER_METHOD_GID:
+        testPidUserGid(USER_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
+                        << method);
+    }
+}