2 * Copyright (c) 2015-2019 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.
17 * @file test_cases_helpers.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests for cynara-helper-credentials-socket and cynara-helper-credentials-dbus
26 #include <sys/types.h>
31 #include <dbus/dbus.h>
32 #include <glib-object.h>
34 #include <systemd/sd-bus.h>
36 #include <tests_common.h>
37 #include <access_provider.h>
38 #include <dpl/test/test_runner.h>
40 #include <synchronization_pipe.h>
41 #include <tests_common.h>
43 #include <passwd_access.h>
44 #include <cynara_helpers_creds.h>
45 #include <cynara_helpers_dbus.h>
47 #include <cynara-creds-gdbus.h>
48 #include <cynara-creds-self.h>
49 #include <scoped_process_label.h>
51 class ProcessCredentials {
53 ProcessCredentials() {}
55 const std::string &label(void) const {
59 uid_t uid(void) const {
60 return PasswdAccess::uid(APP_USER);
63 gid_t gid(void) const {
64 return PasswdAccess::gid("users");
68 std::string m_label = "cynara_helpers";
71 cynara_client_creds getClientDefaultMethod() {
72 cynara_client_creds def;
73 int ret = cynara_creds_get_default_client_method(&def);
74 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
75 "cynara_creds_get_default_client_method failed with " << ret);
79 cynara_user_creds getUserDefaultMethod() {
80 cynara_user_creds def;
81 int ret = cynara_creds_get_default_user_method(&def);
82 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
83 "cynara_creds_get_default_user_method failed with " << ret);
88 void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
89 const struct ProcessCredentials &peerCredentials) {
90 SecurityServer::AccessProvider ap(peerCredentials.label());
91 ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
94 int sock = UDSHelpers::createServer(&sockaddr);
95 SockUniquePtr sockPtr(&sock);
97 int clientSock = UDSHelpers::acceptClient(sock);
99 UDSHelpers::waitForDisconnect(clientSock);
102 typedef std::function<void(int sock, pid_t pid,
103 const ProcessCredentials &peerCredentials)> SocketAssertionFn;
105 void socketTestTemplate(SocketAssertionFn assertion, const std::string &scope) {
106 const auto sockaddr = UDSHelpers::makeAbstractAddress("helper_" + scope + ".socket");
107 const ProcessCredentials peerCredentials;
109 SynchronizationPipe pipe;
111 pid_t pid = runInChild(std::bind(udsServer, std::ref(pipe), std::cref(sockaddr),
112 std::cref(peerCredentials)));
114 pipe.claimParentEp();
116 int sock = UDSHelpers::createClient(&sockaddr);
117 SockUniquePtr sockPtr(&sock);
119 assertion(sock, pid, peerCredentials);
122 RUNNER_TEST_GROUP_INIT(cynara_creds_socket)
124 void testSocketClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
125 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
126 CStringPtr label(CynaraHelperCredentials::socketGetClient(sock, method));
127 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
128 "Labels don't match ret = " << label.get()
129 << "; expected = " << peerCredentials.label());
133 void testSocketClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
134 socketTestTemplate([method] (int sock, pid_t pid, const ProcessCredentials &) {
135 CStringPtr clientPidStr(CynaraHelperCredentials::socketGetClient(sock, method));
136 pid_t clientPid = std::stoi(clientPidStr.get());
137 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
138 << "; expected = " << pid);
142 RUNNER_MULTIPROCESS_TEST_SMACK(tccs01_socket_credentials_client_smack)
144 testSocketClientSmack();
147 RUNNER_MULTIPROCESS_TEST(tccs02_socket_credentials_client_pid)
149 testSocketClientPid();
152 RUNNER_MULTIPROCESS_TEST_SMACK(tccs03_socket_credentials_client_default)
154 auto method = getClientDefaultMethod();
156 case CLIENT_METHOD_SMACK:
157 testSocketClientSmack(CLIENT_METHOD_DEFAULT);
159 case CLIENT_METHOD_PID:
160 testSocketClientPid(CLIENT_METHOD_DEFAULT);
163 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
168 void testSocketUserUid(cynara_user_creds method = USER_METHOD_UID) {
169 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
170 CStringPtr uidStr(CynaraHelperCredentials::socketGetUser(sock, method));
171 uid_t uid = std::stoul(uidStr.get());
172 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
173 << "; expected = "<< peerCredentials.uid());
177 void testSocketUserGid(cynara_user_creds method = USER_METHOD_GID) {
178 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
179 CStringPtr gidStr(CynaraHelperCredentials::socketGetUser(sock, method));
180 gid_t gid = std::stoul(gidStr.get());
181 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
182 << "; expected = "<< peerCredentials.gid());
186 RUNNER_MULTIPROCESS_TEST(tccs04_socket_credentials_user_uid)
191 RUNNER_MULTIPROCESS_TEST(tccs05_socket_credentials_user_gid)
196 RUNNER_MULTIPROCESS_TEST(tccs06_socket_credentials_user_default)
198 auto method = getUserDefaultMethod();
200 case USER_METHOD_UID:
201 testSocketUserUid(USER_METHOD_DEFAULT);
203 case USER_METHOD_GID:
204 testSocketUserGid(USER_METHOD_DEFAULT);
207 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
212 RUNNER_MULTIPROCESS_TEST(tccs07_socket_credentials_pid)
214 socketTestTemplate([] (int sock, pid_t expectedPid, const ProcessCredentials &) {
215 pid_t peerPid(CynaraHelperCredentials::socketGetPid(sock));
216 RUNNER_ASSERT_MSG(peerPid == expectedPid, "Pids don't match ret = " << peerPid
217 << "; expected = "<< expectedPid);
221 // TODO: Create utility namespace for DBus, maybe?
222 DBusConnectionPtr createDBusConnection(const std::string &name) {
225 dbus_error_init(&err);
226 DBusConnection *conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
227 RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != 1, "Error in dbus_bus_get: " << err.message);
228 dbus_connection_set_exit_on_disconnect(conn, FALSE);
230 DBusConnectionPtr ret(conn, [] (DBusConnection *conn) {
231 dbus_connection_close(conn);
232 dbus_connection_unref(conn);
235 if (name.empty() == false) {
236 dbus_bus_request_name(conn, name.c_str(), DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
237 RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != TRUE,
238 "Error in dbus_bus_request_name: " << err.message);
244 void dbusServer(SynchronizationPipe &pipe, const std::string &requestedName,
245 const ProcessCredentials &peerCredentials) {
246 // for DBus connection, System must have access to our peer creds as well.
247 SecurityServer::AccessProvider systemAp("System");
248 systemAp.addObjectRule(peerCredentials.label(), "rwx");
251 SecurityServer::AccessProvider ap(peerCredentials.label());
252 ap.addObjectRule("System", "w");
253 ap.addObjectRule("System::Run", "x");
254 ap.addObjectRule("System::Shared", "rwx"); // for GDB
255 ap.addSubjectRule("System::Privileged", "rwx"); // for piping
256 ap.addObjectRule("System::Privileged", "rwx"); // for GDB and piping
257 ap.addObjectRule("User", "r"); // for /usr/lib/debug access
258 ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
261 auto conn = createDBusConnection(requestedName);
266 typedef std::function<void(DBusConnectionPtr conn, pid_t pid,
267 const std::string &requestedName,
268 const ProcessCredentials &peerCredentials)> DBusAssertionFn;
270 void dbusTestTemplate(DBusAssertionFn assertion, const std::string &/*scope*/) {
271 std::string requestedName = "tests.dbus.cynara";
272 const ProcessCredentials peerCredentials;
274 SynchronizationPipe pipe;
275 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
276 std::cref(peerCredentials)));
278 pipe.claimParentEp();
281 auto conn = createDBusConnection("");
282 assertion(std::move(conn), pid, requestedName, peerCredentials);
286 RUNNER_TEST_GROUP_INIT(cynara_creds_dbus)
288 void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
289 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
290 const ProcessCredentials &) {
291 CStringPtr clientPidStr(CynaraHelperCredentials::dbusGetClient(std::move(conn),
292 requestedName.c_str(), method, CYNARA_API_SUCCESS));
293 pid_t clientPid = std::stoi(clientPidStr.get());
294 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
295 << "; expected = " << pid);
299 void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
300 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
301 const ProcessCredentials &peerCredentials) {
302 CStringPtr label(CynaraHelperCredentials::dbusGetClient(std::move(conn),
303 requestedName.c_str(), method, CYNARA_API_SUCCESS));
304 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
305 "Labels don't match ret = " << label.get()
306 << "; expected = " << peerCredentials.label());
310 RUNNER_MULTIPROCESS_TEST(tccd01_dbus_credentials_client_pid)
315 RUNNER_MULTIPROCESS_TEST_SMACK(tccd02_dbus_credentials_client_smack)
317 testDbusClientSmack();
320 RUNNER_MULTIPROCESS_TEST_SMACK(tccd03_dbus_credentials_client_default)
322 auto method = getClientDefaultMethod();
324 case CLIENT_METHOD_SMACK:
325 testDbusClientSmack(CLIENT_METHOD_DEFAULT);
327 case CLIENT_METHOD_PID:
328 testDbusClientPid(CLIENT_METHOD_DEFAULT);
331 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
336 void testDbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
337 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
338 const ProcessCredentials &peerCredentials) {
339 CStringPtr uidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
340 requestedName.c_str(), method, CYNARA_API_SUCCESS));
341 uid_t uid = std::stoul(uidStr.get());
342 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
343 << "; expected = "<< peerCredentials.uid());
347 void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
348 // Acquiring gid from dbus connection is not yet implemented for cynara helpers
349 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
350 const ProcessCredentials &) {
351 CStringPtr gidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
352 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
356 RUNNER_MULTIPROCESS_TEST(tccd04_dbus_credentials_user_uid)
361 RUNNER_MULTIPROCESS_TEST(tccd05_dbus_credentials_user_gid)
366 RUNNER_MULTIPROCESS_TEST(tccd06_dbus_credentials_user_default) {
367 auto method = getUserDefaultMethod();
369 case USER_METHOD_UID:
370 testDbusUserUid(USER_METHOD_DEFAULT);
372 case USER_METHOD_GID:
373 testDbusUserGid(USER_METHOD_DEFAULT);
376 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
381 RUNNER_TEST_SMACK(tccd06_dbus_credentials_pid) {
382 dbusTestTemplate([] (DBusConnectionPtr conn, pid_t expectedPid,
383 const std::string &requestedName, const ProcessCredentials &) {
384 auto helperPid = CynaraHelperCredentials::dbusGetPid(std::move(conn),
385 requestedName.c_str(), CYNARA_API_SUCCESS);
386 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
387 << "; expected = " << expectedPid);
391 GDBusConnectionPtr createGDBusConnection() {
392 GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
394 return GDBusConnectionPtr(conn, [] (GDBusConnection *conn) {
395 g_object_unref(G_OBJECT(conn));
400 typedef std::function<void(GDBusConnectionPtr conn, pid_t pid,
401 const std::string &requestedName,
402 const ProcessCredentials &peerCredentials)> GDBusAssertionFn;
404 void gdbusTestTemplate(GDBusAssertionFn assertion, const std::string &/*scope*/) {
405 std::string requestedName = "tests.dbus.cynara";
406 const ProcessCredentials peerCredentials;
408 SynchronizationPipe pipe;
409 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
410 std::cref(peerCredentials)));
412 pipe.claimParentEp();
415 auto conn = createGDBusConnection();
416 assertion(std::move(conn), pid, requestedName, peerCredentials);
421 RUNNER_TEST_GROUP_INIT(cynara_creds_gdbus)
423 void testGdbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
424 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t pid,
425 const std::string &requestedName,
426 const ProcessCredentials &) {
427 GStringPtr clientPidStr(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
428 requestedName.c_str(), method));
429 pid_t clientPid = std::stoi(clientPidStr.get());
430 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
431 << "; expected = " << pid);
435 void testGdbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
436 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
437 const std::string &requestedName,
438 const ProcessCredentials &peerCredentials) {
439 GStringPtr label(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
440 requestedName.c_str(), method));
441 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
442 "Labels don't match ret = " << label.get()
443 << "; expected = " << peerCredentials.label());
447 RUNNER_MULTIPROCESS_TEST(tccgd01_gdbus_credentials_client_pid)
449 testGdbusClientPid();
452 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd02_gdbus_credentials_client_smack)
454 testGdbusClientSmack();
457 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd03_gdbus_credentials_client_default)
459 auto method = getClientDefaultMethod();
461 case CLIENT_METHOD_SMACK:
462 testGdbusClientSmack(CLIENT_METHOD_DEFAULT);
464 case CLIENT_METHOD_PID:
465 testGdbusClientPid(CLIENT_METHOD_DEFAULT);
468 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
473 void testGdbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
474 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
475 const std::string &requestedName,
476 const ProcessCredentials &peerCredentials) {
477 GStringPtr uidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
478 requestedName.c_str(), method));
479 uid_t uid = std::stoul(uidStr.get());
480 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
481 << "; expected = "<< peerCredentials.uid());
485 void testGdbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
486 // Getting gid for gdbus connection is not yet implemented in cynara helpers
487 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
488 const std::string &requestedName,
489 const ProcessCredentials &) {
490 GStringPtr gidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
491 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
495 RUNNER_MULTIPROCESS_TEST(tccgd04_gdbus_credentials_user_uid)
500 RUNNER_MULTIPROCESS_TEST(tccgd05_gdbus_credentials_user_gid)
505 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_user_default) {
506 auto method = getUserDefaultMethod();
508 case USER_METHOD_UID:
509 testGdbusUserUid(USER_METHOD_DEFAULT);
511 case USER_METHOD_GID:
512 testGdbusUserGid(USER_METHOD_DEFAULT);
515 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
520 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_pid) {
521 gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t expectedPid,
522 const std::string &requestedName, const ProcessCredentials &) {
523 auto helperPid = CynaraHelperCredentials::gdbusGetPid(std::move(conn),
524 requestedName.c_str());
525 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
526 << "; expected = " << expectedPid);
531 SdBusConnectionPtr createSdBusConnection(const std::string &requestedName) {
534 int r = sd_bus_default_system(&bus);
535 RUNNER_ASSERT_MSG(r >= 0, "Failed to connect do system bus: %s" << strerror(-r));
537 if (requestedName.empty() == false) {
538 r = sd_bus_request_name(bus, requestedName.c_str(), SD_BUS_NAME_REPLACE_EXISTING);
539 RUNNER_ASSERT_MSG(r >= 0, "Error in sd_bus_request_name");
542 SdBusConnectionPtr ret(bus, [] (sd_bus *busConnection) {
543 sd_bus_unref(busConnection);
549 typedef std::function<void(SdBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
550 const ProcessCredentials &peerCredentials)> SdBusAssertionFn;
552 void sdBusTestTemplate(SdBusAssertionFn assertion) {
553 std::string requestedName = "tests.dbus.cynara";
554 const ProcessCredentials peerCredentials;
556 SynchronizationPipe pipe;
557 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
558 std::cref(peerCredentials)));
560 pipe.claimParentEp();
563 auto conn = createSdBusConnection("");
564 assertion(std::move(conn), pid, requestedName, peerCredentials);
569 RUNNER_TEST_GROUP_INIT(cynara_creds_sd_bus)
571 void testSdBusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
572 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t pid,
573 const std::string &requestedName,
574 const ProcessCredentials &) {
575 CStringPtr clientPidStr(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
576 requestedName.c_str(), method, CYNARA_API_SUCCESS));
577 pid_t clientPid = std::stoi(clientPidStr.get());
578 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
579 << "; expected = " << pid);
583 void testSdBusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
584 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
585 const std::string &requestedName,
586 const ProcessCredentials &peerCredentials) {
587 CStringPtr label(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
588 requestedName.c_str(), method, CYNARA_API_SUCCESS));
589 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
590 "Labels don't match ret = " << label.get()
591 << "; expected = " << peerCredentials.label());
595 RUNNER_MULTIPROCESS_TEST(tccsd01_sd_bus_credentials_client_pid)
597 testSdBusClientPid();
600 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd02_sd_bus_credentials_client_smack)
602 testSdBusClientSmack();
605 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd03_sd_bus_credentials_client_default)
607 auto method = getClientDefaultMethod();
609 case CLIENT_METHOD_SMACK:
610 testSdBusClientSmack(CLIENT_METHOD_DEFAULT);
612 case CLIENT_METHOD_PID:
613 testSdBusClientPid(CLIENT_METHOD_DEFAULT);
616 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
621 void testSdBusUserUid(cynara_user_creds method = USER_METHOD_UID) {
622 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
623 const std::string &requestedName,
624 const ProcessCredentials &peerCredentials) {
625 CStringPtr uidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
626 requestedName.c_str(), method, CYNARA_API_SUCCESS));
627 uid_t uid = std::stoul(uidStr.get());
628 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
629 << "; expected = "<< peerCredentials.uid());
633 void testSdBusUserGid(cynara_user_creds method = USER_METHOD_GID) {
634 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
635 const std::string &requestedName,
636 const ProcessCredentials &peerCredentials) {
637 CStringPtr gidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
638 requestedName.c_str(), method, CYNARA_API_SUCCESS));
639 gid_t gid = std::stoul(gidStr.get());
640 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
641 << "; expected = "<< peerCredentials.gid());
645 RUNNER_MULTIPROCESS_TEST(tccsd04_sd_bus_credentials_user_uid)
650 RUNNER_MULTIPROCESS_TEST(tccsd05_sd_bus_credentials_user_gid)
655 RUNNER_MULTIPROCESS_TEST(tccsd06_sd_bus_credentials_user_default)
657 auto method = getUserDefaultMethod();
659 case USER_METHOD_UID:
660 testSdBusUserUid(USER_METHOD_DEFAULT);
662 case USER_METHOD_GID:
663 testSdBusUserGid(USER_METHOD_DEFAULT);
666 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
671 RUNNER_TEST_SMACK(tccd07_sd_bus_credentials_pid) {
672 sdBusTestTemplate([] (SdBusConnectionPtr conn, pid_t expectedPid,
673 const std::string &requestedName, const ProcessCredentials &) {
674 auto helperPid = CynaraHelperCredentials::sdBusGetPid(std::move(conn),
675 requestedName.c_str(), CYNARA_API_SUCCESS);
676 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
677 << "; expected = " << expectedPid);
682 RUNNER_TEST_GROUP_INIT(cynara_creds_self)
684 void testCredsClientSelf(cynara_client_creds method, const std::string &expected) {
686 int ret = cynara_creds_self_get_client(method, &client);
687 CStringPtr clientPtr(client);
688 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_client failed with " << ret);
689 RUNNER_ASSERT_MSG(expected == client, "expected client doesn't match, expected: " << expected
690 << ", got : " << client);
693 void testCredsUserSelf(cynara_user_creds method, const std::string &expected) {
695 int ret = cynara_creds_self_get_user(method, &user);
696 CStringPtr clientPtr(user);
697 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_user failed with " << ret);
698 RUNNER_ASSERT_MSG(expected == user, "expected user doesn't match, expected: " << expected
699 << ", got : " << user);
702 void testSelfClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
703 std::string label = "test-label";
704 ScopedProcessLabel spl(label, false);
705 testCredsClientSelf(method, label);
708 void testSelfClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
709 pid_t pid = getpid();
710 testCredsClientSelf(method, std::to_string(pid));
713 void testSelfUserUid(cynara_user_creds method = USER_METHOD_UID) {
714 uid_t uid = getuid();
715 testCredsUserSelf(method, std::to_string(uid));
718 void testSelfUserGid(cynara_user_creds method = USER_METHOD_GID) {
719 gid_t gid = getgid();
720 testCredsUserSelf(method, std::to_string(gid));
723 RUNNER_CHILD_TEST_SMACK(tccsl01_self_credentials_client_smack) {
724 testSelfClientSmack();
727 RUNNER_CHILD_TEST_SMACK(tccsl02_self_credentials_client_pid) {
731 RUNNER_CHILD_TEST_SMACK(tccsl03_self_credentials_user_uid) {
735 RUNNER_CHILD_TEST_SMACK(tccsl04_self_credentials_user_gid) {
739 RUNNER_CHILD_TEST_SMACK(tccsl05_self_credentials_client_default) {
740 auto method = getClientDefaultMethod();
742 case CLIENT_METHOD_SMACK:
743 testSelfClientSmack(CLIENT_METHOD_DEFAULT);
745 case CLIENT_METHOD_PID:
746 testSelfClientPid(CLIENT_METHOD_DEFAULT);
749 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value " << method);
753 RUNNER_CHILD_TEST_SMACK(tccsl06_self_credentials_user_default) {
754 auto method = getUserDefaultMethod();
756 case USER_METHOD_UID:
757 testSelfUserUid(USER_METHOD_DEFAULT);
759 case USER_METHOD_GID:
760 testSelfUserGid(USER_METHOD_DEFAULT);
763 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value " << method);