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