From: Daniel Palmer <daniel@thingy.jp>
To: w@1wt.eu, linux@weissschuh.net
Cc: linux-kernel@vger.kernel.org, Daniel Palmer <daniel@thingy.jp>
Subject: [PATCH 2/2] selftests/nolibc: Add basic test for sendfile()
Date: Fri, 28 Aug 2026 19:32:05 +0900 [thread overview]
Message-ID: <20260828103205.384589-3-daniel@thingy.jp> (raw)
In-Reply-To: <20260828103205.384589-1-daniel@thingy.jp>
Basic smoke test for sendfile().
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
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
next prev parent reply other threads:[~2026-08-28 10:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-28 10:32 [PATCH 0/2] nolibc: Add sendfile() Daniel Palmer
2026-08-28 10:32 ` [PATCH 1/2] tools/nolibc: " Daniel Palmer
2026-08-28 10:32 ` Daniel Palmer [this message]
2026-08-28 16:52 ` [PATCH 0/2] nolibc: " Thomas Weißschuh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260828103205.384589-3-daniel@thingy.jp \
--to=daniel@thingy.jp \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=w@1wt.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®