SM : Cleanup - privacy manager test cases
[platform/core/test/security-tests.git] / src / security-manager-tests / common / app_install_helper.h
1 /*
2  * Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #pragma once
17
18 #include <fcntl.h>
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <sys/types.h>
23 #include <vector>
24 #include <unistd.h>
25
26 #include <security-manager-types.h>
27
28 struct AppInstallHelper {
29
30     using TypePathsMap = std::map<app_install_path_type, std::vector<std::string>>;
31     AppInstallHelper(const std::string &appNamePrefix,
32                      const std::string &pkgNamePrefix,
33                      bool isLocal,
34                      uid_t uid,
35                      std::string version = std::string())
36       : m_appName(appNamePrefix), m_pkgName(pkgNamePrefix), m_isLocal(isLocal), m_uidGid(uid), m_version(version),
37         m_installType(SM_APP_INSTALL_NONE), m_isHybrid(false), m_creatorPid(getpid())
38     {
39         setInstallPath();
40     }
41
42     AppInstallHelper(const std::string &appNamePrefix,
43                      const std::string &pkgNamePrefix,
44                      uid_t uid)
45       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, uid)
46     {}
47
48     AppInstallHelper(const std::string &namePrefix)
49       : AppInstallHelper(namePrefix, namePrefix, false, geteuid())
50     {}
51
52     AppInstallHelper(const std::string &appNamePrefix, const std::string &pkgNamePrefix)
53       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, geteuid())
54     {}
55
56     AppInstallHelper(const std::string &namePrefix, uid_t uid)
57       : AppInstallHelper(namePrefix, namePrefix, true, uid)
58     {}
59
60     // TODO - copy constructor should be deleted
61     AppInstallHelper(const AppInstallHelper &other) = default;
62     AppInstallHelper(AppInstallHelper &&other);
63
64     // App info getters and setters
65     std::string getAppId() const;
66     std::string getPkgId() const;
67     int getUID() const;
68     int getGID() const;
69     void setVersion(const std::string &version);
70     std::string getVersion() const;
71     void setAuthor(const std::string &author);
72     std::string getAuthor() const;
73     void setInstallType(app_install_type type);
74     app_install_type getInstallType();
75     void setHybrid();
76     bool getIsHybrid() const;
77
78     // Path types creation and removal on file system
79     void createInstallDir();
80     void createTrustedDir(int i = 0);
81     void createPrivateDir();
82     void createPublicDir();
83     void createSharedFile(int i = 0);
84     void createSharedRODir();
85     void createPrivateRODir();
86     void removePaths();
87
88     // Path getters
89     std::string getInstallDir() const;
90     std::string getTrustedDir(int i = 0) const;
91     std::string getPrivateDir() const;
92     std::string getPrivateRODir() const;
93     std::string getPublicDir() const;
94     std::string getSharedPath(int i = 0) const;
95     std::string getSharedRODir() const;
96     const TypePathsMap& getDirsMap() const;
97     const TypePathsMap& getFilesMap() const;
98
99     // Privileges
100     void addPrivilege(const std::string &privilege);
101     void addPrivileges(const std::vector<std::string> &privileges);
102     std::vector<std::string> getPrivileges() const;
103
104     // Smack
105     std::string generateAppLabel() const;
106     std::string generatePkgLabel() const;
107     void revokeRules() const;
108     virtual ~AppInstallHelper() {
109         if (m_creatorPid == getpid())
110             removePaths();
111     }
112
113 protected:
114     void setInstallPath();
115     bool create(std::function<int(const char*, mode_t)> &&creatFun, const std::string &path);
116     std::string m_appName;
117     std::string m_pkgName;
118     bool m_isLocal;
119     int m_uidGid;
120     std::string m_version;
121     app_install_type m_installType;
122     bool m_isHybrid;
123     std::string m_installDir;
124     TypePathsMap m_dirTypeMap;
125     TypePathsMap m_fileTypeMap;
126     std::vector<std::string> m_privileges;
127     std::string m_author;
128
129     pid_t m_creatorPid;
130 };