SM : Don't add version by default in app install
[platform/core/test/security-tests.git] / src / security-manager-tests / common / app_install_helper.cpp
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
17 #include <fcntl.h>
18 #include <map>
19 #include <string>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/smack.h>
23 #include <unistd.h>
24 #include <utility>
25
26 #include <security-manager-types.h>
27
28 #include <dpl/test/test_runner.h>
29 #include <sm_commons.h>
30 #include <tzplatform.h>
31
32 #include "app_install_helper.h"
33
34 std::string AppInstallHelper::getInstallDir() const {
35     return m_installDir + getPkgId();
36 }
37
38 std::string AppInstallHelper::getTrustedDir(int i) const {
39     return getInstallDir() + "/trustedDir" + std::to_string(i);
40 }
41
42 std::string AppInstallHelper::getPrivateDir() const {
43     return getInstallDir() + "/app_dir/";
44 }
45
46 std::string AppInstallHelper::getPrivateRODir() const {
47     return getInstallDir() + "/app_dir_ro/";
48 }
49
50 std::string AppInstallHelper::getPublicDir() const {
51     return getInstallDir() + "/app_public_ro/";
52 }
53
54 std::string AppInstallHelper::getSharedPath(int i) const {
55     return getPrivateDir() + "shareme" + std::to_string(i);
56 }
57
58 std::string AppInstallHelper::getSharedRODir() const {
59     return getInstallDir() + "/app_dir_rw_others_ro/";
60 }
61
62 std::string AppInstallHelper::getAppId() const {
63     return m_appName + "_app_id";
64 }
65
66 std::string AppInstallHelper::getPkgId() const {
67     return m_pkgName + "_pkg_id";
68 }
69
70 void AppInstallHelper::setVersion(const std::string &version) {
71     m_version = version;
72 }
73
74 std::string AppInstallHelper::getVersion() const {
75     return m_version;
76 }
77
78 int AppInstallHelper::getUID() const {
79     return m_uidGid;
80 }
81
82 int AppInstallHelper::getGID() const {
83     return m_uidGid;
84 }
85
86 void AppInstallHelper::createInstallDir() {
87     create(mkdir, getInstallDir());
88 }
89
90 void AppInstallHelper::createTrustedDir(int i) {
91     if (create(mkdir, getTrustedDir(i)))
92         m_dirTypeMap[SECURITY_MANAGER_PATH_TRUSTED_RW].emplace_back(getTrustedDir(i));
93 }
94
95 void AppInstallHelper::createPrivateDir() {
96     if (create(mkdir, getPrivateDir()))
97         m_dirTypeMap[SECURITY_MANAGER_PATH_RW].emplace_back(getPrivateDir());
98 }
99
100 void AppInstallHelper::createPublicDir() {
101     if (mkdir(getPublicDir().c_str(), 0777) == 0) {
102         m_dirTypeMap[SECURITY_MANAGER_PATH_PUBLIC_RO].emplace_back(getPublicDir());
103     }
104 }
105
106 void AppInstallHelper::createSharedFile(int i) {
107     if (create(creat, getSharedPath(i)))
108         m_fileTypeMap[SECURITY_MANAGER_PATH_RW].emplace_back(getSharedPath(i));
109 }
110
111 void AppInstallHelper::createSharedRODir() {
112     if (create(mkdir, getSharedRODir()))
113         m_dirTypeMap[SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO].emplace_back(getSharedRODir());
114 }
115
116 void AppInstallHelper::createPrivateRODir() {
117     if (create(mkdir, getPrivateRODir()))
118         m_dirTypeMap[SECURITY_MANAGER_PATH_RO].emplace_back(getPrivateRODir());
119 }
120
121 bool AppInstallHelper::getIsHybrid() const {
122     return m_isHybrid;
123 }
124
125 void AppInstallHelper::addPrivilege(const std::string &privilege) {
126     m_privileges.push_back(privilege);
127 }
128
129 void AppInstallHelper::addPrivileges(const std::vector<std::string> &privileges) {
130     std::copy(privileges.begin(), privileges.end(), std::back_inserter(m_privileges));
131 }
132
133 std::vector<std::string> AppInstallHelper::getPrivileges() const {
134     return m_privileges;
135 }
136
137 void AppInstallHelper::revokeRules() const {
138     RUNNER_ASSERT_MSG(
139         0 == smack_revoke_subject(generateAppLabel().c_str()),
140         "Revoking smack subject failed");
141 }
142
143 std::string AppInstallHelper::generateAppLabel() const {
144     return generateProcessLabel(getAppId(), getPkgId(), getIsHybrid());
145 }
146
147 std::string AppInstallHelper::generatePkgLabel() const {
148     return generatePathRWLabel(getPkgId());
149 }
150
151 const AppInstallHelper::TypePathsMap& AppInstallHelper::getDirsMap() const {
152     return m_dirTypeMap;
153 }
154
155 const AppInstallHelper::TypePathsMap& AppInstallHelper::getFilesMap() const {
156     return m_fileTypeMap;
157 }
158
159 void AppInstallHelper::removePaths() {
160     // FIXME - remove special treatment for shared ro
161     for (const auto &oneTypePaths : m_dirTypeMap)
162         if (oneTypePaths.first != SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO)
163             for (const auto& path : oneTypePaths.second)
164                 rmdir(path.c_str());
165
166     m_dirTypeMap.clear();
167
168     for (const auto &oneTypePaths : m_fileTypeMap)
169         if (oneTypePaths.first != SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO)
170             for (const auto& path : oneTypePaths.second)
171                 unlink(path.c_str());
172
173     m_fileTypeMap.clear();
174
175     rmdir(m_installDir.c_str());
176 }
177
178 void AppInstallHelper::setInstallPath() {
179     if (m_isLocal)
180         m_installDir = TzPlatformConfig::appDirPath(getUID());
181     else
182         m_installDir = TzPlatformConfig::globalAppDir() + "/";
183 }
184
185 bool AppInstallHelper::create(std::function<int(const char*, mode_t)> &&creatFun, const std::string &path) {
186     if (creatFun(path.c_str(), 0751) == 0) {
187        // Local paths need user change
188        if (!m_isLocal || chown(path.c_str(), m_uidGid, m_uidGid) == 0)
189                 return true;
190        }
191     return false;
192 }
193
194 void AppInstallHelper::setAuthor(const std::string &author) {
195     m_author = author;
196 }
197 std::string AppInstallHelper::getAuthor() const {
198     return m_author;
199 }
200
201 void AppInstallHelper::setInstallType(app_install_type type) {
202     m_installType = type;
203 }
204 app_install_type AppInstallHelper::getInstallType() {
205     return m_installType;
206 }
207