Merge branch 'tizen' into yaca 52/96652/1
authorZbigniew Jasinski <z.jasinski@samsung.com>
Wed, 9 Nov 2016 14:03:25 +0000 (15:03 +0100)
committerZbigniew Jasinski <z.jasinski@samsung.com>
Wed, 9 Nov 2016 14:07:54 +0000 (15:07 +0100)
Change-Id: I8f39cc854d3eaf1352574f2dd683cdf1e8dd5a1e
Signed-off-by: Zbigniew Jasinski <z.jasinski@samsung.com>
28 files changed:
packaging/security-tests.spec
src/ckm/ckm-common.cpp
src/ckm/ckm-common.h
src/common/CMakeLists.txt
src/common/access_provider.cpp
src/common/access_provider.h
src/common/cynara_helpers_creds.cpp [new file with mode: 0644]
src/common/cynara_helpers_creds.h [moved from src/cynara-tests/common/cynara_test_helpers.h with 54% similarity]
src/common/cynara_helpers_dbus.h [new file with mode: 0644]
src/common/memory.h
src/common/security-tests.conf
src/common/tests_common.cpp
src/common/tests_common.h
src/cynara-tests/CMakeLists.txt
src/cynara-tests/common/cynara_test_client_async_client.cpp
src/cynara-tests/common/cynara_test_client_async_client.h
src/cynara-tests/common/cynara_test_client_async_request_monitor.cpp
src/cynara-tests/common/cynara_test_client_async_request_monitor.h
src/cynara-tests/common/cynara_test_cynara_mask.cpp
src/cynara-tests/common/cynara_test_cynara_mask.h
src/cynara-tests/common/cynara_test_helpers.cpp [deleted file]
src/cynara-tests/db_patterns/default/checksum
src/cynara-tests/db_patterns/defaultAllowed/checksum
src/cynara-tests/db_patterns/nonEmptyDatabase/checksum
src/cynara-tests/test_cases_agent.cpp
src/cynara-tests/test_cases_helpers.cpp
src/framework/include/dpl/test/test_runner.h
src/framework/src/test_runner.cpp

index d9626c2..d9319fa 100644 (file)
@@ -22,6 +22,8 @@ BuildRequires: pkgconfig(libxml-2.0)
 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)
@@ -63,7 +65,10 @@ ln -sf /etc/smack/test_smack_rules %{buildroot}/etc/smack/test_smack_rules_lnk
 
 %post
 id -u  security_test_user 1>/dev/null 2>&1 || \
-    useradd -r -g users -s /sbin/nologin -c "for tests only" security_test_user
+    gum-utils -o -a --username=security_test_user --usertype=normal
+
+# Reload dbus daemon to apply newly installed configuration
+systemctl reload dbus
 
 echo "security-tests postinst done ..."
 
@@ -99,4 +104,4 @@ echo "security-tests postinst done ..."
 %{_prefix}/share/yaca-test
 
 %postun
-id -u security_test_user  1>/dev/null 2>&1 && userdel security_test_user
+id -u security_test_user 1>/dev/null 2>&1 && gum-utils -o -d --uid=`id -u security_test_user`
index 4f6880d..a1144c1 100644 (file)
@@ -95,13 +95,6 @@ std::string aliasWithLabel(const char *label, const char *alias)
     return std::string(alias);
 }
 
-// changes process label
-void change_label(const char* label)
-{
-    int ret = smack_set_label_for_self(label);
-    RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self("<<label<<"). Error: " << ret);
-}
-
 ScopedLabel::ScopedLabel(const char* label) : m_original_label(getLabel())
 {
     change_label(label);
index 9bfebe0..c3908ca 100644 (file)
@@ -90,9 +90,6 @@ std::string getOwnerIdFromSelf();
 
 std::string aliasWithLabel(const char *label, const char *alias);
 
-// changes process label
-void change_label(const char* label);
-
 // changes process label upon construction and restores it upon destruction
 class ScopedLabel
 {
index 994fee1..4e1055f 100644 (file)
@@ -8,6 +8,9 @@ PKG_CHECK_MODULES(COMMON_TARGET_DEP
     sqlite3
     libgum
     glib-2.0
+    cynara-creds-socket
+    cynara-creds-dbus
+    cynara-creds-gdbus
     REQUIRED
     )
 
@@ -28,6 +31,7 @@ SET(COMMON_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/src/common/synchronization_pipe.cpp
     ${PROJECT_SOURCE_DIR}/src/common/timeout.cpp
     ${PROJECT_SOURCE_DIR}/src/common/temp_test_user.cpp
+    ${PROJECT_SOURCE_DIR}/src/common/cynara_helpers_creds.cpp
     )
 
 #system and local includes
index e9f91dc..1d3257a 100644 (file)
 
 namespace SecurityServer {
 
-AccessProvider::AccessProvider(const std::string &mySubject)
-  : m_mySubject(mySubject)
+AccessProvider::AccessProvider(const std::string &myLabel)
+  : m_myLabel(myLabel)
 {}
 
 void AccessProvider::allowSS() {
-    m_smackAccess.add(m_mySubject, "System::Run", "x");
+    m_smackAccess.add(m_myLabel, "System::Run", "x");
+}
+
+void AccessProvider::addSubjectRule(const std::string &subject, const std::string &rule) {
+    m_smackAccess.add(subject, m_myLabel, rule);
 }
 
 void AccessProvider::addObjectRule(const std::string &object, const std::string &rule) {
-    m_smackAccess.add(m_mySubject, object, rule);
+    m_smackAccess.add(m_myLabel, object, rule);
 }
 
 void AccessProvider::apply() {
@@ -48,10 +52,10 @@ void AccessProvider::apply() {
 }
 
 void AccessProvider::applyAndSwithToUser(int uid, int gid) {
-    RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_mySubject.c_str()),
-        "Error in smack_revoke_subject(" << m_mySubject << ")");
+    RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_myLabel.c_str()),
+        "Error in smack_revoke_subject(" << m_myLabel << ")");
     apply();
-    RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(m_mySubject.c_str()),
+    RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(m_myLabel.c_str()),
         "Error in smack_set_label_for_self.");
     RUNNER_ASSERT_MSG(0 == setgid(gid),
         "Error in setgid.");
