2 * Copyright (c) 2015 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()
83 TemporaryTestUser::GumdRunner TemporaryTestUser::m_runner = TemporaryTestUser::GumdRunner();
85 TemporaryTestUser::GumdRunner::GumdRunner()
88 int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
89 RUNNER_ASSERT_MSG(ret != -1, "socketpair() failed");
92 RUNNER_ASSERT_MSG(pid != -1, "fork() failed");
100 prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); // Don't outlive parent process
101 run(); // Never returns
105 TemporaryTestUser::GumdRunner::~GumdRunner()
110 bool TemporaryTestUser::GumdRunner::userCreate(std::string userName,
111 GumUserType userType, bool offline, uid_t &uid, gid_t &gid)
113 put(static_cast<int>(opType::USER_CREATE));
114 put(userName.size());
115 put(userName.c_str(), userName.size());
119 bool ret = get<bool>();
127 bool TemporaryTestUser::GumdRunner::userRemove(uid_t uid, bool offline)
129 put(static_cast<int>(opType::USER_REMOVE));
136 void TemporaryTestUser::GumdRunner::run()
138 DEFINE_SMARTPTR(g_object_unref, GumUser, GumUserPtr);
141 switch(static_cast<opType>(get<int>())) {
142 case opType::USER_CREATE:
144 size_t userNameLen = get<size_t>();
145 std::unique_ptr<char[]> userNamePtr(new char[userNameLen + 1]);
146 get(userNamePtr.get(), userNameLen);
147 userNamePtr[userNameLen] = '\0';
148 GumUserType userType = get<GumUserType>();
149 bool offline = get<bool>();
155 GumUserPtr userPtr(gum_user_create_sync(offline));
157 g_object_set(G_OBJECT(userPtr.get()),
158 "username", userNamePtr.get(),
159 "usertype", userType,
161 gboolean ret = gum_user_add_sync(userPtr.get());
163 g_object_get(G_OBJECT(userPtr.get()), "uid", &uid, nullptr);
164 g_object_get(G_OBJECT(userPtr.get()), "gid", &gid, nullptr);
177 case opType::USER_REMOVE:
179 uid_t uid = get<uid_t>();
180 bool offline = get<bool>();
183 GumUserPtr userPtr(gum_user_get_sync(uid, offline));
185 gboolean ret = gum_user_delete_sync(userPtr.get(), TRUE);
186 result = (ret == TRUE);
188 put(static_cast<bool>(result));
195 void TemporaryTestUser::GumdRunner::get(void *buf, size_t cnt)
198 for (size_t pos = 0; pos < cnt; pos += ret) {
199 ret = TEMP_FAILURE_RETRY(
200 read(m_sock, static_cast<char *>(buf) + pos, cnt - pos));
201 RUNNER_ASSERT_MSG(ret != -1, "read() failed");
205 void TemporaryTestUser::GumdRunner::put(const void *buf, size_t cnt)
208 for (size_t pos = 0; pos < cnt; pos += ret) {
209 ret = TEMP_FAILURE_RETRY(
210 write(m_sock, static_cast<const char *>(buf) + pos, cnt - pos));
211 RUNNER_ASSERT_MSG(ret != -1, "write() failed");
216 T TemporaryTestUser::GumdRunner::get()
224 void TemporaryTestUser::GumdRunner::put(const T x)