2 * Copyright (c) 2015-2016 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.
18 * @file temp_test_user.cpp
19 * @author Jan Cybulski (j.cybulski@partner.samsung.com)
20 * @author Rafal Krypa (j.cybulski@samsung.com)
22 * @brief File with class for users management
27 #include <tests_common.h>
28 #include <temp_test_user.h>
29 #include <glib-object.h>
30 #include <dpl/test/test_runner.h>
33 #include <sys/prctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
38 void TemporaryTestUser::create(void)
44 Below line of code is a hack for Gumd commit that removes Smack management capabilities:
46 | commit 9b45c1afa49103dcb4101f4b28bf7c145f3294a6
47 |/ Author: Yunmi Ha <yunmi.ha@samsung.com>
48 | Date: Tue Jul 5 13:40:16 2016 +0900
50 | Remove smack capability
52 | with wearable profile, CAP_MAC_ADMIN and CAP_MAC_OVERRIDE capabilities are removed.
53 | (can't use useradd/del/modify function without offline option.)
54 | with other profile, only CAP_MAC_OVERRIDE capability is removed.
56 | For this, gumd launcher was changed to systemd.
58 | Change-Id: Ic95fceed41afc41e37e93606c3abf830536ac7d6
59 | Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
63 bool ret = m_runner.userCreate(m_userName, m_userType, m_offline, m_uid, m_gid);
64 RUNNER_ASSERT_MSG(ret, "Failed to add user");
65 RUNNER_ASSERT_MSG(m_uid != 0, "Something strange happened during user creation. uid == 0.");
66 RUNNER_ASSERT_MSG(m_gid != 0, "Something strange happened during user creation. gid == 0.");
69 void TemporaryTestUser::remove(void)
74 m_runner.userRemove(m_uid, m_offline);
78 TemporaryTestUser::~TemporaryTestUser()
80 if (m_creatorPid == getpid())
84 TemporaryTestUser::GumdRunner TemporaryTestUser::m_runner = TemporaryTestUser::GumdRunner();
86 TemporaryTestUser::GumdRunner::GumdRunner()
89 int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
90 RUNNER_ASSERT_MSG(ret != -1, "socketpair() failed");
93 RUNNER_ASSERT_MSG(pid != -1, "fork() failed");
101 prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); // Don't outlive parent process
102 run(); // Never returns
106 TemporaryTestUser::GumdRunner::~GumdRunner()
111 bool TemporaryTestUser::GumdRunner::userCreate(std::string userName,
112 GumUserType userType, bool offline, uid_t &uid, gid_t &gid)
114 put(static_cast<int>(opType::USER_CREATE));
115 put(userName.size());
116 put(userName.c_str(), userName.size());
120 bool ret = get<bool>();
128 bool TemporaryTestUser::GumdRunner::userRemove(uid_t uid, bool offline)
130 put(static_cast<int>(opType::USER_REMOVE));
137 void TemporaryTestUser::GumdRunner::run()
139 DEFINE_SMARTPTR(g_object_unref, GumUser, GumUserPtr);
142 switch(static_cast<opType>(get<int>())) {
143 case opType::USER_CREATE:
145 size_t userNameLen = get<size_t>();
146 std::unique_ptr<char[]> userNamePtr(new char[userNameLen + 1]);
147 get(userNamePtr.get(), userNameLen);
148 userNamePtr[userNameLen] = '\0';
149 GumUserType userType = get<GumUserType>();
150 bool offline = get<bool>();
156 GumUserPtr userPtr(gum_user_create_sync(offline));
158 g_object_set(G_OBJECT(userPtr.get()),
159 "username", userNamePtr.get(),
160 "usertype", userType,
162 gboolean ret = gum_user_add_sync(userPtr.get());
164 g_object_get(G_OBJECT(userPtr.get()), "uid", &uid, nullptr);
165 g_object_get(G_OBJECT(userPtr.get()), "gid", &gid, nullptr);
178 case opType::USER_REMOVE:
180 uid_t uid = get<uid_t>();
181 bool offline = get<bool>();
184 GumUserPtr userPtr(gum_user_get_sync(uid, offline));
186 gboolean ret = gum_user_delete_sync(userPtr.get(), TRUE);
187 result = (ret == TRUE);
189 put(static_cast<bool>(result));
196 void TemporaryTestUser::GumdRunner::get(void *buf, size_t cnt)
199 for (size_t pos = 0; pos < cnt; pos += ret) {
200 ret = TEMP_FAILURE_RETRY(
201 read(m_sock, static_cast<char *>(buf) + pos, cnt - pos));
202 RUNNER_ASSERT_MSG(ret != -1, "read() failed");
206 void TemporaryTestUser::GumdRunner::put(const void *buf, size_t cnt)
209 for (size_t pos = 0; pos < cnt; pos += ret) {
210 ret = TEMP_FAILURE_RETRY(
211 write(m_sock, static_cast<const char *>(buf) + pos, cnt - pos));
212 RUNNER_ASSERT_MSG(ret != -1, "write() failed");
217 T TemporaryTestUser::GumdRunner::get()
225 void TemporaryTestUser::GumdRunner::put(const T x)