diff --git a/userspace/libsinsp/test/events_net.ut.cpp b/userspace/libsinsp/test/events_net.ut.cpp index 2966c4fed7b..400bf355f6c 100644 --- a/userspace/libsinsp/test/events_net.ut.cpp +++ b/userspace/libsinsp/test/events_net.ut.cpp @@ -615,3 +615,43 @@ TEST_F(sinsp_with_test_input, net_connect_enter_event_is_missing_wo_fd_param_exi fdinfo = evt->get_fd_info(); ASSERT_EQ(fdinfo, nullptr); } + +#ifndef _WIN32 +TEST_F(sinsp_with_test_input, net_recvmsg_scm_rights) +{ + add_default_init_thread(); + open_inspector(SCAP_MODE_LIVE); + + auto tinfo = m_inspector.m_thread_manager->get_thread_ref(1); + ASSERT_NE(tinfo, nullptr); + uint64_t num_fds = 0; + auto count = [&num_fds](int64_t fd, const sinsp_fdinfo_t& fdinfo) { + num_fds++; + return true; + }; + + // Clear what we got from initial proc scan + tinfo->m_fdtable.clear(); + + sockaddr_un unix_socket = test_utils::fill_sockaddr_un("/tmp/test"); + std::vector socktuple = test_utils::pack_socktuple(reinterpret_cast(&unix_socket), reinterpret_cast(&unix_socket)); + char cmsg_buf[80]; + struct cmsghdr* cmsg = (struct cmsghdr*)cmsg_buf; + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + cmsg->cmsg_type = SCM_RIGHTS; + + // Create a socket where we can recv the message + add_event_advance_ts(increasing_ts(), 1, PPME_SOCKET_SOCKET_E, 3, (uint32_t) PPM_AF_UNIX, (uint32_t) SOCK_STREAM, 0); + add_event_advance_ts(increasing_ts(), 1, PPME_SOCKET_SOCKET_X, 1, 1); + + // Trigger a proc scan for init process + add_event_advance_ts(increasing_ts(), 1, PPME_SOCKET_RECVMSG_E, 1, 1); + add_event_advance_ts(increasing_ts(), 1, PPME_SOCKET_RECVMSG_X, 5, return_value, 5, scap_const_sized_buffer{"test", 5}, scap_const_sized_buffer{socktuple.data(), socktuple.size()}, scap_const_sized_buffer{cmsg_buf, sizeof(cmsg_buf)}); + + // Count the file descriptors again and check that there are some more + // We can't make too many assumptions on the exact number of file descriptors we'll find + tinfo = m_inspector.m_thread_manager->get_thread_ref(1); + tinfo->loop_fds(count); + ASSERT_GT(num_fds, 1); +} +#endif \ No newline at end of file diff --git a/userspace/libsinsp/test/test_utils.cpp b/userspace/libsinsp/test/test_utils.cpp index e401727b00c..3480f829ebd 100644 --- a/userspace/libsinsp/test/test_utils.cpp +++ b/userspace/libsinsp/test/test_utils.cpp @@ -20,16 +20,13 @@ limitations under the License. #include -#if defined(__linux__) -#include -#else -#if !defined(_WIN32) +#ifndef _WIN32 #include -# endif //_WIN32 +#endif //_WIN32 + #ifndef UNIX_PATH_MAX #define UNIX_PATH_MAX 108 -#endif -#endif +#endif //UNIX_PATH_MAX #if !defined(_WIN32) #include @@ -61,6 +58,15 @@ struct sockaddr_in6 fill_sockaddr_in6(int32_t ipv6_port, const char* ipv6_string inet_pton(AF_INET6, ipv6_string, &(sockaddr.sin6_addr)); return sockaddr; } + +struct sockaddr_un fill_sockaddr_un(const char* path) +{ + struct sockaddr_un sockaddr; + memset(&sockaddr, 0, sizeof(sockaddr)); + sockaddr.sun_family = AF_UNIX; + strncpy(sockaddr.sun_path, path, sizeof(sockaddr.sun_path)); + return sockaddr; +} #endif //_WIN32 std::string to_null_delimited(const std::vector list) diff --git a/userspace/libsinsp/test/test_utils.h b/userspace/libsinsp/test/test_utils.h index 10d8ea81e86..9aa6524be65 100644 --- a/userspace/libsinsp/test/test_utils.h +++ b/userspace/libsinsp/test/test_utils.h @@ -24,6 +24,7 @@ limitations under the License. #include #if !defined(_WIN32) #include +#include #endif //_WIN32 #include @@ -90,8 +91,9 @@ template std::set unordered_set_to_ordered(std::unordered_set unordered_set); #if !defined(_WIN32) -struct sockaddr_in fill_sockaddr_in(int32_t ipv4_port, const char* ipv4_string); -struct sockaddr_in6 fill_sockaddr_in6(int32_t ipv6_port, const char* ipv6_string); +sockaddr_in fill_sockaddr_in(int32_t ipv4_port, const char* ipv4_string); +sockaddr_in6 fill_sockaddr_in6(int32_t ipv6_port, const char* ipv6_string); +sockaddr_un fill_sockaddr_un(const char* path); std::vector pack_sockaddr(sockaddr *sa); std::vector pack_socktuple(sockaddr *src, sockaddr *dest); #endif //_WIN32