index 452d418..26124b4 100644 (file)
@@ -30,11 +30,12 @@ namespace SecurityServer {
 
 class AccessProvider {
 public:
-    AccessProvider(const std::string &mySubject);
+    AccessProvider(const std::string &myLabel);
 
     AccessProvider(const AccessProvider &second) = delete;
     AccessProvider& operator=(const AccessProvider &second) = delete;
 
+    void addSubjectRule(const std::string &subject, const std::string &rule);
     void addObjectRule(const std::string &object, const std::string &rule);
     void allowSS();
     void apply();
@@ -42,7 +43,7 @@ public:
 
     virtual ~AccessProvider(){}
 private:
-    std::string m_mySubject;
+    std::string m_myLabel;;
     SmackAccess m_smackAccess;
 };
 
diff --git a/src/common/cynara_helpers_creds.cpp b/src/common/cynara_helpers_creds.cpp
new file mode 100644 (file)
index 0000000..dc1c904
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2015-2016 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.
+ */
+/**
+ * @file        cynara_helper_credentials.cpp
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Helpers for cynara-helpers
+ */
+
+#include <dpl/test/test_runner.h>
+
+#include <cynara-creds-dbus.h>
+#include <cynara-creds-socket.h>
+#include <cynara-creds-gdbus.h>
+
+#include "cynara_helpers_creds.h"
+
+namespace CynaraHelperCredentials {
+
+char *socketGetClient(int sock, cynara_client_creds method, int expectedResult) {
+    char *buff = nullptr;
+    auto ret = cynara_creds_socket_get_client(sock, method, &buff);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_socket_get_client failed, ret = " << ret
+                      << "; expected = " << expectedResult);
+    return buff;
+}
+
+char *socketGetUser(int sock, cynara_user_creds method, int expectedResult) {
+    char *buff = nullptr;
+    auto ret = cynara_creds_socket_get_user(sock, method, &buff);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_socket_get_user failed, ret = " << ret
+                      << "; expected = " << expectedResult);
+    return buff;
+}
+
+pid_t socketGetPid(int sock, int expectedResult) {
+    pid_t pid = -1;
+    auto ret = cynara_creds_socket_get_pid(sock, &pid);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_socket_get_pid failed, ret = " << ret << "; expected = "
+                      << expectedResult);
+    return pid;
+}
+
+char *dbusGetClient(DBusConnectionPtr connection, const char *uniqueName,
+                    cynara_client_creds method, int expectedResult) {
+    char *buff = nullptr;
+    auto ret = cynara_creds_dbus_get_client(connection.get(), uniqueName, method, &buff);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_dbus_get_client failed, ret = " << ret
+                      << "; expected = " << expectedResult);
+    return buff;
+}
+
+char *dbusGetUser(DBusConnectionPtr connection, const char *uniqueName, cynara_user_creds method,
+                  int expectedResult) {
+    char *buff = nullptr;
+    auto ret = cynara_creds_dbus_get_user(connection.get(), uniqueName, method, &buff);
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                      "cynara_creds_dbus_get_user failed, ret = " << ret
+                      << "; expected = " << expectedResult);
+    return buff;
+}
+
+pid_t dbusGetPid(DBusConnectionPtr connection, const char *uniqueName, int expectedResult) {
+    pid_t pid = -1;
+    auto ret = cynara_creds_dbus_get_pid(connection.get(), uniqueName, &pid);
+    RUNNER_ASSERT_MSG(ret == expectedResult, "cynara_creds_dbus_get_pid failed, ret = " << ret
+                                             << "; expected = " << expectedResult);
+    return pid;
+}
+
+char *gdbusGetClient(GDBusConnectionPtr connection, const char *uniqueName,
+                     cynara_client_creds method, int expectedResult) {
+    char *buff = nullptr;
+    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 = nullptr;
+    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 = -1;
+    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
similarity index 54%
rename from src/cynara-tests/common/cynara_test_helpers.h
rename to src/common/cynara_helpers_creds.h
index 1acd6f8..cafe423 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2016 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.
@@ -14,7 +14,7 @@
  *    limitations under the License.
  */
 /**
- * @file        cynara_test_helpers.h
+ * @file        cynara_helper_credentials.h
  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
  * @version     1.0
  * @brief       Helpers for cynara-helpers
 #ifndef CYNARA_TEST_HELPERS_H_
 #define CYNARA_TEST_HELPERS_H_
 
+#include <string>
 #include <sys/types.h>
 
 #include <cynara-creds-commons.h>
 #include <cynara-error.h>
+#include <cynara_helpers_dbus.h>
 
 namespace CynaraHelperCredentials {
 
@@ -38,7 +40,27 @@ char *socketGetUser(int sock, cynara_user_creds method,
 
 pid_t socketGetPid(int sock, int expectedResult = CYNARA_API_SUCCESS);
 
-} // namespace CynaraHelperCredentials
+DBusConnectionPtr createDBusConnection(const std::string &name);
+
+char *dbusGetClient(DBusConnectionPtr connection, const char *uniqueName,
+                    cynara_client_creds method, int expectedResult = CYNARA_API_SUCCESS);
+
+
+char *dbusGetUser(DBusConnectionPtr connection, const char *uniqueName, cynara_user_creds method,
+                  int expectedResult = CYNARA_API_SUCCESS);
+
+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_
diff --git a/src/common/cynara_helpers_dbus.h b/src/common/cynara_helpers_dbus.h
new file mode 100644 (file)
index 0000000..19c1b24
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015-2016 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.
+ */
+/**
+ * @file        cynara_helpers_dbus.h
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+#ifndef CYNARA_TEST_HELPERS_DBUS_H_
+#define CYNARA_TEST_HELPERS_DBUS_H_
+
+#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 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 0ae6722..f3cc851 100644 (file)
@@ -3,8 +3,11 @@
 <busconfig>
   <!-- Only root can own this service -->
   <policy user="root">
-       <allow own="tests.dbus.client"/>
-       <allow own="test.method.caller"/>
-       <allow own="test.method.server"/>
+    <allow own="tests.dbus.client"/>
+    <allow own="test.method.caller"/>
+    <allow own="test.method.server"/>
+  </policy>
+  <policy user="security_test_user">
+    <allow own="tests.dbus.cynara"/>
   </policy>
 </busconfig>
index 470235d..e34f401 100644 (file)
@@ -230,3 +230,11 @@ void removeDir(const std::string &path)
 
     RUNNER_ASSERT_ERRNO_MSG(0 == rmdir(path.c_str()), "rmdir for <" << path << "> failed");
 }
+
+// changes process label
+void change_label(const char* label)
+{
+    int ret = smack_set_label_for_self(label);
+    RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self("<<label<<"). Error: " << ret);
+}
+
index b2d12fb..851e1db 100644 (file)
@@ -55,7 +55,7 @@ void mktreeSafe(const std::string &path, mode_t mode);
 void creatSafe(const std::string &path, mode_t mode);
 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
 void removeDir(const std::string &path);
-
+void change_label(const char* label);
 
 #define RUNNER_TEST_SMACK(Proc, ...)                                                        \
     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                                  \
@@ -145,4 +145,4 @@ void removeDir(const std::string &path);
     }                                                                                       \
     void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
 
-#endif
\ No newline at end of file
+#endif
index 98dcde1..7a42d65 100644 (file)
@@ -24,9 +24,13 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
     cynara-agent
     cynara-client
     cynara-client-async
+    cynara-creds-dbus
+    cynara-creds-gdbus
+    cynara-creds-self
     cynara-creds-socket
     cynara-plugin
     dbus-1
+    glib-2.0
     )
 
 #files to compile
@@ -43,7 +47,6 @@ SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_cynara_mask.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_env.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_file_operations.cpp
-    ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_helpers.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/plugins/plugins.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/cynara-test.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases.cpp
@@ -71,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 6c70813..57fc289 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * 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.
@@ -35,7 +35,7 @@ static std::string suffix(const std::string &major, const std::string &minor)
 CheckData::CheckData(const std::string &major, const std::string &minor) :
     m_client("client" + suffix(major, minor)),
     m_session("session" + suffix(major, minor)),
-    m_user("user" + suffix(major, minor)),
+    m_user("user" + suffix(major, std::string())),
     m_privilege("privilege" + suffix(major, minor))
 {
 }
@@ -63,15 +63,12 @@ Client::Client(const StatusFunction &userFunction)
     assertStatus(DISCONNECTED);
 }
 
-Client::~Client() noexcept(false)
+Client::~Client()
 {
-    bool oops = std::uncaught_exception();
     try {
         RUNNER_DEFER_SCOPE(cynara_async_finish(m_cynara););
         assertStatus(DISCONNECTED);
     } catch (...) {
-        if (!oops)
-            throw;
         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
     }
 }
index 37abdc5..425e8a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * 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.
@@ -51,7 +51,7 @@ public:
     };
 
     Client(const StatusFunction &userFunction = StatusFunction());
-    ~Client() noexcept(false);
+    ~Client();
 
     void assertStatus(enum SocketStatus expectedStatus);
     void checkCache(const CheckData &checkData, int expectedResult);
index 33244d0..da05fbd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * 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.
 
 namespace CynaraTestClientAsync {
 
-RequestMonitor::~RequestMonitor() noexcept(false)
+RequestMonitor::~RequestMonitor()
 {
-    bool oops = std::uncaught_exception();
     try {
         for (auto ent : m_requests)
         {
-            RUNNER_ERROR_MSG("There was no callback for request with:"
-                          << "id = " << ent.first << ","
-                          << "expectedResponse = " << ent.second.m_expectedResponse << ","
+            RUNNER_ERROR_MSG("There was no callback for request with: "
+                          << "id = " << ent.first << ", "
+                          << "expectedResponse = " << ent.second.m_expectedResponse << ", "
                           << "expectedCause = " << ent.second.m_expectedCause << ".");
         }
-        RUNNER_ASSERT_MSG(m_requests.empty(),
-                             m_requests.size() << "requests does not receive callback.");
+        if (!m_requests.empty())
+            RUNNER_ERROR_MSG(m_requests.size() << " requests did not receive callback.");
     } catch (...) {
-        if (!oops)
-            throw;
         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing"
                              " CynaraTestAsync::RequestMonitor.");
     }
index 16d4926..98f7f55 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * 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.
@@ -36,7 +36,7 @@ struct RequestEntity
 class RequestMonitor
 {
 public:
-    ~RequestMonitor() noexcept(false);
+    ~RequestMonitor();
 
     void registerRequest(cynara_check_id id, const RequestEntity &request);
 
index 7044895..1394761 100644 (file)
@@ -33,15 +33,12 @@ CynaraMask::CynaraMask() : m_serviceManager(CynaraTestConsts::SERVICE)
     m_serviceManager.stopService();
 }
 
-CynaraMask::~CynaraMask() noexcept(false)
+CynaraMask::~CynaraMask()
 {
-    bool oops = std::uncaught_exception();
     try {
         m_serviceManager.unmaskService();
         m_serviceManager.startService();
     } catch (...) {
-        if (!oops)
-            throw;
         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraMask.");
     }
 }
index fee1e66..f8f1724 100644 (file)
@@ -29,7 +29,7 @@ class CynaraMask
 {
 public:
     CynaraMask();
-    ~CynaraMask() noexcept(false);
+    ~CynaraMask();
 
 private:
     ServiceManager m_serviceManager;
diff --git a/src/cynara-tests/common/cynara_test_helpers.cpp b/src/cynara-tests/common/cynara_test_helpers.cpp
deleted file mode 100644 (file)
index 033fbec..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (c) 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.
- */
-/**
- * @file        cynara_test_helpers.cpp
- * @author      Aleksander Zdyb <a.zdyb@samsung.com>
- * @version     1.0
- * @brief       Helpers for cynara-helpers
- */
-
-#include <dpl/test/test_runner.h>
-
-#include <cynara-creds-socket.h>
-
-#include "cynara_test_helpers.h"
-
-namespace CynaraHelperCredentials {
-
-char *socketGetClient(int sock, cynara_client_creds method, int expectedResult) {
-    char *buff;
-    auto ret = cynara_creds_socket_get_client(sock, method, &buff);
-    RUNNER_ASSERT_MSG(ret == expectedResult,
-                      "cynara_creds_socket_get_client failed, ret = " << ret
-                      << "; expected = " << expectedResult);
-    return buff;
-}
-
-char *socketGetUser(int sock, cynara_user_creds method, int expectedResult) {
-    char *buff;
-    auto ret = cynara_creds_socket_get_user(sock, method, &buff);
-    RUNNER_ASSERT_MSG(ret == expectedResult,
-                      "cynara_creds_socket_get_user failed, ret = " << ret
-                      << "; expected = " << expectedResult);
-    return buff;
-}
-
-pid_t socketGetPid(int sock, int expectedResult) {
-    pid_t pid;
-    auto ret = cynara_creds_socket_get_pid(sock, &pid);
-    RUNNER_ASSERT_MSG(ret == expectedResult,
-                      "cynara_creds_socket_get_pid failed, ret = " << ret << "; expected = "
-                      << expectedResult);
-    return pid;
-}
-
-} //namespace CynaraHelperCredentials
index 9998a60..119fc0e 100644 (file)
@@ -1,2 +1,2 @@
-buckets;$1$$6ZlVs5lw2nZgVmiw0BdY21
-_;$1$$qRPK7m23GJusamGpoGLby/
+buckets;4f15a73541bf5a52e25ef4c2b990d224
+_;d41d8cd98f00b204e9800998ecf8427e
index 90c581c..b3101f7 100644 (file)
@@ -1,2 +1,2 @@
-buckets;$1$$UYHKvrIkGoSTO5hIgvCLg0
-_;$1$$qRPK7m23GJusamGpoGLby/
+buckets;c79674219909ebad81c33b580c70e31b
+_;d41d8cd98f00b204e9800998ecf8427e
index ebaaf4c..0a16164 100644 (file)
@@ -1,2 +1,2 @@
-buckets;$1$$6ZlVs5lw2nZgVmiw0BdY21
-_;$1$$nssatAXP6yl4N8gjldhxf0
+buckets;4f15a73541bf5a52e25ef4c2b990d224
+_;4e1fa590dd3adf658786e376962bd124
index a8c74ae..6d90e67 100644 (file)
@@ -52,7 +52,7 @@ void loadAgentPlugin()
     loadServicePlugins(paths);
 }
 
