82175ce9257805a26cb5cff7f9c49c635a36d5a3
[platform/core/test/security-tests.git] / src / common / scoped_installer.h
1 /*
2  * Copyright (c) 2016-2019 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 #pragma once
18
19 #include <ftw.h>
20 #include <string>
21 #include <sys/capability.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <vector>
25
26 #include <security-manager-types.h>
27
28 #include <app_install_helper.h>
29 #include <memory.h>
30 #include <sm_request.h>
31 #include <sm_api.h>
32 #include <temp_test_user.h>
33 #include <synchronization_pipe.h>
34 #include <dpl/test/safe_cleanup.h>
35
36 class ScopedInstaller {
37 public:
38     ScopedInstaller(const AppInstallHelper &app, bool requestUid = true)
39         : m_appIds({app.getAppId()}),
40           m_uid(app.getUID()),
41           m_installType(app.getInstallType()),
42           m_shouldUninstall(true),
43           m_requestUid(requestUid),
44           m_creatorPid(getpid())
45     {
46         SecurityManagerTest::InstallRequest instReq;
47         instReq.setAppId(app.getAppId());
48         instReq.setPkgId(app.getPkgId());
49         if (app.getIsHybrid())
50             instReq.setHybrid();
51         if (app.getInstallType() != SM_APP_INSTALL_NONE)
52             instReq.setInstallType(app.getInstallType());
53         if (requestUid)
54             instReq.setUid(app.getUID());
55         if (!app.getVersion().empty())
56             instReq.setAppTizenVersion(app.getVersion());
57         if (!app.getAuthor().empty())
58             instReq.setAuthorId(app.getAuthor());
59         for (const auto& typePaths : app.getDirsMap())
60             for (const auto& path : typePaths.second)
61                 instReq.addPath(path, typePaths.first);
62         for (const auto& typePaths : app.getFilesMap())
63             for (const auto& path : typePaths.second)
64                 instReq.addPath(path, typePaths.first);
65         for (const auto &priv : app.getPrivileges())
66             instReq.addPrivilege(priv);
67         for (const auto &priv : app.getAppDefinedPrivileges())
68             instReq.addAppDefinedPrivilege(priv);
69
70         SecurityManagerTest::Api::install(instReq);
71     }
72
73     ScopedInstaller(const std::vector<std::string> &appIds, const std::string &pkgId)
74         : m_appIds(appIds),
75           m_uid(0),
76           m_installType(SM_APP_INSTALL_NONE),
77           m_shouldUninstall(true),
78           m_requestUid(false),
79           m_creatorPid(getpid())
80     {
81         SecurityManagerTest::InstallRequest instReq;
82
83         instReq.setPkgId(pkgId);
84         for (unsigned int i = 0; i < appIds.size(); i++) {
85             if (i > 0)
86                 instReq.nextApp();
87
88             instReq.setAppId(appIds[i]);
89         }
90
91         SecurityManagerTest::Api::install(instReq);
92     }
93
94     ScopedInstaller(const ScopedInstaller &) = delete;
95     ScopedInstaller(ScopedInstaller &&other)
96         : m_appIds(std::move(other.m_appIds)),
97           m_uid(other.m_uid),
98           m_installType(other.m_installType),
99           m_shouldUninstall(other.m_shouldUninstall),
100           m_requestUid(other.m_requestUid),
101           m_creatorPid(other.m_creatorPid)
102     {
103         other.m_uid = 0;
104         other.m_shouldUninstall = false;
105         other.m_creatorPid = -1;
106     }
107
108     ScopedInstaller& operator=(const ScopedInstaller &) = delete;
109
110     virtual ~ScopedInstaller() {
111         if (m_creatorPid == getpid())
112         {
113             SafeCleanup::run([this]{ uninstallApp(); });
114         }
115     }
116
117     void uninstallApp() {
118         if (!m_shouldUninstall)
119             return;
120         SecurityManagerTest::InstallRequest uninstReq;
121         for (unsigned int i = 0; i < m_appIds.size(); i++) {
122             if (i > 0)
123                 uninstReq.nextApp();
124
125             uninstReq.setAppId(m_appIds[i]);
126         }
127         if (m_requestUid)
128             uninstReq.setUid(m_uid);
129         if (m_installType != SM_APP_INSTALL_NONE)
130             uninstReq.setInstallType(m_installType);
131         SecurityManagerTest::Api::uninstall(uninstReq);
132         m_shouldUninstall = false;
133     }
134
135 protected:
136     std::vector<std::string> m_appIds;
137     uid_t m_uid;
138     app_install_type m_installType;
139     bool m_shouldUninstall;
140     bool m_requestUid;
141     pid_t m_creatorPid;
142 };