2 * Copyright (c) 2015-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.
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 <app_context.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 AppContext ctx(peerCredentials.label());
91 ctx.apply(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 AppContext ctx(peerCredentials.label());
247 ctx.allowAccessFrom("System", "rwx"); // for DBus connection
248 ctx.allowAccessFrom("System::Privileged", "rwx"); // for piping
249 ctx.allowAccessTo("System", "w");
250 ctx.allowAccessTo("System::Privileged", "rwx"); // for GDB and piping
251 ctx.allowAccessTo("System::Run", "x");
252 ctx.allowAccessTo("System::Shared", "rwx"); // for GDB
253 ctx.allowAccessTo("User", "r"); // for /usr/lib/debug access
254 ctx.apply(peerCredentials.uid(), peerCredentials.gid());
257 auto conn = createDBusConnection(requestedName);
262 typedef std::function<void(DBusConnectionPtr conn, pid_t pid,
263 const std::string &requestedName,
264 const ProcessCredentials &peerCredentials)> DBusAssertionFn;
266 void dbusTestTemplate(DBusAssertionFn assertion, const std::string &/*scope*/) {
267 std::string requestedName = "tests.dbus.cynara";
268 const ProcessCredentials peerCredentials;
270 SynchronizationPipe pipe;
271 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
272 std::cref(peerCredentials)));
274 pipe.claimParentEp();
277 auto conn = createDBusConnection("");
278 assertion(std::move(conn), pid, requestedName, peerCredentials);
282 RUNNER_TEST_GROUP_INIT(cynara_creds_dbus)
284 void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
285 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
286 const ProcessCredentials &) {
287 CStringPtr clientPidStr(CynaraHelperCredentials::dbusGetClient(std::move(conn),
288 requestedName.c_str(), method, CYNARA_API_SUCCESS));
289 pid_t clientPid = std::stoi(clientPidStr.get());
290 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
291 << "; expected = " << pid);
295 void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
296 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
297 const ProcessCredentials &peerCredentials) {
298 CStringPtr label(CynaraHelperCredentials::dbusGetClient(std::move(conn),
299 requestedName.c_str(), method, CYNARA_API_SUCCESS));
300 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
301 "Labels don't match ret = " << label.get()
302 << "; expected = " << peerCredentials.label());
306 RUNNER_MULTIPROCESS_TEST(tccd01_dbus_credentials_client_pid)
311 RUNNER_MULTIPROCESS_TEST_SMACK(tccd02_dbus_credentials_client_smack)
313 testDbusClientSmack();
316 RUNNER_MULTIPROCESS_TEST_SMACK(tccd03_dbus_credentials_client_default)
318 auto method = getClientDefaultMethod();
320 case CLIENT_METHOD_SMACK:
321 testDbusClientSmack(CLIENT_METHOD_DEFAULT);
323 case CLIENT_METHOD_PID:
324 testDbusClientPid(CLIENT_METHOD_DEFAULT);
327 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
332 void testDbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
333 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
334 const ProcessCredentials &peerCredentials) {
335 CStringPtr uidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
336 requestedName.c_str(), method, CYNARA_API_SUCCESS));
337 uid_t uid = std::stoul(uidStr.get());
338 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
339 << "; expected = "<< peerCredentials.uid());
343 void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
344 // Acquiring gid from dbus connection is not yet implemented for cynara helpers
345 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
346 const ProcessCredentials &) {
347 CStringPtr gidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
348 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
352 RUNNER_MULTIPROCESS_TEST(tccd04_dbus_credentials_user_uid)
357 RUNNER_MULTIPROCESS_TEST(tccd05_dbus_credentials_user_gid)
362 RUNNER_MULTIPROCESS_TEST(tccd06_dbus_credentials_user_default) {
363 auto method = getUserDefaultMethod();
365 case USER_METHOD_UID:
366 testDbusUserUid(USER_METHOD_DEFAULT);
368 case USER_METHOD_GID:
369 testDbusUserGid(USER_METHOD_DEFAULT);
372 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
377 RUNNER_TEST_SMACK(tccd06_dbus_credentials_pid) {
378 dbusTestTemplate([] (DBusConnectionPtr conn, pid_t expectedPid,
379 const std::string &requestedName, const ProcessCredentials &) {
380 auto helperPid = CynaraHelperCredentials::dbusGetPid(std::move(conn),
381 requestedName.c_str(), CYNARA_API_SUCCESS);
382 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
383 << "; expected = " << expectedPid);
387 GDBusConnectionPtr createGDBusConnection() {
388 GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
390 return GDBusConnectionPtr(conn, [] (GDBusConnection *conn) {
391 g_object_unref(G_OBJECT(conn));
396 typedef std::function<void(GDBusConnectionPtr conn, pid_t pid,
397 const std::string &requestedName,
398 const ProcessCredentials &peerCredentials)> GDBusAssertionFn;
400 void gdbusTestTemplate(GDBusAssertionFn assertion, const std::string &/*scope*/) {
401 std::string requestedName = "tests.dbus.cynara";
402 const ProcessCredentials peerCredentials;
404 SynchronizationPipe pipe;
405 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
406 std::cref(peerCredentials)));
408 pipe.claimParentEp();
411 auto conn = createGDBusConnection();
412 assertion(std::move(conn), pid, requestedName, peerCredentials);
417 RUNNER_TEST_GROUP_INIT(cynara_creds_gdbus)
419 void testGdbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
420 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t pid,
421 const std::string &requestedName,
422 const ProcessCredentials &) {
423 GStringPtr clientPidStr(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
424 requestedName.c_str(), method));
425 pid_t clientPid = std::stoi(clientPidStr.get());
426 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
427 << "; expected = " << pid);
431 void testGdbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
432 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
433 const std::string &requestedName,
434 const ProcessCredentials &peerCredentials) {
435 GStringPtr label(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
436 requestedName.c_str(), method));
437 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
438 "Labels don't match ret = " << label.get()
439 << "; expected = " << peerCredentials.label());
443 RUNNER_MULTIPROCESS_TEST(tccgd01_gdbus_credentials_client_pid)
445 testGdbusClientPid();
448 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd02_gdbus_credentials_client_smack)
450 testGdbusClientSmack();
453 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd03_gdbus_credentials_client_default)
455 auto method = getClientDefaultMethod();
457 case CLIENT_METHOD_SMACK:
458 testGdbusClientSmack(CLIENT_METHOD_DEFAULT);
460 case CLIENT_METHOD_PID:
461 testGdbusClientPid(CLIENT_METHOD_DEFAULT);
464 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
469 void testGdbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
470 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
471 const std::string &requestedName,
472 const ProcessCredentials &peerCredentials) {
473 GStringPtr uidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
474 requestedName.c_str(), method));
475 uid_t uid = std::stoul(uidStr.get());
476 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
477 << "; expected = "<< peerCredentials.uid());
481 void testGdbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
482 // Getting gid for gdbus connection is not yet implemented in cynara helpers
483 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
484 const std::string &requestedName,
485 const ProcessCredentials &) {
486 GStringPtr gidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
487 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
491 RUNNER_MULTIPROCESS_TEST(tccgd04_gdbus_credentials_user_uid)
496 RUNNER_MULTIPROCESS_TEST(tccgd05_gdbus_credentials_user_gid)
501 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_user_default) {
502 auto method = getUserDefaultMethod();
504 case USER_METHOD_UID:
505 testGdbusUserUid(USER_METHOD_DEFAULT);
507 case USER_METHOD_GID:
508 testGdbusUserGid(USER_METHOD_DEFAULT);
511 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
516 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_pid) {
517 gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t expectedPid,
518 const std::string &requestedName, const ProcessCredentials &) {
519 auto helperPid = CynaraHelperCredentials::gdbusGetPid(std::move(conn),
520 requestedName.c_str());
521 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
522 << "; expected = " << expectedPid);
527 SdBusConnectionPtr createSdBusConnection(const std::string &requestedName) {
530 int r = sd_bus_default_system(&bus);
531 RUNNER_ASSERT_MSG(r >= 0, "Failed to connect do system bus: %s" << strerror(-r));
533 if (requestedName.empty() == false) {
534 r = sd_bus_request_name(bus, requestedName.c_str(), SD_BUS_NAME_REPLACE_EXISTING);
535 RUNNER_ASSERT_MSG(r >= 0, "Error in sd_bus_request_name");
538 SdBusConnectionPtr ret(bus, [] (sd_bus *busConnection) {
539 sd_bus_unref(busConnection);
545 typedef std::function<void(SdBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
546 const ProcessCredentials &peerCredentials)> SdBusAssertionFn;
548 void sdBusTestTemplate(SdBusAssertionFn assertion) {
549 std::string requestedName = "tests.dbus.cynara";
550 const ProcessCredentials peerCredentials;
552 SynchronizationPipe pipe;
553 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
554 std::cref(peerCredentials)));
556 pipe.claimParentEp();
559 auto conn = createSdBusConnection("");
560 assertion(std::move(conn), pid, requestedName, peerCredentials);
565 RUNNER_TEST_GROUP_INIT(cynara_creds_sd_bus)
567 void testSdBusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
568 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t pid,
569 const std::string &requestedName,
570 const ProcessCredentials &) {
571 CStringPtr clientPidStr(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
572 requestedName.c_str(), method, CYNARA_API_SUCCESS));
573 pid_t clientPid = std::stoi(clientPidStr.get());
574 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
575 << "; expected = " << pid);
579 void testSdBusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
580 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
581 const std::string &requestedName,
582 const ProcessCredentials &peerCredentials) {
583 CStringPtr label(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
584 requestedName.c_str(), method, CYNARA_API_SUCCESS));
585 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
586 "Labels don't match ret = " << label.get()
587 << "; expected = " << peerCredentials.label());
591 RUNNER_MULTIPROCESS_TEST(tccsd01_sd_bus_credentials_client_pid)
593 testSdBusClientPid();
596 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd02_sd_bus_credentials_client_smack)
598 testSdBusClientSmack();
601 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd03_sd_bus_credentials_client_default)
603 auto method = getClientDefaultMethod();
605 case CLIENT_METHOD_SMACK:
606 testSdBusClientSmack(CLIENT_METHOD_DEFAULT);
608 case CLIENT_METHOD_PID:
609 testSdBusClientPid(CLIENT_METHOD_DEFAULT);
612 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
617 void testSdBusUserUid(cynara_user_creds method = USER_METHOD_UID) {
618 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
619 const std::string &requestedName,
620 const ProcessCredentials &peerCredentials) {
621 CStringPtr uidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
622 requestedName.c_str(), method, CYNARA_API_SUCCESS));
623 uid_t uid = std::stoul(uidStr.get());
624 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
625 << "; expected = "<< peerCredentials.uid());
629 void testSdBusUserGid(cynara_user_creds method = USER_METHOD_GID) {
630 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
631 const std::string &requestedName,
632 const ProcessCredentials &peerCredentials) {
633 CStringPtr gidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
634 requestedName.c_str(), method, CYNARA_API_SUCCESS));
635 gid_t gid = std::stoul(gidStr.get());
636 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
637 << "; expected = "<< peerCredentials.gid());
641 RUNNER_MULTIPROCESS_TEST(tccsd04_sd_bus_credentials_user_uid)
646 RUNNER_MULTIPROCESS_TEST(tccsd05_sd_bus_credentials_user_gid)
651 RUNNER_MULTIPROCESS_TEST(tccsd06_sd_bus_credentials_user_default)
653 auto method = getUserDefaultMethod();
655 case USER_METHOD_UID:
656 testSdBusUserUid(USER_METHOD_DEFAULT);
658 case USER_METHOD_GID:
659 testSdBusUserGid(USER_METHOD_DEFAULT);
662 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
667 RUNNER_TEST_SMACK(tccd07_sd_bus_credentials_pid) {
668 sdBusTestTemplate([] (SdBusConnectionPtr conn, pid_t expectedPid,
669 const std::string &requestedName, const ProcessCredentials &) {
670 auto helperPid = CynaraHelperCredentials::sdBusGetPid(std::move(conn),
671 requestedName.c_str(), CYNARA_API_SUCCESS);
672 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
673 << "; expected = " << expectedPid);
678 RUNNER_TEST_GROUP_INIT(cynara_creds_self)
680 void testCredsClientSelf(cynara_client_creds method, const std::string &expected) {
682 int ret = cynara_creds_self_get_client(method, &client);
683 CStringPtr clientPtr(client);
684 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_client failed with " << ret);
685 RUNNER_ASSERT_MSG(expected == client, "expected client doesn't match, expected: " << expected
686 << ", got : " << client);
689 void testCredsUserSelf(cynara_user_creds method, const std::string &expected) {
691 int ret = cynara_creds_self_get_user(method, &user);
692 CStringPtr clientPtr(user);
693 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_user failed with " << ret);
694 RUNNER_ASSERT_MSG(expected == user, "expected user doesn't match, expected: " << expected
695 << ", got : " << user);
698 void testSelfClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
699 std::string label = "test-label";
700 ScopedProcessLabel spl(label, false);
701 testCredsClientSelf(method, label);
704 void testSelfClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
705 pid_t pid = getpid();
706 testCredsClientSelf(method, std::to_string(pid));
709 void testSelfUserUid(cynara_user_creds method = USER_METHOD_UID) {
710 uid_t uid = getuid();
711 testCredsUserSelf(method, std::to_string(uid));
714 void testSelfUserGid(cynara_user_creds method = USER_METHOD_GID) {
715 gid_t gid = getgid();
716 testCredsUserSelf(method, std::to_string(gid));
719 RUNNER_CHILD_TEST_SMACK(tccsl01_self_credentials_client_smack) {
720 testSelfClientSmack();
723 RUNNER_CHILD_TEST_SMACK(tccsl02_self_credentials_client_pid) {
727 RUNNER_CHILD_TEST_SMACK(tccsl03_self_credentials_user_uid) {
731 RUNNER_CHILD_TEST_SMACK(tccsl04_self_credentials_user_gid) {
735 RUNNER_CHILD_TEST_SMACK(tccsl05_self_credentials_client_default) {
736 auto method = getClientDefaultMethod();
738 case CLIENT_METHOD_SMACK:
739 testSelfClientSmack(CLIENT_METHOD_DEFAULT);
741 case CLIENT_METHOD_PID:
742 testSelfClientPid(CLIENT_METHOD_DEFAULT);
745 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value " << method);
749 RUNNER_CHILD_TEST_SMACK(tccsl06_self_credentials_user_default) {
750 auto method = getUserDefaultMethod();
752 case USER_METHOD_UID:
753 testSelfUserUid(USER_METHOD_DEFAULT);
755 case USER_METHOD_GID:
756 testSelfUserGid(USER_METHOD_DEFAULT);
759 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value " << method);