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