Change directory policy 57/263057/7
authorJunghyun Yeon <jungh.yeon@samsung.com>
Wed, 25 Aug 2021 02:33:31 +0000 (11:33 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Thu, 26 Aug 2021 02:49:28 +0000 (11:49 +0900)
- Create root directory when installing RPM package.
- Change codes to set uid/gid with designated user and group.
- Cleanup unit test for reduce duplicated codes.

Change-Id: Id7621cced36340e045a7864b1b5e9e3756d3d36e
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
13 files changed:
packaging/pkgmgr-tool.spec
src/res-copy/include/abstract_request_handler.hh
src/res-copy/include/file_util.hh
src/res-copy/src/abstract_request_handler.cc
src/res-copy/src/condition_validator.cc
src/res-copy/src/copy_request_handler.cc
src/res-copy/src/createdir_request_handler.cc
src/res-copy/src/file_util.cc
src/res-copy/src/remove_request_handler.cc
src/res-copy/src/uninstall_request_handler.cc
tests/mock/os_mock.cc
tests/mock/os_mock.h
tests/unit_tests/res-copy/src/test_request_handler.cc

index 2562d5af8728fa50de8fe3a366b96cc94f6ce941..5d0110a058e8eb45162bf7e0275780de5280644b 100644 (file)
@@ -98,6 +98,8 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 
 %post
 /sbin/ldconfig
+mkdir -p %{_sysconfdir}/skel/priv_shared_res
+chsmack -t -a User::Home %{_sysconfdir}/skel/priv_shared_res
 
 # Update mime database to support package mime types
 update-mime-database %{_datadir}/mime
index c0dcb7bf466dfaa180a8f7f24ed612c618e97afd..aa61ab88faf893651fbf1fea3092850e8c17150b 100644 (file)
@@ -38,6 +38,8 @@ class AbstractRequestHandler {
 
  protected:
   std::string GetRootPath();
+  std::string GetSrcRootPath();
+  std::string GetDstRootPath();
   const std::string GetPkgID() const;
   const std::list<ResPathInfo> GetPathList() const;
   uid_t GetUID() const;
index ccec6d173c48e7da8afda6058bd005ce6aa604c2..238516a8f444251bf999dbe72a718e21244702eb 100644 (file)
@@ -26,37 +26,32 @@ namespace bf = boost::filesystem;
 
 namespace res_handler {
 
-// TODO(jungh.yeon) : is this necessary?
 enum FSFlag : int {
   FS_NONE              = 0,
   FS_MERGE_SKIP        = (1 << 0),
   FS_MERGE_OVERWRITE   = (1 << 1),
   FS_COMMIT_COPY_FILE  = (1 << 2),
-  FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
 };
 
 FSFlag operator|(FSFlag a, FSFlag b);
 
-bool CreateDir(const boost::filesystem::path& path, uid_t uid = 0);
-bool CreateDirs(const std::string& pkgid, const boost::filesystem::path& path, uid_t uid = 0);
+bool CreateDir(const boost::filesystem::path& path);
+bool CreateDirs(const std::string& pkgid, const boost::filesystem::path& path);
 
 bool CopyDir(const boost::filesystem::path& src,
              const boost::filesystem::path& dst,
-             uid_t uid, FSFlag flags = FS_NONE, bool skip_symlink = false);
+             FSFlag flags = FS_NONE, bool skip_symlink = false);
 
 bool CopyFile(const boost::filesystem::path& src,
              const boost::filesystem::path& dst);
 
 bool RemoveAll(const boost::filesystem::path& path);
 
-boost::optional<gid_t> GetGidByUid(uid_t uid);
-
 bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
-                      boost::filesystem::perms permissions, uid_t uid,
-                      gid_t gid);
+                      mode_t  mode, uid_t uid, gid_t gid);
 
 bool SetDirPermissions(const boost::filesystem::path& path,
-                      boost::filesystem::perms permissions);
+                      mode_t mode);
 
 bool SetOwnership(const bf::path& path, uid_t uid, gid_t gid);
 
