Add sd-bus cynara API tests
[platform/core/test/security-tests.git] / src / cynara-tests / test_cases_helpers.cpp
index 33481a6..ec5ce0e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2018 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.
@@ -31,6 +31,7 @@
 #include <dbus/dbus.h>
 #include <glib-object.h>
 #include <gio/gio.h>
+#include <systemd/sd-bus.h>
 
 #include <tests_common.h>
 #include <access_provider.h>
@@ -525,6 +526,158 @@ RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_pid) {
     }, "tccgd06");
 }
 
+
+SdBusConnectionPtr createSdBusConnection(const std::string &requestedName) {
+    sd_bus *bus = NULL;
+
+    int r = sd_bus_default_system(&bus);
+    RUNNER_ASSERT_MSG(r >= 0, "Failed to connect do system bus: %s" << strerror(-r));
+
+    if (requestedName.empty() == false) {
+        r = sd_bus_request_name(bus, requestedName.c_str(), SD_BUS_NAME_REPLACE_EXISTING);
+        RUNNER_ASSERT_MSG(r >= 0, "Error in sd_bus_request_name");
+    }
+
+    SdBusConnectionPtr ret(bus, [] (sd_bus *busConnection) {
+        sd_bus_unref(busConnection);
+    });
+
+    return ret;
+}
+
+typedef std::function<void(SdBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
+                           const ProcessCredentials &peerCredentials)> SdBusAssertionFn;
+
+void sdBusTestTemplate(SdBusAssertionFn assertion) {
+    std::string requestedName = "tests.dbus.cynara";
+    const ProcessCredentials peerCredentials;
+
+    SynchronizationPipe pipe;
+    pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
+                           std::cref(peerCredentials)));
+
+    pipe.claimParentEp();
+    pipe.wait();
+
+    auto conn = createSdBusConnection("");
+    assertion(std::move(conn), pid, requestedName, peerCredentials);
+    pipe.post();
+}
+
+
+RUNNER_TEST_GROUP_INIT(cynara_creds_sd_bus)
+
+void testSdBusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
+    sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t pid,
+                        const std::string &requestedName,
+                        const ProcessCredentials &) {
+        CStringPtr clientPidStr(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
+                                requestedName.c_str(), method, CYNARA_API_SUCCESS));
+        pid_t clientPid = std::stoi(clientPidStr.get());
+        RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
+                          << "; expected = " << pid);
+    });
+}
+
+void testSdBusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
+    sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
+                        const std::string &requestedName,
+                        const ProcessCredentials &peerCredentials) {
+        CStringPtr label(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
+                         requestedName.c_str(), method, CYNARA_API_SUCCESS));
+        RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
+                          "Labels don't match ret = " << label.get()
+                          << "; expected = " << peerCredentials.label());
+    });
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd01_sd_bus_credentials_client_pid)
+{
+    testSdBusClientPid();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccsd02_sd_bus_credentials_client_smack)
+{
+    testSdBusClientSmack();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccsd03_sd_bus_credentials_client_default)
+{
+    auto method = getClientDefaultMethod();
+    switch(method) {
+    case CLIENT_METHOD_SMACK:
+        testSdBusClientSmack(CLIENT_METHOD_DEFAULT);
+        break;
+    case CLIENT_METHOD_PID:
+        testSdBusClientPid(CLIENT_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
+                        << method);
+    }
+}
+
+void testSdBusUserUid(cynara_user_creds method = USER_METHOD_UID) {
+    sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
+                        const std::string &requestedName,
+                        const ProcessCredentials &peerCredentials) {
+        CStringPtr uidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
+                          requestedName.c_str(), method, CYNARA_API_SUCCESS));
+        uid_t uid = std::stoul(uidStr.get());
+        RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
+                          << "; expected = "<< peerCredentials.uid());
+    });
+}
+
+void testSdBusUserGid(cynara_user_creds method = USER_METHOD_GID) {
+    sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
+                        const std::string &requestedName,
+                        const ProcessCredentials &peerCredentials) {
+        CStringPtr gidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
+                          requestedName.c_str(), method, CYNARA_API_SUCCESS));
+        gid_t gid = std::stoul(gidStr.get());
+        RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
+                          << "; expected = "<< peerCredentials.gid());
+    });
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd04_sd_bus_credentials_user_uid)
+{
+    testSdBusUserUid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd05_sd_bus_credentials_user_gid)
+{
+    testSdBusUserGid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccsd06_sd_bus_credentials_user_default)
+{
+    auto method = getUserDefaultMethod();
+    switch(method) {
+    case USER_METHOD_UID:
+        testSdBusUserUid(USER_METHOD_DEFAULT);
+        break;
+    case USER_METHOD_GID:
+        testSdBusUserGid(USER_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
+                        << method);
+    }
+}
+
+RUNNER_TEST_SMACK(tccd07_sd_bus_credentials_pid) {
+    sdBusTestTemplate([] (SdBusConnectionPtr conn, pid_t expectedPid,
+                          const std::string &requestedName, const ProcessCredentials &) {
+        auto helperPid = CynaraHelperCredentials::sdBusGetPid(std::move(conn),
+                         requestedName.c_str(), CYNARA_API_SUCCESS);
+        RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
+                          << "; expected = " << expectedPid);
+    });
+}
+
+
 RUNNER_TEST_GROUP_INIT(cynara_creds_self)
 
 void testCredsClientSelf(cynara_client_creds method, const std::string &expected) {