Separate file operations into separate file 25/33525/3
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 12 Jan 2015 15:34:56 +0000 (16:34 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Wed, 14 Jan 2015 06:29:29 +0000 (07:29 +0100)
All files operations done in cynara_test_env are moved to new namespace FileOperations
and implemented in cynara_test_file_operations.cpp file.
cynaraDbExists() function was generalized to dirExists(string) function.

Change-Id: Ic610078330074a648dbb24ddec7297a337a41de0

tests/cynara-tests/CMakeLists.txt
tests/cynara-tests/common/cynara_test_env.cpp
tests/cynara-tests/common/cynara_test_file_operations.cpp [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_file_operations.h [new file with mode: 0644]

index aac94f6..bfbc659 100644 (file)
@@ -37,6 +37,7 @@ SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client_async_status_monitor.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_commons.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_env.cpp
+    ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_file_operations.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/cynara-test.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/test_cases.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/test_cases_async.cpp
index 2dc69c4..8f98d71 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.
  */
 
 #include <cynara_test_commons.h>
-#include <cynara_test_env.h>
-#include <tests_common.h>
+#include <cynara_test_file_operations.h>
 #include <dbus_access.h>
-#include <memory.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/sendfile.h>
-#include <unistd.h>
-#include <errno.h>
-#include <ftw.h>
-#include <fcntl.h>
-#include <pwd.h>
-#include <dirent.h>
-
-namespace
-{
-
-typedef CStringPtr PwBufPtr;
-
-int removeContents(const char *fpath, const struct stat * /*sb*/,
-                   int tflag, struct FTW * /*ftwbuf*/)
-{
-    if (tflag == FTW_F)
-        RUNNER_ASSERT_ERRNO_MSG(!unlink(fpath), "Unable to unlink " << fpath << " file");
-    else
-        RUNNER_ASSERT_MSG(tflag == FTW_DP, "Visited file should not exist. Path: " << fpath);
-    return 0;
-}
-
-void copyFile(const std::string &src, const std::string &dst)
-{
-    int inFd = TEMP_FAILURE_RETRY(open(src.c_str(), O_RDONLY));
-    RUNNER_ASSERT_ERRNO_MSG(inFd > 0, "Opening " << src << " file failed");
-    FdUniquePtr inFdPtr(&inFd);
-
-    int outFd = TEMP_FAILURE_RETRY(creat(dst.c_str(), 0700));
-    RUNNER_ASSERT_ERRNO_MSG(outFd > 0, "Creating " << dst << " file failed");
-    FdUniquePtr outFdPtr(&outFd);
-
-    long int len = sysconf(_SC_GETPW_R_SIZE_MAX);
-    RUNNER_ASSERT_MSG(len != -1, "No suggested buflen");
-    size_t buflen = len;
-    char *buf = static_cast<char*>(malloc(buflen));
-
-    PwBufPtr pwBufPtr(buf);
-
-    struct passwd pwbuf, *pwbufp = nullptr;
-    int ret = TEMP_FAILURE_RETRY(getpwnam_r(CynaraTestConsts::USER.c_str(),
-                                            &pwbuf, buf, buflen, &pwbufp));
-    RUNNER_ASSERT_ERRNO_MSG(ret == 0, "getpwnam_r failed on " << CynaraTestConsts::USER << " user");
-    RUNNER_ASSERT_MSG(pwbufp, "User " << CynaraTestConsts::USER << " does not exist");
-
-    ret = fchown(outFd, pwbufp->pw_uid, pwbufp->pw_gid);
-    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fchown failed");
-
-    ret = smack_fsetlabel(outFd, CynaraTestConsts::LABEL.c_str(), SMACK_LABEL_ACCESS);
-    RUNNER_ASSERT_MSG(ret == 0, "Setting smack label failed");
-
-    struct stat statSrc;
-    ret = fstat(inFd, &statSrc);
-    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fstat failed");
-
-    ret = sendfile(outFd, inFd, 0, statSrc.st_size);
-    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "sendfile failed");
-}
-
-void copyDir(const std::string &source, const std::string &destination)
-{
-    DIR *dirPtr = nullptr;
-    struct dirent *direntPtr;
-
-    RUNNER_ASSERT_ERRNO_MSG(dirPtr = opendir(source.c_str()),
-                               "opening " << source << " dir failed");
-    DirPtr dirScopedPtr(dirPtr);
-
-    while((direntPtr = readdir(dirPtr)) != nullptr) {
-        if (!strcmp(direntPtr->d_name, ".")
-         || !strcmp(direntPtr->d_name, ".."))
-            continue;
-        std::string tempDest = destination + "/" + direntPtr->d_name;
-        std::string tempSrc = source + "/" + direntPtr->d_name;
-        copyFile(tempSrc, tempDest);
-    }
-}
-
-void clear(const std::string &dir)
-{
-    int ret = nftw(dir.c_str(), removeContents, 2, FTW_DEPTH | FTW_PHYS);
-    if (ret == -1)
-        RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "nftw failed");
-}
-
-void removeDirIfExists(const std::string &dir)
-{
-    RUNNER_ASSERT_ERRNO_MSG(!rmdir(dir.c_str()) || errno == ENOENT,
-                               "Removing " << dir << " dir failed");
-}
+#include <tests_common.h>
 
-bool cynaraDbExists()
-{
-    struct stat st;
-    int ret = stat(CynaraTestConsts::DB_DIR.c_str(), &st);
-    if (ret == -1 && errno == ENOENT) {
-        return false;
-    } else if (ret == -1) {
-        RUNNER_ASSERT_ERRNO_MSG(false, "Cannot stat " << CynaraTestConsts::DB_DIR
-                                          << " not due to its nonexistence");
-    }
-    RUNNER_ASSERT_MSG(st.st_mode & S_IFDIR, CynaraTestConsts::DB_DIR << " is not a directory");
-    return true;
-}
+#include <cynara_test_env.h>
 
-}
+using namespace FileOperations;
 
 CynaraTestEnv::CynaraTestEnv(const char *dirName)
     : m_dbPresent(false)
