SM : AppInstallHelper, ScopedInstaller, TempTestuser fork safe
[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_creatorPid(getpid())
40     {
41         SecurityManagerTest::InstallRequest instReq;
42
43         if (m_appInstallHelper.getIsHybrid())
44             instReq.setHybrid();
45         instReq.setAppId(m_appInstallHelper.getAppId());
46         instReq.setPkgId(m_appInstallHelper.getPkgId());
47         if (m_appInstallHelper.getInstallType() != SM_APP_INSTALL_NONE)
48             instReq.setInstallType(m_appInstallHelper.getInstallType());
49         if (requestUid)
50             instReq.setUid(m_appInstallHelper.getUID());
51         if (!m_appInstallHelper.getVersion().empty())
52             instReq.setAppTizenVersion(m_appInstallHelper.getVersion());
53         if (!m_appInstallHelper.getAuthor().empty())
54             instReq.setAuthorId(m_appInstallHelper.getAuthor());
55         for (const auto& typePaths : m_appInstallHelper.getDirsMap())
56             for (const auto& path : typePaths.second)
57                 instReq.addPath(path, typePaths.first);
58         for (const auto& typePaths : m_appInstallHelper.getFilesMap())
59             for (const auto& path : typePaths.second)
60                 instReq.addPath(path, typePaths.first);
61         for (const auto &priv : m_appInstallHelper.getPrivileges()) {
62             instReq.addPrivilege(priv.c_str());
63         }
64         SecurityManagerTest::Api::install(instReq);
65     }
66
67     virtual ~ScopedInstaller() {
68         if (m_creatorPid == getpid())
69             uninstallApp();
70     }
71
72     void uninstallApp() {
73         if (!m_shouldUninstall)
74             return;
75         SecurityManagerTest::InstallRequest uninstReq;
76         uninstReq.setAppId(m_appInstallHelper.getAppId());
77         uninstReq.setUid(m_appInstallHelper.getUID());
78
79         SecurityManagerTest::Api::uninstall(uninstReq);
80         m_shouldUninstall = false;
81     }
82
83     AppInstallHelper& getAIH() {
84         return m_appInstallHelper;
85     }
86
87 protected:
88     AppInstallHelper m_appInstallHelper;
89     bool m_shouldUninstall;
90     pid_t m_creatorPid;
91 };