582ea1dae7816a6be70371273b7a1ac9dd85b172
[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     virtual ~ScopedInstaller() {
69         if (m_creatorPid == getpid())
70             uninstallApp();
71     }
72
73     void uninstallApp() {
74         if (!m_shouldUninstall)
75             return;
76         SecurityManagerTest::InstallRequest uninstReq;
77         uninstReq.setAppId(m_appInstallHelper.getAppId());
78         if (m_requestUid)
79             uninstReq.setUid(m_appInstallHelper.getUID());
80
81         SecurityManagerTest::Api::uninstall(uninstReq);
82         m_shouldUninstall = false;
83     }
84
85     AppInstallHelper& getAIH() {
86         return m_appInstallHelper;
87     }
88
89 protected:
90     AppInstallHelper m_appInstallHelper;
91     bool m_shouldUninstall;
92     bool m_requestUid;
93     pid_t m_creatorPid;
94 };