Merge branch 'tizen' into security-manager
[platform/core/test/security-tests.git] / src / security-manager-tests / common / scoped_installer.h
1 /*
2  * Copyright (c) 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
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
35 class ScopedInstaller {
36 public:
37     ScopedInstaller(const AppInstallHelper &app, bool requestUid = true)
38         : m_appId(app.getAppId()),
39           m_uid(app.getUID()),
40           m_shouldUninstall(true),
41           m_requestUid(requestUid),
42           m_creatorPid(getpid())
43     {
44         SecurityManagerTest::InstallRequest instReq;
45         instReq.setAppId(app.getAppId());
46         instReq.setPkgId(app.getPkgId());
47         if (app.getIsHybrid())
48             instReq.setHybrid();
49         if (app.getInstallType() != SM_APP_INSTALL_NONE)
50             instReq.setInstallType(app.getInstallType());
51         if (requestUid)
52             instReq.setUid(app.getUID());
53         if (!app.getVersion().empty())
54             instReq.setAppTizenVersion(app.getVersion());
55         if (!app.getAuthor().empty())
56             instReq.setAuthorId(app.getAuthor());
57         for (const auto& typePaths : app.getDirsMap())
58             for (const auto& path : typePaths.second)
59                 instReq.addPath(path, typePaths.first);
60         for (const auto& typePaths : app.getFilesMap())
61             for (const auto& path : typePaths.second)
62                 instReq.addPath(path, typePaths.first);
63         for (const auto &priv : app.getPrivileges()) {
64             instReq.addPrivilege(priv.c_str());
65         }
66         SecurityManagerTest::Api::install(instReq);
67     }
68
69     ScopedInstaller(const ScopedInstaller &) = delete;
70     ScopedInstaller(ScopedInstaller &&other)
71         : m_appId(std::move(other.m_appId)),
72           m_uid(other.m_uid),
73           m_shouldUninstall(other.m_shouldUninstall),
74           m_requestUid(other.m_requestUid),
75           m_creatorPid(other.m_creatorPid)
76     {
77         other.m_uid = 0;
78         other.m_shouldUninstall = false;
79         other.m_creatorPid = -1;
80     }
81
82     ScopedInstaller& operator=(const ScopedInstaller &) = delete;
83
84     virtual ~ScopedInstaller() {
85         if (m_creatorPid == getpid())
86             uninstallApp();
87     }
88
89     void uninstallApp() {
90         if (!m_shouldUninstall)
91             return;
92         SecurityManagerTest::InstallRequest uninstReq;
93         uninstReq.setAppId(m_appId);
94         if (m_requestUid)
95             uninstReq.setUid(m_uid);
96
97         SecurityManagerTest::Api::uninstall(uninstReq);
98         m_shouldUninstall = false;
99     }
100
101 protected:
102     std::string m_appId;
103     uid_t m_uid;
104     bool m_shouldUninstall;
105     bool m_requestUid;
106     pid_t m_creatorPid;
107 };