SM : Cleanup - privacy manager test cases
[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_db.h>
31 #include <sm_request.h>
32 #include <sm_api.h>
33 #include <temp_test_user.h>
34 #include <synchronization_pipe.h>
35
36 class ScopedInstaller {
37 public:
38     ScopedInstaller(const AppInstallHelper &appInstallHelper, bool requestUid = true)
39         : m_appInstallHelper(appInstallHelper), m_shouldUninstall(true), m_requestUid(requestUid),
40           m_creatorPid(getpid())
41     {
42         SecurityManagerTest::InstallRequest instReq;
43
44         if (m_appInstallHelper.getIsHybrid())
45             instReq.setHybrid();
46         instReq.setAppId(m_appInstallHelper.getAppId());
47         instReq.setPkgId(m_appInstallHelper.getPkgId());
48         if (m_appInstallHelper.getInstallType() != SM_APP_INSTALL_NONE)
49             instReq.setInstallType(m_appInstallHelper.getInstallType());
50         if (requestUid)
51             instReq.setUid(m_appInstallHelper.getUID());
52         if (!m_appInstallHelper.getVersion().empty())
53             instReq.setAppTizenVersion(m_appInstallHelper.getVersion());
54         if (!m_appInstallHelper.getAuthor().empty())
55             instReq.setAuthorId(m_appInstallHelper.getAuthor());
56         for (const auto& typePaths : m_appInstallHelper.getDirsMap())
57             for (const auto& path : typePaths.second)
58                 instReq.addPath(path, typePaths.first);
59         for (const auto& typePaths : m_appInstallHelper.getFilesMap())
60             for (const auto& path : typePaths.second)
61                 instReq.addPath(path, typePaths.first);
62         for (const auto &priv : m_appInstallHelper.getPrivileges()) {
63             instReq.addPrivilege(priv.c_str());
64         }
65         SecurityManagerTest::Api::install(instReq);
66     }
67
68     ScopedInstaller(const ScopedInstaller &) = delete;
69     ScopedInstaller(ScopedInstaller &&other)
70         : m_appInstallHelper(std::move(other.m_appInstallHelper)),
71           m_shouldUninstall(other.m_shouldUninstall),
72           m_requestUid(other.m_requestUid),
73           m_creatorPid(other.m_creatorPid)
74     {
75         other.m_shouldUninstall = false;
76         other.m_creatorPid = -1;
77     }
78
79     ScopedInstaller& operator=(const ScopedInstaller &) = delete;
80
81     virtual ~ScopedInstaller() {
82         if (m_creatorPid == getpid())
83             uninstallApp();
84     }
85
86     void uninstallApp() {
87         if (!m_shouldUninstall)
88             return;
89         SecurityManagerTest::InstallRequest uninstReq;
90         uninstReq.setAppId(m_appInstallHelper.getAppId());
91         if (m_requestUid)
92             uninstReq.setUid(m_appInstallHelper.getUID());
93
94         SecurityManagerTest::Api::uninstall(uninstReq);
95         m_shouldUninstall = false;
96     }
97
98     AppInstallHelper& getAIH() {
99         return m_appInstallHelper;
100     }
101
102 protected:
103     AppInstallHelper m_appInstallHelper;
104     bool m_shouldUninstall;
105     bool m_requestUid;
106     pid_t m_creatorPid;
107 };