Add tests for cynara-creds-gdbus 31/38531/10
authorJacek Bukarewicz <j.bukarewicz@samsung.com>
Thu, 9 Apr 2015 09:45:14 +0000 (11:45 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 29 Jun 2016 10:38:51 +0000 (12:38 +0200)
Change-Id: I60b759f81845cb4fa6765de00a90a305358dc211
Signed-off-by: Radoslaw Bartosiak <r.bartosiak@samsung.com>
Signed-off-by: Lukasz Kostyra <l.kostyra@samsung.com>
packaging/security-tests.spec
src/common/memory.h
src/cynara-tests/CMakeLists.txt
src/cynara-tests/common/cynara_test_helpers.cpp
src/cynara-tests/common/cynara_test_helpers.h
src/cynara-tests/common/cynara_test_helpers_dbus.h
src/cynara-tests/test_cases_helpers.cpp

index d48021a..c382bfe 100644 (file)
@@ -22,6 +22,7 @@ BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(libwebappenc)
 BuildRequires: cynara-devel
 BuildRequires: libcynara-creds-dbus-devel
+BuildRequires: libcynara-creds-gdbus-devel
 BuildRequires: pkgconfig(libtzplatform-config)
 BuildRequires: boost-devel
 BuildRequires: pkgconfig(vconf)
index 4ed2407..1ee120b 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2014-2015 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.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
 #ifndef MEMORY_H
 #define MEMORY_H
 
@@ -5,6 +20,7 @@
 #include <sys/smack.h>
 #include <dirent.h>
 #include <glob.h>
+#include <glib.h>
 
 #define DEFINE_SMARTPTR(func, type, name) \
     struct deleter_##func { \
@@ -22,6 +38,7 @@ void closePtr(int *fd);
 // Defining specializations of unique_ptr
 
 DEFINE_SMARTPTR(free, char, CStringPtr);
+DEFINE_SMARTPTR(g_free, gchar, GStringPtr);
 DEFINE_SMARTPTR(closePtr, int, FdUniquePtr);
 DEFINE_SMARTPTR(smack_accesses_free, smack_accesses, SmackAccessesPtr);
 DEFINE_SMARTPTR(closedir, DIR, DirPtr);
index 83f4d40..fa5ccd3 100644 (file)
@@ -25,9 +25,11 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
     cynara-client
     cynara-client-async
     cynara-creds-dbus
+    cynara-creds-gdbus
     cynara-creds-socket
     cynara-plugin
     dbus-1
+    glib-2.0
     )
 
 #files to compile
@@ -72,8 +74,10 @@ ADD_EXECUTABLE(${CYNARA_TARGET_TEST} ${CYNARA_TARGET_TEST_SOURCES})
 #linker directories
 TARGET_LINK_LIBRARIES(${CYNARA_TARGET_TEST}
     ${CYNARA_TARGET_DEP_LIBRARIES}
-    dpl-test-framework
     tests-common
+    dpl-test-framework
+    gobject-2.0
+    gio-2.0
     )
 
 #place for output file
index a86bead..da39c40 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <cynara-creds-dbus.h>
 #include <cynara-creds-socket.h>
+#include <cynara-creds-gdbus.h>
 
 #include "cynara_test_helpers.h"
 
@@ -84,4 +85,32 @@ pid_t dbusGetPid(DBusConnectionPtr connection, const char *uniqueName, int expec
     return pid;
 }
 
+char *gdbusGetClient(GDBusConnectionPtr connection, const char *uniqueName,
+                     cynara_client_creds method, int expectedResult) {
+    char *buff;
+    auto ret = cynara_creds_gdbus_get_client(connection.get(), uniqueName, method, &buff);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_gdbus_get_client failed, ret = " << ret
+                      << "; expected = " << expectedResult);
+    return buff;
+}
+
+char *gdbusGetUser(GDBusConnectionPtr connection, const char *uniqueName, cynara_user_creds method,
+                   int expectedResult) {
+    char *buff;
+    auto ret = cynara_creds_gdbus_get_user(connection.get(), uniqueName, method, &buff);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_gdbus_get_user failed, ret = " << ret
+                      << "; expected = " << expectedResult);
+    return buff;
+}
+
+pid_t gdbusGetPid(GDBusConnectionPtr connection, const char *uniqueName) {
+    pid_t pid;
+    auto ret = cynara_creds_gdbus_get_pid(connection.get(), uniqueName, &pid);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                      "cynara_creds_gdbus_get_pid failed, ret = " << ret);
+    return pid;
+}
+
 } //namespace CynaraHelperCredentials
index 8bfc646..d790fe3 100644 (file)
@@ -28,7 +28,6 @@
 
 #include <cynara-creds-commons.h>
 #include <cynara-error.h>
-
 #include <cynara_test_helpers_dbus.h>
 
 namespace CynaraHelperCredentials {
@@ -53,6 +52,15 @@ char *dbusGetUser(DBusConnectionPtr connection, const char *uniqueName, cynara_u
 pid_t dbusGetPid(DBusConnectionPtr connection, const char *uniqueName,
                  int expectedResult = CYNARA_API_SUCCESS);
 
+char *gdbusGetClient(GDBusConnectionPtr connection, const char *uniqueName,
+                     cynara_client_creds method, int expectedResult = CYNARA_API_SUCCESS);
+
+
+char *gdbusGetUser(GDBusConnectionPtr connection, const char *uniqueName, cynara_user_creds method,
+                   int expectedResult = CYNARA_API_SUCCESS);
+
+pid_t gdbusGetPid(GDBusConnectionPtr connection, const char *uniqueName);
+
 } // namespace CynaraHelperCredentials
 
 #endif // CYNARA_TEST_HELPERS_H_
index c348b08..46c827e 100644 (file)
 
 #include <dbus/dbus.h>
 #include <functional>
+#include <gio/gio.h>
 #include <memory>
 
 typedef std::function<void(DBusConnection *)> DBusConnectionCloseAndUnref;
+typedef std::function<void(GDBusConnection *)> GDBusConnectionCloseAndUnref;
 typedef std::unique_ptr<DBusConnection, DBusConnectionCloseAndUnref> DBusConnectionPtr;
+typedef std::unique_ptr<GDBusConnection, GDBusConnectionCloseAndUnref> GDBusConnectionPtr;
 
 
 #endif // CYNARA_TEST_HELPERS_DBUS_H_
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");
+}