-void setAgentPolicy(int expectedResult = CYNARA_API_SUCCESS)
+void setAgentPolicy(const std::string &user, int expectedResult = CYNARA_API_SUCCESS)
 {
     const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
     const char *wildcard = CYNARA_ADMIN_WILDCARD;
@@ -63,7 +63,7 @@ void setAgentPolicy(int expectedResult = CYNARA_API_SUCCESS)
     auto policyType = testAgentPolicies[0].type;
 
     CynaraPoliciesContainer cp;
-    cp.add(bucket, wildcard, wildcard, wildcard, policyType, extra);
+    cp.add(bucket, wildcard, user.c_str(), wildcard, policyType, extra);
 
     Admin admin;
     admin.setPolicies(cp, expectedResult);
@@ -89,28 +89,31 @@ void getAgentRequest(Agent &agent, AgentRequest &request, Client &client,
 void tcag01_set_agent_type_policy_without_plugin_func()
 {
     loadServicePlugins(DirectoryPaths());
-    setAgentPolicy(CYNARA_API_INVALID_PARAM);
+    // user in setAgentPolicy can be empty here, it does not matter in this test
+    setAgentPolicy(std::string(), CYNARA_API_INVALID_PARAM);
 }
 
 void tcag02_set_agent_type_policy_with_plugin_loaded_func()
 {
     loadAgentPlugin();
-    setAgentPolicy();
+    // user in setAgentPolicy can be empty here, it does not matter in this test
+    setAgentPolicy(std::string());
 }
 
 void tcag03_check_with_no_agent_func()
 {
     std::string testNo("03");
+    CheckData data(testNo);
     cynara_check_id id;
     RequestEntity callbackData = {RequestFunction(),
                                   CYNARA_API_ACCESS_DENIED,
                                   CYNARA_CALL_CAUSE_ANSWER};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Client client;
-    client.createRequest({testNo}, id, callbackData);
+    client.createRequest(data, id, callbackData);
     client.assertStatus(READWRITE);
 
     //send requests
@@ -140,18 +143,19 @@ void tcag05_agent_request_timeout_func()
 void tcag06_check_with_unregistered_agent_func()
 {
     std::string testNo("06");
+    CheckData data(testNo);
     cynara_check_id id;
     RequestEntity callbackData = {RequestFunction(),
                                   CYNARA_API_ACCESS_DENIED,
                                   CYNARA_CALL_CAUSE_ANSWER};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Agent agent;
 
     Client client;
-    client.createRequest({testNo}, id, callbackData);
+    client.createRequest(data, id, callbackData);
     client.assertStatus(READWRITE);
 
     //send requests
@@ -169,7 +173,7 @@ void tcag07_get_request_func()
                                   CYNARA_CALL_CAUSE_ANSWER};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Agent agent;
     AgentRequest agentRequest;
@@ -198,7 +202,7 @@ void tcag08_get_request_and_respond_with_wrong_id_func()
                                   CYNARA_CALL_CAUSE_FINISH};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Agent agent;
     AgentRequest agentRequest;
@@ -226,7 +230,7 @@ void tcag09_get_request_and_correct_responded_id_func()
                                   CYNARA_CALL_CAUSE_ANSWER};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Agent agent;
     AgentRequest agentRequest;
@@ -256,7 +260,7 @@ void tcag10_cancel_request_func()
                                   CYNARA_CALL_CAUSE_CANCEL};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Agent agent;
     AgentRequest agentRequest;
