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