SM : Modify AppInstallHelper class 20/88520/6
authorMateusz Forc <m.forc@samsung.com>
Mon, 19 Sep 2016 10:56:54 +0000 (12:56 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 21 Sep 2016 13:12:30 +0000 (15:12 +0200)
* Add local installation
* Change vector of dirs to map (path,pathType)
* Add support of user UID,GID
* Fix sharedRO path cleanup
Change-Id: I4d34162fa237d4037a21a3b999e56977961f9a16

src/security-manager-tests/common/app_install_helper.h
src/security-manager-tests/common/tzplatform.cpp
src/security-manager-tests/common/tzplatform.h

index ff62982..32eaa23 100644 (file)
 #pragma once
 
 #include <fcntl.h>
-#include <unistd.h>
+#include <map>
 #include <string>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/smack.h>
+#include <unistd.h>
+#include <utility>
+
+#include <security-manager-types.h>
 
 #include <dpl/test/test_runner.h>
 #include <sm_commons.h>
 #include <tzplatform.h>
 
+const uid_t OWNER_UID = 5001;
+const std::string TIZEN_VERSION = "3.0";
+
 struct AppInstallHelper {
-    AppInstallHelper(const std::string &name)
-      : m_appName(name), m_pkgName(name)
+
+    using TypePathsMap = std::map<app_install_path_type, std::vector<std::string>>;
+    AppInstallHelper(const std::string &appName,
+                     const std::string &pkgName,
+                     bool isLocal = false,
+                     uid_t uid = OWNER_UID,
+                     std::string version = TIZEN_VERSION)
+      : m_appName(appName), m_pkgName(pkgName), m_isLocal(isLocal), m_uidGid(uid), m_version(version)
+    {
+        setInstallPath();
+    }
+
+    AppInstallHelper(const std::string &appName, const std::string &pkgName, std::string version, bool isLocal = false)
+      : AppInstallHelper(appName, pkgName, isLocal, OWNER_UID, version)
+    {}
+
+    AppInstallHelper(const std::string &name, bool isLocal = false, uid_t uid = OWNER_UID)
+      : AppInstallHelper(name, name, isLocal, uid, TIZEN_VERSION)
     {}
 
-    AppInstallHelper(const std::string &appName, const std::string &pkgName)
-      : m_appName(appName), m_pkgName(pkgName)
+    AppInstallHelper(const std::string &appName, const std::string &pkgName, bool isLocal = false)
+      : AppInstallHelper(appName, pkgName, isLocal, OWNER_UID, TIZEN_VERSION)
     {}
 
+    /**
+     * This constructor needs to be defined, because in other tests : private_sharing
+     * when AppInstallHelper object is initialized from : {"app_id", "pkg_id"}
+     * none of arguments is std::string type, but const char*, so
+     * implicit conversion is done and AppInstallHelper(const std::string&, int, bool) is called
+     */
+    AppInstallHelper(const char* const appName, const char* const pkgName, bool isLocal = false)
+      : m_appName(appName), m_pkgName(pkgName), m_isLocal(isLocal)
+    {
+        setInstallPath();
+    }
+
     std::string getInstallDir() const {
-        return  TzPlatformConfig::globalAppDir() + "/" + getPkgId();
+        return m_installDir + getPkgId();
     }
 
     std::string getTrustedDir(int i = 0) const {
@@ -50,6 +85,15 @@ struct AppInstallHelper {
     std::string getSharedPath(int i = 0) const {
         return getPrivateDir() + "shareme" + std::to_string(i);
     }
+
+    std::string getSharedRODir() const {
+        return getInstallDir() + "/app_dir_rw_others_ro/";
+    }
+
+    std::string getPrivateRODir() const {
+        return getInstallDir() + "/app_dir_ro/";
+    }
+
     std::string getAppId() const {
         return m_appName + "_app_id";
     }
@@ -58,36 +102,51 @@ struct AppInstallHelper {
         return m_pkgName + "_pkg_id";
     }
 
+    std::string getVersion() const {
+        return m_version;
+    }
+
+    int getUID() const {
+        return m_uidGid;
+    }
+
+    int getGID() const {
+        return m_uidGid;
+    }
+
     void createInstallDir() {
-        if (mkdir(getInstallDir().c_str(), 0777) == 0) {
-            m_dirs.push_back(getInstallDir());
-        }
+        create(mkdir, getInstallDir());
     }
 
     void createTrustedDir(int i = 0) {
-        if (mkdir(getTrustedDir(i).c_str(), 0777) == 0) {
-            m_dirs.push_back(getTrustedDir(i));
-        }
+        if (create(mkdir, getTrustedDir(i)))
+            m_dirTypeMap[SECURITY_MANAGER_PATH_TRUSTED_RW].emplace_back(getTrustedDir(i));
     }
+
     void createPrivateDir() {
-        if (mkdir(getPrivateDir().c_str(), 0777) == 0) {
-            m_dirs.push_back(getPrivateDir());
-        }
+        if (create(mkdir, getPrivateDir()))
+            m_dirTypeMap[SECURITY_MANAGER_PATH_RW].emplace_back(getPrivateDir());
     }
 
     void createSharedFile(int i = 0) {
-        if (creat(getSharedPath(i).c_str(), 0777) == 0) {
-            m_files.push_back(getSharedPath(i));
-        }
+        if (create(creat, getSharedPath(i)))
+            m_fileTypeMap[SECURITY_MANAGER_PATH_RW].emplace_back(getSharedPath(i));
+    }
+
+    void createSharedRODir() {
+        if (create(mkdir, getSharedRODir()))
+            m_dirTypeMap[SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO].emplace_back(getSharedRODir());
+    }
+
+    void createPrivateRODir() {
+        if (create(mkdir, getPrivateRODir()))
+            m_dirTypeMap[SECURITY_MANAGER_PATH_RO].emplace_back(getPrivateRODir());
     }
 
     void revokeRules() const {
         RUNNER_ASSERT_MSG(
             0 == smack_revoke_subject(generateAppLabel().c_str()),
             "Revoking smack subject failed");
-        RUNNER_ASSERT_MSG(
-            0 == smack_revoke_subject(generatePkgLabel().c_str()),
-            "Revoking smack subject failed");
     }
 
     std::string generateAppLabel() const {
@@ -98,26 +157,61 @@ struct AppInstallHelper {
         return generatePathRWLabel(getPkgId());
     }
 
+    const TypePathsMap& getDirsMap() const {
+        return m_dirTypeMap;
+    }
+
+    const TypePathsMap& getFilesMap() const {
+        return m_fileTypeMap;
+    }
+
     void removePaths() {
-        for (const auto &dir : m_dirs) {
-            rmdir(dir.c_str());
-        }
-        m_dirs.clear();
-        for (const auto &file : m_files) {
-            unlink(file.c_str());
-        }
-        m_files.clear();
+        // FIXME - remove special treatment for shared ro
+        for (const auto &oneTypePaths : m_dirTypeMap)
+            if (oneTypePaths.first != SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO)
+                for (const auto& path : oneTypePaths.second)
+                    rmdir(path.c_str());
+
+        m_dirTypeMap.clear();
+
+        for (const auto &oneTypePaths : m_fileTypeMap)
+            if (oneTypePaths.first != SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO)
+                for (const auto& path : oneTypePaths.second)
+                    unlink(path.c_str());
+
+        m_fileTypeMap.clear();
+
+        rmdir(m_installDir.c_str());
     }
 
     virtual ~AppInstallHelper() {
-        // TODO we should also remove trusted dirs created with custom params
         removePaths();
     }
 
 protected:
+    void setInstallPath() {
+        if (m_isLocal)
+            m_installDir = TzPlatformConfig::appDirPath(getUID());
+        else
+            m_installDir = TzPlatformConfig::globalAppDir() + "/";
+    }
+
+    bool create(std::function<int(const char*, mode_t)> &&creatFun, const std::string &path) {
+        if (creatFun(path.c_str(), 0751) == 0) {
+           // Local paths need user change
+           if (!m_isLocal || chown(path.c_str(), m_uidGid, m_uidGid) == 0)
+                    return true;
+           }
+        return false;
+    }
+
     std::string m_appName;
     std::string m_pkgName;
-    std::vector<std::string> m_dirs;
-    std::vector<std::string> m_files;
-};
+    bool m_isLocal;
+    int m_uidGid;
+    std::string m_version;
 
+    std::string m_installDir;
+    TypePathsMap m_dirTypeMap;
+    TypePathsMap m_fileTypeMap;
+};
index 315bb48..522f74e 100644 (file)
@@ -44,21 +44,26 @@ uid_t getGlobalGroupId(void)
 const std::string appDirPath(const TemporaryTestUser &user, const std::string &appId,
                              const std::string &pkgId)
 {
+    return appDirPath(user.getUid()) + pkgId + "/" + appId;
+}
+
+const std::string appDirPath(uid_t uid)
+{
     struct tzplatform_context *tzCtxPtr = nullptr;
 
     RUNNER_ASSERT(0 == tzplatform_context_create(&tzCtxPtr));
     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
 
-    RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, user.getUid()),
-                      "Unable to set user <" << user.getUserName() << "> for tzplatform context");
+    RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, uid),
+                      "Unable to set user with uid <" << uid << "> for tzplatform context");
 
     const char *appDir = tzplatform_context_getenv(tzCtxPtr,
-                                getGlobalUserId() == user.getUid() ? TZ_SYS_RW_APP : TZ_USER_APP);
+                                getGlobalUserId() == uid ? TZ_SYS_RW_APP : TZ_USER_APP);
     RUNNER_ASSERT_MSG(nullptr != appDir,
                       "tzplatform_context_getenv failed"
-                          << "for getting sys rw app of user <" << user.getUserName() << ">");
+                          << "for getting sys rw app of user with uid<" << uid << ">");
 
-    return std::string(appDir) + "/" + pkgId + "/" + appId;
+    return std::string(appDir) + "/";
 }
 
 const std::string globalAppDir()
index 8217150..443fae3 100644 (file)
@@ -28,7 +28,9 @@ uid_t getGlobalGroupId(void);
 
 const std::string appDirPath(const TemporaryTestUser &user, const std::string &appId,
                              const std::string &pkgId);
+
 const std::string globalAppDir();
 
+const std::string appDirPath(uid_t uid);
 }