Add tests for cynara-creds-gdbus
[platform/core/test/security-tests.git] / src / cynara-tests / test_cases_helpers.cpp
index a8ff29f..55f899c 100644 (file)
@@ -29,6 +29,8 @@
 #include <utility>
 
 #include <dbus/dbus.h>
+#include <glib-object.h>
+#include <gio/gio.h>
 
 #include <tests_common.h>
 #include <access_provider.h>
@@ -41,6 +43,7 @@
 
 #include <cynara_test_helpers.h>
 #include <cynara_test_helpers_dbus.h>
+#include <cynara-creds-gdbus.h>
 
 class ProcessCredentials {
 public:
@@ -276,3 +279,78 @@ RUNNER_TEST_SMACK(tccd04_dbus_credentials_pid) {
                           << "; expected = " << expectedPid);
     }, "tccd04");
 }
+
+GDBusConnectionPtr createGDBusConnection() {
+    GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
+
+    return GDBusConnectionPtr(conn, [] (GDBusConnection *conn) {
+        g_object_unref(G_OBJECT(conn));
+    });
+}
+
+
+typedef std::function<void(GDBusConnectionPtr conn, pid_t pid,
+                           const std::string &requestedName,
+                           const ProcessCredentials &peerCredentials)> GDBusAssertionFn;
+
+void gdbusTestTemplate(GDBusAssertionFn assertion, const std::string &/*scope*/) {
+    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 = createGDBusConnection();
+    assertion(std::move(conn), pid, requestedName, peerCredentials);
+    pipe.post();
+}
+
+
+RUNNER_TEST_GROUP_INIT(cynara_creds_gdbus)
+
+RUNNER_TEST_SMACK(tccgd01_gdbus_credentials_client_pid) {
+    gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
+                          const ProcessCredentials &) {
+        GStringPtr clientPidStr(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
+                                requestedName.c_str(), CLIENT_METHOD_PID));
+        pid_t clientPid = std::stoi(clientPidStr.get());
+        RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
+                          << "; expected = " << pid);
+    }, "tccgd01");
+}
+
+RUNNER_TEST_SMACK(tccgd02_gdbus_credentials_client_smack) {
+    gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t, const std::string &requestedName,
+                          const ProcessCredentials &peerCredentials) {
+        GStringPtr label(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
+                         requestedName.c_str(), CLIENT_METHOD_SMACK));
+        RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
+                          "Labels don't match ret = " << label.get()
+                          << "; expected = " << peerCredentials.label());
+    }, "tccgd02");
+}
+
+RUNNER_TEST_SMACK(tccgd03_gdbus_credentials_user_uid) {
+    gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t, const std::string &requestedName,
+                          const ProcessCredentials &peerCredentials) {
+        GStringPtr uidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
+                          requestedName.c_str(), 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());
+    }, "tccgd03");
+}
+
+RUNNER_TEST_SMACK(tccgd04_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);
+    }, "tccgd04");
+}