From b0a785b2bd80c67c641a20c6e8b603b9cde37901 Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Wed, 21 Dec 2022 20:44:21 +0100 Subject: [PATCH] [cynara] Tests for new credentials helpers API Change-Id: I321c20f18c41840fd6f534de50d89b7acedceb9a --- src/cynara-tests/test_cases_helpers.cpp | 702 +++++++++++++++++++++++++++++++- 1 file changed, 687 insertions(+), 15 deletions(-) diff --git a/src/cynara-tests/test_cases_helpers.cpp b/src/cynara-tests/test_cases_helpers.cpp index 86ec5e0..00ae296 100644 --- a/src/cynara-tests/test_cases_helpers.cpp +++ b/src/cynara-tests/test_cases_helpers.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015-2022 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,10 +44,27 @@ #include #include +#include #include #include #include + +static const cynara_client_creds G_CLIENT_METHOD_DEFAULT = CLIENT_METHOD_DEFAULT; +static const cynara_client_creds G_CLIENT_METHOD_SMACK = CLIENT_METHOD_SMACK; +static const cynara_client_creds G_CLIENT_METHOD_PID = CLIENT_METHOD_PID; + +static const cynara_user_creds G_USER_METHOD_DEFAULT = USER_METHOD_DEFAULT; +static const cynara_user_creds G_USER_METHOD_UID = USER_METHOD_UID; +static const cynara_user_creds G_USER_METHOD_GID = USER_METHOD_GID; + +static const cynara_client_creds G_CLIENT_METHOD_LOW = (cynara_client_creds)-1; +static const cynara_client_creds G_CLIENT_METHOD_HIGH = (cynara_client_creds)(CLIENT_METHOD_PID+10); + +static const cynara_user_creds G_USER_METHOD_LOW = (cynara_user_creds)-1; +static const cynara_user_creds G_USER_METHOD_HIGH = (cynara_user_creds)(USER_METHOD_GID+10); + + class ProcessCredentials { public: ProcessCredentials() {} @@ -218,6 +235,9 @@ RUNNER_MULTIPROCESS_TEST(tccs07_socket_credentials_pid) }, "tccs05"); } + +//=====================================DBus=========================================== + // TODO: Create utility namespace for DBus, maybe? DBusConnectionPtr createDBusConnection(const std::string &name) { DBusError err; @@ -263,7 +283,7 @@ typedef std::function DBusAssertionFn; -void dbusTestTemplate(DBusAssertionFn assertion, const std::string &/*scope*/) { +void dbusTestTemplate(DBusAssertionFn assertion) { std::string requestedName = "tests.dbus.cynara"; const ProcessCredentials peerCredentials; @@ -279,6 +299,7 @@ void dbusTestTemplate(DBusAssertionFn assertion, const std::string &/*scope*/) { pipe.post(); } + RUNNER_TEST_GROUP_INIT(cynara_creds_dbus) void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) { @@ -289,7 +310,7 @@ void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) { pid_t clientPid = std::stoi(clientPidStr.get()); RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid << "; expected = " << pid); - }, "tccd01"); + }); } void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) { @@ -300,7 +321,7 @@ void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) { RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(), "Labels don't match ret = " << label.get() << "; expected = " << peerCredentials.label()); - }, "tccd02"); + }); } RUNNER_MULTIPROCESS_TEST(tccd01_dbus_credentials_client_pid) @@ -337,7 +358,7 @@ void testDbusUserUid(cynara_user_creds method = USER_METHOD_UID) { uid_t uid = std::stoul(uidStr.get()); RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid << "; expected = "<< peerCredentials.uid()); - }, "tccd04"); + }); } void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) { @@ -346,7 +367,7 @@ void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) { const ProcessCredentials &) { CStringPtr gidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn), requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED)); - }, "tccd04"); + }); } RUNNER_MULTIPROCESS_TEST(tccd04_dbus_credentials_user_uid) @@ -374,16 +395,342 @@ RUNNER_MULTIPROCESS_TEST(tccd06_dbus_credentials_user_default) { } } -RUNNER_TEST_SMACK(tccd06_dbus_credentials_pid) { +RUNNER_TEST_SMACK(tccd07_dbus_credentials_pid) { dbusTestTemplate([] (DBusConnectionPtr conn, pid_t expectedPid, const std::string &requestedName, const ProcessCredentials &) { auto helperPid = CynaraHelperCredentials::dbusGetPid(std::move(conn), requestedName.c_str(), CYNARA_API_SUCCESS); RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid << "; expected = " << expectedPid); - }, "tccd06"); + }); +} + +void testDbusCredsDefault(int expectedResult, char **pClientBuff, + char **pUserBuff, pid_t *pPidCred) +{ + // reset variables if set by previous test cases + if (pClientBuff != nullptr) *pClientBuff = nullptr; + if (pUserBuff != nullptr) *pUserBuff = nullptr; + if (pPidCred != nullptr) *pPidCred = -1; + + dbusTestTemplate([&] (DBusConnectionPtr conn, pid_t pid, + const std::string &requestedName, + const ProcessCredentials &peerCredentials) { + cynara_user_creds userMethod = getUserDefaultMethod(); + cynara_client_creds clientMethod = getClientDefaultMethod(); + + // exception override + if (expectedResult == CYNARA_API_SUCCESS && userMethod == USER_METHOD_GID) { + expectedResult = CYNARA_API_METHOD_NOT_SUPPORTED; + } + + int ret = cynara_creds_dbus_get_default(conn.get(), requestedName.c_str(), + pClientBuff, pUserBuff, pPidCred); + + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_creds_dbus_get_default returned unexpected result: " << ret << + "; expected: " << expectedResult); + + if (ret != CYNARA_API_SUCCESS) { + return; + } + + if (pClientBuff != nullptr) { + switch (clientMethod) { + case CLIENT_METHOD_SMACK: + RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(), + "Client labels don't match, ret = " + << *pClientBuff << "; expected = " + << peerCredentials.label()); + free(*pClientBuff); + break; + case CLIENT_METHOD_PID: + RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid), + "PIDs don't match, ret = " + << *pClientBuff << "; expected = " << pid); + free(*pClientBuff); + break; + default: + break; + } + } + + if (pUserBuff != nullptr) { + switch (userMethod) { + case USER_METHOD_UID: + RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()), + "UIDs don't match, ret = " + << *pUserBuff + << "; expected = " << peerCredentials.uid()); + free(*pUserBuff); + break; + default: + break; + } + } + + if (pPidCred != nullptr) { + RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = " + << *pPidCred + << "; expected = " << pid); + } + }); +} + +RUNNER_MULTIPROCESS_TEST(tccd08_dbus_credentials_default_positive_all) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, &pidCred); +} + +RUNNER_MULTIPROCESS_TEST(tccd09_dbus_credentials_default_positive_partial_single) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, nullptr); + testDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, nullptr); + testDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, nullptr, &pidCred); +} + +RUNNER_MULTIPROCESS_TEST(tccd10_dbus_credentials_default_positive_partial_double) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, &pidCred); + testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, &pidCred); + testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, nullptr); +} + +RUNNER_MULTIPROCESS_TEST(tccd11_dbus_credentials_default_negative_incomplete) +{ + testDbusCredsDefault(CYNARA_API_INVALID_PARAM, nullptr, nullptr, nullptr); +} + +void testDbusCreds(int expectedResult, + const cynara_client_creds *pClientMethod, char **pClientBuff, + const cynara_user_creds *pUserMethod, char **pUserBuff, + pid_t *pPidCred) +{ + // reset variables if set by previous test cases + if (pClientBuff != nullptr) *pClientBuff = nullptr; + if (pUserBuff != nullptr) *pUserBuff = nullptr; + if (pPidCred != nullptr) *pPidCred = -1; + + dbusTestTemplate([&] (DBusConnectionPtr conn, pid_t pid, + const std::string &requestedName, + const ProcessCredentials &peerCredentials) { + + int ret = cynara_creds_dbus_get(conn.get(), requestedName.c_str(), + pClientMethod, pClientBuff, + pUserMethod, pUserBuff, pPidCred); + + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_creds_dbus_get returned unexpected result: " << ret << + "; expected: " << expectedResult); + + if (ret != CYNARA_API_SUCCESS) { + return; + } + + if (pClientBuff != nullptr && pClientMethod != nullptr) { + switch (*pClientMethod) { + case CLIENT_METHOD_SMACK: + RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(), + "Client labels don't match, ret = " + << *pClientBuff << "; expected = " + << peerCredentials.label()); + free(*pClientBuff); + break; + case CLIENT_METHOD_PID: + RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid), + "PIDs don't match, ret = " + << *pClientBuff << "; expected = " << pid); + free(*pClientBuff); + break; + default: + break; + } + } + + if (pUserBuff != nullptr && pUserMethod != nullptr) { + switch (*pUserMethod) { + case USER_METHOD_UID: + RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()), + "UIDs don't match, ret = " + << *pUserBuff + << "; expected = " << peerCredentials.uid()); + free(*pUserBuff); + break; + default: + break; + } + } + + if (pPidCred != nullptr) { + RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = " + << *pPidCred + << "; expected = " << pid); + } + }); +} + +RUNNER_MULTIPROCESS_TEST(tccd12_dbus_credentials_positive_all) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_SMACK, &clientBuff, + &G_USER_METHOD_UID, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_UID, &userBuff, &pidCred); + + testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_SMACK, &clientBuff, + &G_USER_METHOD_GID, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_GID, &userBuff, &pidCred); +} + +RUNNER_MULTIPROCESS_TEST(tccd13_dbus_credentials_positive_partial_single) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_DEFAULT, nullptr, + nullptr); + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_SMACK, &clientBuff, + nullptr, nullptr, + nullptr); + + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, nullptr, + &G_USER_METHOD_DEFAULT, &userBuff, + nullptr); + testDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, &userBuff, + nullptr); + + testDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, nullptr, + &pidCred); + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, nullptr, + nullptr, nullptr, + &pidCred); + testDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + nullptr, nullptr, + &pidCred); +} + +RUNNER_MULTIPROCESS_TEST(tccd14_dbus_credentials_positive_partial_double) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, nullptr, + &G_USER_METHOD_UID, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_DEFAULT, nullptr, &pidCred); + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + nullptr, nullptr, &pidCred); + + testDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_SMACK, &clientBuff, + &G_USER_METHOD_UID, &userBuff, nullptr); +} + +RUNNER_MULTIPROCESS_TEST(tccd14_dbus_credentials_negative_incomplete) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, &clientBuff, + &G_USER_METHOD_UID, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, &clientBuff, + &G_USER_METHOD_UID, nullptr, &pidCred); + testDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, &clientBuff, + nullptr, nullptr, &pidCred); + + testDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + nullptr, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, nullptr, + nullptr, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, nullptr, + nullptr, &userBuff, &pidCred); + + testDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, nullptr, + &G_USER_METHOD_UID, nullptr, nullptr); + testDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, nullptr, + nullptr, nullptr, nullptr); + testDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, nullptr, nullptr); + testDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, nullptr, + nullptr, nullptr, nullptr); +} + +RUNNER_MULTIPROCESS_TEST(tccd14_dbus_credentials_negative_incorrect) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_LOW, &clientBuff, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_HIGH, &clientBuff, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + &G_USER_METHOD_LOW, &userBuff, &pidCred); + testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + &G_USER_METHOD_HIGH, &userBuff, &pidCred); } + +//===============================GDBUS====================================== + GDBusConnectionPtr createGDBusConnection() { GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL); @@ -397,7 +744,7 @@ typedef std::function GDBusAssertionFn; -void gdbusTestTemplate(GDBusAssertionFn assertion, const std::string &/*scope*/) { +void gdbusTestTemplate(GDBusAssertionFn assertion) { std::string requestedName = "tests.dbus.cynara"; const ProcessCredentials peerCredentials; @@ -425,7 +772,7 @@ void testGdbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) { pid_t clientPid = std::stoi(clientPidStr.get()); RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid << "; expected = " << pid); - }, "tccgd01"); + }); } void testGdbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) { @@ -437,7 +784,7 @@ void testGdbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) { RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(), "Labels don't match ret = " << label.get() << "; expected = " << peerCredentials.label()); - }, "tccgd02"); + }); } RUNNER_MULTIPROCESS_TEST(tccgd01_gdbus_credentials_client_pid) @@ -475,7 +822,7 @@ void testGdbusUserUid(cynara_user_creds method = USER_METHOD_UID) { uid_t uid = std::stoul(uidStr.get()); RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid << "; expected = "<< peerCredentials.uid()); - }, "tccgd04"); + }); } void testGdbusUserGid(cynara_user_creds method = USER_METHOD_GID) { @@ -485,7 +832,7 @@ void testGdbusUserGid(cynara_user_creds method = USER_METHOD_GID) { const ProcessCredentials &) { GStringPtr gidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn), requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED)); - }, "tccgd04"); + }); } RUNNER_MULTIPROCESS_TEST(tccgd04_gdbus_credentials_user_uid) @@ -513,16 +860,341 @@ RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_user_default) { } } -RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_pid) { +RUNNER_MULTIPROCESS_TEST(tccgd07_gdbus_credentials_pid) { gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t expectedPid, const std::string &requestedName, const ProcessCredentials &) { auto helperPid = CynaraHelperCredentials::gdbusGetPid(std::move(conn), requestedName.c_str()); RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid << "; expected = " << expectedPid); - }, "tccgd06"); + }); +} + +void testGDbusCredsDefault(int expectedResult, char **pClientBuff, + char **pUserBuff, pid_t *pPidCred) +{ + // reset variables if set by previous test cases + if (pClientBuff != nullptr) *pClientBuff = nullptr; + if (pUserBuff != nullptr) *pUserBuff = nullptr; + if (pPidCred != nullptr) *pPidCred = -1; + + gdbusTestTemplate([&] (GDBusConnectionPtr conn, pid_t pid, + const std::string &requestedName, + const ProcessCredentials &peerCredentials) { + cynara_user_creds userMethod = getUserDefaultMethod(); + cynara_client_creds clientMethod = getClientDefaultMethod(); + + // exception override + if (expectedResult == CYNARA_API_SUCCESS && userMethod == USER_METHOD_GID) { + expectedResult = CYNARA_API_METHOD_NOT_SUPPORTED; + } + + int ret = cynara_creds_gdbus_get_default(conn.get(), requestedName.c_str(), + pClientBuff, pUserBuff, pPidCred); + + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_creds_gdbus_get_default returned unexpected result: " << ret << + "; expected: " << expectedResult); + + if (ret != CYNARA_API_SUCCESS) { + return; + } + + if (pClientBuff != nullptr) { + switch (clientMethod) { + case CLIENT_METHOD_SMACK: + RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(), + "Client labels don't match, ret = " + << *pClientBuff << "; expected = " + << peerCredentials.label()); + g_free(*pClientBuff); + break; + case CLIENT_METHOD_PID: + RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid), + "PIDs don't match, ret = " + << *pClientBuff << "; expected = " << pid); + g_free(*pClientBuff); + break; + default: + break; + } + } + + if (pUserBuff != nullptr) { + switch (userMethod) { + case USER_METHOD_UID: + RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()), + "UIDs don't match, ret = " + << *pUserBuff + << "; expected = " << peerCredentials.uid()); + g_free(*pUserBuff); + break; + default: + break; + } + } + + if (pPidCred != nullptr) { + RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = " + << *pPidCred + << "; expected = " << pid); + } + }); +} + +RUNNER_MULTIPROCESS_TEST(tccgd08_gdbus_credentials_default_positive_all) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, &pidCred); +} + +RUNNER_MULTIPROCESS_TEST(tccgd09_gdbus_credentials_default_positive_partial_single) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, nullptr); + testGDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, nullptr); + testGDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, nullptr, &pidCred); } +RUNNER_MULTIPROCESS_TEST(tccgd10_gdbus_credentials_default_positive_partial_double) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, &pidCred); + testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, &pidCred); + testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, nullptr); +} + +RUNNER_MULTIPROCESS_TEST(tccgd11_gdbus_credentials_default_negative_incomplete) +{ + testGDbusCredsDefault(CYNARA_API_INVALID_PARAM, nullptr, nullptr, nullptr); +} + +void testGDbusCreds(int expectedResult, + const cynara_client_creds *pClientMethod, char **pClientBuff, + const cynara_user_creds *pUserMethod, char **pUserBuff, + pid_t *pPidCred) +{ + // reset variables if set by previous test cases + if (pClientBuff != nullptr) *pClientBuff = nullptr; + if (pUserBuff != nullptr) *pUserBuff = nullptr; + if (pPidCred != nullptr) *pPidCred = -1; + + gdbusTestTemplate([&] (GDBusConnectionPtr conn, pid_t pid, + const std::string &requestedName, + const ProcessCredentials &peerCredentials) { + + int ret = cynara_creds_gdbus_get(conn.get(), requestedName.c_str(), + pClientMethod, pClientBuff, + pUserMethod, pUserBuff, pPidCred); + + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_creds_gdbus_get returned unexpected result: " << ret << + "; expected: " << expectedResult); + + if (ret != CYNARA_API_SUCCESS) { + return; + } + + if (pClientBuff != nullptr && pClientMethod != nullptr) { + switch (*pClientMethod) { + case CLIENT_METHOD_SMACK: + RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(), + "Client labels don't match, ret = " + << *pClientBuff << "; expected = " + << peerCredentials.label()); + g_free(*pClientBuff); + break; + case CLIENT_METHOD_PID: + RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid), + "PIDs don't match, ret = " + << *pClientBuff << "; expected = " << pid); + g_free(*pClientBuff); + break; + default: + break; + } + } + + if (pUserBuff != nullptr && pUserMethod != nullptr) { + switch (*pUserMethod) { + case USER_METHOD_UID: + RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()), + "UIDs don't match, ret = " + << *pUserBuff + << "; expected = " << peerCredentials.uid()); + g_free(*pUserBuff); + break; + default: + break; + } + } + + if (pPidCred != nullptr) { + RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = " + << *pPidCred + << "; expected = " << pid); + } + }); +} + +RUNNER_MULTIPROCESS_TEST(tccgd12_gdbus_credentials_positive_all) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_SMACK, &clientBuff, + &G_USER_METHOD_UID, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_UID, &userBuff, &pidCred); + + testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_SMACK, &clientBuff, + &G_USER_METHOD_GID, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_GID, &userBuff, &pidCred); +} + +RUNNER_MULTIPROCESS_TEST(tccgd13_gdbus_credentials_positive_partial_single) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_DEFAULT, nullptr, + nullptr); + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_SMACK, &clientBuff, + nullptr, nullptr, + nullptr); + + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, nullptr, + &G_USER_METHOD_DEFAULT, &userBuff, + nullptr); + testGDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, &userBuff, + nullptr); + + testGDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, nullptr, + &pidCred); + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, nullptr, + nullptr, nullptr, + &pidCred); + testGDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + nullptr, nullptr, + &pidCred); + } + +RUNNER_MULTIPROCESS_TEST(tccgd14_gdbus_credentials_positive_partial_double) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, nullptr, + &G_USER_METHOD_UID, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_SUCCESS, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_PID, &clientBuff, + &G_USER_METHOD_DEFAULT, nullptr, &pidCred); + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + nullptr, nullptr, &pidCred); + + testGDbusCreds(CYNARA_API_SUCCESS, + &G_CLIENT_METHOD_SMACK, &clientBuff, + &G_USER_METHOD_UID, &userBuff, nullptr); +} + +RUNNER_MULTIPROCESS_TEST(tccgd14_gdbus_credentials_negative_incomplete) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, &clientBuff, + &G_USER_METHOD_UID, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, &clientBuff, + &G_USER_METHOD_UID, nullptr, &pidCred); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, &clientBuff, + nullptr, nullptr, &pidCred); + + testGDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + nullptr, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, nullptr, + nullptr, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, nullptr, + nullptr, &userBuff, &pidCred); + + testGDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, nullptr, + &G_USER_METHOD_UID, nullptr, nullptr); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + &G_CLIENT_METHOD_DEFAULT, nullptr, + nullptr, nullptr, nullptr); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, nullptr, + &G_USER_METHOD_DEFAULT, nullptr, nullptr); + testGDbusCreds(CYNARA_API_INVALID_PARAM, + nullptr, nullptr, + nullptr, nullptr, nullptr); +} + +RUNNER_MULTIPROCESS_TEST(tccgd14_gdbus_credentials_negative_incorrect) +{ + char *clientBuff = nullptr; + char *userBuff = nullptr; + pid_t pidCred = -1; + + testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_LOW, &clientBuff, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_HIGH, &clientBuff, + &G_USER_METHOD_DEFAULT, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + &G_USER_METHOD_LOW, &userBuff, &pidCred); + testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED, + &G_CLIENT_METHOD_DEFAULT, &clientBuff, + &G_USER_METHOD_HIGH, &userBuff, &pidCred); +} + + +//================================SdBus=============================================== SdBusConnectionPtr createSdBusConnection(const std::string &requestedName) { sd_bus *bus = NULL; -- 2.7.4