Message ID | 9221753abe0509ef5cbb474a31873012e0e40706.1690733545.git.tanyuan@tinylab.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp1826634vqg; Sun, 30 Jul 2023 23:19:46 -0700 (PDT) X-Google-Smtp-Source: APBJJlH6/WtWfJEyMDk722oZRL8Fa4MQFwIzOowLA0E3mvd93E7iEXEp5Qq5wOr1S8mxkn9q6ubR X-Received: by 2002:a05:6a20:5483:b0:13a:cfdf:d7a1 with SMTP id i3-20020a056a20548300b0013acfdfd7a1mr10708689pzk.2.1690784385691; Sun, 30 Jul 2023 23:19:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690784385; cv=none; d=google.com; s=arc-20160816; b=MvBEU6ZlF1xOHeNBa/1AKau8k46zohzRLAFWzAgwUwR/bhh+5XCsN86XkPxD4qKXQD DxQo7f8WdQxRX1zj/wcFr7jy2stzJJfncxj56KwIIw6BXQ7Ui1NxfJI6UPXV1KwYAHtV zCHupEalljueCaXVOmR5wRkPK08r2i0RD/Qk6taJ5B4d/p5mHHBF7B47GK7CrbHVRdL7 r+Ri2vsM3VklUaTwzO3hJBaxQAY+hTWLyq1NoddOKmcxeTvqR1TUdwKgt4RH9s9Gcnpv pT0Y1QleR4OoD0olxFHCnHA0lY80MnBqDzRIUJ6MRh+S9bZtRKd4VuQirZ+uF7CZn1S6 212g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=JMaPzOGjgfVkSjQGLxdtHl4jNZJbK8eVXGPANJZlJeY=; fh=8heE8X7c/6KBQVGdiUUfEdY5Aql55kcYv0bNCPp9yaU=; b=G/F4NAO8nOGd7LE54QxCT6IfqSSot7r91u+1jDdD5ld0v1CCTqE79h3a6GSfuf7glq bM0dHnPPe/Z/WMVIAQn/K8W9GU4CEDxPz8MJhwZ+WYg5fXZmPmWpN3SpvMJahZyubYOx k7aLr/kjOj/DEGzZRjoR3z6/nbsMmWVAzU71Bl9jG/eLGeAS164QIZG+gmThm3nYICC4 K3oRFsRJzsb0s7ZMRtBkYjAlf2mkpNda1l/jIWYjneDl92+2kZ7T727X6td39uG/xkxy u/aFTnJC3+iWZo6+YphGYGxa+6Okjna2z0TOWqKahei0ZrCo5Y2NdTLzkBP0g90xOuVg tZ+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020a63d043000000b0055fdd2c3fd4si4250386pgi.22.2023.07.30.23.19.32; Sun, 30 Jul 2023 23:19:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230096AbjGaFvb (ORCPT <rfc822;dengxinlin2429@gmail.com> + 99 others); Mon, 31 Jul 2023 01:51:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbjGaFvX (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 31 Jul 2023 01:51:23 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E61781730; Sun, 30 Jul 2023 22:51:11 -0700 (PDT) X-QQ-mid: bizesmtp75t1690782665t6sax2el Received: from dslab-main2-ubuntu.tail147f4.ts ( [202.201.15.117]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 31 Jul 2023 13:51:03 +0800 (CST) X-QQ-SSF: 01200000000000704000000A0000000 X-QQ-FEAT: jXjag1m6xl4TUwwOcxAHuSNvjezdnyUkeJyXtjE6BB1LN2sUyINMeltLYijKJ UMECH7SIllnAtxMSpIe2wh4MC38HJh6Vjamnwx6HerLMhs4ijK/ySftTd6SmXUXtSgBI6zF fdgepgMm9EFt3L9YSC/bFVrBnGow+n8WogQzdxwoXciLOZSmjVtdtnUWdmd3l4n9cOKh7s9 FitwrNZKDEevJn3RWFS4ficqy+A4GOlYHO3dvlEZkBA+p5n13KQF6lTPIIF6EMZDy+nWUNi PoJzti3ZCeUjBV0P90IyLi0sDKCraQ/Hzwrd2oFMoiZ78XlgKImyNU8Ac8D0eeCwTXCiniw JLazDYb9coRHXrMul12RLTJ7BMefgZSoqRYPt9kHjU+giphf34TNGRv9foQPw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 9840545900577853987 From: Yuan Tan <tanyuan@tinylab.org> To: w@1wt.eu, thomas@t-8ch.de Cc: falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Yuan Tan <tanyuan@tinylab.org> Subject: [PATCH v2 2/2] selftests/nolibc: add testcase for pipe Date: Mon, 31 Jul 2023 13:51:00 +0800 Message-Id: <9221753abe0509ef5cbb474a31873012e0e40706.1690733545.git.tanyuan@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <cover.1690733545.git.tanyuan@tinylab.org> References: <cover.1690733545.git.tanyuan@tinylab.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz5a-1 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772915928216825311 X-GMAIL-MSGID: 1772915928216825311 |
Series |
tools/nolibc: add pipe(), pipe2() and their testcase
|
|
Commit Message
Yuan Tan
July 31, 2023, 5:51 a.m. UTC
Add a testcase of pipe that child process sends message to parent
process.
Here we use memcmp() to avoid the output buffer issue.
Suggested-by: Thomas Weißschuh <thomas@t-8ch.de>
Suggested-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/all/c5de2d13-3752-4e1b-90d9-f58cca99c702@t-8ch.de/
Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
---
tools/testing/selftests/nolibc/nolibc-test.c | 35 ++++++++++++++++++++
1 file changed, 35 insertions(+)
Comments
On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > Add a testcase of pipe that child process sends message to parent > process. Thinking about it some more: What's the advantage of going via a child process? The pipe should work the same within the same process. > Here we use memcmp() to avoid the output buffer issue. This sentence is meaningless without the background from v1. You can drop it. > Suggested-by: Thomas Weißschuh <thomas@t-8ch.de> > Suggested-by: Willy Tarreau <w@1wt.eu> > Link: https://lore.kernel.org/all/c5de2d13-3752-4e1b-90d9-f58cca99c702@t-8ch.de/ > Signed-off-by: Yuan Tan <tanyuan@tinylab.org> > --- > tools/testing/selftests/nolibc/nolibc-test.c | 35 ++++++++++++++++++++ > 1 file changed, 35 insertions(+) > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > index 03b1d30f5507..2653ab8d5124 100644 > --- a/tools/testing/selftests/nolibc/nolibc-test.c > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > @@ -767,6 +767,41 @@ int test_mmap_munmap(void) > return ret; > } > > +int test_pipe(void) Should be static and actually get called :-) > +{ > + const char *const msg = "hello, nolibc"; > + int pipefd[2]; > + char buf[32]; > + pid_t pid; > + ssize_t len; > + > + if (pipe(pipefd) == -1) > + return 1; > + > + pid = fork(); > + > + switch (pid) { > + case -1: > + return 1; > + > + case 0: > + close(pipefd[0]); > + write(pipefd[1], msg, strlen(msg)); > + close(pipefd[1]); > + exit(EXIT_SUCCESS); > + > + default: > + close(pipefd[1]); > + len = read(pipefd[0], buf, sizeof(buf)); > + close(pipefd[0]); > + waitpid(pid, NULL, 0); > + > + if (len != strlen(msg)) > + return 1; > + return !!memcmp(buf, msg, len); > + } > +} > + > > /* Run syscall tests between IDs <min> and <max>. > * Return 0 on success, non-zero on failure. > -- > 2.34.1 >
Hi Thomas, On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > Add a testcase of pipe that child process sends message to parent > > process. > > Thinking about it some more: > > What's the advantage of going via a child process? > The pipe should work the same within the same process. > The pipe is commonly used for process communication, and I think as a test case it is supposed to cover the most common scenarios. > > Here we use memcmp() to avoid the output buffer issue. > > This sentence is meaningless without the background from v1. > You can drop it. > Got it. > > Suggested-by: Thomas Weißschuh <thomas@t-8ch.de> > > Suggested-by: Willy Tarreau <w@1wt.eu> > > Link: https://lore.kernel.org/all/c5de2d13-3752-4e1b-90d9-f58cca99c702@t-8ch.de/ > > Signed-off-by: Yuan Tan <tanyuan@tinylab.org> > > --- > > tools/testing/selftests/nolibc/nolibc-test.c | 35 ++++++++++++++++++++ > > 1 file changed, 35 insertions(+) > > > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > > index 03b1d30f5507..2653ab8d5124 100644 > > --- a/tools/testing/selftests/nolibc/nolibc-test.c > > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > > @@ -767,6 +767,41 @@ int test_mmap_munmap(void) > > return ret; > > } > > > > +int test_pipe(void) > > Should be static and actually get called :-) > > > +{ > > + const char *const msg = "hello, nolibc"; > > + int pipefd[2]; > > + char buf[32]; > > + pid_t pid; > > + ssize_t len; > > + > > + if (pipe(pipefd) == -1) > > + return 1; > > + > > + pid = fork(); > > + > > + switch (pid) { > > + case -1: > > + return 1; > > + > > + case 0: > > + close(pipefd[0]); > > + write(pipefd[1], msg, strlen(msg)); > > + close(pipefd[1]); > > + exit(EXIT_SUCCESS); > > + > > + default: > > + close(pipefd[1]); > > + len = read(pipefd[0], buf, sizeof(buf)); > > + close(pipefd[0]); > > + waitpid(pid, NULL, 0); > > + > > + if (len != strlen(msg)) > > + return 1; > > + return !!memcmp(buf, msg, len); > > + } > > +} > > + > > > > /* Run syscall tests between IDs <min> and <max>. > > * Return 0 on success, non-zero on failure. > > -- > > 2.34.1 > > >
On 2023-07-31 20:35:28+0800, Yuan Tan wrote: > On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > > Add a testcase of pipe that child process sends message to parent > > > process. > > > > Thinking about it some more: > > > > What's the advantage of going via a child process? > > The pipe should work the same within the same process. > > > > The pipe is commonly used for process communication, and I think as a > test case it is supposed to cover the most common scenarios. The testcase is supposed to cover the code of nolibc. It should be the *minimal* amount of code to be reasonable sure that the code in nolibc does the correct thing. If pipe() returns a value that behaves like a pipe I see no reason to doubt it will also survive fork(). Validating that would mean testing the kernel and not nolibc. For the kernel there are different testsuites. Less code means less work for everyone involved, now and in the future.
Hi Thomas, On Mon, 2023-07-31 at 17:41 +0200, Thomas Weißschuh wrote: > On 2023-07-31 20:35:28+0800, Yuan Tan wrote: > > On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > > > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > > > Add a testcase of pipe that child process sends message to > > > > parent > > > > process. > > > > > > Thinking about it some more: > > > > > > What's the advantage of going via a child process? > > > The pipe should work the same within the same process. > > > > > > > The pipe is commonly used for process communication, and I think as > > a > > test case it is supposed to cover the most common scenarios. > > The testcase is supposed to cover the code of nolibc. > It should be the *minimal* amount of code to be reasonable sure that > the > code in nolibc does the correct thing. > If pipe() returns a value that behaves like a pipe I see no reason to > doubt it will also survive fork(). > > Validating that would mean testing the kernel and not nolibc. > For the kernel there are different testsuites. > > Less code means less work for everyone involved, now and in the > future. > It's a good point and I never thought about this aspect. I wonder whether the code below is enough? static int test_pipe(void) { int pipefd[2]; if (pipe(pipefd) == -1) return 1; close(pipefd[0]); close(pipefd[1]); return 0; } And I forgot to add this line: CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; I will add it in next patch.
On 2023-08-01 02:01:36+0800, Yuan Tan wrote: > Hi Thomas, > On Mon, 2023-07-31 at 17:41 +0200, Thomas Weißschuh wrote: > > On 2023-07-31 20:35:28+0800, Yuan Tan wrote: > > > On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > > > > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > > > > Add a testcase of pipe that child process sends message to > > > > > parent > > > > > process. > > > > > > > > Thinking about it some more: > > > > > > > > What's the advantage of going via a child process? > > > > The pipe should work the same within the same process. > > > > > > > > > > The pipe is commonly used for process communication, and I think as > > > a > > > test case it is supposed to cover the most common scenarios. > > > > The testcase is supposed to cover the code of nolibc. > > It should be the *minimal* amount of code to be reasonable sure that > > the > > code in nolibc does the correct thing. > > If pipe() returns a value that behaves like a pipe I see no reason to > > doubt it will also survive fork(). > > > > Validating that would mean testing the kernel and not nolibc. > > For the kernel there are different testsuites. > > > > Less code means less work for everyone involved, now and in the > > future. > > > > It's a good point and I never thought about this aspect. > > I wonder whether the code below is enough? > > static int test_pipe(void) > { > int pipefd[2]; > > if (pipe(pipefd) == -1) > return 1; > > close(pipefd[0]); > close(pipefd[1]); > > return 0; > } That is very barebones. If accidentally a wrong syscall number was used and the used syscall would not take any arguments this test would still succeed. Keeping the write-read-cycle from the previous revision would test that nicely. Essentially the same code as before but without the fork(). > > And I forgot to add this line: > > CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; > > I will add it in next patch. >
Hi Thomas, On Mon, 2023-07-31 at 20:28 +0200, Thomas Weißschuh wrote: > On 2023-08-01 02:01:36+0800, Yuan Tan wrote: > > Hi Thomas, > > On Mon, 2023-07-31 at 17:41 +0200, Thomas Weißschuh wrote: > > > On 2023-07-31 20:35:28+0800, Yuan Tan wrote: > > > > On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > > > > > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > > > > > Add a testcase of pipe that child process sends message to > > > > > > parent > > > > > > process. > > > > > > > > > > Thinking about it some more: > > > > > > > > > > What's the advantage of going via a child process? > > > > > The pipe should work the same within the same process. > > > > > > > > > > > > > The pipe is commonly used for process communication, and I > > > > think as > > > > a > > > > test case it is supposed to cover the most common scenarios. > > > > > > The testcase is supposed to cover the code of nolibc. > > > It should be the *minimal* amount of code to be reasonable sure > > > that > > > the > > > code in nolibc does the correct thing. > > > If pipe() returns a value that behaves like a pipe I see no > > > reason to > > > doubt it will also survive fork(). > > > > > > Validating that would mean testing the kernel and not nolibc. > > > For the kernel there are different testsuites. > > > > > > Less code means less work for everyone involved, now and in the > > > future. > > > > > > > It's a good point and I never thought about this aspect. > > > > I wonder whether the code below is enough? > > > > static int test_pipe(void) > > { > > int pipefd[2]; > > > > if (pipe(pipefd) == -1) > > return 1; > > > > close(pipefd[0]); > > close(pipefd[1]); > > > > return 0; > > } > > That is very barebones. > > If accidentally a wrong syscall number was used and the used syscall > would not take any arguments this test would still succeed. > > Keeping the write-read-cycle from the previous revision would test > that > nicely. Essentially the same code as before but without the fork(). > > > > > And I forgot to add this line: > > > > CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; > > > > I will add it in next patch. > > > In the situation you described, that is indeed the case. Would this be fine? static int test_pipe(void) { const char *const msg = "hello, nolibc"; int pipefd[2]; char buf[32]; ssize_t len; if (pipe(pipefd) == -1) return 1; write(pipefd[1], msg, strlen(msg)); close(pipefd[1]); len = read(pipefd[0], buf, sizeof(buf)); close(pipefd[0]); if (len != strlen(msg)) return 1; return !!memcmp(buf, msg, len); }
On 2023-08-01 14:51:40+0800, Yuan Tan wrote: > Hi Thomas, > > On Mon, 2023-07-31 at 20:28 +0200, Thomas Weißschuh wrote: > > On 2023-08-01 02:01:36+0800, Yuan Tan wrote: > > > Hi Thomas, > > > On Mon, 2023-07-31 at 17:41 +0200, Thomas Weißschuh wrote: > > > > On 2023-07-31 20:35:28+0800, Yuan Tan wrote: > > > > > On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > > > > > > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > > > > > > Add a testcase of pipe that child process sends message to > > > > > > > parent > > > > > > > process. > > > > > > > > > > > > Thinking about it some more: > > > > > > > > > > > > What's the advantage of going via a child process? > > > > > > The pipe should work the same within the same process. > > > > > > > > > > > > > > > > The pipe is commonly used for process communication, and I > > > > > think as > > > > > a > > > > > test case it is supposed to cover the most common scenarios. > > > > > > > > The testcase is supposed to cover the code of nolibc. > > > > It should be the *minimal* amount of code to be reasonable sure > > > > that > > > > the > > > > code in nolibc does the correct thing. > > > > If pipe() returns a value that behaves like a pipe I see no > > > > reason to > > > > doubt it will also survive fork(). > > > > > > > > Validating that would mean testing the kernel and not nolibc. > > > > For the kernel there are different testsuites. > > > > > > > > Less code means less work for everyone involved, now and in the > > > > future. > > > > > > > > > > It's a good point and I never thought about this aspect. > > > > > > I wonder whether the code below is enough? > > > > > > static int test_pipe(void) > > > { > > > int pipefd[2]; > > > > > > if (pipe(pipefd) == -1) > > > return 1; > > > > > > close(pipefd[0]); > > > close(pipefd[1]); > > > > > > return 0; > > > } > > > > That is very barebones. > > > > If accidentally a wrong syscall number was used and the used syscall > > would not take any arguments this test would still succeed. > > > > Keeping the write-read-cycle from the previous revision would test > > that > > nicely. Essentially the same code as before but without the fork(). > > > > > > > > And I forgot to add this line: > > > > > > CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; > > > > > > I will add it in next patch. > > > > > > > In the situation you described, that is indeed the case. > > Would this be fine? > > static int test_pipe(void) > { > const char *const msg = "hello, nolibc"; > int pipefd[2]; > char buf[32]; > ssize_t len; > > if (pipe(pipefd) == -1) > return 1; > > write(pipefd[1], msg, strlen(msg)); > close(pipefd[1]); > len = read(pipefd[0], buf, sizeof(buf)); > close(pipefd[0]); > > if (len != strlen(msg)) > return 1; > > return !!memcmp(buf, msg, len); > } Looks good! The return value of write() could also be validated but given we validate the return value from read() it shouldn't make a difference. (Also the manual manipulation of "buf" is gone that necessitated the check in v1 of the series)
Hi Thomas, On Tue, 2023-08-01 at 09:20 +0200, Thomas Weißschuh wrote: > On 2023-08-01 14:51:40+0800, Yuan Tan wrote: > > Hi Thomas, > > > > On Mon, 2023-07-31 at 20:28 +0200, Thomas Weißschuh wrote: > > > On 2023-08-01 02:01:36+0800, Yuan Tan wrote: > > > > Hi Thomas, > > > > On Mon, 2023-07-31 at 17:41 +0200, Thomas Weißschuh wrote: > > > > > On 2023-07-31 20:35:28+0800, Yuan Tan wrote: > > > > > > On Mon, 2023-07-31 at 08:10 +0200, Thomas Weißschuh wrote: > > > > > > > On 2023-07-31 13:51:00+0800, Yuan Tan wrote: > > > > > > > > Add a testcase of pipe that child process sends message > > > > > > > > to > > > > > > > > parent > > > > > > > > process. > > > > > > > > > > > > > > Thinking about it some more: > > > > > > > > > > > > > > What's the advantage of going via a child process? > > > > > > > The pipe should work the same within the same process. > > > > > > > > > > > > > > > > > > > The pipe is commonly used for process communication, and I > > > > > > think as > > > > > > a > > > > > > test case it is supposed to cover the most common > > > > > > scenarios. > > > > > > > > > > The testcase is supposed to cover the code of nolibc. > > > > > It should be the *minimal* amount of code to be reasonable > > > > > sure > > > > > that > > > > > the > > > > > code in nolibc does the correct thing. > > > > > If pipe() returns a value that behaves like a pipe I see no > > > > > reason to > > > > > doubt it will also survive fork(). > > > > > > > > > > Validating that would mean testing the kernel and not nolibc. > > > > > For the kernel there are different testsuites. > > > > > > > > > > Less code means less work for everyone involved, now and in > > > > > the > > > > > future. > > > > > > > > > > > > > It's a good point and I never thought about this aspect. > > > > > > > > I wonder whether the code below is enough? > > > > > > > > static int test_pipe(void) > > > > { > > > > int pipefd[2]; > > > > > > > > if (pipe(pipefd) == -1) > > > > return 1; > > > > > > > > close(pipefd[0]); > > > > close(pipefd[1]); > > > > > > > > return 0; > > > > } > > > > > > That is very barebones. > > > > > > If accidentally a wrong syscall number was used and the used > > > syscall > > > would not take any arguments this test would still succeed. > > > > > > Keeping the write-read-cycle from the previous revision would > > > test > > > that > > > nicely. Essentially the same code as before but without the > > > fork(). > > > > > > > > > > > And I forgot to add this line: > > > > > > > > CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; > > > > > > > > I will add it in next patch. > > > > > > > > > > > In the situation you described, that is indeed the case. > > > > Would this be fine? > > > > static int test_pipe(void) > > { > > const char *const msg = "hello, nolibc"; > > int pipefd[2]; > > char buf[32]; > > ssize_t len; > > > > if (pipe(pipefd) == -1) > > return 1; > > > > write(pipefd[1], msg, strlen(msg)); > > close(pipefd[1]); > > len = read(pipefd[0], buf, sizeof(buf)); > > close(pipefd[0]); > > > > if (len != strlen(msg)) > > return 1; > > > > return !!memcmp(buf, msg, len); > > } > > Looks good! > > The return value of write() could also be validated but given we > validate the return value from read() it shouldn't make a difference. > > (Also the manual manipulation of "buf" is gone that necessitated the > check in v1 of the series) > I am sorry that I didn't catch your last sentence. Did you mean this piece of code does not need any further modifications right?
Aug 1, 2023 14:23:22 Yuan Tan <tanyuan@tinylab.org>: >>> Would this be fine? >>> >>> static int test_pipe(void) >>> { >>> const char *const msg = "hello, nolibc"; >>> int pipefd[2]; >>> char buf[32]; >>> ssize_t len; >>> >>> if (pipe(pipefd) == -1) >>> return 1; >>> >>> write(pipefd[1], msg, strlen(msg)); >>> close(pipefd[1]); >>> len = read(pipefd[0], buf, sizeof(buf)); >>> close(pipefd[0]); >>> >>> if (len != strlen(msg)) >>> return 1; >>> >>> return !!memcmp(buf, msg, len); >>> } >> >> Looks good! >> >> The return value of write() could also be validated but given we >> validate the return value from read() it shouldn't make a difference. >> >> (Also the manual manipulation of "buf" is gone that necessitated the >> check in v1 of the series) >> > > I am sorry that I didn't catch your last sentence. > > Did you mean this piece of code does not need any further modifications > right? Yes, for me this is great! Sorry for being unclear.
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 03b1d30f5507..2653ab8d5124 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -767,6 +767,41 @@ int test_mmap_munmap(void) return ret; } +int test_pipe(void) +{ + const char *const msg = "hello, nolibc"; + int pipefd[2]; + char buf[32]; + pid_t pid; + ssize_t len; + + if (pipe(pipefd) == -1) + return 1; + + pid = fork(); + + switch (pid) { + case -1: + return 1; + + case 0: + close(pipefd[0]); + write(pipefd[1], msg, strlen(msg)); + close(pipefd[1]); + exit(EXIT_SUCCESS); + + default: + close(pipefd[1]); + len = read(pipefd[0], buf, sizeof(buf)); + close(pipefd[0]); + waitpid(pid, NULL, 0); + + if (len != strlen(msg)) + return 1; + return !!memcmp(buf, msg, len); + } +} + /* Run syscall tests between IDs <min> and <max>. * Return 0 on success, non-zero on failure.