Cynara: Add tests for cynara self helpers 27/77427/2
authorZofia Abramowska <z.abramowska@samsung.com>
Wed, 29 Jun 2016 12:01:03 +0000 (14:01 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 29 Jun 2016 14:27:04 +0000 (16:27 +0200)
Change-Id: I0c21a38f64b2a348f4c5a7dc6d1f5c27a0aae306

src/ckm/ckm-common.cpp
src/ckm/ckm-common.h
src/common/tests_common.cpp
src/common/tests_common.h
src/cynara-tests/CMakeLists.txt
src/cynara-tests/test_cases_helpers.cpp

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 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 fa5ccd3..6fa7aa5 100644 (file)
@@ -26,6 +26,7 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
     cynara-client-async
     cynara-creds-dbus
     cynara-creds-gdbus
+    cynara-creds-self
     cynara-creds-socket
     cynara-plugin
     dbus-1
index 55f899c..5d40bed 100644 (file)
@@ -44,6 +44,7 @@
 #include <cynara_test_helpers.h>
 #include <cynara_test_helpers_dbus.h>
 #include <cynara-creds-gdbus.h>
+#include <cynara-creds-self.h>
 
 class ProcessCredentials {
 public:
@@ -354,3 +355,104 @@ RUNNER_TEST_SMACK(tccgd04_gdbus_credentials_pid) {
                           << "; expected = " << expectedPid);
     }, "tccgd04");
 }
+
+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();
+}
+
+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;
+}
+
+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);
+    }
+}