From: Lukasz Wojciechowski Date: Mon, 12 Jan 2015 15:34:56 +0000 (+0100) Subject: Separate file operations into separate file X-Git-Tag: security-manager_5.5_testing~153 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=96c5364fce76ec0b6a29c7178adbb008e11a5a43;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git Separate file operations into separate file 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 --- diff --git a/tests/cynara-tests/CMakeLists.txt b/tests/cynara-tests/CMakeLists.txt index aac94f6..bfbc659 100644 --- a/tests/cynara-tests/CMakeLists.txt +++ b/tests/cynara-tests/CMakeLists.txt @@ -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 diff --git a/tests/cynara-tests/common/cynara_test_env.cpp b/tests/cynara-tests/common/cynara_test_env.cpp index 2dc69c4..8f98d71 100644 --- a/tests/cynara-tests/common/cynara_test_env.cpp +++ b/tests/cynara-tests/common/cynara_test_env.cpp @@ -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. @@ -15,120 +15,13 @@ */ #include -#include -#include +#include #include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -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(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 -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 -} +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 index 0000000..132fc1f --- /dev/null +++ b/tests/cynara-tests/common/cynara_test_file_operations.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +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(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 index 0000000..109823a --- /dev/null +++ b/tests/cynara-tests/common/cynara_test_file_operations.h @@ -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 + +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