[SM] Apply/drop sharing tests
[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 <unistd.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <sys/smack.h>
22
23 #include <string>
24
25
26 struct AppInstallHelper {
27     AppInstallHelper(const std::string &name)
28       : m_name(name)
29     {}
30
31     std::string getInstallDir() {
32         return "/usr/apps/" + getPkgId();
33     }
34
35     std::string getTrustedDir(int i = 0) {
36         return getInstallDir() + "/trustedDir" + std::to_string(i);
37     }
38
39     std::string getPrivateDir() {
40         return getInstallDir() + "/app_dir/";
41     }
42
43     std::string getSharedPath(int i = 0) {
44         return getPrivateDir() + "shareme" + std::to_string(i);
45     }
46     std::string getAppId() {
47         return m_name + "_app_id";
48     }
49
50     std::string getPkgId() {
51         return m_name + "_pkg_id";
52     }
53
54     void createInstallDir() {
55         if (mkdir(getInstallDir().c_str(), 0777) == 0) {
56             m_dirs.push_back(getInstallDir());
57         }
58     }
59
60     void createTrustedDir(int i = 0) {
61         if (mkdir(getTrustedDir(i).c_str(), 0777) == 0) {
62             m_dirs.push_back(getTrustedDir(i));
63         }
64     }
65     void createPrivateDir() {
66         if (mkdir(getPrivateDir().c_str(), 0777) == 0) {
67             m_dirs.push_back(getPrivateDir());
68         }
69     }
70
71     void createSharedFile(int i = 0) {
72         if (creat(getSharedPath(i).c_str(), 0777) == 0) {
73             m_files.push_back(getSharedPath(i));
74         }
75     }
76
77     void revokeRules() {
78         RUNNER_ASSERT_MSG(
79             0 == smack_revoke_subject(generateAppLabel().c_str()),
80             "Revoking smack subject failed");
81         RUNNER_ASSERT_MSG(
82             0 == smack_revoke_subject(generatePkgLabel().c_str()),
83             "Revoking smack subject failed");
84     }
85
86     std::string generateAppLabel() {
87         return "User::App::" + getAppId();
88     }
89
90     std::string generatePkgLabel() {
91         return "User::Pkg::" + getPkgId();
92     }
93
94     virtual ~AppInstallHelper() {
95         // TODO we should also remove trusted dirs created with custom params
96         for (const auto &dir : m_dirs) {
97             rmdir(dir.c_str());
98         }
99         for (const auto &file : m_files) {
100             unlink(file.c_str());
101         }
102     }
103
104 protected:
105     std::string m_name;
106     std::vector<std::string> m_dirs;
107     std::vector<std::string> m_files;
108 };
109