2 * Copyright (c) 2015-2022 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-dbus.h>
48 #include <cynara-creds-gdbus.h>
49 #include <cynara-creds-self.h>
50 #include <scoped_process_label.h>
53 static const cynara_client_creds G_CLIENT_METHOD_DEFAULT = CLIENT_METHOD_DEFAULT;
54 static const cynara_client_creds G_CLIENT_METHOD_SMACK = CLIENT_METHOD_SMACK;
55 static const cynara_client_creds G_CLIENT_METHOD_PID = CLIENT_METHOD_PID;
57 static const cynara_user_creds G_USER_METHOD_DEFAULT = USER_METHOD_DEFAULT;
58 static const cynara_user_creds G_USER_METHOD_UID = USER_METHOD_UID;
59 static const cynara_user_creds G_USER_METHOD_GID = USER_METHOD_GID;
61 static const cynara_client_creds G_CLIENT_METHOD_LOW = (cynara_client_creds)-1;
62 static const cynara_client_creds G_CLIENT_METHOD_HIGH = (cynara_client_creds)(CLIENT_METHOD_PID+10);
64 static const cynara_user_creds G_USER_METHOD_LOW = (cynara_user_creds)-1;
65 static const cynara_user_creds G_USER_METHOD_HIGH = (cynara_user_creds)(USER_METHOD_GID+10);
68 class ProcessCredentials {
70 ProcessCredentials() {}
72 const std::string &label(void) const {
76 uid_t uid(void) const {
77 return PasswdAccess::uid(APP_USER);
80 gid_t gid(void) const {
81 return PasswdAccess::gid("users");
85 std::string m_label = "cynara_helpers";
88 cynara_client_creds getClientDefaultMethod() {
89 cynara_client_creds def;
90 int ret = cynara_creds_get_default_client_method(&def);
91 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
92 "cynara_creds_get_default_client_method failed with " << ret);
96 cynara_user_creds getUserDefaultMethod() {
97 cynara_user_creds def;
98 int ret = cynara_creds_get_default_user_method(&def);
99 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
100 "cynara_creds_get_default_user_method failed with " << ret);
105 void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
106 const struct ProcessCredentials &peerCredentials) {
107 AppContext ctx(peerCredentials.label());
108 ctx.apply(peerCredentials.uid(), peerCredentials.gid());
111 int sock = UDSHelpers::createServer(&sockaddr);
112 SockUniquePtr sockPtr(&sock);
114 int clientSock = UDSHelpers::acceptClient(sock);
116 UDSHelpers::waitForDisconnect(clientSock);
119 typedef std::function<void(int sock, pid_t pid,
120 const ProcessCredentials &peerCredentials)> SocketAssertionFn;
122 void socketTestTemplate(SocketAssertionFn assertion, const std::string &scope) {
123 const auto sockaddr = UDSHelpers::makeAbstractAddress("helper_" + scope + ".socket");
124 const ProcessCredentials peerCredentials;
126 SynchronizationPipe pipe;
128 pid_t pid = runInChild(std::bind(udsServer, std::ref(pipe), std::cref(sockaddr),
129 std::cref(peerCredentials)));
131 pipe.claimParentEp();
133 int sock = UDSHelpers::createClient(&sockaddr);
134 SockUniquePtr sockPtr(&sock);
136 assertion(sock, pid, peerCredentials);
139 RUNNER_TEST_GROUP_INIT(cynara_creds_socket)
141 void testSocketClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
142 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
143 CStringPtr label(CynaraHelperCredentials::socketGetClient(sock, method));
144 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
145 "Labels don't match ret = " << label.get()
146 << "; expected = " << peerCredentials.label());
150 void testSocketClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
151 socketTestTemplate([method] (int sock, pid_t pid, const ProcessCredentials &) {
152 CStringPtr clientPidStr(CynaraHelperCredentials::socketGetClient(sock, method));
153 pid_t clientPid = std::stoi(clientPidStr.get());
154 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
155 << "; expected = " << pid);
159 RUNNER_MULTIPROCESS_TEST_SMACK(tccs01_socket_credentials_client_smack)
161 testSocketClientSmack();
164 RUNNER_MULTIPROCESS_TEST(tccs02_socket_credentials_client_pid)
166 testSocketClientPid();
169 RUNNER_MULTIPROCESS_TEST_SMACK(tccs03_socket_credentials_client_default)
171 auto method = getClientDefaultMethod();
173 case CLIENT_METHOD_SMACK:
174 testSocketClientSmack(CLIENT_METHOD_DEFAULT);
176 case CLIENT_METHOD_PID:
177 testSocketClientPid(CLIENT_METHOD_DEFAULT);
180 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
185 void testSocketUserUid(cynara_user_creds method = USER_METHOD_UID) {
186 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
187 CStringPtr uidStr(CynaraHelperCredentials::socketGetUser(sock, method));
188 uid_t uid = std::stoul(uidStr.get());
189 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
190 << "; expected = "<< peerCredentials.uid());
194 void testSocketUserGid(cynara_user_creds method = USER_METHOD_GID) {
195 socketTestTemplate([method] (int sock, pid_t, const ProcessCredentials &peerCredentials) {
196 CStringPtr gidStr(CynaraHelperCredentials::socketGetUser(sock, method));
197 gid_t gid = std::stoul(gidStr.get());
198 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
199 << "; expected = "<< peerCredentials.gid());
203 RUNNER_MULTIPROCESS_TEST(tccs04_socket_credentials_user_uid)
208 RUNNER_MULTIPROCESS_TEST(tccs05_socket_credentials_user_gid)
213 RUNNER_MULTIPROCESS_TEST(tccs06_socket_credentials_user_default)
215 auto method = getUserDefaultMethod();
217 case USER_METHOD_UID:
218 testSocketUserUid(USER_METHOD_DEFAULT);
220 case USER_METHOD_GID:
221 testSocketUserGid(USER_METHOD_DEFAULT);
224 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
229 RUNNER_MULTIPROCESS_TEST(tccs07_socket_credentials_pid)
231 socketTestTemplate([] (int sock, pid_t expectedPid, const ProcessCredentials &) {
232 pid_t peerPid(CynaraHelperCredentials::socketGetPid(sock));
233 RUNNER_ASSERT_MSG(peerPid == expectedPid, "Pids don't match ret = " << peerPid
234 << "; expected = "<< expectedPid);
239 //=====================================DBus===========================================
241 // TODO: Create utility namespace for DBus, maybe?
242 DBusConnectionPtr createDBusConnection(const std::string &name) {
245 dbus_error_init(&err);
246 DBusConnection *conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
247 RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != 1, "Error in dbus_bus_get: " << err.message);
248 dbus_connection_set_exit_on_disconnect(conn, FALSE);
250 DBusConnectionPtr ret(conn, [] (DBusConnection *conn) {
251 dbus_connection_close(conn);
252 dbus_connection_unref(conn);
255 if (name.empty() == false) {
256 dbus_bus_request_name(conn, name.c_str(), DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
257 RUNNER_ASSERT_MSG(dbus_error_is_set(&err) != TRUE,
258 "Error in dbus_bus_request_name: " << err.message);
264 void dbusServer(SynchronizationPipe &pipe, const std::string &requestedName,
265 const ProcessCredentials &peerCredentials) {
266 AppContext ctx(peerCredentials.label());
267 ctx.allowAccessFrom("System", "rwx"); // for DBus connection
268 ctx.allowAccessFrom("System::Privileged", "rwx"); // for piping
269 ctx.allowAccessTo("System", "w");
270 ctx.allowAccessTo("System::Privileged", "rwx"); // for GDB and piping
271 ctx.allowAccessTo("System::Run", "x");
272 ctx.allowAccessTo("System::Shared", "rwx"); // for GDB
273 ctx.allowAccessTo("User", "r"); // for /usr/lib/debug access
274 ctx.apply(peerCredentials.uid(), peerCredentials.gid());
277 auto conn = createDBusConnection(requestedName);
282 typedef std::function<void(DBusConnectionPtr conn, pid_t pid,
283 const std::string &requestedName,
284 const ProcessCredentials &peerCredentials)> DBusAssertionFn;
286 void dbusTestTemplate(DBusAssertionFn assertion) {
287 std::string requestedName = "tests.dbus.cynara";
288 const ProcessCredentials peerCredentials;
290 SynchronizationPipe pipe;
291 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
292 std::cref(peerCredentials)));
294 pipe.claimParentEp();
297 auto conn = createDBusConnection("");
298 assertion(std::move(conn), pid, requestedName, peerCredentials);
303 RUNNER_TEST_GROUP_INIT(cynara_creds_dbus)
305 void testDbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
306 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
307 const ProcessCredentials &) {
308 CStringPtr clientPidStr(CynaraHelperCredentials::dbusGetClient(std::move(conn),
309 requestedName.c_str(), method, CYNARA_API_SUCCESS));
310 pid_t clientPid = std::stoi(clientPidStr.get());
311 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
312 << "; expected = " << pid);
316 void testDbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
317 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
318 const ProcessCredentials &peerCredentials) {
319 CStringPtr label(CynaraHelperCredentials::dbusGetClient(std::move(conn),
320 requestedName.c_str(), method, CYNARA_API_SUCCESS));
321 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
322 "Labels don't match ret = " << label.get()
323 << "; expected = " << peerCredentials.label());
327 RUNNER_MULTIPROCESS_TEST(tccd01_dbus_credentials_client_pid)
332 RUNNER_MULTIPROCESS_TEST_SMACK(tccd02_dbus_credentials_client_smack)
334 testDbusClientSmack();
337 RUNNER_MULTIPROCESS_TEST_SMACK(tccd03_dbus_credentials_client_default)
339 auto method = getClientDefaultMethod();
341 case CLIENT_METHOD_SMACK:
342 testDbusClientSmack(CLIENT_METHOD_DEFAULT);
344 case CLIENT_METHOD_PID:
345 testDbusClientPid(CLIENT_METHOD_DEFAULT);
348 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
353 void testDbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
354 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
355 const ProcessCredentials &peerCredentials) {
356 CStringPtr uidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
357 requestedName.c_str(), method, CYNARA_API_SUCCESS));
358 uid_t uid = std::stoul(uidStr.get());
359 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
360 << "; expected = "<< peerCredentials.uid());
364 void testDbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
365 // Acquiring gid from dbus connection is not yet implemented for cynara helpers
366 dbusTestTemplate([method] (DBusConnectionPtr conn, pid_t, const std::string &requestedName,
367 const ProcessCredentials &) {
368 CStringPtr gidStr(CynaraHelperCredentials::dbusGetUser(std::move(conn),
369 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
373 RUNNER_MULTIPROCESS_TEST(tccd04_dbus_credentials_user_uid)
378 RUNNER_MULTIPROCESS_TEST(tccd05_dbus_credentials_user_gid)
383 RUNNER_MULTIPROCESS_TEST(tccd06_dbus_credentials_user_default) {
384 auto method = getUserDefaultMethod();
386 case USER_METHOD_UID:
387 testDbusUserUid(USER_METHOD_DEFAULT);
389 case USER_METHOD_GID:
390 testDbusUserGid(USER_METHOD_DEFAULT);
393 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
398 RUNNER_TEST_SMACK(tccd07_dbus_credentials_pid) {
399 dbusTestTemplate([] (DBusConnectionPtr conn, pid_t expectedPid,
400 const std::string &requestedName, const ProcessCredentials &) {
401 auto helperPid = CynaraHelperCredentials::dbusGetPid(std::move(conn),
402 requestedName.c_str(), CYNARA_API_SUCCESS);
403 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
404 << "; expected = " << expectedPid);
408 void testDbusCredsDefault(int expectedResult, char **pClientBuff,
409 char **pUserBuff, pid_t *pPidCred)
411 // reset variables if set by previous test cases
412 if (pClientBuff != nullptr) *pClientBuff = nullptr;
413 if (pUserBuff != nullptr) *pUserBuff = nullptr;
414 if (pPidCred != nullptr) *pPidCred = -1;
416 dbusTestTemplate([&] (DBusConnectionPtr conn, pid_t pid,
417 const std::string &requestedName,
418 const ProcessCredentials &peerCredentials) {
419 cynara_user_creds userMethod = getUserDefaultMethod();
420 cynara_client_creds clientMethod = getClientDefaultMethod();
422 // exception override
423 if (expectedResult == CYNARA_API_SUCCESS && userMethod == USER_METHOD_GID) {
424 expectedResult = CYNARA_API_METHOD_NOT_SUPPORTED;
427 int ret = cynara_creds_dbus_get_default(conn.get(), requestedName.c_str(),
428 pClientBuff, pUserBuff, pPidCred);
430 RUNNER_ASSERT_MSG(ret == expectedResult,
431 "cynara_creds_dbus_get_default returned unexpected result: " << ret <<
432 "; expected: " << expectedResult);
434 if (ret != CYNARA_API_SUCCESS) {
438 if (pClientBuff != nullptr) {
439 switch (clientMethod) {
440 case CLIENT_METHOD_SMACK:
441 RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(),
442 "Client labels don't match, ret = "
443 << *pClientBuff << "; expected = "
444 << peerCredentials.label());
447 case CLIENT_METHOD_PID:
448 RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid),
449 "PIDs don't match, ret = "
450 << *pClientBuff << "; expected = " << pid);
458 if (pUserBuff != nullptr) {
459 switch (userMethod) {
460 case USER_METHOD_UID:
461 RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()),
462 "UIDs don't match, ret = "
464 << "; expected = " << peerCredentials.uid());
472 if (pPidCred != nullptr) {
473 RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = "
475 << "; expected = " << pid);
480 RUNNER_MULTIPROCESS_TEST(tccd08_dbus_credentials_default_positive_all)
482 char *clientBuff = nullptr;
483 char *userBuff = nullptr;
486 testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, &pidCred);
489 RUNNER_MULTIPROCESS_TEST(tccd09_dbus_credentials_default_positive_partial_single)
491 char *clientBuff = nullptr;
492 char *userBuff = nullptr;
495 testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, nullptr);
496 testDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, nullptr);
497 testDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, nullptr, &pidCred);
500 RUNNER_MULTIPROCESS_TEST(tccd10_dbus_credentials_default_positive_partial_double)
502 char *clientBuff = nullptr;
503 char *userBuff = nullptr;
506 testDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, &pidCred);
507 testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, &pidCred);
508 testDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, nullptr);
511 RUNNER_MULTIPROCESS_TEST(tccd11_dbus_credentials_default_negative_incomplete)
513 testDbusCredsDefault(CYNARA_API_INVALID_PARAM, nullptr, nullptr, nullptr);
516 void testDbusCreds(int expectedResult,
517 const cynara_client_creds *pClientMethod, char **pClientBuff,
518 const cynara_user_creds *pUserMethod, char **pUserBuff,
521 // reset variables if set by previous test cases
522 if (pClientBuff != nullptr) *pClientBuff = nullptr;
523 if (pUserBuff != nullptr) *pUserBuff = nullptr;
524 if (pPidCred != nullptr) *pPidCred = -1;
526 dbusTestTemplate([&] (DBusConnectionPtr conn, pid_t pid,
527 const std::string &requestedName,
528 const ProcessCredentials &peerCredentials) {
530 int ret = cynara_creds_dbus_get(conn.get(), requestedName.c_str(),
531 pClientMethod, pClientBuff,
532 pUserMethod, pUserBuff, pPidCred);
534 RUNNER_ASSERT_MSG(ret == expectedResult,
535 "cynara_creds_dbus_get returned unexpected result: " << ret <<
536 "; expected: " << expectedResult);
538 if (ret != CYNARA_API_SUCCESS) {
542 if (pClientBuff != nullptr && pClientMethod != nullptr) {
543 switch (*pClientMethod) {
544 case CLIENT_METHOD_SMACK:
545 RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(),
546 "Client labels don't match, ret = "
547 << *pClientBuff << "; expected = "
548 << peerCredentials.label());
551 case CLIENT_METHOD_PID:
552 RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid),
553 "PIDs don't match, ret = "
554 << *pClientBuff << "; expected = " << pid);
562 if (pUserBuff != nullptr && pUserMethod != nullptr) {
563 switch (*pUserMethod) {
564 case USER_METHOD_UID:
565 RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()),
566 "UIDs don't match, ret = "
568 << "; expected = " << peerCredentials.uid());
576 if (pPidCred != nullptr) {
577 RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = "
579 << "; expected = " << pid);
584 RUNNER_MULTIPROCESS_TEST(tccd12_dbus_credentials_positive_all)
586 char *clientBuff = nullptr;
587 char *userBuff = nullptr;
590 testDbusCreds(CYNARA_API_SUCCESS,
591 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
592 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
593 testDbusCreds(CYNARA_API_SUCCESS,
594 &G_CLIENT_METHOD_SMACK, &clientBuff,
595 &G_USER_METHOD_UID, &userBuff, &pidCred);
596 testDbusCreds(CYNARA_API_SUCCESS,
597 &G_CLIENT_METHOD_PID, &clientBuff,
598 &G_USER_METHOD_UID, &userBuff, &pidCred);
600 testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
601 &G_CLIENT_METHOD_SMACK, &clientBuff,
602 &G_USER_METHOD_GID, &userBuff, &pidCred);
603 testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
604 &G_CLIENT_METHOD_PID, &clientBuff,
605 &G_USER_METHOD_GID, &userBuff, &pidCred);
608 RUNNER_MULTIPROCESS_TEST(tccd13_dbus_credentials_positive_partial_single)
610 char *clientBuff = nullptr;
611 char *userBuff = nullptr;
614 testDbusCreds(CYNARA_API_SUCCESS,
615 &G_CLIENT_METHOD_PID, &clientBuff,
616 &G_USER_METHOD_DEFAULT, nullptr,
618 testDbusCreds(CYNARA_API_SUCCESS,
619 &G_CLIENT_METHOD_SMACK, &clientBuff,
623 testDbusCreds(CYNARA_API_SUCCESS,
624 &G_CLIENT_METHOD_DEFAULT, nullptr,
625 &G_USER_METHOD_DEFAULT, &userBuff,
627 testDbusCreds(CYNARA_API_SUCCESS,
629 &G_USER_METHOD_DEFAULT, &userBuff,
632 testDbusCreds(CYNARA_API_SUCCESS,
634 &G_USER_METHOD_DEFAULT, nullptr,
636 testDbusCreds(CYNARA_API_SUCCESS,
637 &G_CLIENT_METHOD_DEFAULT, nullptr,
640 testDbusCreds(CYNARA_API_SUCCESS,
646 RUNNER_MULTIPROCESS_TEST(tccd14_dbus_credentials_positive_partial_double)
648 char *clientBuff = nullptr;
649 char *userBuff = nullptr;
652 testDbusCreds(CYNARA_API_SUCCESS,
653 &G_CLIENT_METHOD_DEFAULT, nullptr,
654 &G_USER_METHOD_UID, &userBuff, &pidCred);
655 testDbusCreds(CYNARA_API_SUCCESS,
657 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
659 testDbusCreds(CYNARA_API_SUCCESS,
660 &G_CLIENT_METHOD_PID, &clientBuff,
661 &G_USER_METHOD_DEFAULT, nullptr, &pidCred);
662 testDbusCreds(CYNARA_API_SUCCESS,
663 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
664 nullptr, nullptr, &pidCred);
666 testDbusCreds(CYNARA_API_SUCCESS,
667 &G_CLIENT_METHOD_SMACK, &clientBuff,
668 &G_USER_METHOD_UID, &userBuff, nullptr);
671 RUNNER_MULTIPROCESS_TEST(tccd14_dbus_credentials_negative_incomplete)
673 char *clientBuff = nullptr;
674 char *userBuff = nullptr;
677 testDbusCreds(CYNARA_API_INVALID_PARAM,
678 nullptr, &clientBuff,
679 &G_USER_METHOD_UID, &userBuff, &pidCred);
680 testDbusCreds(CYNARA_API_INVALID_PARAM,
681 nullptr, &clientBuff,
682 &G_USER_METHOD_UID, nullptr, &pidCred);
683 testDbusCreds(CYNARA_API_INVALID_PARAM,
684 nullptr, &clientBuff,
685 nullptr, nullptr, &pidCred);
687 testDbusCreds(CYNARA_API_INVALID_PARAM,
688 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
689 nullptr, &userBuff, &pidCred);
690 testDbusCreds(CYNARA_API_INVALID_PARAM,
691 &G_CLIENT_METHOD_DEFAULT, nullptr,
692 nullptr, &userBuff, &pidCred);
693 testDbusCreds(CYNARA_API_INVALID_PARAM,
695 nullptr, &userBuff, &pidCred);
697 testDbusCreds(CYNARA_API_INVALID_PARAM,
698 &G_CLIENT_METHOD_DEFAULT, nullptr,
699 &G_USER_METHOD_UID, nullptr, nullptr);
700 testDbusCreds(CYNARA_API_INVALID_PARAM,
701 &G_CLIENT_METHOD_DEFAULT, nullptr,
702 nullptr, nullptr, nullptr);
703 testDbusCreds(CYNARA_API_INVALID_PARAM,
705 &G_USER_METHOD_DEFAULT, nullptr, nullptr);
706 testDbusCreds(CYNARA_API_INVALID_PARAM,
708 nullptr, nullptr, nullptr);
711 RUNNER_MULTIPROCESS_TEST(tccd14_dbus_credentials_negative_incorrect)
713 char *clientBuff = nullptr;
714 char *userBuff = nullptr;
717 testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
718 &G_CLIENT_METHOD_LOW, &clientBuff,
719 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
720 testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
721 &G_CLIENT_METHOD_HIGH, &clientBuff,
722 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
723 testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
724 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
725 &G_USER_METHOD_LOW, &userBuff, &pidCred);
726 testDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
727 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
728 &G_USER_METHOD_HIGH, &userBuff, &pidCred);
732 //===============================GDBUS======================================
734 GDBusConnectionPtr createGDBusConnection() {
735 GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
737 return GDBusConnectionPtr(conn, [] (GDBusConnection *conn) {
738 g_object_unref(G_OBJECT(conn));
743 typedef std::function<void(GDBusConnectionPtr conn, pid_t pid,
744 const std::string &requestedName,
745 const ProcessCredentials &peerCredentials)> GDBusAssertionFn;
747 void gdbusTestTemplate(GDBusAssertionFn assertion) {
748 std::string requestedName = "tests.dbus.cynara";
749 const ProcessCredentials peerCredentials;
751 SynchronizationPipe pipe;
752 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
753 std::cref(peerCredentials)));
755 pipe.claimParentEp();
758 auto conn = createGDBusConnection();
759 assertion(std::move(conn), pid, requestedName, peerCredentials);
764 RUNNER_TEST_GROUP_INIT(cynara_creds_gdbus)
766 void testGdbusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
767 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t pid,
768 const std::string &requestedName,
769 const ProcessCredentials &) {
770 GStringPtr clientPidStr(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
771 requestedName.c_str(), method));
772 pid_t clientPid = std::stoi(clientPidStr.get());
773 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
774 << "; expected = " << pid);
778 void testGdbusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
779 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
780 const std::string &requestedName,
781 const ProcessCredentials &peerCredentials) {
782 GStringPtr label(CynaraHelperCredentials::gdbusGetClient(std::move(conn),
783 requestedName.c_str(), method));
784 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
785 "Labels don't match ret = " << label.get()
786 << "; expected = " << peerCredentials.label());
790 RUNNER_MULTIPROCESS_TEST(tccgd01_gdbus_credentials_client_pid)
792 testGdbusClientPid();
795 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd02_gdbus_credentials_client_smack)
797 testGdbusClientSmack();
800 RUNNER_MULTIPROCESS_TEST_SMACK(tccgd03_gdbus_credentials_client_default)
802 auto method = getClientDefaultMethod();
804 case CLIENT_METHOD_SMACK:
805 testGdbusClientSmack(CLIENT_METHOD_DEFAULT);
807 case CLIENT_METHOD_PID:
808 testGdbusClientPid(CLIENT_METHOD_DEFAULT);
811 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
816 void testGdbusUserUid(cynara_user_creds method = USER_METHOD_UID) {
817 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
818 const std::string &requestedName,
819 const ProcessCredentials &peerCredentials) {
820 GStringPtr uidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
821 requestedName.c_str(), method));
822 uid_t uid = std::stoul(uidStr.get());
823 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
824 << "; expected = "<< peerCredentials.uid());
828 void testGdbusUserGid(cynara_user_creds method = USER_METHOD_GID) {
829 // Getting gid for gdbus connection is not yet implemented in cynara helpers
830 gdbusTestTemplate([method] (GDBusConnectionPtr conn, pid_t,
831 const std::string &requestedName,
832 const ProcessCredentials &) {
833 GStringPtr gidStr(CynaraHelperCredentials::gdbusGetUser(std::move(conn),
834 requestedName.c_str(), method, CYNARA_API_METHOD_NOT_SUPPORTED));
838 RUNNER_MULTIPROCESS_TEST(tccgd04_gdbus_credentials_user_uid)
843 RUNNER_MULTIPROCESS_TEST(tccgd05_gdbus_credentials_user_gid)
848 RUNNER_MULTIPROCESS_TEST(tccgd06_gdbus_credentials_user_default) {
849 auto method = getUserDefaultMethod();
851 case USER_METHOD_UID:
852 testGdbusUserUid(USER_METHOD_DEFAULT);
854 case USER_METHOD_GID:
855 testGdbusUserGid(USER_METHOD_DEFAULT);
858 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
863 RUNNER_MULTIPROCESS_TEST(tccgd07_gdbus_credentials_pid) {
864 gdbusTestTemplate([] (GDBusConnectionPtr conn, pid_t expectedPid,
865 const std::string &requestedName, const ProcessCredentials &) {
866 auto helperPid = CynaraHelperCredentials::gdbusGetPid(std::move(conn),
867 requestedName.c_str());
868 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
869 << "; expected = " << expectedPid);
873 void testGDbusCredsDefault(int expectedResult, char **pClientBuff,
874 char **pUserBuff, pid_t *pPidCred)
876 // reset variables if set by previous test cases
877 if (pClientBuff != nullptr) *pClientBuff = nullptr;
878 if (pUserBuff != nullptr) *pUserBuff = nullptr;
879 if (pPidCred != nullptr) *pPidCred = -1;
881 gdbusTestTemplate([&] (GDBusConnectionPtr conn, pid_t pid,
882 const std::string &requestedName,
883 const ProcessCredentials &peerCredentials) {
884 cynara_user_creds userMethod = getUserDefaultMethod();
885 cynara_client_creds clientMethod = getClientDefaultMethod();
887 // exception override
888 if (expectedResult == CYNARA_API_SUCCESS && userMethod == USER_METHOD_GID) {
889 expectedResult = CYNARA_API_METHOD_NOT_SUPPORTED;
892 int ret = cynara_creds_gdbus_get_default(conn.get(), requestedName.c_str(),
893 pClientBuff, pUserBuff, pPidCred);
895 RUNNER_ASSERT_MSG(ret == expectedResult,
896 "cynara_creds_gdbus_get_default returned unexpected result: " << ret <<
897 "; expected: " << expectedResult);
899 if (ret != CYNARA_API_SUCCESS) {
903 if (pClientBuff != nullptr) {
904 switch (clientMethod) {
905 case CLIENT_METHOD_SMACK:
906 RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(),
907 "Client labels don't match, ret = "
908 << *pClientBuff << "; expected = "
909 << peerCredentials.label());
910 g_free(*pClientBuff);
912 case CLIENT_METHOD_PID:
913 RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid),
914 "PIDs don't match, ret = "
915 << *pClientBuff << "; expected = " << pid);
916 g_free(*pClientBuff);
923 if (pUserBuff != nullptr) {
924 switch (userMethod) {
925 case USER_METHOD_UID:
926 RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()),
927 "UIDs don't match, ret = "
929 << "; expected = " << peerCredentials.uid());
937 if (pPidCred != nullptr) {
938 RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = "
940 << "; expected = " << pid);
945 RUNNER_MULTIPROCESS_TEST(tccgd08_gdbus_credentials_default_positive_all)
947 char *clientBuff = nullptr;
948 char *userBuff = nullptr;
951 testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, &pidCred);
954 RUNNER_MULTIPROCESS_TEST(tccgd09_gdbus_credentials_default_positive_partial_single)
956 char *clientBuff = nullptr;
957 char *userBuff = nullptr;
960 testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, nullptr);
961 testGDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, nullptr);
962 testGDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, nullptr, &pidCred);
965 RUNNER_MULTIPROCESS_TEST(tccgd10_gdbus_credentials_default_positive_partial_double)
967 char *clientBuff = nullptr;
968 char *userBuff = nullptr;
971 testGDbusCredsDefault(CYNARA_API_SUCCESS, nullptr, &userBuff, &pidCred);
972 testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, nullptr, &pidCred);
973 testGDbusCredsDefault(CYNARA_API_SUCCESS, &clientBuff, &userBuff, nullptr);
976 RUNNER_MULTIPROCESS_TEST(tccgd11_gdbus_credentials_default_negative_incomplete)
978 testGDbusCredsDefault(CYNARA_API_INVALID_PARAM, nullptr, nullptr, nullptr);
981 void testGDbusCreds(int expectedResult,
982 const cynara_client_creds *pClientMethod, char **pClientBuff,
983 const cynara_user_creds *pUserMethod, char **pUserBuff,
986 // reset variables if set by previous test cases
987 if (pClientBuff != nullptr) *pClientBuff = nullptr;
988 if (pUserBuff != nullptr) *pUserBuff = nullptr;
989 if (pPidCred != nullptr) *pPidCred = -1;
991 gdbusTestTemplate([&] (GDBusConnectionPtr conn, pid_t pid,
992 const std::string &requestedName,
993 const ProcessCredentials &peerCredentials) {
995 int ret = cynara_creds_gdbus_get(conn.get(), requestedName.c_str(),
996 pClientMethod, pClientBuff,
997 pUserMethod, pUserBuff, pPidCred);
999 RUNNER_ASSERT_MSG(ret == expectedResult,
1000 "cynara_creds_gdbus_get returned unexpected result: " << ret <<
1001 "; expected: " << expectedResult);
1003 if (ret != CYNARA_API_SUCCESS) {
1007 if (pClientBuff != nullptr && pClientMethod != nullptr) {
1008 switch (*pClientMethod) {
1009 case CLIENT_METHOD_SMACK:
1010 RUNNER_ASSERT_MSG(*pClientBuff == peerCredentials.label(),
1011 "Client labels don't match, ret = "
1012 << *pClientBuff << "; expected = "
1013 << peerCredentials.label());
1014 g_free(*pClientBuff);
1016 case CLIENT_METHOD_PID:
1017 RUNNER_ASSERT_MSG(*pClientBuff == std::to_string(pid),
1018 "PIDs don't match, ret = "
1019 << *pClientBuff << "; expected = " << pid);
1020 g_free(*pClientBuff);
1027 if (pUserBuff != nullptr && pUserMethod != nullptr) {
1028 switch (*pUserMethod) {
1029 case USER_METHOD_UID:
1030 RUNNER_ASSERT_MSG(*pUserBuff == std::to_string(peerCredentials.uid()),
1031 "UIDs don't match, ret = "
1033 << "; expected = " << peerCredentials.uid());
1041 if (pPidCred != nullptr) {
1042 RUNNER_ASSERT_MSG(*pPidCred == pid, "PIDs don't match, ret = "
1044 << "; expected = " << pid);
1049 RUNNER_MULTIPROCESS_TEST(tccgd12_gdbus_credentials_positive_all)
1051 char *clientBuff = nullptr;
1052 char *userBuff = nullptr;
1055 testGDbusCreds(CYNARA_API_SUCCESS,
1056 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
1057 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
1058 testGDbusCreds(CYNARA_API_SUCCESS,
1059 &G_CLIENT_METHOD_SMACK, &clientBuff,
1060 &G_USER_METHOD_UID, &userBuff, &pidCred);
1061 testGDbusCreds(CYNARA_API_SUCCESS,
1062 &G_CLIENT_METHOD_PID, &clientBuff,
1063 &G_USER_METHOD_UID, &userBuff, &pidCred);
1065 testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
1066 &G_CLIENT_METHOD_SMACK, &clientBuff,
1067 &G_USER_METHOD_GID, &userBuff, &pidCred);
1068 testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
1069 &G_CLIENT_METHOD_PID, &clientBuff,
1070 &G_USER_METHOD_GID, &userBuff, &pidCred);
1073 RUNNER_MULTIPROCESS_TEST(tccgd13_gdbus_credentials_positive_partial_single)
1075 char *clientBuff = nullptr;
1076 char *userBuff = nullptr;
1079 testGDbusCreds(CYNARA_API_SUCCESS,
1080 &G_CLIENT_METHOD_PID, &clientBuff,
1081 &G_USER_METHOD_DEFAULT, nullptr,
1083 testGDbusCreds(CYNARA_API_SUCCESS,
1084 &G_CLIENT_METHOD_SMACK, &clientBuff,
1088 testGDbusCreds(CYNARA_API_SUCCESS,
1089 &G_CLIENT_METHOD_DEFAULT, nullptr,
1090 &G_USER_METHOD_DEFAULT, &userBuff,
1092 testGDbusCreds(CYNARA_API_SUCCESS,
1094 &G_USER_METHOD_DEFAULT, &userBuff,
1097 testGDbusCreds(CYNARA_API_SUCCESS,
1099 &G_USER_METHOD_DEFAULT, nullptr,
1101 testGDbusCreds(CYNARA_API_SUCCESS,
1102 &G_CLIENT_METHOD_DEFAULT, nullptr,
1105 testGDbusCreds(CYNARA_API_SUCCESS,
1111 RUNNER_MULTIPROCESS_TEST(tccgd14_gdbus_credentials_positive_partial_double)
1113 char *clientBuff = nullptr;
1114 char *userBuff = nullptr;
1117 testGDbusCreds(CYNARA_API_SUCCESS,
1118 &G_CLIENT_METHOD_DEFAULT, nullptr,
1119 &G_USER_METHOD_UID, &userBuff, &pidCred);
1120 testGDbusCreds(CYNARA_API_SUCCESS,
1122 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
1124 testGDbusCreds(CYNARA_API_SUCCESS,
1125 &G_CLIENT_METHOD_PID, &clientBuff,
1126 &G_USER_METHOD_DEFAULT, nullptr, &pidCred);
1127 testGDbusCreds(CYNARA_API_SUCCESS,
1128 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
1129 nullptr, nullptr, &pidCred);
1131 testGDbusCreds(CYNARA_API_SUCCESS,
1132 &G_CLIENT_METHOD_SMACK, &clientBuff,
1133 &G_USER_METHOD_UID, &userBuff, nullptr);
1136 RUNNER_MULTIPROCESS_TEST(tccgd14_gdbus_credentials_negative_incomplete)
1138 char *clientBuff = nullptr;
1139 char *userBuff = nullptr;
1142 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1143 nullptr, &clientBuff,
1144 &G_USER_METHOD_UID, &userBuff, &pidCred);
1145 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1146 nullptr, &clientBuff,
1147 &G_USER_METHOD_UID, nullptr, &pidCred);
1148 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1149 nullptr, &clientBuff,
1150 nullptr, nullptr, &pidCred);
1152 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1153 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
1154 nullptr, &userBuff, &pidCred);
1155 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1156 &G_CLIENT_METHOD_DEFAULT, nullptr,
1157 nullptr, &userBuff, &pidCred);
1158 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1160 nullptr, &userBuff, &pidCred);
1162 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1163 &G_CLIENT_METHOD_DEFAULT, nullptr,
1164 &G_USER_METHOD_UID, nullptr, nullptr);
1165 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1166 &G_CLIENT_METHOD_DEFAULT, nullptr,
1167 nullptr, nullptr, nullptr);
1168 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1170 &G_USER_METHOD_DEFAULT, nullptr, nullptr);
1171 testGDbusCreds(CYNARA_API_INVALID_PARAM,
1173 nullptr, nullptr, nullptr);
1176 RUNNER_MULTIPROCESS_TEST(tccgd14_gdbus_credentials_negative_incorrect)
1178 char *clientBuff = nullptr;
1179 char *userBuff = nullptr;
1182 testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
1183 &G_CLIENT_METHOD_LOW, &clientBuff,
1184 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
1185 testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
1186 &G_CLIENT_METHOD_HIGH, &clientBuff,
1187 &G_USER_METHOD_DEFAULT, &userBuff, &pidCred);
1188 testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
1189 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
1190 &G_USER_METHOD_LOW, &userBuff, &pidCred);
1191 testGDbusCreds(CYNARA_API_METHOD_NOT_SUPPORTED,
1192 &G_CLIENT_METHOD_DEFAULT, &clientBuff,
1193 &G_USER_METHOD_HIGH, &userBuff, &pidCred);
1197 //================================SdBus===============================================
1199 SdBusConnectionPtr createSdBusConnection(const std::string &requestedName) {
1202 int r = sd_bus_default_system(&bus);
1203 RUNNER_ASSERT_MSG(r >= 0, "Failed to connect do system bus: %s" << strerror(-r));
1205 if (requestedName.empty() == false) {
1206 r = sd_bus_request_name(bus, requestedName.c_str(), SD_BUS_NAME_REPLACE_EXISTING);
1207 RUNNER_ASSERT_MSG(r >= 0, "Error in sd_bus_request_name");
1210 SdBusConnectionPtr ret(bus, [] (sd_bus *busConnection) {
1211 sd_bus_unref(busConnection);
1217 typedef std::function<void(SdBusConnectionPtr conn, pid_t pid, const std::string &requestedName,
1218 const ProcessCredentials &peerCredentials)> SdBusAssertionFn;
1220 void sdBusTestTemplate(SdBusAssertionFn assertion) {
1221 std::string requestedName = "tests.dbus.cynara";
1222 const ProcessCredentials peerCredentials;
1224 SynchronizationPipe pipe;
1225 pid_t pid = runInChild(std::bind(dbusServer, std::ref(pipe), std::cref(requestedName),
1226 std::cref(peerCredentials)));
1228 pipe.claimParentEp();
1231 auto conn = createSdBusConnection("");
1232 assertion(std::move(conn), pid, requestedName, peerCredentials);
1237 RUNNER_TEST_GROUP_INIT(cynara_creds_sd_bus)
1239 void testSdBusClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
1240 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t pid,
1241 const std::string &requestedName,
1242 const ProcessCredentials &) {
1243 CStringPtr clientPidStr(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
1244 requestedName.c_str(), method, CYNARA_API_SUCCESS));
1245 pid_t clientPid = std::stoi(clientPidStr.get());
1246 RUNNER_ASSERT_MSG(pid == clientPid, "PIDs don't match ret = " << clientPid
1247 << "; expected = " << pid);
1251 void testSdBusClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
1252 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
1253 const std::string &requestedName,
1254 const ProcessCredentials &peerCredentials) {
1255 CStringPtr label(CynaraHelperCredentials::sdBusGetClient(std::move(conn),
1256 requestedName.c_str(), method, CYNARA_API_SUCCESS));
1257 RUNNER_ASSERT_MSG(peerCredentials.label() == label.get(),
1258 "Labels don't match ret = " << label.get()
1259 << "; expected = " << peerCredentials.label());
1263 RUNNER_MULTIPROCESS_TEST(tccsd01_sd_bus_credentials_client_pid)
1265 testSdBusClientPid();
1268 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd02_sd_bus_credentials_client_smack)
1270 testSdBusClientSmack();
1273 RUNNER_MULTIPROCESS_TEST_SMACK(tccsd03_sd_bus_credentials_client_default)
1275 auto method = getClientDefaultMethod();
1277 case CLIENT_METHOD_SMACK:
1278 testSdBusClientSmack(CLIENT_METHOD_DEFAULT);
1280 case CLIENT_METHOD_PID:
1281 testSdBusClientPid(CLIENT_METHOD_DEFAULT);
1284 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value "
1289 void testSdBusUserUid(cynara_user_creds method = USER_METHOD_UID) {
1290 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
1291 const std::string &requestedName,
1292 const ProcessCredentials &peerCredentials) {
1293 CStringPtr uidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
1294 requestedName.c_str(), method, CYNARA_API_SUCCESS));
1295 uid_t uid = std::stoul(uidStr.get());
1296 RUNNER_ASSERT_MSG(peerCredentials.uid() == uid, "UIDs don't match ret = " << uid
1297 << "; expected = "<< peerCredentials.uid());
1301 void testSdBusUserGid(cynara_user_creds method = USER_METHOD_GID) {
1302 sdBusTestTemplate([method] (SdBusConnectionPtr conn, pid_t,
1303 const std::string &requestedName,
1304 const ProcessCredentials &peerCredentials) {
1305 CStringPtr gidStr(CynaraHelperCredentials::sdBusGetUser(std::move(conn),
1306 requestedName.c_str(), method, CYNARA_API_SUCCESS));
1307 gid_t gid = std::stoul(gidStr.get());
1308 RUNNER_ASSERT_MSG(peerCredentials.gid() == gid, "GIDs don't match ret = " << gid
1309 << "; expected = "<< peerCredentials.gid());
1313 RUNNER_MULTIPROCESS_TEST(tccsd04_sd_bus_credentials_user_uid)
1318 RUNNER_MULTIPROCESS_TEST(tccsd05_sd_bus_credentials_user_gid)
1323 RUNNER_MULTIPROCESS_TEST(tccsd06_sd_bus_credentials_user_default)
1325 auto method = getUserDefaultMethod();
1327 case USER_METHOD_UID:
1328 testSdBusUserUid(USER_METHOD_DEFAULT);
1330 case USER_METHOD_GID:
1331 testSdBusUserGid(USER_METHOD_DEFAULT);
1334 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value "
1339 RUNNER_TEST_SMACK(tccd07_sd_bus_credentials_pid) {
1340 sdBusTestTemplate([] (SdBusConnectionPtr conn, pid_t expectedPid,
1341 const std::string &requestedName, const ProcessCredentials &) {
1342 auto helperPid = CynaraHelperCredentials::sdBusGetPid(std::move(conn),
1343 requestedName.c_str(), CYNARA_API_SUCCESS);
1344 RUNNER_ASSERT_MSG(helperPid == expectedPid, "PIDs don't match ret = " << helperPid
1345 << "; expected = " << expectedPid);
1350 RUNNER_TEST_GROUP_INIT(cynara_creds_self)
1352 void testCredsClientSelf(cynara_client_creds method, const std::string &expected) {
1354 int ret = cynara_creds_self_get_client(method, &client);
1355 CStringPtr clientPtr(client);
1356 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_client failed with " << ret);
1357 RUNNER_ASSERT_MSG(expected == client, "expected client doesn't match, expected: " << expected
1358 << ", got : " << client);
1361 void testCredsUserSelf(cynara_user_creds method, const std::string &expected) {
1363 int ret = cynara_creds_self_get_user(method, &user);
1364 CStringPtr clientPtr(user);
1365 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, "cynara_creds_self_get_user failed with " << ret);
1366 RUNNER_ASSERT_MSG(expected == user, "expected user doesn't match, expected: " << expected
1367 << ", got : " << user);
1370 void testSelfClientSmack(cynara_client_creds method = CLIENT_METHOD_SMACK) {
1371 std::string label = "test-label";
1372 ScopedProcessLabel spl(label, false);
1373 testCredsClientSelf(method, label);
1376 void testSelfClientPid(cynara_client_creds method = CLIENT_METHOD_PID) {
1377 pid_t pid = getpid();
1378 testCredsClientSelf(method, std::to_string(pid));
1381 void testSelfUserUid(cynara_user_creds method = USER_METHOD_UID) {
1382 uid_t uid = getuid();
1383 testCredsUserSelf(method, std::to_string(uid));
1386 void testSelfUserGid(cynara_user_creds method = USER_METHOD_GID) {
1387 gid_t gid = getgid();
1388 testCredsUserSelf(method, std::to_string(gid));
1391 RUNNER_CHILD_TEST_SMACK(tccsl01_self_credentials_client_smack) {
1392 testSelfClientSmack();
1395 RUNNER_CHILD_TEST_SMACK(tccsl02_self_credentials_client_pid) {
1396 testSelfClientPid();
1399 RUNNER_CHILD_TEST_SMACK(tccsl03_self_credentials_user_uid) {
1403 RUNNER_CHILD_TEST_SMACK(tccsl04_self_credentials_user_gid) {
1407 RUNNER_CHILD_TEST_SMACK(tccsl05_self_credentials_client_default) {
1408 auto method = getClientDefaultMethod();
1410 case CLIENT_METHOD_SMACK:
1411 testSelfClientSmack(CLIENT_METHOD_DEFAULT);
1413 case CLIENT_METHOD_PID:
1414 testSelfClientPid(CLIENT_METHOD_DEFAULT);
1417 RUNNER_FAIL_MSG("cynara_creds_get_default_client_method returned unexpected value " << method);
1421 RUNNER_CHILD_TEST_SMACK(tccsl06_self_credentials_user_default) {
1422 auto method = getUserDefaultMethod();
1424 case USER_METHOD_UID:
1425 testSelfUserUid(USER_METHOD_DEFAULT);
1427 case USER_METHOD_GID:
1428 testSelfUserGid(USER_METHOD_DEFAULT);
1431 RUNNER_FAIL_MSG("cynara_creds_get_default_user_method returned unexpected value " << method);