@@ -272,7 +276,8 @@ void tcag10_cancel_request_func()
                          CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT);
     agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege);
     client.cancel(id);
-    Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED,
+    auto testTimeLimit2 = std::chrono::seconds(8);
+    Timeout::callAndWait(testTimeLimit2, Timeout::ExpectMode::FINISHED,
                          restartCynaraServiceAndSockets, getAgentRequest,
                          std::ref(agent), std::ref(agentRequest), std::ref(client),
                          CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT);
@@ -291,7 +296,7 @@ void tcag11_cancel_processed_request_func()
                                   CYNARA_CALL_CAUSE_CANCEL};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
 
     Agent agent;
     AgentRequest agentRequest;
@@ -329,7 +334,8 @@ void tcag12_create_two_requests_func()
                                    CYNARA_CALL_CAUSE_CANCEL};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    // both data1 and data2 have the same user, so it does not matter which one will be used
+    setAgentPolicy(data1.m_user);
 
     Agent agent;
     AgentRequest agentRequest1, agentRequest2, agentRequest3;
@@ -369,12 +375,13 @@ void tcag13_create_many_requests_func()
 {
     const int numberOfRequests = 4;
     std::string testNo("13");
+    CheckData policyData(testNo);
     cynara_check_id ids[numberOfRequests];
     RequestEntity callbackData = {RequestFunction(),
                                   CYNARA_API_ACCESS_DENIED,
                                   CYNARA_CALL_CAUSE_ANSWER};
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(policyData.m_user);
 
     Agent agent;
     AgentRequest agentRequests[numberOfRequests];
@@ -406,7 +413,7 @@ void tcag14_client_disconnects_func()
                                   CYNARA_CALL_CAUSE_FINISH};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
     Agent agent;
     AgentRequest agentRequest;
     auto testTimeLimit = std::chrono::seconds(5);
