Merge branch 'tizen' into security-manager
[platform/core/test/security-tests.git] / src / common / tests_common.cpp
index 6808b57..2332abb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2013-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.
@@ -22,6 +22,7 @@
  */
 
 #include "tests_common.h"
+#include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -35,25 +36,15 @@ int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
 
 const char *WGT_APP_ID = "QwCqJ0ttyS";
 
-int smack_runtime_check(void)
-{
-    static int smack_present = -1;
-    if (-1 == smack_present) {
-        if (smack_smackfs_path()) {
-            smack_present = 1;
-        } else {
-            smack_present = 0;
-        }
-    }
-    return smack_present;
-}
-
-int smack_check(void)
+bool smack_check(void)
 {
 #ifndef WRT_SMACK_ENABLED
-    return 0;
+    return false;
 #else
-    return smack_runtime_check();
+    static int smack_present = -1;
+    if (-1 == smack_present)
+        smack_present = smack_smackfs_path() == nullptr ? 0 : 1;
+    return smack_present == 1;
 #endif
 }
 
@@ -183,3 +174,63 @@ int files_compare(int fd1, int fd2)
 
     return result;
 }
+
+void mkdirSafe(const std::string &path, mode_t mode)
+{
+    RUNNER_ASSERT_ERRNO_MSG(0 == mkdir(path.c_str(), mode) || errno == EEXIST,
+                            "mkdir for <" << path << "> with mode <" << mode << "> failed");
+}
+
+void mktreeSafe(const std::string &path, mode_t mode)
+{
+    // Create subsequent parent directories
+    // Assume that path is absolute - i.e. starts with '/'
+    for (size_t pos = 0; (pos = path.find("/", pos + 1)) != std::string::npos; )
+        mkdirSafe(path.substr(0, pos).c_str(), mode);
+
+    mkdirSafe(path, mode);
+}
+
+void creatSafe(const std::string &path, mode_t mode)
+{
+    RUNNER_ASSERT_ERRNO_MSG(-1 != creat(path.c_str(), mode),
+                            "creat for <" << path << "> with mode <" << mode << "> failed");
+}
+
+void symlinkSafe(const std::string &targetPath, const std::string &linkPath)
+{
+    RUNNER_ASSERT_ERRNO_MSG(0 == symlink(targetPath.c_str(), linkPath.c_str()),
+                            "symlink for <" << linkPath << "> to <" << targetPath << "> failed");
+}
+
+void removeDir(const std::string &path)
+{
+    DIR *d = opendir(path.c_str());
+
+    if (nullptr == d) {
+        RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "opendir of <" << path << "> failed");
+        return;
+    }
+
+    struct dirent *dirEntry;
+    while (nullptr != (dirEntry = readdir(d))) {
+        std::string entryName(dirEntry->d_name);
+        if (entryName == "." || entryName == "..")
+            continue;
+
+        std::string entryPath(path + "/" + entryName);
+        struct stat st;
+
+        RUNNER_ASSERT_ERRNO_MSG(0 == lstat(entryPath.c_str(), &st),
+                                "stat for <" << entryPath << "> failed");
+        if (S_ISDIR(st.st_mode))
+            removeDir(entryPath);
+        else
+            RUNNER_ASSERT_ERRNO_MSG(0 == unlink(entryPath.c_str()),
+                                    "unlink for <" << entryPath << "> failed");
+    }
+
+    closedir(d);
+
+    RUNNER_ASSERT_ERRNO_MSG(0 == rmdir(path.c_str()), "rmdir for <" << path << "> failed");
+}