X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=tests%2Funit%2Ftest_utils_compat_poll.c;h=436af5f6f24e1c57ef77fbbc973205cb86d9441c;hb=35a9ac4170f36547aa2ae7c19ccbb7bbb0bcf71b;hp=0cb692abccb5d0186115dcabd1a09d12accb4b43;hpb=f7f52067bec1aab9a5660075f851a7ccea1d39fe;p=lttng-tools.git diff --git a/tests/unit/test_utils_compat_poll.c b/tests/unit/test_utils_compat_poll.c index 0cb692abc..436af5f6f 100644 --- a/tests/unit/test_utils_compat_poll.c +++ b/tests/unit/test_utils_compat_poll.c @@ -5,14 +5,8 @@ * * Copyright (C) 2019 Yannick Lamarre * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by as - * published by the Free Software Foundation; only version 2 of the License. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. */ #include @@ -27,6 +21,8 @@ #include #include +#include +#include /* Verification without trashing test order in the child process */ #define childok(e, test, ...) do { \ @@ -49,9 +45,9 @@ int lttng_opt_mi; #define MAGIC_VALUE ((char) 0x5A) #ifdef HAVE_EPOLL -#define NUM_TESTS 47 +#define NUM_TESTS 48 #else -#define NUM_TESTS 46 +#define NUM_TESTS 47 #endif #ifdef HAVE_EPOLL @@ -61,6 +57,7 @@ int lttng_opt_mi; #define CLOE_VALUE FD_CLOEXEC #endif +static void test_epoll_compat(void) { /* @@ -186,6 +183,121 @@ static void test_mod_wait(void) } } +static void destroy_pipe(void *pipe) +{ + lttng_pipe_destroy(pipe); +} + +static int run_active_set_combination(unsigned int fd_count, + unsigned int active_fds_mask) +{ + int ret = 0; + unsigned int i; + const unsigned int active_fds_count = __builtin_popcount(active_fds_mask); + struct lttng_poll_event poll_events; + struct lttng_dynamic_pointer_array pipes; + struct lttng_pipe *pipe = NULL; + + lttng_poll_init(&poll_events); + lttng_dynamic_pointer_array_init(&pipes, destroy_pipe); + + ret = lttng_poll_create(&poll_events, fd_count, 0); + if (ret) { + diag("Failed to create poll set for %u file descriptors", + fd_count); + goto end; + } + + for (i = 0; i < fd_count; i++) { + pipe = lttng_pipe_open(0); + + if (!pipe) { + diag("Failed to allocate pipe"); + ret = -1; + goto end; + } + + ret = lttng_poll_add(&poll_events, lttng_pipe_get_readfd(pipe), + LPOLLIN); + if (ret) { + diag("Failed to add file descriptor to poll set"); + ret = -1; + goto end; + } + + ret = lttng_dynamic_pointer_array_add_pointer(&pipes, pipe); + if (ret) { + diag("Failed to add pipe to pipes array"); + ret = -1; + goto end; + } + + /* Ownership transferred to the pointer array. */ + pipe = NULL; + } + + /* Write one byte for all active fds that should be active. */ + for (i = 0; i < fd_count; i++) { + struct lttng_pipe *pipe; + + /* Should this fd be made active? */ + if (!(active_fds_mask & (1 << i))) { + continue; + } + + pipe = lttng_dynamic_pointer_array_get_pointer(&pipes, i); + + ret = lttng_pipe_write(pipe, &(char) {'a'}, sizeof(char)); + if (ret != sizeof(char)) { + diag("Failed to write to pipe"); + ret = -1; + goto end; + } + } + + ret = lttng_poll_wait(&poll_events, 0); + if (ret != active_fds_count) { + diag("lttng_poll_wait returned %d, expected %u active file descriptors", + ret, active_fds_count); + ret = -1; + goto end; + } else { + /* Success! */ + ret = 0; + } + +end: + lttng_dynamic_pointer_array_reset(&pipes); + lttng_poll_clean(&poll_events); + lttng_pipe_destroy(pipe); + return ret; +} + +static void test_active_set_combinations(unsigned int fd_count) +{ + unsigned int i, all_active_mask = 0; + + /* Do you really want to test more than 4,294,967,295 combinations? */ + assert(fd_count <= 32); + + for (i = 0; i < fd_count; i++) { + all_active_mask |= (1 << i); + } + + for (i = 0; i <= all_active_mask; i++) { + const int ret = run_active_set_combination(fd_count, i); + + if (ret) { + goto fail; + } + } + + pass("Test all combinations of active file descriptors for %u file descriptors", fd_count); + return; +fail: + fail("Test all combinations of active file descriptors for %u file descriptors", fd_count); +} + static void test_func_def(void) { #ifdef LTTNG_POLL_GETFD @@ -230,5 +342,6 @@ int main(void) test_alloc(); test_add_del(); test_mod_wait(); + test_active_set_combinations(8); return exit_status(); }