SM: Rename and use one label generators
[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 <unistd.h>
20 #include <string>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/smack.h>
24
25 #include <dpl/test/test_runner.h>
26 #include <sm_commons.h>
27 #include <tzplatform.h>
28
29 struct AppInstallHelper {
30     AppInstallHelper(const std::string &name)
31       : m_appName(name), m_pkgName(name)
32     {}
33
34     AppInstallHelper(const std::string &appName, const std::string &pkgName)
35       : m_appName(appName), m_pkgName(pkgName)
36     {}
37
38     std::string getInstallDir() const {
39         return  TzPlatformConfig::globalAppDir() + "/" + getPkgId();
40     }
41
42     std::string getTrustedDir(int i = 0) const {
43         return getInstallDir() + "/trustedDir" + std::to_string(i);
44     }
45
46     std::string getPrivateDir() const {
47         return getInstallDir() + "/app_dir/";
48     }
49
50     std::string getSharedPath(int i = 0) const {
51         return getPrivateDir() + "shareme" + std::to_string(i);
52     }
53     std::string getAppId() const {
54         return m_appName + "_app_id";
55     }
56
57     std::string getPkgId() const {
58         return m_pkgName + "_pkg_id";
59     }
60
61     void createInstallDir() {
62         if (mkdir(getInstallDir().c_str(), 0777) == 0) {
63             m_dirs.push_back(getInstallDir());
64         }
65     }
66
67     void createTrustedDir(int i = 0) {
68         if (mkdir(getTrustedDir(i).c_str(), 0777) == 0) {
69             m_dirs.push_back(getTrustedDir(i));
70         }
71     }
72     void createPrivateDir() {
73         if (mkdir(getPrivateDir().c_str(), 0777) == 0) {
74             m_dirs.push_back(getPrivateDir());
75         }
76     }
77
78     void createSharedFile(int i = 0) {
79         if (creat(getSharedPath(i).c_str(), 0777) == 0) {
80             m_files.push_back(getSharedPath(i));
81         }
82     }
83
84     void revokeRules() const {
85         RUNNER_ASSERT_MSG(
86             0 == smack_revoke_subject(generateAppLabel().c_str()),
87             "Revoking smack subject failed");
88         RUNNER_ASSERT_MSG(
89             0 == smack_revoke_subject(generatePkgLabel().c_str()),
90             "Revoking smack subject failed");
91     }
92
93     std::string generateAppLabel() const {
94         return generateProcessLabel(getAppId());
95     }
96
97     std::string generatePkgLabel() const {
98         return generatePathRWLabel(getPkgId());
99     }
100
101     void removePaths() {
102         for (const auto &dir : m_dirs) {
103             rmdir(dir.c_str());
104         }
105         m_dirs.clear();
106         for (const auto &file : m_files) {
107             unlink(file.c_str());
108         }
109         m_files.clear();
110     }
111
112     virtual ~AppInstallHelper() {
113         // TODO we should also remove trusted dirs created with custom params
114         removePaths();
115     }
116
117 protected:
118     std::string m_appName;
119     std::string m_pkgName;
120     std::vector<std::string> m_dirs;
121     std::vector<std::string> m_files;
122 };
123