From: Zofia Abramowska Date: Thu, 12 Dec 2024 14:08:45 +0000 (+0100) Subject: cynara: Add tests for PID credentials API X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ef652c7b5683ca3cccd6193a01e29da5dffaae8e;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git cynara: Add tests for PID credentials API Change-Id: Idb6acd9f73f869bae4eb05c49f020f624bb16ed7 --- diff --git a/src/cynara-tests/CMakeLists.txt b/src/cynara-tests/CMakeLists.txt index 63ff2674..8916e809 100644 --- a/src/cynara-tests/CMakeLists.txt +++ b/src/cynara-tests/CMakeLists.txt @@ -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 diff --git a/src/cynara-tests/test_cases_helpers.cpp b/src/cynara-tests/test_cases_helpers.cpp index 00ae296e..0ff8f15f 100644 --- a/src/cynara-tests/test_cases_helpers.cpp +++ b/src/cynara-tests/test_cases_helpers.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include @@ -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 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); + } +}