@@ -437,7 +444,7 @@ void tcag15_agent_disconnects_func()
                                   CYNARA_CALL_CAUSE_ANSWER};
 
     loadAgentPlugin();
-    setAgentPolicy();
+    setAgentPolicy(data.m_user);
     Client client;
     client.createRequest(data, id, callbackData);
     client.assertStatus(READWRITE);
index 43a73f4..06aa14d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2016 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.
@@ -17,7 +17,7 @@
  * @file        test_cases_helpers.cpp
  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
  * @version     1.0
- * @brief       Tests for cynara-helper-credentials-socket
+ * @brief       Tests for cynara-helper-credentials-socket and cynara-helper-credentials-dbus
  */
 
 #include <cstdlib>
 #include <sys/types.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <utility>
 
+#include <dbus/dbus.h>
+#include <glib-object.h>
+#include <gio/gio.h>
+
+#include <tests_common.h>
 #include <access_provider.h>
 #include <dpl/test/test_runner.h>
 #include <memory.h>
 #include <tests_common.h>
 #include <uds.h>
 #include <passwd_access.h>
+#include <cynara_helpers_creds.h>
+#include <cynara_helpers_dbus.h>
 
-#include <cynara_test_helpers.h>
+#include <cynara-creds-gdbus.h>
+#include <cynara-creds-self.h>
 
 class ProcessCredentials {
 public:
@@ -69,6 +78,23 @@ pid_t runInChild(const std::function<void(void)> &process) {
     return pid;
 }
 
+cynara_client_creds getClientDefaultMethod() {
+    cynara_client_creds def;
+    int ret = cynara_creds_get_default_client_method(&def);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                      "cynara_creds_get_default_client_method failed with " << ret);
+    return def;
+}
+
+cynara_user_creds getUserDefaultMethod() {
+    cynara_user_creds def;
+    int ret = cynara_creds_get_default_user_method(&def);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                      "cynara_creds_get_default_user_method failed with " << ret);
+    return def;
+}
+
+
 void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
                const struct ProcessCredentials &peerCredentials) {
     SecurityServer::AccessProvider ap(peerCredentials.label());
@@ -105,61 +131,493 @@ void socketTestTemplate(SocketAssertionFn assertion, const std::string &scope) {
 
 RUNNER_TEST_GROUP_INIT(cynara_creds_socket)
 
-RUNNER_CHILD_TEST_SMACK(tccs01_socket_credentials_client_smack)
-{
-    socketTestTemplate([] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
-        CStringPtr label(CynaraHelperCredentials::socketGetClient(sock, CLIENT_METHOD_SMACK));
+void testSocketClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
+    socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
+        CStringPtr label(CynaraHelperCredentials::socketGetClient(sock, method));
         RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
                           "Labels don't match ret = " << label.get()
                           << "; expected = " << peerCredentials.label());
     }, "tccs01");
 }
 
