2 * Copyright (c) 2016-2020 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,
39 bool requestUid = true,
40 lib_retcode expectedResult = SECURITY_MANAGER_SUCCESS)
41 : m_appIds({app.getAppId()}),
43 m_installType(app.getInstallType()),
44 m_shouldUninstall(expectedResult == SECURITY_MANAGER_SUCCESS),
45 m_requestUid(requestUid),
46 m_creatorPid(getpid())
48 SecurityManagerTest::InstallRequest instReq;
49 instReq.setAppId(app.getAppId());
50 instReq.setPkgId(app.getPkgId());
51 if (app.getIsHybrid())
53 if (app.getInstallType() != SM_APP_INSTALL_NONE)
54 instReq.setInstallType(app.getInstallType());
56 instReq.setUid(app.getUID());
57 if (!app.getVersion().empty())
58 instReq.setAppTizenVersion(app.getVersion());
59 if (!app.getAuthor().empty())
60 instReq.setAuthorId(app.getAuthor());
61 for (const auto& typePaths : app.getDirsMap())
62 for (const auto& path : typePaths.second)
63 instReq.addPath(path, typePaths.first);
64 for (const auto& typePaths : app.getFilesMap())
65 for (const auto& path : typePaths.second)
66 instReq.addPath(path, typePaths.first);
67 for (const auto &priv : app.getPrivileges())
68 instReq.addPrivilege(priv);
69 for (const auto &priv : app.getAppDefinedPrivileges())
70 instReq.addAppDefinedPrivilege(priv);
72 SecurityManagerTest::Api::install(instReq, expectedResult);
75 ScopedInstaller(const std::vector<std::string> &appIds, const std::string &pkgId)
78 m_installType(SM_APP_INSTALL_NONE),
79 m_shouldUninstall(true),
81 m_creatorPid(getpid())
83 SecurityManagerTest::InstallRequest instReq;
85 instReq.setPkgId(pkgId);
86 for (unsigned int i = 0; i < appIds.size(); i++) {
90 instReq.setAppId(appIds[i]);
93 SecurityManagerTest::Api::install(instReq);
96 ScopedInstaller(const ScopedInstaller &) = delete;
97 ScopedInstaller(ScopedInstaller &&other)
98 : m_appIds(std::move(other.m_appIds)),
100 m_installType(other.m_installType),
101 m_shouldUninstall(other.m_shouldUninstall),
102 m_requestUid(other.m_requestUid),
103 m_creatorPid(other.m_creatorPid)
106 other.m_shouldUninstall = false;
107 other.m_creatorPid = -1;
110 ScopedInstaller& operator=(const ScopedInstaller &) = delete;
112 virtual ~ScopedInstaller() {
113 if (m_creatorPid == getpid())
115 SafeCleanup::run([this]{ uninstallApp(); });
119 void uninstallApp() {
120 if (!m_shouldUninstall)
122 SecurityManagerTest::InstallRequest uninstReq;
123 for (unsigned int i = 0; i < m_appIds.size(); i++) {
127 uninstReq.setAppId(m_appIds[i]);
130 uninstReq.setUid(m_uid);
131 if (m_installType != SM_APP_INSTALL_NONE)
132 uninstReq.setInstallType(m_installType);
133 SecurityManagerTest::Api::uninstall(uninstReq);
134 m_shouldUninstall = false;
138 std::vector<std::string> m_appIds;
140 app_install_type m_installType;
141 bool m_shouldUninstall;