Skip to content

Commit

Permalink
new(userspace/libsinsp/test): add a test to trigger the proc scan on …
Browse files Browse the repository at this point in the history
…recvmsg

Signed-off-by: Lorenzo Susini <[email protected]>
  • Loading branch information
loresuso committed Oct 13, 2023
1 parent 73ad7c5 commit c088d34
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
40 changes: 40 additions & 0 deletions userspace/libsinsp/test/events_net.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> socktuple = test_utils::pack_socktuple(reinterpret_cast<sockaddr*>(&unix_socket), reinterpret_cast<sockaddr*>(&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
20 changes: 13 additions & 7 deletions userspace/libsinsp/test/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ limitations under the License.

#include <cstring>

#if defined(__linux__)
#include <linux/un.h>
#else
#if !defined(_WIN32)
#ifndef _WIN32
#include <sys/un.h>
# endif //_WIN32
#endif //_WIN32

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
#endif
#endif //UNIX_PATH_MAX

#if !defined(_WIN32)
#include <arpa/inet.h>
Expand Down Expand Up @@ -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<std::string> list)
Expand Down
6 changes: 4 additions & 2 deletions userspace/libsinsp/test/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include <unordered_set>
#if !defined(_WIN32)
#include <netinet/in.h>
#include <sys/un.h>
#endif //_WIN32
#include <event_stats.h>

Expand Down Expand Up @@ -90,8 +91,9 @@ template<typename T>
std::set<T> unordered_set_to_ordered(std::unordered_set<T> 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<uint8_t> pack_sockaddr(sockaddr *sa);
std::vector<uint8_t> pack_socktuple(sockaddr *src, sockaddr *dest);
#endif //_WIN32
Expand Down

0 comments on commit c088d34

Please sign in to comment.