-RUNNER_CHILD_TEST_SMACK(tccs02_socket_credentials_client_pid)
-{
-    socketTestTemplate([] (int sock, pid_t pid, const ProcessCredentials &) {
-        CStringPtr clientPidStr(CynaraHelperCredentials::socketGetClient(sock, CLIENT_METHOD_PID));
+void testSocketClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
+    socketTestTemplate([method] (int sock, pid_t pid, const ProcessCredentials &) {
+        CStringPtr clientPidStr(CynaraHelperCredentials::socketGetClient(sock, method));
         pid_t clientPid = std::stoi(clientPidStr.get());
         RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
                           << "; expected = " << pid);
     }, "tccs02");
 }
 
-RUNNER_CHILD_TEST_SMACK(tccs03_socket_credentials_user_uid)
+RUNNER_MULTIPROCESS_TEST_SMACK(tccs01_socket_credentials_client_smack)
+{
+    testSocketClientSmack();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccs02_socket_credentials_client_pid)
+{
+    testSocketClientPid();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccs03_socket_credentials_client_default)
 {
-    socketTestTemplate([] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
-        CStringPtr uidStr(CynaraHelperCredentials::socketGetUser(sock, USER_METHOD_UID));
+    auto method = getClientDefaultMethod();
+    switch(method) {
+    case CLIENT_METHOD_SMACK:
+        testSocketClientSmack(CLIENT_METHOD_DEFAULT);
+        break;
+    case CLIENT_METHOD_PID:
+        testSocketClientPid(CLIENT_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
+                        << method);
+    }
+}
+
+void testSocketUserUid(cynara_user_creds method = USER_METHOD_UID) {
+    socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
+        CStringPtr uidStr(CynaraHelperCredentials::socketGetUser(sock, method));
         uid_t uid = std::stoul(uidStr.get());
         RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
                           << "; expected = "<< peerCredentials.uid());
-    }, "tccs03");
+    }, "tccs04");
 }
 
-RUNNER_CHILD_TEST_SMACK(tccs04_socket_credentials_user_gid)
-{
-    socketTestTemplate([] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
-        CStringPtr gidStr(CynaraHelperCredentials::socketGetUser(sock, USER_METHOD_GID));
+void testSocketUserGid(cynara_user_creds method = USER_METHOD_GID) {
+    socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
+        CStringPtr gidStr(CynaraHelperCredentials::socketGetUser(sock, method));
         gid_t gid = std::stoul(gidStr.get());
         RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
                           << "; expected = "<< peerCredentials.gid());
-    }, "tccs04");
+    }, "tccs05");
+}
+
+RUNNER_MULTIPROCESS_TEST(tccs04_socket_credentials_user_uid)
+{
+    testSocketUserUid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccs05_socket_credentials_user_gid)
+{
+    testSocketUserGid();
 }
 
