2 * Copyright (c) 2015-2018 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>
50 class ProcessCredentials {
52 ProcessCredentials() {}
54 const std::string &label(void) const {
58 uid_t uid(void) const {
59 return PasswdAccess::uid(APP_USER);
62 gid_t gid(void) const {
63 return PasswdAccess::gid("users");
67 std::string m_label = "cynara_helpers";
70 cynara_client_creds getClientDefaultMethod() {
71 cynara_client_creds def;
72 int ret = cynara_creds_get_default_client_method(&def);
73 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
74 "cynara_creds_get_default_client_method failed with " << ret);
78 cynara_user_creds getUserDefaultMethod() {
79 cynara_user_creds def;
80 int ret = cynara_creds_get_default_user_method(&def);
81 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
82 "cynara_creds_get_default_user_method failed with " << ret);
87 void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
88 const struct ProcessCredentials &peerCredentials) {
89 SecurityServer::AccessProvider ap(peerCredentials.label());
90 ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
93 int sock = UDSHelpers::createServer(&sockaddr);
94 SockUniquePtr sockPtr(&sock);
96 int clientSock = UDSHelpers::acceptClient(sock);
98 UDSHelpers::waitForDisconnect(clientSock);
101 typedef std::function<void(int sock, pid_t pid,
102 const ProcessCredentials &peerCredentials)> SocketAssertionFn;
104 void socketTestTemplate(SocketAssertionFn assertion, const std::string &scope) {
105 const auto sockaddr = UDSHelpers::makeAbstractAddress("helper_" + scope + ".socket");
106 const ProcessCredentials peerCredentials;
108 SynchronizationPipe pipe;
110 pid_t pid = runInChild(std::bind(udsServer, std::ref(pipe), std::cref(sockaddr),
111 std::cref(peerCredentials)));
113 pipe.claimParentEp();
115 int sock = UDSHelpers::createClient(&sockaddr);
116 SockUniquePtr sockPtr(&sock);
118 assertion(sock, pid, peerCredentials);
121 RUNNER_TEST_GROUP_INIT(cynara_creds_socket)
123 void testSocketClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
124 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
125 CStringPtr label(CynaraHelperCredentials::socketGetClient(sock, method));
126 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
127 "Labels don't match ret = " << label.get()
128 << "; expected = " << peerCredentials.label());
132 void testSocketClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
133 socketTestTemplate([method] (int sock, pid_t pid, const ProcessCredentials &) {
134 CStringPtr clientPidStr(CynaraHelperCredentials::socketGetClient(sock, method));
135 pid_t clientPid = std::stoi(clientPidStr.get());
136 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
137 << "; expected = " << pid);
141 RUNNER_MULTIPROCESS_TEST_SMACK(tccs01_socket_credentials_client_smack)
143 testSocketClientSmack();
146 RUNNER_MULTIPROCESS_TEST(tccs02_socket_credentials_client_pid)
148 testSocketClientPid();
151 RUNNER_MULTIPROCESS_TEST_SMACK(tccs03_socket_credentials_client_default)
153 auto method = getClientDefaultMethod();
155 case CLIENT_METHOD_SMACK:
156 testSocketClientSmack(CLIENT_METHOD_DEFAULT);
158 case CLIENT_METHOD_PID:
159 testSocketClientPid(CLIENT_METHOD_DEFAULT);
162 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
167 void testSocketUserUid(cynara_user_creds method = USER_METHOD_UID) {
168 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
169 CStringPtr uidStr(CynaraHelperCredentials::socketGetUser(sock, method));
170 uid_t uid = std::stoul(uidStr.get());
171 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
172 << "; expected = "<< peerCredentials.uid());
176 void testSocketUserGid(cynara_user_creds method = USER_METHOD_GID) {
177 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
178 CStringPtr gidStr(CynaraHelperCredentials::socketGetUser(sock, method));
179 gid_t gid = std::stoul(gidStr.get());
180 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
181 << "; expected = "<< peerCredentials.gid());
185 RUNNER_MULTIPROCESS_TEST(tccs04_socket_credentials_user_uid)
190 RUNNER_MULTIPROCESS_TEST(tccs05_socket_credentials_user_gid)
195 RUNNER_MULTIPROCESS_TEST(tccs06_socket_credentials_user_default)
197 auto method = getUserDefaultMethod();
199 case USER_METHOD_UID:
200 testSocketUserUid(USER_METHOD_DEFAULT);
202 case USER_METHOD_GID:
203 testSocketUserGid(USER_METHOD_DEFAULT);
206 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
211 RUNNER_MULTIPROCESS_TEST(tccs07_socket_credentials_pid)
213 socketTestTemplate([] (int sock, pid_t expectedPid, const ProcessCredentials &) {
214 pid_t peerPid(CynaraHelperCredentials::socketGetPid(sock));
215 RUNNER_ASSERT_MSG(peerPid == expectedPid, "Pids don't match ret = " << peerPid
216 << "; expected = "<< expectedPid);
220 // TODO: Create utility namespace for DBus, maybe?
221 DBusConnectionPtr createDBusConnection(const std::string &name) {
224 dbus_error_init(&err);
225 DBusConnection *conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
226 RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != 1, "Error in dbus_bus_get: " << err.message);
227 dbus_connection_set_exit_on_disconnect(conn, FALSE);
229 DBusConnectionPtr ret(conn, [] (DBusConnection *conn) {
230 dbus_connection_close(conn);
231 dbus_connection_unref(conn);
234 if (name.empty() == false) {
235 dbus_bus_request_name(conn, name.c_str(), DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
236 RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != TRUE,
237 "Error in dbus_bus_request_name: " << err.message);
243 void dbusServer(SynchronizationPipe &pipe, const std::string &requestedName,
244 const ProcessCredentials &peerCredentials) {
245 // for DBus connection, System must have access to our peer creds as well.
246 SecurityServer::AccessProvider systemAp("System");
247 systemAp.addObjectRule(peerCredentials.label(), "rwx");
250 SecurityServer::AccessProvider ap(peerCredentials.label());
251 ap.addObjectRule("System", "w");
252 ap.addObjectRule("System::Run", "x");
253 ap.addObjectRule("System::Shared", "rwx"); // for GDB
254 ap.addSubjectRule("System::Privileged", "rwx"); // for piping
255 ap.addObjectRule("System::Privileged", "rwx"); // for GDB and piping
256 ap.addObjectRule("User", "r"); // for /usr/lib/debug access
257 ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
260 auto conn = createDBusConnection(requestedName);
265 typedef std::function<void(DBusConnectionPtr conn, pid_t pid,
266 const std::string &requestedName,
267 const ProcessCredentials &peerCredentials)> DBusAssertionFn;
269 void dbusTestTemplate(DBusAssertionFn assertion, const std::string &/*scope*/) {
270 std::string requestedName = "tests.dbus.cynara";
271 const ProcessCredentials peerCredentials;
273 SynchronizationPipe pipe;
274 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
275 std::cref(peerCredentials)));
277 pipe.claimParentEp();
280 auto conn = createDBusConnection("");
281 assertion(std::move(conn), pid, requestedName, peerCredentials);
285 RUNNER_TEST_GROUP_INIT(cynara_creds_dbus)
287 void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
288 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
289 const ProcessCredentials &) {
290 CStringPtr clientPidStr(CynaraHelperCredentials::dbusGetClient(std::move(conn),
291 requestedName.c_str(), method, CYNARA_API_SUCCESS));
292 pid_t clientPid = std::stoi(clientPidStr.get());
293 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
294 << "; expected = " << pid);
298 void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
299 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
300 const ProcessCredentials &peerCredentials) {
301 CStringPtr label(CynaraHelperCredentials::dbusGetClient(std::move(conn),
302 requestedName.c_str(), method, CYNARA_API_SUCCESS));
303 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
304 "Labels don't match ret = " << label.get()
305 << "; expected = " << peerCredentials.label());
309 RUNNER_MULTIPROCESS_TEST(tccd01_dbus_credentials_client_pid)
314 RUNNER_MULTIPROCESS_TEST_SMACK(tccd02_dbus_credentials_client_smack)
316 testDbusClientSmack();
319 RUNNER_MULTIPROCESS_TEST_SMACK(tccd03_dbus_credentials_client_default)
321 auto method = getClientDefaultMethod();
323 case CLIENT_METHOD_SMACK:
324 testDbusClientSmack(CLIENT_METHOD_DEFAULT);
326 case CLIENT_METHOD_PID:
327 testDbusClientPid(CLIENT_METHOD_DEFAULT);
330 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
335 void testDbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
336 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
337 const ProcessCredentials &peerCredentials) {
338 CStringPtr uidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
339 requestedName.c_str(), method, CYNARA_API_SUCCESS));
340 uid_t uid = std::stoul(uidStr.get());
341 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
342 << "; expected = "<< peerCredentials.uid());
346 void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
347 // Acquiring gid from dbus connection is not yet implemented for cynara helpers
348 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
349 const ProcessCredentials &) {
350 CStringPtr gidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
351 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
355 RUNNER_MULTIPROCESS_TEST(tccd04_dbus_credentials_user_uid)
360 RUNNER_MULTIPROCESS_TEST(tccd05_dbus_credentials_user_gid)
365 RUNNER_MULTIPROCESS_TEST(tccd06_dbus_credentials_user_default) {
366 auto method = getUserDefaultMethod();
368 case USER_METHOD_UID:
369 testDbusUserUid(USER_METHOD_DEFAULT);
371 case USER_METHOD_GID:
372 testDbusUserGid(USER_METHOD_DEFAULT);
375 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
380 RUNNER_TEST_SMACK(tccd06_dbus_credentials_pid) {
381 dbusTestTemplate([] (DBusConnectionPtr conn, pid_t expectedPid,
382 const std::string &requestedName, const ProcessCredentials &) {
383 auto helperPid = CynaraHelperCredentials::dbusGetPid(std::move(conn),
384 requestedName.c_str(), CYNARA_API_SUCCESS);
385 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
386 << "; expected = " << expectedPid);
390 GDBusConnectionPtr createGDBusConnection() {
391 GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
393 return GDBusConnectionPtr(conn, [] (GDBusConnection *conn) {
394 g_object_unref(G_OBJECT(conn));
399 typedef std::function<void(GDBusConnectionPtr conn, pid_t pid,
400 const std::string &requestedName,
401 const ProcessCredentials &peerCredentials)> GDBusAssertionFn;
403 void gdbusTestTemplate(GDBusAssertionFn assertion, const std::string &/*scope*/) {
404 std::string requestedName = "tests.dbus.cynara";
405 const ProcessCredentials peerCredentials;
407 SynchronizationPipe pipe;
408 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
409 std::cref(peerCredentials)));
411 pipe.claimParentEp();
414 auto conn = createGDBusConnection();
415 assertion(std::move(conn), pid, requestedName, peerCredentials);
420 RUNNER_TEST_GROUP_INIT(cynara_creds_gdbus)
422 void testGdbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
423 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t pid,
424 const std::string &requestedName,
425 const ProcessCredentials &) {
426 GStringPtr clientPidStr(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
427 requestedName.c_str(), method));
428 pid_t clientPid = std::stoi(clientPidStr.get());
429 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
430 << "; expected = " << pid);
434 void testGdbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
435 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
436 const std::string &requestedName,
437 const ProcessCredentials &peerCredentials) {
438 GStringPtr label(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
439 requestedName.c_str(), method));
440 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
441 "Labels don't match ret = " << label.get()
442 << "; expected = " << peerCredentials.label());
446 RUNNER_MULTIPROCESS_TEST(tccgd01_gdbus_credentials_client_pid)
448 testGdbusClientPid();
451 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd02_gdbus_credentials_client_smack)
453 testGdbusClientSmack();
456 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd03_gdbus_credentials_client_default)
458 auto method = getClientDefaultMethod();
460 case CLIENT_METHOD_SMACK:
461 testGdbusClientSmack(CLIENT_METHOD_DEFAULT);
463 case CLIENT_METHOD_PID:
464 testGdbusClientPid(CLIENT_METHOD_DEFAULT);
467 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
472 void testGdbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
473 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
474 const std::string &requestedName,
475 const ProcessCredentials &peerCredentials) {
476 GStringPtr uidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
477 requestedName.c_str(), method));
478 uid_t uid = std::stoul(uidStr.get());
479 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
480 << "; expected = "<< peerCredentials.uid());
484 void testGdbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
485 // Getting gid for gdbus connection is not yet implemented in cynara helpers
486 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
487 const std::string &requestedName,
488 const ProcessCredentials &) {
489 GStringPtr gidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
490 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
494 RUNNER_MULTIPROCESS_TEST(tccgd04_gdbus_credentials_user_uid)
499 RUNNER_MULTIPROCESS_TEST(tccgd05_gdbus_credentials_user_gid)
504 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_user_default) {
505 auto method = getUserDefaultMethod();
507 case USER_METHOD_UID:
508 testGdbusUserUid(USER_METHOD_DEFAULT);
510 case USER_METHOD_GID:
511 testGdbusUserGid(USER_METHOD_DEFAULT);
514 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
519 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_pid) {
520 gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t expectedPid,
521 const std::string &requestedName, const ProcessCredentials &) {
522 auto helperPid = CynaraHelperCredentials::gdbusGetPid(std::move(conn),
523 requestedName.c_str());
524 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
525 << "; expected = " << expectedPid);
530 SdBusConnectionPtr createSdBusConnection(const std::string &requestedName) {
533 int r = sd_bus_default_system(&bus);
534 RUNNER_ASSERT_MSG(r >= 0, "Failed to connect do system bus: %s" << strerror(-r));
536 if (requestedName.empty() == false) {
537 r = sd_bus_request_name(bus, requestedName.c_str(), SD_BUS_NAME_REPLACE_EXISTING);
538 RUNNER_ASSERT_MSG(r >= 0, "Error in sd_bus_request_name");
541 SdBusConnectionPtr ret(bus, [] (sd_bus *busConnection) {
542 sd_bus_unref(busConnection);
548 typedef std::function<void(SdBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
549 const ProcessCredentials &peerCredentials)> SdBusAssertionFn;
551 void sdBusTestTemplate(SdBusAssertionFn assertion) {
552 std::string requestedName = "tests.dbus.cynara";
553 const ProcessCredentials peerCredentials;
555 SynchronizationPipe pipe;
556 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
557 std::cref(peerCredentials)));
559 pipe.claimParentEp();
562 auto conn = createSdBusConnection("");
563 assertion(std::move(conn), pid, requestedName, peerCredentials);
568 RUNNER_TEST_GROUP_INIT(cynara_creds_sd_bus)
570 void testSdBusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
571 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t pid,
572 const std::string &requestedName,
573 const ProcessCredentials &) {
574 CStringPtr clientPidStr(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
575 requestedName.c_str(), method, CYNARA_API_SUCCESS));
576 pid_t clientPid = std::stoi(clientPidStr.get());
577 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
578 << "; expected = " << pid);
582 void testSdBusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
583 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
584 const std::string &requestedName,
585 const ProcessCredentials &peerCredentials) {
586 CStringPtr label(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
587 requestedName.c_str(), method, CYNARA_API_SUCCESS));
588 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
589 "Labels don't match ret = " << label.get()
590 << "; expected = " << peerCredentials.label());
594 RUNNER_MULTIPROCESS_TEST(tccsd01_sd_bus_credentials_client_pid)
596 testSdBusClientPid();
599 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd02_sd_bus_credentials_client_smack)
601 testSdBusClientSmack();
604 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd03_sd_bus_credentials_client_default)
606 auto method = getClientDefaultMethod();
608 case CLIENT_METHOD_SMACK:
609 testSdBusClientSmack(CLIENT_METHOD_DEFAULT);
611 case CLIENT_METHOD_PID:
612 testSdBusClientPid(CLIENT_METHOD_DEFAULT);
615 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
620 void testSdBusUserUid(cynara_user_creds method = USER_METHOD_UID) {
621 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
622 const std::string &requestedName,
623 const ProcessCredentials &peerCredentials) {
624 CStringPtr uidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
625 requestedName.c_str(), method, CYNARA_API_SUCCESS));
626 uid_t uid = std::stoul(uidStr.get());
627 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
628 << "; expected = "<< peerCredentials.uid());
632 void testSdBusUserGid(cynara_user_creds method = USER_METHOD_GID) {
633 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
634 const std::string &requestedName,
635 const ProcessCredentials &peerCredentials) {
636 CStringPtr gidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
637 requestedName.c_str(), method, CYNARA_API_SUCCESS));
638 gid_t gid = std::stoul(gidStr.get());
639 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
640 << "; expected = "<< peerCredentials.gid());
644 RUNNER_MULTIPROCESS_TEST(tccsd04_sd_bus_credentials_user_uid)
649 RUNNER_MULTIPROCESS_TEST(tccsd05_sd_bus_credentials_user_gid)
654 RUNNER_MULTIPROCESS_TEST(tccsd06_sd_bus_credentials_user_default)
656 auto method = getUserDefaultMethod();
658 case USER_METHOD_UID:
659 testSdBusUserUid(USER_METHOD_DEFAULT);
661 case USER_METHOD_GID:
662 testSdBusUserGid(USER_METHOD_DEFAULT);
665 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
670 RUNNER_TEST_SMACK(tccd07_sd_bus_credentials_pid) {
671 sdBusTestTemplate([] (SdBusConnectionPtr conn, pid_t expectedPid,
672 const std::string &requestedName, const ProcessCredentials &) {
673 auto helperPid = CynaraHelperCredentials::sdBusGetPid(std::move(conn),
674 requestedName.c_str(), CYNARA_API_SUCCESS);
675 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
676 << "; expected = " << expectedPid);
681 RUNNER_TEST_GROUP_INIT(cynara_creds_self)
683 void testCredsClientSelf(cynara_client_creds method, const std::string &expected) {
685 int ret = cynara_creds_self_get_client(method, &client);
686 CStringPtr clientPtr(client);
687 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_client failed with " << ret);
688 RUNNER_ASSERT_MSG(expected == client, "expected client doesn't match, expected: " << expected
689 << ", got : " << client);
692 void testCredsUserSelf(cynara_user_creds method, const std::string &expected) {
694 int ret = cynara_creds_self_get_user(method, &user);
695 CStringPtr clientPtr(user);
696 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_user failed with " << ret);
697 RUNNER_ASSERT_MSG(expected == user, "expected user doesn't match, expected: " << expected
698 << ", got : " << user);
701 void testSelfClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
702 std::string label = "test-label";
703 change_label(label.c_str());
704 testCredsClientSelf(method, label);
707 void testSelfClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
708 pid_t pid = getpid();
709 testCredsClientSelf(method, std::to_string(pid));
712 void testSelfUserUid(cynara_user_creds method = USER_METHOD_UID) {
713 uid_t uid = getuid();
714 testCredsUserSelf(method, std::to_string(uid));
717 void testSelfUserGid(cynara_user_creds method = USER_METHOD_GID) {
718 gid_t gid = getgid();
719 testCredsUserSelf(method, std::to_string(gid));
722 RUNNER_CHILD_TEST_SMACK(tccsl01_self_credentials_client_smack) {
723 testSelfClientSmack();
726 RUNNER_CHILD_TEST_SMACK(tccsl02_self_credentials_client_pid) {
730 RUNNER_CHILD_TEST_SMACK(tccsl03_self_credentials_user_uid) {
734 RUNNER_CHILD_TEST_SMACK(tccsl04_self_credentials_user_gid) {
738 RUNNER_CHILD_TEST_SMACK(tccsl05_self_credentials_client_default) {
739 auto method = getClientDefaultMethod();
741 case CLIENT_METHOD_SMACK:
742 testSelfClientSmack(CLIENT_METHOD_DEFAULT);
744 case CLIENT_METHOD_PID:
745 testSelfClientPid(CLIENT_METHOD_DEFAULT);
748 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value " << method);
752 RUNNER_CHILD_TEST_SMACK(tccsl06_self_credentials_user_default) {
753 auto method = getUserDefaultMethod();
755 case USER_METHOD_UID:
756 testSelfUserUid(USER_METHOD_DEFAULT);
758 case USER_METHOD_GID:
759 testSelfUserGid(USER_METHOD_DEFAULT);
762 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value " << method);