2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/capability.h>
23 #include <sys/types.h>
26 #include <security-manager-types.h>
28 #include <app_install_helper.h>
30 #include <sm_request.h>
32 #include <temp_test_user.h>
33 #include <synchronization_pipe.h>
34 #include <dpl/test/safe_cleanup.h>
36 class ScopedInstaller {
38 ScopedInstaller(const AppInstallHelper &app, bool requestUid = true)
39 : m_appId(app.getAppId()),
41 m_installType(app.getInstallType()),
42 m_shouldUninstall(true),
43 m_requestUid(requestUid),
44 m_creatorPid(getpid())
46 SecurityManagerTest::InstallRequest instReq;
47 instReq.setAppId(app.getAppId());
48 instReq.setPkgId(app.getPkgId());
49 if (app.getIsHybrid())
51 if (app.getInstallType() != SM_APP_INSTALL_NONE)
52 instReq.setInstallType(app.getInstallType());
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);
70 SecurityManagerTest::Api::install(instReq);
73 ScopedInstaller(const ScopedInstaller &) = delete;
74 ScopedInstaller(ScopedInstaller &&other)
75 : m_appId(std::move(other.m_appId)),
77 m_installType(other.m_installType),
78 m_shouldUninstall(other.m_shouldUninstall),
79 m_requestUid(other.m_requestUid),
80 m_creatorPid(other.m_creatorPid)
83 other.m_shouldUninstall = false;
84 other.m_creatorPid = -1;
87 ScopedInstaller& operator=(const ScopedInstaller &) = delete;
89 virtual ~ScopedInstaller() {
90 if (m_creatorPid == getpid())
92 SafeCleanup::run([this]{ uninstallApp(); });
97 if (!m_shouldUninstall)
99 SecurityManagerTest::InstallRequest uninstReq;
100 uninstReq.setAppId(m_appId);
102 uninstReq.setUid(m_uid);
103 if (m_installType != SM_APP_INSTALL_NONE)
104 uninstReq.setInstallType(m_installType);
105 SecurityManagerTest::Api::uninstall(uninstReq);
106 m_shouldUninstall = false;
112 app_install_type m_installType;
113 bool m_shouldUninstall;