-RUNNER_CHILD_TEST_SMACK(tccs05_cynara_creds_socket_pid)
+RUNNER_MULTIPROCESS_TEST(tccs06_socket_credentials_user_default)
 {
-    const auto sockaddr = UDSHelpers::makeAbstractAddress("helper_tccs05.socket");
+    auto method = getUserDefaultMethod();
+    switch(method) {
+    case USER_METHOD_UID:
+        testSocketUserUid(USER_METHOD_DEFAULT);
+        break;
+    case USER_METHOD_GID:
+        testSocketUserGid(USER_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
+                 << method);
+    }
+}
+
+RUNNER_MULTIPROCESS_TEST(tccs07_socket_credentials_pid)
+{
+    socketTestTemplate([] (int sock, pid_t expectedPid, const ProcessCredentials &) {
+        pid_t peerPid(CynaraHelperCredentials::socketGetPid(sock));
+        RUNNER_ASSERT_MSG(peerPid == expectedPid, "Pids don't match ret = " << peerPid
+                          << "; expected = "<< expectedPid);
+    }, "tccs05");
+}
+
+// TODO: Create utility namespace for DBus, maybe?
+DBusConnectionPtr createDBusConnection(const std::string &name) {
+    DBusError err;
+
+    dbus_error_init(&err);
+    DBusConnection *conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
+    RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != 1, "Error in dbus_bus_get: " << err.message);
+    dbus_connection_set_exit_on_disconnect(conn, FALSE);
+
+    DBusConnectionPtr ret(conn, [] (DBusConnection *conn) {
+        dbus_connection_close(conn);
+        dbus_connection_unref(conn);
+    });
+
+    if (name.empty() == false) {
+        dbus_bus_request_name(conn, name.c_str(), DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
+        RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != TRUE,
+                          "Error in dbus_bus_request_name: " << err.message);
+    }
+
+    return ret;
+}
+
+void dbusServer(SynchronizationPipe &pipe, const std::string &requestedName,
+                const ProcessCredentials &peerCredentials) {
+    // for DBus connection, System must have access to our peer creds as well.
+    SecurityServer::AccessProvider systemAp("System");
+    systemAp.addObjectRule(peerCredentials.label(), "rwx");
+    systemAp.apply();
+
+    SecurityServer::AccessProvider ap(peerCredentials.label());
+    ap.addObjectRule("System", "w");
+    ap.addObjectRule("System::Run", "x");
+    ap.addObjectRule("System::Shared", "rwx"); // for GDB
+    ap.addSubjectRule("System::Privileged", "rwx"); // for piping
+    ap.addObjectRule("System::Privileged", "rwx"); // for GDB and piping
+    ap.addObjectRule("User", "r"); // for /usr/lib/debug access
+    ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
+    pipe.claimChildEp();
+
+    auto conn = createDBusConnection(requestedName);
+    pipe.post();
+    pipe.wait();
+}
+
+typedef std::function<void(DBusConnectionPtr conn, pid_t pid,
+                           const std::string &requestedName,
+                           const ProcessCredentials &peerCredentials)> DBusAssertionFn;
+
+void dbusTestTemplate(DBusAssertionFn assertion, const std::string &/*scope*/) {
+    std::string requestedName = "tests.dbus.cynara";
     const ProcessCredentials peerCredentials;
 
     SynchronizationPipe pipe;
-    pid_t expectedPid = runInChild(std::bind(udsServer, std::ref(pipe), std::cref(sockaddr),
-                                   std::cref(peerCredentials)));
+    pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
+                           std::cref(peerCredentials)));
 
     pipe.claimParentEp();
     pipe.wait();
-    int sock = UDSHelpers::createClient(&sockaddr);
-    SockUniquePtr sockPtr(&sock);
 
