From f878a895d74222e6ff9adfacacfdad066012182b Mon Sep 17 00:00:00 2001 From: Marco Casaroli Date: Tue, 17 Nov 2026 10:47:42 +0100 Subject: [PATCH 1/2] testing/testsuites: Add blank lines after declarations in fs_sendfile_test.c. nxstyle reports "Missing blank line after declarations" in test_nuttx_fs_sendfile01() and test_nuttx_fs_sendfile02(). Add the two blank lines. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli --- testing/testsuites/kernel/fs/cases/fs_sendfile_test.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c b/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c index 30337e3ae64..d1e2afb91a2 100644 --- a/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c +++ b/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c @@ -77,6 +77,7 @@ void test_nuttx_fs_sendfile01(FAR void **state) int fd_i; int test_flag = 0; struct stat f_stat; + set_test(); /* open file readonly */ @@ -156,6 +157,7 @@ void test_nuttx_fs_sendfile02(FAR void **state) int fd_i; off_t offset; struct stat f_stat; + set_test(); /* open file readonly */ From ab8f959445f11e5c65a2a0b1be3ef377a07686fb Mon Sep 17 00:00:00 2001 From: Marco Casaroli Date: Tue, 17 Nov 2026 10:47:42 +0100 Subject: [PATCH 2/2] testing/testsuites: Add sendfile() cases with a nonblocking pipe. test_nuttx_fs_sendfile03 and test_nuttx_fs_sendfile04 send a file of 8 KiB to a nonblocking pipe that fills up, with and without an offset. Each call that sends n bytes must move the offset (or the file position) by n. The call on a full pipe must fail with EAGAIN and must not move it. The pipe must get each byte of the file once, in order. The cases need a pipe: they are built only with CONFIG_PIPES and a CONFIG_DEV_PIPE_SIZE larger than 0, as pipe2(). Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli --- .../kernel/fs/cases/fs_sendfile_test.c | 188 ++++++++++++++++++ testing/testsuites/kernel/fs/cmocka_fs_test.c | 8 + testing/testsuites/kernel/fs/include/fstest.h | 2 + 3 files changed, 198 insertions(+) diff --git a/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c b/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c index d1e2afb91a2..107b0d39580 100644 --- a/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c +++ b/testing/testsuites/kernel/fs/cases/fs_sendfile_test.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,9 @@ #define O_FILE "outputFile" #define I_FILE1 "inputFile1" #define I_FILE2 "inputFile2" +#define P_FILE "pipeInputFile" +#define P_SIZE 8192 +#define P_BUFSIZE 256 static void set_test(void) { @@ -58,6 +62,160 @@ static void set_test(void) close(fd); } +#if defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 0 + +/**************************************************************************** + * Name: sendfile_pipe_send + * + * Description: + * Call sendfile() and check that the offset (or the file position if + * offset is NULL) moves by the count that it returns. + * + * Returned Value: + * The count, 0 for EAGAIN, or -1 on a failure. + ****************************************************************************/ + +static ssize_t sendfile_pipe_send(int outfd, int infd, FAR off_t *offset, + size_t count) +{ + off_t before = offset != NULL ? *offset : lseek(infd, 0, SEEK_CUR); + off_t after; + ssize_t ret; + + ret = sendfile(outfd, infd, offset, count); + if (ret < 0 && errno == EAGAIN) + { + ret = 0; + } + else if (ret <= 0) + { + syslog(LOG_ERR, "sendfile fail, ret %zd, errno %d\n", ret, errno); + return -1; + } + + after = offset != NULL ? *offset : lseek(infd, 0, SEEK_CUR); + if (after - before != ret) + { + syslog(LOG_ERR, "sendfile sent %zd but the offset moved %" PRIdOFF + "\n", ret, after - before); + return -1; + } + + return ret; +} + +/**************************************************************************** + * Name: sendfile_pipe + * + * Description: + * Send a file to a nonblocking pipe that fills up. Each byte must + * arrive once, and the count that sendfile() returns must match the + * offset and the bytes in the pipe. + * + * Returned Value: + * 0 if all checks pass, else 1. + ****************************************************************************/ + +static int sendfile_pipe(FAR off_t *offset) +{ + unsigned char buf[P_BUFSIZE]; + size_t received = 0; + size_t sent = 0; + ssize_t ret; + ssize_t n; + ssize_t i; + int fail = 0; + int fds[2]; + int fd; + + fd = open(P_FILE, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0777); + if (fd < 0) + { + syslog(LOG_ERR, "open %s fail, errno %d\n", P_FILE, errno); + return 1; + } + + for (n = 0; n < P_SIZE; n += P_BUFSIZE) + { + for (i = 0; i < P_BUFSIZE; i++) + { + buf[i] = (n + i) % 251; + } + + if (write(fd, buf, P_BUFSIZE) != P_BUFSIZE) + { + syslog(LOG_ERR, "write %s fail, errno %d\n", P_FILE, errno); + close(fd); + return 1; + } + } + + if (lseek(fd, 0, SEEK_SET) != 0 || pipe2(fds, O_CLOEXEC | O_NONBLOCK) < 0) + { + syslog(LOG_ERR, "lseek or pipe2 fail, errno %d\n", errno); + close(fd); + return 1; + } + + while (sent < P_SIZE) + { + /* The pipe is empty: the call must send some bytes */ + + ret = sendfile_pipe_send(fds[1], fd, offset, P_SIZE - sent); + if (ret <= 0) + { + fail = 1; + break; + } + + sent += ret; + + /* The pipe is full now: the next call must send nothing */ + + if (sent < P_SIZE && + sendfile_pipe_send(fds[1], fd, offset, P_SIZE - sent) != 0) + { + fail = 1; + break; + } + + while ((n = read(fds[0], buf, P_BUFSIZE)) > 0) + { + for (i = 0; i < n; i++) + { + if (buf[i] != (received + i) % 251) + { + fail = 1; + } + } + + received += n; + } + + if (received != sent) + { + syslog(LOG_ERR, "the pipe got %zu bytes, sendfile sent %zu\n", + received, sent); + fail = 1; + break; + } + } + + /* With an offset, sendfile() must not move the file position */ + + if (offset != NULL && lseek(fd, 0, SEEK_CUR) != 0) + { + syslog(LOG_ERR, "sendfile moved the file position\n"); + fail = 1; + } + + close(fds[0]); + close(fds[1]); + close(fd); + return fail; +} +#endif + /**************************************************************************** * Name: sendfile * Example description: @@ -201,3 +359,33 @@ void test_nuttx_fs_sendfile02(FAR void **state) assert_int_equal(offset, f_stat.st_size); } + +#if defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 0 + +/**************************************************************************** + * Name: sendfile + * Example description: + * 1.Test sendfile with an offset to a nonblocking pipe that fills up + * Expect results: TEST PASSED + ****************************************************************************/ + +void test_nuttx_fs_sendfile03(FAR void **state) +{ + off_t offset = 0; + + assert_int_equal(sendfile_pipe(&offset), 0); + assert_int_equal(offset, P_SIZE); +} + +/**************************************************************************** + * Name: sendfile + * Example description: + * 1.Test sendfile without an offset to a nonblocking pipe that fills up + * Expect results: TEST PASSED + ****************************************************************************/ + +void test_nuttx_fs_sendfile04(FAR void **state) +{ + assert_int_equal(sendfile_pipe(NULL), 0); +} +#endif diff --git a/testing/testsuites/kernel/fs/cmocka_fs_test.c b/testing/testsuites/kernel/fs/cmocka_fs_test.c index 2b4101246c6..ce991ea250c 100644 --- a/testing/testsuites/kernel/fs/cmocka_fs_test.c +++ b/testing/testsuites/kernel/fs/cmocka_fs_test.c @@ -178,6 +178,14 @@ int main(int argc, char *argv[]) cmocka_unit_test_setup_teardown(test_nuttx_fs_sendfile02, test_nuttx_fs_test_group_setup, test_nuttx_fs_test_group_teardown), +#if defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 0 + cmocka_unit_test_setup_teardown(test_nuttx_fs_sendfile03, + test_nuttx_fs_test_group_setup, + test_nuttx_fs_test_group_teardown), + cmocka_unit_test_setup_teardown(test_nuttx_fs_sendfile04, + test_nuttx_fs_test_group_setup, + test_nuttx_fs_test_group_teardown), +#endif cmocka_unit_test_setup_teardown(test_nuttx_fs_stream01, test_nuttx_fs_test_group_setup, test_nuttx_fs_test_group_teardown), diff --git a/testing/testsuites/kernel/fs/include/fstest.h b/testing/testsuites/kernel/fs/include/fstest.h index 050b587c85b..11d26f25181 100644 --- a/testing/testsuites/kernel/fs/include/fstest.h +++ b/testing/testsuites/kernel/fs/include/fstest.h @@ -225,6 +225,8 @@ void test_nuttx_fs_append01(FAR void **state); void test_nuttx_fs_sendfile01(FAR void **state); void test_nuttx_fs_sendfile02(FAR void **state); +void test_nuttx_fs_sendfile03(FAR void **state); +void test_nuttx_fs_sendfile04(FAR void **state); /* cases/fs_stream_test.c * ************************************************/