Tests for new sd-bus credentials API sandbox/lpawelczyk/creds-tests
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 31 Jan 2023 16:14:13 +0000 (17:14 +0100)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 31 Jan 2023 16:19:20 +0000 (17:19 +0100)
Change-Id: I8763295675a8d19fb34f9b3957ee1cb65f77791d

src/cynara-tests/test_cases_helpers.cpp

index 00ae296e798871c12f1a06d96c898728ae8caf78..42e305755429263ef49c33d094140bc65ca621e6 100644 (file)
@@ -46,6 +46,7 @@
 
 #include <cynara-creds-dbus.h>
 #include <cynara-creds-gdbus.h>
+#include <cynara-creds-sd-bus.h>
 #include <cynara-creds-self.h>
 #include <scoped_process_label.h>
 
@@ -1346,6 +1347,331 @@ RUNNER_TEST_SMACK(tccd07_sd_bus_credentials_pid) {
     });
 }
 
+void testSdBusCredsDefault(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;
+
+    sdBusTestTemplate([&] (SdBusConnectionPtr 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_sd_bus_get_default(conn.get(), requestedName.c_str(),
+                                                  pClientBuff, pUserBuff, pPidCred);
+
+        RUNNER_ASSERT_MSG(ret == expectedResult,
+                          "cynara_creds_sd_bus_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(tccsd08_sd_bus_credentials_default_positive_all)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, &pidCred);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd09_sd_bus_credentials_default_positive_partial_single)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, nullptr);
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, nullptr);
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, nullptr, nullptr, &pidCred);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd10_sd_bus_credentials_default_positive_partial_double)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, &pidCred);
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, &pidCred);
+    testSdBusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, nullptr);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd11_sd_bus_credentials_default_negative_incomplete)
+{
+    testDbusCredsDefault(CYNARA_API_INVALID_PARAM, nullptr, nullptr, nullptr);
+}
+
+void testSdBusCreds(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;
+
+    sdBusTestTemplate([&] (SdBusConnectionPtr conn, pid_t pid,
+                           const std::string &requestedName,
+                           const ProcessCredentials &peerCredentials) {
+
+        int ret = cynara_creds_sd_bus_get(conn.get(), requestedName.c_str(),
+                                          pClientMethod, pClientBuff,
+                                          pUserMethod, pUserBuff, pPidCred);
+
+        RUNNER_ASSERT_MSG(ret == expectedResult,
+                          "cynara_creds_sd_bus_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(tccsd12_sd_bus_credentials_positive_all)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_DEFAULT, &clientBuff,
+                   &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_SMACK, &clientBuff,
+                   &G_USER_METHOD_UID, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_PID, &clientBuff,
+                   &G_USER_METHOD_UID, &userBuff, &pidCred);
+
+    testSdBusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
+                   &G_CLIENT_METHOD_SMACK, &clientBuff,
+                   &G_USER_METHOD_GID, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
+                   &G_CLIENT_METHOD_PID, &clientBuff,
+                   &G_USER_METHOD_GID, &userBuff, &pidCred);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd13_sd_bus_credentials_positive_partial_single)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_PID, &clientBuff,
+                   &G_USER_METHOD_DEFAULT, nullptr,
+                   nullptr);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_SMACK, &clientBuff,
+                   nullptr, nullptr,
+                   nullptr);
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_DEFAULT, nullptr,
+                   &G_USER_METHOD_DEFAULT, &userBuff,
+                   nullptr);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   nullptr, nullptr,
+                   &G_USER_METHOD_DEFAULT, &userBuff,
+                   nullptr);
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   nullptr, nullptr,
+                   &G_USER_METHOD_DEFAULT, nullptr,
+                   &pidCred);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_DEFAULT, nullptr,
+                   nullptr, nullptr,
+                   &pidCred);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   nullptr, nullptr,
+                   nullptr, nullptr,
+                   &pidCred);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd14_sd_bus_credentials_positive_partial_double)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_DEFAULT, nullptr,
+                   &G_USER_METHOD_UID, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   nullptr, nullptr,
+                   &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_PID, &clientBuff,
+                   &G_USER_METHOD_DEFAULT, nullptr, &pidCred);
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_DEFAULT, &clientBuff,
+                   nullptr, nullptr, &pidCred);
+
+    testSdBusCreds(CYNARA_API_SUCCESS,
+                   &G_CLIENT_METHOD_SMACK, &clientBuff,
+                   &G_USER_METHOD_UID, &userBuff, nullptr);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd14_sd_bus_credentials_negative_incomplete)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   nullptr, &clientBuff,
+                   &G_USER_METHOD_UID, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   nullptr, &clientBuff,
+                   &G_USER_METHOD_UID, nullptr, &pidCred);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   nullptr, &clientBuff,
+                   nullptr, nullptr, &pidCred);
+
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   &G_CLIENT_METHOD_DEFAULT, &clientBuff,
+                   nullptr, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   &G_CLIENT_METHOD_DEFAULT, nullptr,
+                   nullptr, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   nullptr, nullptr,
+                   nullptr, &userBuff, &pidCred);
+
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   &G_CLIENT_METHOD_DEFAULT, nullptr,
+                   &G_USER_METHOD_UID, nullptr, nullptr);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   &G_CLIENT_METHOD_DEFAULT, nullptr,
+                   nullptr, nullptr, nullptr);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   nullptr, nullptr,
+                   &G_USER_METHOD_DEFAULT, nullptr, nullptr);
+    testSdBusCreds(CYNARA_API_INVALID_PARAM,
+                   nullptr, nullptr,
+                   nullptr, nullptr, nullptr);
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd14_sd_bus_credentials_negative_incorrect)
+{
+    char *clientBuff = nullptr;
+    char *userBuff = nullptr;
+    pid_t pidCred = -1;
+
+    testSdBusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
+                   &G_CLIENT_METHOD_LOW, &clientBuff,
+                   &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
+                   &G_CLIENT_METHOD_HIGH, &clientBuff,
+                   &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
+                   &G_CLIENT_METHOD_DEFAULT, &clientBuff,
+                   &G_USER_METHOD_LOW, &userBuff, &pidCred);
+    testSdBusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
+                   &G_CLIENT_METHOD_DEFAULT, &clientBuff,
+                   &G_USER_METHOD_HIGH, &userBuff, &pidCred);
+}
+
+
+//===============================SELF======================================
 
 RUNNER_TEST_GROUP_INIT(cynara_creds_self)