-    pid_t helperPid = CynaraHelperCredentials::socketGetPid(sock);
-    RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
-                      << "; expected = " << expectedPid);
+    auto conn = createDBusConnection("");
+    assertion(std::move(conn), pid, requestedName, peerCredentials);
+    pipe.post();
+}
+
+RUNNER_TEST_GROUP_INIT(cynara_creds_dbus)
+
+void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
+    dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
+                         const ProcessCredentials &) {
+        CStringPtr clientPidStr(CynaraHelperCredentials::dbusGetClient(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);
+    }, "tccd01");
+}
+
+void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
+    dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
+                         const ProcessCredentials &peerCredentials) {
+        CStringPtr label(CynaraHelperCredentials::dbusGetClient(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());
+    }, "tccd02");
+}
+
+RUNNER_MULTIPROCESS_TEST(tccd01_dbus_credentials_client_pid)
+{
+    testDbusClientPid();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccd02_dbus_credentials_client_smack)
+{
+    testDbusClientSmack();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccd03_dbus_credentials_client_default)
+{
+    auto method = getClientDefaultMethod();
+    switch(method) {
+    case CLIENT_METHOD_SMACK:
+        testDbusClientSmack(CLIENT_METHOD_DEFAULT);
+        break;
+    case CLIENT_METHOD_PID:
+        testDbusClientPid(CLIENT_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
+                        << method);
+    }
+}
+
+void testDbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
+    dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
+                         const ProcessCredentials &peerCredentials) {
+        CStringPtr uidStr(CynaraHelperCredentials::dbusGetUser(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());
+    }, "tccd04");
+}
+
+void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
+    // Acquiring gid from dbus connection is not yet implemented for cynara helpers
+    dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
+                         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)
+{
+    testDbusUserUid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccd05_dbus_credentials_user_gid)
+{
+    testDbusUserGid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccd06_dbus_credentials_user_default) {
+    auto method = getUserDefaultMethod();
+    switch(method) {
+    case USER_METHOD_UID:
+        testDbusUserUid(USER_METHOD_DEFAULT);
+        break;
+    case USER_METHOD_GID:
+        testDbusUserGid(USER_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
+                        << method);
+    }
+}
+
+RUNNER_TEST_SMACK(tccd06_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");
+}
+
+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)
+
+void testGdbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
+    gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t pid,
+                                const std::string &requestedName,
+                                const ProcessCredentials &) {
+        GStringPtr clientPidStr(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
+                                requestedName.c_str(), method));
+        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) {
+    gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
+                                const std::string &requestedName,
+                                const ProcessCredentials &peerCredentials) {
+        GStringPtr label(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
+                         requestedName.c_str(), method));
+        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)
+{
+    testGdbusClientPid();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccgd02_gdbus_credentials_client_smack)
+{
+    testGdbusClientSmack();
+}
+
+RUNNER_MULTIPROCESS_TEST_SMACK(tccgd03_gdbus_credentials_client_default)
+{
+    auto method = getClientDefaultMethod();
+    switch(method) {
+    case CLIENT_METHOD_SMACK:
+        testGdbusClientSmack(CLIENT_METHOD_DEFAULT);
+        break;
+    case CLIENT_METHOD_PID:
+        testGdbusClientPid(CLIENT_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
+                        << method);
+    }
+}
+
+void testGdbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
+    gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
+                                const std::string &requestedName,
+                                const ProcessCredentials &peerCredentials) {
+        GStringPtr uidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
+                          requestedName.c_str(), method));
+        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) {
+    // Getting gid for gdbus connection is not yet implemented in cynara helpers
+    gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
+                                const std::string &requestedName,
+                                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)
+{
+    testGdbusUserUid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccgd05_gdbus_credentials_user_gid)
+{
+    testGdbusUserGid();
+}
+
+RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_user_default) {
+    auto method = getUserDefaultMethod();
+    switch(method) {
+    case USER_METHOD_UID:
+        testGdbusUserUid(USER_METHOD_DEFAULT);
+        break;
+    case USER_METHOD_GID:
+        testGdbusUserGid(USER_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
+                        << method);
+    }
+}
+
+RUNNER_MULTIPROCESS_TEST(tccgd06_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");
+}
+
+RUNNER_TEST_GROUP_INIT(cynara_creds_self)
+
+void testCredsClientSelf(cynara_client_creds method, const std::string &expected) {
+    char *client;
+    int ret = cynara_creds_self_get_client(method, &client);
+    CStringPtr clientPtr(client);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_client failed with " << ret);
+    RUNNER_ASSERT_MSG(expected == client, "expected client doesn't match, expected: " << expected
+                                          << ", got : " << client);
+}
+
+void testCredsUserSelf(cynara_user_creds method, const std::string &expected) {
+    char *user;
+    int ret = cynara_creds_self_get_user(method, &user);
+    CStringPtr clientPtr(user);
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_user failed with " << ret);
+    RUNNER_ASSERT_MSG(expected == user, "expected user doesn't match, expected: " << expected
+                                          << ", got : " << user);
+}
+
+void testSelfClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
+    std::string label = "test-label";
+    change_label(label.c_str());
+    testCredsClientSelf(method, label);
+}
+
+void testSelfClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
+    pid_t pid = getpid();
+    testCredsClientSelf(method, std::to_string(pid));
+}
+
+void testSelfUserUid(cynara_user_creds method = USER_METHOD_UID) {
+    uid_t uid = getuid();
+    testCredsUserSelf(method, std::to_string(uid));
+}
+
+void testSelfUserGid(cynara_user_creds method = USER_METHOD_GID) {
+    gid_t gid = getgid();
+    testCredsUserSelf(method, std::to_string(gid));
+}
+
+RUNNER_CHILD_TEST_SMACK(tccsl01_self_credentials_client_smack) {
+    testSelfClientSmack();
+}
+
+RUNNER_CHILD_TEST_SMACK(tccsl02_self_credentials_client_pid) {
+    testSelfClientPid();
+}
+
+RUNNER_CHILD_TEST_SMACK(tccsl03_self_credentials_user_uid) {
+    testSelfUserUid();
+}
+
+RUNNER_CHILD_TEST_SMACK(tccsl04_self_credentials_user_gid) {
+    testSelfUserGid();
+}
+
+RUNNER_CHILD_TEST_SMACK(tccsl05_self_credentials_client_default) {
+    auto method = getClientDefaultMethod();
+    switch(method) {
+    case CLIENT_METHOD_SMACK:
+        testSelfClientSmack(CLIENT_METHOD_DEFAULT);
+        break;
+    case CLIENT_METHOD_PID:
+        testSelfClientPid(CLIENT_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value " << method);
+    }
+}
+
+RUNNER_CHILD_TEST_SMACK(tccsl06_self_credentials_user_default) {
+    auto method = getUserDefaultMethod();
+    switch(method) {
+    case USER_METHOD_UID:
+        testSelfUserUid(USER_METHOD_DEFAULT);
+        break;
+    case USER_METHOD_GID:
+        testSelfUserGid(USER_METHOD_DEFAULT);
+        break;
+    default:
+        RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value " << method);
+    }
 }
index 4f01833..c8fbdcb 100644 (file)
@@ -246,9 +246,7 @@ protected:
                                     __FILE__,                                         \
                                     __LINE__,                                         \
                                     assertMsg.str());                                 \
-            if (!std::uncaught_exception())                                           \
-                throw e;                                                              \
-            DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
+            throw e;                                                                  \
         }                                                                             \
     } while (0)
 
@@ -267,9 +265,7 @@ protected:
                                     __FILE__,                                         \
                                     __LINE__,                                         \
                                     assertMsg.str());                                 \
-            if (!std::uncaught_exception())                                           \
-                throw e;                                                              \
-            DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
+            throw e;                                                                  \
         }                                                                             \
     } while (0)
 
index f1c5d80..f4a625f 100644 (file)
@@ -782,12 +782,11 @@ void TestRunner::deferEnd()
     if (m_deferDeepness > 0)
         return;
 
-    bool oops = std::uncaught_exception();
-    size_t additionalExceptions = oops ? 0 : 1;
-    for (size_t i = additionalExceptions; i < m_deferredExceptionsMessages.size(); ++i)
+    for (size_t i = 0; i < m_deferredExceptionsMessages.size(); ++i) {
         addFailReason(m_deferredExceptionsMessages[i]);
+    }
 
-    if (!oops && !m_deferredExceptionsMessages.empty())
+    if (!m_deferredExceptionsMessages.empty())
     {
         m_deferredExceptionsMessages.clear();
         switch (m_firstDeferredExceptionType) {