@@ -149,10 +42,9 @@ void CynaraTestEnv::save()
     dbusAccess.maskService();
     dbusAccess.stopService();
 
-    m_dbPresent = cynaraDbExists();
+    m_dbPresent = dirExists(CynaraTestConsts::DB_DIR);
     if (m_dbPresent) {
-        RUNNER_ASSERT_ERRNO_MSG(!mkdir(m_dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO),
-                                   "Unable to make " << m_dir << " test directory");
+        makeDir(m_dir);
         copyDir(CynaraTestConsts::DB_DIR, m_dir);
     }
 
diff --git a/tests/cynara-tests/common/cynara_test_file_operations.cpp b/tests/cynara-tests/common/cynara_test_file_operations.cpp
new file mode 100644 (file)
index 0000000..132fc1f
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * 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.
+ */
+
+#include <cstdlib>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <ftw.h>
+#include <pwd.h>
+#include <sys/sendfile.h>
+#include <sys/smack.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <cynara_test_commons.h>
+#include <dpl/test/test_runner.h>
+#include <memory.h>
+
+#include <cynara_test_file_operations.h>
+
+namespace FileOperations
+{
+
+static int removeContents(const char *fpath, const struct stat * /*sb*/,
+                          int tflag, struct FTW * /*ftwbuf*/)
+{
+    if (tflag == FTW_F)
+        RUNNER_ASSERT_ERRNO_MSG(!unlink(fpath), "Unable to unlink " << fpath << " file");
+    else
+        RUNNER_ASSERT_MSG(tflag == FTW_DP, "Visited file should not exist. Path: " << fpath);
+    return 0;
+}
+
+bool dirExists(const std::string &directory)
+{
+    struct stat st;
+    int ret = stat(directory.c_str(), &st);
+    if (ret == -1 && errno == ENOENT) {
+        return false;
+    } else if (ret == -1) {
+        RUNNER_ASSERT_ERRNO_MSG(false, "Cannot stat " << directory
+                                          << " not due to its nonexistence");
+    }
+    RUNNER_ASSERT_MSG(st.st_mode & S_IFDIR, directory << " is not a directory");
+    return true;
+}
+
+void copyFile(const std::string &src, const std::string &dst)
+{
+    using PwBufPtr = CStringPtr;
+    int inFd = TEMP_FAILURE_RETRY(open(src.c_str(), O_RDONLY));
+    RUNNER_ASSERT_ERRNO_MSG(inFd > 0, "Opening " << src << " file failed");
+    FdUniquePtr inFdPtr(&inFd);
+
+    int outFd = TEMP_FAILURE_RETRY(creat(dst.c_str(), 0700));
+    RUNNER_ASSERT_ERRNO_MSG(outFd > 0, "Creating " << dst << " file failed");
+    FdUniquePtr outFdPtr(&outFd);
+
+    long int len = sysconf(_SC_GETPW_R_SIZE_MAX);
+    RUNNER_ASSERT_MSG(len != -1, "No suggested buflen");
+    size_t buflen = len;
+    char *buf = static_cast<char*>(malloc(buflen));
+
+    PwBufPtr pwBufPtr(buf);
+
+    struct passwd pwbuf, *pwbufp = nullptr;
+    int ret = TEMP_FAILURE_RETRY(getpwnam_r(CynaraTestConsts::USER.c_str(),
+                                            &pwbuf, buf, buflen, &pwbufp));
+    RUNNER_ASSERT_ERRNO_MSG(ret == 0, "getpwnam_r failed on " << CynaraTestConsts::USER << " user");
+    RUNNER_ASSERT_MSG(pwbufp, "User " << CynaraTestConsts::USER << " does not exist");
+
+    ret = fchown(outFd, pwbufp->pw_uid, pwbufp->pw_gid);
+    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fchown failed");
+
+    ret = smack_fsetlabel(outFd, CynaraTestConsts::LABEL.c_str(), SMACK_LABEL_ACCESS);
+    RUNNER_ASSERT_MSG(ret == 0, "Setting smack label failed");
+
+    struct stat statSrc;
+    ret = fstat(inFd, &statSrc);
+    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "fstat failed");
+
+    ret = sendfile(outFd, inFd, 0, statSrc.st_size);
+    RUNNER_ASSERT_ERRNO_MSG(ret != -1, "sendfile failed");
+}
+
+void copyDir(const std::string &source, const std::string &destination)
+{
+    DIR *dirPtr = nullptr;
+    struct dirent *direntPtr;
+
+    RUNNER_ASSERT_ERRNO_MSG(dirPtr = opendir(source.c_str()),
+                               "opening " << source << " dir failed");
+    DirPtr dirScopedPtr(dirPtr);
+
+    while((direntPtr = readdir(dirPtr)) != nullptr) {
+        if (!strcmp(direntPtr->d_name, ".")
+         || !strcmp(direntPtr->d_name, ".."))
+            continue;
+        std::string tempDest = destination + "/" + direntPtr->d_name;
+        std::string tempSrc = source + "/" + direntPtr->d_name;
+        copyFile(tempSrc, tempDest);
+    }
+}
+
+void makeDir(const std::string &directory)
+{
+    RUNNER_ASSERT_ERRNO_MSG(!mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IRWXO),
+                               "Unable to make " << directory << " test directory");
+}
+
+void clear(const std::string &dir)
+{
+    int ret = nftw(dir.c_str(), removeContents, 2, FTW_DEPTH | FTW_PHYS);
+    if (ret == -1)
+        RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "nftw failed");
+}
+
+void removeDirIfExists(const std::string &dir)
+{
+    RUNNER_ASSERT_ERRNO_MSG(!rmdir(dir.c_str()) || errno == ENOENT,
+                               "Removing " << dir << " dir failed");
+}
+
+} // namespace FileOperations
diff --git a/tests/cynara-tests/common/cynara_test_file_operations.h b/tests/cynara-tests/common/cynara_test_file_operations.h
new file mode 100644 (file)
index 0000000..109823a
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#ifndef CYNARA_TEST_FILE_OPERATIONS_H
+#define CYNARA_TEST_FILE_OPERATIONS_H
+
+#include <string>
+
+namespace FileOperations
+{
+
+bool dirExists(const std::string &directory);
+void copyFile(const std::string &src, const std::string &dst);
+void copyDir(const std::string &source, const std::string &destination);
+void makeDir(const std::string &directory);
+void clear(const std::string &dir);
+void removeDirIfExists(const std::string &dir);
+
+} // namespace FileOperations
+
+#endif //CYNARA_TEST_FILE_OPERATIONS_H