index 0f95365ad249c2166ce3d501540693742238b71b..113f38e420dc5b2fed585dbaae3a6af33386bfd2 100644 (file)
 #include "include/request_type.hh"
 #include "include/res_path_info.hh"
 
+namespace {
+
+constexpr char kPrivSharedRes[] = "/priv_shared_res/";
+constexpr char kAppsRw[] = "/apps_rw/";
+
+}  // namespace
+
 namespace res_handler {
 
 std::string AbstractRequestHandler::GetRootPath() {
   return root_path_;
 }
 
+std::string AbstractRequestHandler::GetSrcRootPath() {
+  return root_path_ + kAppsRw + pkgid_;
+}
+
+std::string AbstractRequestHandler::GetDstRootPath() {
+  return root_path_ + kPrivSharedRes + pkgid_;
+}
+
 const std::string AbstractRequestHandler::GetPkgID() const {
   return pkgid_;
 }
index 095cb6e7fe2ecfe3f04016853cc877d850d30813..6c8d458b253e8c57e17a8258c05ea7c69d5b8da3 100644 (file)
@@ -169,7 +169,7 @@ ErrorType ConditionValidator::CheckRemoveRequest(
     std::list<ResPathInfo> path_list) {
   for (auto& path_info : path_list) {
     boost::filesystem::path dst_path(root_path_);
-    dst_path = dst_path / "shared_res"/ pkgid_ / path_info.GetDstPath();
+    dst_path = dst_path / "priv_shared_res"/ pkgid_ / path_info.GetDstPath();
     if (!boost::filesystem::exists(dst_path)) {
       LOG(ERROR) << "Resource not exists : " << dst_path;
       return ErrorType::ERROR_RES_NOT_FOUND;
index 0544fa28095e071f07c0e46fa5c3170b95647bae..4aca075587dc5f52e023d96adb9010f1617e1c61 100644 (file)
@@ -45,11 +45,10 @@ CopyRequestHandler::CopyRequestHandler(
   AbstractRequestHandler(pkgid, uid, root_path, path_list) {}
 
 ErrorType CopyRequestHandler::Execute() {
-  bf::path root_path(GetRootPath());
-  bf::path src_root_path = root_path / "apps_rw" / GetPkgID();
-  bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
+  bf::path src_root_path(GetSrcRootPath());
+  bf::path dst_root_path(GetDstRootPath());
 
-  if (!CreateDir(dst_root_path, GetUID()))
+  if (!CreateDir(dst_root_path))
     return ErrorType::ERROR_SYSTEM_ERROR;
 
   for (auto& path_info : GetPathList()) {
@@ -62,8 +61,7 @@ ErrorType CopyRequestHandler::Execute() {
     bf::path dst_path = dst_root_path / path_info.GetDstPath();
 
     if (bf::is_directory(src_path)) {
-      if (!CopyDir(src_path, dst_path, GetUID(),
-          FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS, true)) {
+      if (!CopyDir(src_path, dst_path, FS_MERGE_OVERWRITE, true)) {
         LOG(ERROR) << "Failed to copy directory " << src_path;
         return ErrorType::ERROR_SYSTEM_ERROR;
       }
index d03f1f538c250a6adef60a827f138bb63394a1b3..dbc053ff9a917235b748309df1ee3db42aab9017 100644 (file)
@@ -45,15 +45,14 @@ CreateDirRequestHandler::CreateDirRequestHandler(
   AbstractRequestHandler(pkgid, uid, root_path, path_list) {}
 
 ErrorType CreateDirRequestHandler::Execute() {
-  bf::path root_path(GetRootPath());
-  bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
+  bf::path dst_root_path(GetDstRootPath());
 
-  if (!CreateDir(dst_root_path, GetUID()))
+  if (!CreateDir(dst_root_path))
     return ErrorType::ERROR_SYSTEM_ERROR;
 
   for (auto& path_info : GetPathList()) {
     bf::path dst_path = dst_root_path / path_info.GetSrcPath();
-    if (!CreateDirs(GetPkgID(), dst_path, GetUID()))
+    if (!CreateDirs(GetPkgID(), dst_path))
       return ErrorType::ERROR_SYSTEM_ERROR;
   }
 
index b7f1271770c5d6254e90ea4e69e12086cfd31c31..b82383980a79404b519f5be0773c38b19531880f 100644 (file)
@@ -35,8 +35,9 @@ namespace bf = boost::filesystem;
 
 namespace {
 
-const int32_t kPWBufSize = sysconf(_SC_GETPW_R_SIZE_MAX);
-const int32_t kGRBufSize = sysconf(_SC_GETGR_R_SIZE_MAX);
+constexpr uid_t kRootUID = 0;
+constexpr gid_t kPrivPlatformGid = 10212;
+constexpr mode_t kDefaultMode640 = S_IRUSR | S_IWUSR | S_IRGRP;
 
 }  // namespace
 
@@ -47,36 +48,13 @@ FSFlag operator|(FSFlag a, FSFlag b) {
 }
 
 bool SetDirPermissions(const boost::filesystem::path& path,
-                      boost::filesystem::perms permissions) {
-  boost::system::error_code error;
-  bf::permissions(path, permissions, error);
-
-  if (error) {
-    LOG(ERROR) << "Failed to set permissions for directory: " << path
-               << boost::system::system_error(error).what();
+                      mode_t mode) {
+  if (chmod(path.string().c_str(), mode) != 0) {
+    LOG(ERROR) << "Failed to set permissions for path: " << path
+               << ", errno : " << errno;
     return false;
   }
-  return true;
-}
 
-bool CopyOwnershipAndPermissions(const boost::filesystem::path& src,
-                                 const boost::filesystem::path& dst) {
-  if (!bf::exists(src)) {
-    LOG(ERROR) << "Failed to copy ownership and permissions"
-               << " from " << src << " to " << dst;
-    return false;
-  }
-  bf::perms permissions = bf::status(src).permissions();
-  struct stat stats;
-  if (stat(src.c_str(), &stats) != 0)
-    return false;
-
-  if (!SetDirOwnershipAndPermissions(dst, permissions, stats.st_uid,
-                                    stats.st_gid)) {
-    LOG(ERROR) << "Failed to copy ownership and permissions"
-               << " from " << src << " to " << dst;
-    return false;
-  }
   return true;
 }
 
@@ -97,7 +75,7 @@ bool RemoveAll(const bf::path& path) {
 
 bool CopyDir(const boost::filesystem::path& src,
              const boost::filesystem::path& dst,
-             uid_t uid, FSFlag flags, bool skip_symlink) {
+             FSFlag flags, bool skip_symlink) {
   try {
     // Check whether the function call is valid
     if (!bf::exists(src) || !bf::is_directory(src)) {
@@ -107,20 +85,16 @@ bool CopyDir(const boost::filesystem::path& src,
     }
     if (!bf::exists(dst)) {
       // Create the destination directory
-      if (!CreateDir(dst, uid)) {
+      if (!CreateDir(dst)) {
         LOG(ERROR) << "Unable to create destination directory" << dst;
         return false;
       }
-      if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
-        CopyOwnershipAndPermissions(src, dst);
     } else {
       if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
         LOG(ERROR) << "Destination directory " << dst.string()
                    << " already exists.";
         return false;
       }
-      if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS))
-        CopyOwnershipAndPermissions(src, dst);
     }
   } catch (const bf::filesystem_error& error) {
     LOG(ERROR) << "Failed to copy directory: " << error.what();
@@ -150,7 +124,7 @@ bool CopyDir(const boost::filesystem::path& src,
         }
       } else if (bf::is_directory(current)) {
         // Found directory: Recursion
-        if (!CopyDir(current, target, uid, flags, skip_symlink)) {
+        if (!CopyDir(current, target, flags, skip_symlink)) {
           return false;
         }
       } else {
@@ -168,12 +142,10 @@ bool CopyDir(const boost::filesystem::path& src,
         else
           bf::copy_file(current, destination);
 
-        if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
-          CopyOwnershipAndPermissions(current, destination);
-
         if (flags & FS_COMMIT_COPY_FILE) {
           if (flags & FS_MERGE_OVERWRITE)
             bf::remove(target);
+
           bf::rename(destination, target);
         }
       }
@@ -185,7 +157,7 @@ bool CopyDir(const boost::filesystem::path& src,
   return true;
 }
 
-bool CreateDir(const bf::path& path, uid_t uid) {
+bool CreateDir(const bf::path& path) {
   if (bf::exists(path))
     return true;
 
@@ -198,20 +170,18 @@ bool CreateDir(const bf::path& path, uid_t uid) {
     return false;
   }
 
-  boost::optional<gid_t> gid = GetGidByUid(uid);
-  if (!gid)
-    return false;
-
-  bf::perms perms755 = bf::all_all ^ bf::group_write ^ bf::others_write;
-  if (!SetDirOwnershipAndPermissions(path, perms755, uid, *gid))
+  if (!SetDirOwnershipAndPermissions(
+      path, kDefaultMode640, kRootUID, kPrivPlatformGid))
     return false;
 
   return true;
 }
 
 bool CopyFile(const bf::path& src, const bf::path& dst) {
-  bs::error_code error;
+  if (bf::is_symlink(bf::symlink_status(src)))
+    return true;
 
+  bs::error_code error;
   bf::copy_file(src, dst, bf::copy_option::overwrite_if_exists, error);
   if (error) {
     LOG(WARNING) << "copy file " << src << " due to error ["
@@ -219,7 +189,8 @@ bool CopyFile(const bf::path& src, const bf::path& dst) {
     return false;
   }
 
-  if (!CopyOwnershipAndPermissions(src, dst)) {
+  if (!SetDirOwnershipAndPermissions(
+      dst, kDefaultMode640, kRootUID, kPrivPlatformGid)) {
     LOG(ERROR) << "Failed set permission for file " << dst;
     return false;
   }
@@ -227,43 +198,33 @@ bool CopyFile(const bf::path& src, const bf::path& dst) {
   return true;
 }
 
-boost::optional<gid_t> GetGidByUid(uid_t uid) {
-  struct passwd pwd;
-  struct passwd* pwd_result;
-  char buf[kPWBufSize];
-  int ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &pwd_result);
-  if (ret != 0 || pwd_result == nullptr)
-    return {};
-  return pwd.pw_gid;
-}
-
 bool SetOwnership(const bf::path& path, uid_t uid, gid_t gid) {
   int ret = lchown(path.c_str(), uid, gid);
   if (ret != 0) {
     LOG(ERROR) << "Failed to change owner of: " << path;
+    std::cerr << "jungh failed to set lchown : " << ret << std::endl;
     return false;
   }
   return true;
 }
 
 bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
-                      boost::filesystem::perms permissions, uid_t uid,
-                      gid_t gid) {
+                      mode_t mode, uid_t uid, gid_t gid) {
   if (!SetOwnership(path, uid, gid)) {
     LOG(ERROR) << "Failed to change owner: " << path
                << "(" << uid << ", " << gid << ")";
     return false;
   }
-  if (!SetDirPermissions(path, permissions)) {
-    LOG(ERROR) << "Failed to change permission: " << path
-               << std::oct << permissions;
+  if (!SetDirPermissions(path, mode)) {
+    LOG(ERROR) << "Failed to change permission: " << path;
     return false;
   }
 
   return true;
 }
 
-bool CreateDirs(const std::string& pkgid, const boost::filesystem::path& path, uid_t uid) {
+bool CreateDirs(
+    const std::string& pkgid, const boost::filesystem::path& path) {
   boost::system::error_code error;
   bf::create_directories(path, error);
 
@@ -273,22 +234,19 @@ bool CreateDirs(const std::string& pkgid, const boost::filesystem::path& path, u
     return false;
   }
 
-  boost::optional<gid_t> gid = GetGidByUid(uid);
-  bf::perms perms755 = bf::all_all ^ bf::group_write ^ bf::others_write;
-  if (!gid)
-    return false;
-
   bf::path target_path = path;
   while(target_path.filename() != pkgid) {
 
-    if (!SetDirOwnershipAndPermissions(target_path, perms755, uid, *gid))
+    if (!SetDirOwnershipAndPermissions(
+        target_path, kDefaultMode640, kRootUID, kPrivPlatformGid))
       return false;
 
     target_path = target_path.parent_path();
   }
 
-  if (!SetDirOwnershipAndPermissions(target_path, perms755, uid, *gid))
-      return false;
+  if (!SetDirOwnershipAndPermissions(
+      target_path, kDefaultMode640, kRootUID, kPrivPlatformGid))
+    return false;
 
   return true;
 }
index cc3ed1cdc932410cc0e83b956685a6b6b8d71120..9f752a331497cf972717faf18c39e429ce56f1dc 100644 (file)
@@ -45,8 +45,7 @@ RemoveRequestHandler::RemoveRequestHandler(
   AbstractRequestHandler(pkgid, uid, root_path, path_list) {}
 
 ErrorType RemoveRequestHandler::Execute() {
-  bf::path root_path(GetRootPath());
-  bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
+  bf::path dst_root_path(GetDstRootPath());
 
   if (!bf::exists(dst_root_path)) {
     LOG(ERROR) << "root path not exists";
index da0b841989d1341241c74b615f1289aab25146b9..c5912b03d034552c50860728140d67bfdf726ba9 100644 (file)
@@ -47,9 +47,7 @@ ErrorType UninstallRequestHandler::Execute() {
     return ErrorType::ERROR_INVALID_PARAMETER;
   }
 
-  bf::path root_path(GetRootPath());
-  root_path = root_path / "shared_res" / GetPkgID();
-
+  bf::path root_path(GetDstRootPath());
   if (!bf::exists(root_path)) {
     LOG(WARNING) << "path not exists : " << root_path;
     return ErrorType::ERROR_NONE;
index f16a95ebb281a6af740f3bfd0aa4ed3a55a365d6..635386285cfd0c09bb577747a9212e833aca886b 100644 (file)
@@ -34,6 +34,14 @@ extern "C" int fchmod(int fd, mode_t mode) {
   return MOCK_HOOK_P2(OsMock, fchmod, fd, mode);
 }
 
+extern "C" int chmod(const char* pathanme, mode_t mode) {
+  return MOCK_HOOK_P2(OsMock, chmod, pathanme, mode);
+}
+
+extern "C" int lchown(const char* pathname, uid_t uid, gid_t gid) {
+  return MOCK_HOOK_P3(OsMock, lchown, pathname, uid, gid);
+}
+
 extern "C" int smack_setlabel(const char *path, const char* label,
     enum smack_label_type type) {
   return MOCK_HOOK_P3(OsMock, smack_setlabel, path, label, type);
index f935f3fabb22766f4948c66e97ea2223cff4bd83..6de13f770fe7863e031959c7e2fe3fe9a19cb9c3 100644 (file)
@@ -32,6 +32,8 @@ class OsMock : public virtual ModuleMock {
   MOCK_METHOD3(waitpid, pid_t(pid_t, int*, int));
   MOCK_METHOD3(fchown, int(int, uid_t, gid_t));
   MOCK_METHOD2(fchmod, int(int, mode_t));
+  MOCK_METHOD2(chmod, int(const char*, mode_t));
+  MOCK_METHOD3(lchown, int(const char*, uid_t, gid_t));
   MOCK_METHOD3(smack_setlabel, int(const char*, const char*, smack_label_type));
   MOCK_METHOD2(execvp, int(const char*, char* const []));
 };
index fbfce840a78e0f2c831aac7ce928c9385b75e156..33d06bd2ef9114aaf177d1c61147774891e001f1 100644 (file)
@@ -57,6 +57,28 @@ using res_handler::UninstallRequestHandler;
 namespace bf = boost::filesystem;
 namespace bs = boost::system;
 
+namespace {
+
+constexpr char kTestPkgID[] = "test_pkg";
+
+std::string GetTestPkgID() {
+  return kTestPkgID;
+}
+
+std::string GetRootPath() {
+  return "./tests/unit_tests/res-copy/data";
+}
+
+bf::path GetSrcRootPath() {
+  return bf::path(GetRootPath()) / "apps_rw" / kTestPkgID;
+}
+
+bf::path GetDstRootPath() {
+  return bf::path(GetRootPath()) / "priv_shared_res" / kTestPkgID;
+}
+
+}  // namespace
+
 class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
     public ::testing::NiceMock<OsMock> {};
 
@@ -66,16 +88,14 @@ class CopyRequestHandlerTest : public TestFixture {
   virtual ~CopyRequestHandlerTest() {}
 
   virtual void SetUp() {
-    bf::path rootpath(root_path);
+    bf::create_directories(GetDstRootPath() / "dest_dir");
 
-    bf::create_directories(
-        rootpath / "shared_res/test_pkg/dest_dir");
+    EXPECT_CALL(GetMock<OsMock>(), lchown(_, _, _)).WillRepeatedly(Return(0));
+    EXPECT_CALL(GetMock<OsMock>(), chmod(_, _)).WillRepeatedly(Return(0));
   }
   virtual void TearDown() {
-    bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+    bf::remove_all(GetDstRootPath());
   }
-
-  std::string root_path = "./tests/unit_tests/res-copy/data";
 };
 
 class RemoveRequestHandlerTest : public TestFixture {
@@ -84,25 +104,17 @@ class RemoveRequestHandlerTest : public TestFixture {
   virtual ~RemoveRequestHandlerTest() {}
 
   virtual void SetUp() {
-    bf::path rootpath(root_path);
-
-    bf::create_directories(
-        rootpath / "shared_res/test_pkg/new_dir/new_dir2");
-
-    bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
-        rootpath / "shared_res/test_pkg/resource_file.txt");
-
-    bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
-        rootpath / "shared_res/test_pkg/new_dir/resource_file.txt");
-
-    bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
-        rootpath / "shared_res/test_pkg/new_dir/new_dir2/resource_file.txt");
+    bf::create_directories(GetDstRootPath() / "new_dir/new_dir2");
+    bf::copy_file(GetSrcRootPath() / "data/resource_file.txt",
+        GetDstRootPath() / "resource_file.txt");
+    bf::copy_file(GetSrcRootPath() / "data/resource_file.txt",
+        GetDstRootPath() / "new_dir/resource_file.txt");
+    bf::copy_file(GetSrcRootPath() / "data/resource_file.txt",
+        GetDstRootPath() / "new_dir/new_dir2/resource_file.txt");
   }
   virtual void TearDown() {
-    bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+    bf::remove_all(GetDstRootPath());
   }
-
-  std::string root_path = "./tests/unit_tests/res-copy/data";
 };
 
 class UninstallRequestHandlerTest : public TestFixture {
@@ -111,15 +123,12 @@ class UninstallRequestHandlerTest : public TestFixture {
   virtual ~UninstallRequestHandlerTest() {}
 
   virtual void SetUp() {
-    bf::path rootpath(root_path);
-    bf::create_directories(
-        rootpath / "shared_res/test_pkg");
+    bf::create_directories(GetDstRootPath());
   }
+
   virtual void TearDown() {
-    bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+    bf::remove_all(GetDstRootPath());
   }
-
-  std::string root_path = "./tests/unit_tests/res-copy/data";
 };
 
 class CreateDirRequestHandlerTest : public TestFixture {
@@ -128,25 +137,22 @@ class CreateDirRequestHandlerTest : public TestFixture {
   virtual ~CreateDirRequestHandlerTest() {}
 
   virtual void SetUp() {
-    bf::path rootpath(root_path);
-    bf::create_directories(
-        rootpath / "shared_res/test_pkg/existed_dir");
+    bf::create_directories(GetDstRootPath() / "existed_dir");
   }
+
   virtual void TearDown() {
-    bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+    bf::remove_all(GetDstRootPath());
   }
-
-  std::string root_path = "./tests/unit_tests/res-copy/data";
 };
 
 TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_file.txt", "");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "resource_file.txt";
@@ -156,11 +162,11 @@ TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot) {
 TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot_ChangeFileName) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_file.txt", "another_name.txt");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "another_name.txt";
@@ -170,11 +176,11 @@ TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot_ChangeFileName) {
 TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_file.txt", "dest_dir");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "dest_dir";
@@ -187,11 +193,11 @@ TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory) {
 TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory_ChangeFileName) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_file.txt", "dest_dir/newname.txt");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "dest_dir";
@@ -204,11 +210,11 @@ TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory_ChangeFileName) {
 TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1/resource_file3.txt", "");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "resource_file3.txt";
@@ -219,11 +225,11 @@ TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot_ChangeFileName) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back(
       "data/resource_dir1/resource_file3.txt", "newname.txt");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "newname.txt";
@@ -233,11 +239,11 @@ TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot_ChangeFileName) {
 TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1/resource_file3.txt", "dest_dir");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "dest_dir";
@@ -251,11 +257,11 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot_ChangeFileName) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back(
     "data/resource_dir1/resource_file3.txt", "dest_dir/newname.txt");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "dest_dir";
@@ -268,11 +274,11 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot_ChangeFileName) {
 TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1", "");
-  CopyRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "resource_file3.txt";
@@ -289,11 +295,11 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot) {
 TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1", "dest_dir");
-  CopyRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "dest_dir";
@@ -313,11 +319,11 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory) {
 TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory2) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1", "resource_dir1");
-  CopyRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "resource_dir1";
@@ -337,11 +343,11 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory2) {
 TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1/resource_dir2", "");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "resource_file2.txt";
@@ -351,11 +357,11 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToRoot) {
 TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_dir1/resource_dir2", "dest_dir");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg");
+  bf::path check_path = GetDstRootPath();
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "dest_dir";
@@ -368,7 +374,7 @@ TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToDirectory) {
 TEST_F(CopyRequestHandlerTest, ResNotexists) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/not_existed_res", "new_dir");
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
 
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_RES_NOT_FOUND);
 }
@@ -377,21 +383,22 @@ TEST_F(CopyRequestHandlerTest, ReplaceRes) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("data/resource_file.txt", "new_dir");
 
-  CopyRequestHandler handler("test_pkg", 0, root_path, path_list);
+  CopyRequestHandler handler(GetTestPkgID(), 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  CopyRequestHandler replace_handler("test_pkg", 0, root_path, path_list);
-  EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+  CopyRequestHandler replace_handler(
+      GetTestPkgID(), 0, GetRootPath(), path_list);
+  EXPECT_EQ(replace_handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 }
 
 TEST_F(RemoveRequestHandlerTest, RemoveFileAtRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("resource_file.txt", "");
 
-  RemoveRequestHandler handler("test_pkg", 0, root_path, path_list);
+  RemoveRequestHandler handler("test_pkg", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/resource_file.txt");
+  bf::path check_path(GetDstRootPath() / "resource_file.txt");
   EXPECT_FALSE(bf::exists(check_path));
 }
 
@@ -399,11 +406,10 @@ TEST_F(RemoveRequestHandlerTest, RemoveFileAtDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("new_dir/resource_file.txt", "");
 
-  RemoveRequestHandler handler("test_pkg", 0, root_path, path_list);
+  RemoveRequestHandler handler("test_pkg", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(
-      root_path + "/shared_res/test_pkg/new_dir/resource_file.txt");
+  bf::path check_path(GetDstRootPath() / "new_dir/resource_file.txt");
   EXPECT_FALSE(bf::exists(check_path));
 }
 
@@ -411,10 +417,10 @@ TEST_F(RemoveRequestHandlerTest, RemoveDirectoryAtRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("new_dir", "");
 
-  RemoveRequestHandler handler("test_pkg", 0, root_path, path_list);
+  RemoveRequestHandler handler("test_pkg", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/new_dir");
+  bf::path check_path(GetDstRootPath() / "new_dir");
   EXPECT_FALSE(bf::exists(check_path));
 }
 
@@ -422,36 +428,35 @@ TEST_F(RemoveRequestHandlerTest, RemoveDirectoryAtDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("new_dir/new_dir2", "");
 
-  RemoveRequestHandler handler("test_pkg", 0, root_path, path_list);
+  RemoveRequestHandler handler("test_pkg", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/new_dir/new_dir2");
+  bf::path check_path(GetDstRootPath() / "new_dir/new_dir2");
   EXPECT_FALSE(bf::exists(check_path));
 }
 
 TEST_F(UninstallRequestHandlerTest, RemoveRootPath) {
   std::list<ResPathInfo> path_list;
 
-  UninstallRequestHandler handler("test_pkg", 0, root_path, path_list);
+  UninstallRequestHandler handler(
+      "test_pkg", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
-
-  bf::path check_path(root_path + "/shared_res/test_pkg/");
-  EXPECT_FALSE(bf::exists(check_path));
+  EXPECT_FALSE(bf::exists(GetDstRootPath()));
 }
 
 TEST_F(UninstallRequestHandlerTest, RootNotExists) {
   std::list<ResPathInfo> path_list;
-  bf::path check_path(root_path + "/shared_res/test_pkg/");
-  bf::remove_all(check_path);
+  bf::remove_all(GetDstRootPath());
 
-  UninstallRequestHandler handler("test_pkg", 0, root_path, path_list);
+  UninstallRequestHandler handler(
+      "test_pkg", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
 }
 
 TEST_F(UninstallRequestHandlerTest, EmptyPkgID) {
   std::list<ResPathInfo> path_list;
 
-  UninstallRequestHandler handler("", 0, root_path, path_list);
+  UninstallRequestHandler handler("", 0, GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(),
       res_handler::ErrorType::ERROR_INVALID_PARAMETER);
 }
@@ -460,51 +465,52 @@ TEST_F(CreateDirRequestHandlerTest, CreateOneAtRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("new_dir", "");
 
-  CreateDirRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CreateDirRequestHandler handler(
+      "test_pkg", getuid(), GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(),
       res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/new_dir");
-  EXPECT_TRUE(bf::exists(check_path));
+  EXPECT_TRUE(bf::exists(GetDstRootPath() / "new_dir"));
 }
 
 TEST_F(CreateDirRequestHandlerTest, CreateOneAtExistedDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("existed_dir/new_dir", "");
 
-  CreateDirRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CreateDirRequestHandler handler(
+      "test_pkg", getuid(), GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(),
       res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/existed_dir/new_dir");
-  EXPECT_TRUE(bf::exists(check_path));
+  EXPECT_TRUE(bf::exists(GetDstRootPath() / "existed_dir/new_dir"));
 }
 
 TEST_F(CreateDirRequestHandlerTest, CreateHierachyAtRoot) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("new_dir/another_new_dir", "");
 
-  CreateDirRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CreateDirRequestHandler handler(
+      "test_pkg", getuid(), GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(),
       res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/new_dir");
+  bf::path check_path(GetDstRootPath() / "new_dir");
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "another_new_dir";
   EXPECT_TRUE(bf::exists(check_path));
 }
 
-
 TEST_F(CreateDirRequestHandlerTest, CreateHierachyAtExistedDirectory) {
   std::list<ResPathInfo> path_list;
   path_list.emplace_back("existed_dir/new_dir/another_new_dir", "");
 
-  CreateDirRequestHandler handler("test_pkg", getuid(), root_path, path_list);
+  CreateDirRequestHandler handler(
+      "test_pkg", getuid(), GetRootPath(), path_list);
   EXPECT_EQ(handler.Execute(),
       res_handler::ErrorType::ERROR_NONE);
 
-  bf::path check_path(root_path + "/shared_res/test_pkg/existed_dir/new_dir");
+  bf::path check_path(GetDstRootPath() / "existed_dir/new_dir");
   EXPECT_TRUE(bf::exists(check_path));
 
   check_path /= "another_new_dir";