[PATCH 2/2] selftests/nolibc: Add basic test for sendfile()

From: Daniel Palmer

Date: Fri Aug 28 2026 - 06:33:35 EST


Basic smoke test for sendfile().

Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
---
tools/testing/selftests/nolibc/nolibc-test.c | 70 ++++++++++++++++++++
1 file changed, 70 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index ed860b0a15a1..48e1183a00bb 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1563,6 +1563,75 @@ int test_large_file(void)
return ret;
}

+int test_sendfile(void)
+{
+ char data_in[] = "This is some data";
+ const size_t data_sz = sizeof(data_in);
+ char data_out[data_sz];
+ int in_fd, out_fd;
+ int ret = 0;
+
+ /* Create two tmp files */
+ in_fd = open("/tmp", O_TMPFILE | O_RDWR, 0644);
+ if (in_fd == -1)
+ return __LINE__;
+
+ out_fd = open("/tmp", O_TMPFILE | O_RDWR, 0644);
+ if (out_fd == -1) {
+ ret = __LINE__;
+ goto close_in;
+ }
+
+ /* Populate the "in" file */
+ ret = write(in_fd, data_in, data_sz);
+ if (ret != data_sz) {
+ ret = __LINE__;
+ goto close_out;
+ }
+
+ /* Rewind the "in" file */
+ if (lseek(in_fd, 0, SEEK_SET)) {
+ ret = __LINE__;
+ goto close_out;
+ }
+
+ /* Use sendfile() to copy "in" to "out" */
+ ret = sendfile(out_fd, in_fd, NULL, data_sz);
+ if (ret != data_sz) {
+ ret = __LINE__;
+ goto close_out;
+ }
+
+ /* Rewind the "out" file */
+ if (lseek(out_fd, 0, SEEK_SET)) {
+ ret = __LINE__;
+ goto close_out;
+ }
+
+ /* Read back the transferred data */
+ ret = read(out_fd, data_out, data_sz);
+ if (ret != data_sz) {
+ ret = __LINE__;
+ goto close_out;
+ }
+
+ /* Check we have the same data in both files */
+ if (memcmp(data_out, data_in, data_sz)) {
+ ret = __LINE__;
+ goto close_out;
+ }
+
+ /* Test passed */
+ ret = 0;
+
+close_out:
+ close(out_fd);
+close_in:
+ close(in_fd);
+
+ return ret;
+}
+
/* Run syscall tests between IDs <min> and <max>.
* Return 0 on success, non-zero on failure.
*/
@@ -1684,6 +1753,7 @@ int run_syscall(int min, int max)
CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break;
CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
+ CASE_TEST(sendfile); EXPECT_SYSZR(1, test_sendfile()); break;
CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
CASE_TEST(stat_rdev); EXPECT_SYSZR(1, ({ int ret = stat("/dev/null", &stat_buf); ret ?: stat_buf.st_rdev != makedev(1, 3); })); break;
--
2.53.0