From: Andrea Cervesato <andrea.cervesato@suse.de>
To: Linux Test Project <ltp@lists.linux.it>
Subject: [LTP] [PATCH v2 6/6] fs: rewrite stream05 test using new API
Date: Wed, 04 Mar 2026 14:25:44 +0100 [thread overview]
Message-ID: <20260304-stream_refactoring-v2-6-3e48ada8ff1a@suse.com> (raw)
In-Reply-To: <20260304-stream_refactoring-v2-0-3e48ada8ff1a@suse.com>
From: Andrea Cervesato <andrea.cervesato@suse.com>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/fs/stream/stream05.c | 258 +++++++---------------------------
1 file changed, 47 insertions(+), 211 deletions(-)
diff --git a/testcases/kernel/fs/stream/stream05.c b/testcases/kernel/fs/stream/stream05.c
index f561744c3da86cc5d80b97c9707f5583059d21f0..90989451bd45e32350946939a9f1acb7657be3a0 100644
--- a/testcases/kernel/fs/stream/stream05.c
+++ b/testcases/kernel/fs/stream/stream05.c
@@ -1,230 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
+ * Copyright (c) International Business Machines Corp., 2002
+ * ported from SPIE section2/filesuite/stream.c, by Airong Zhang
*
- * Copyright (c) International Business Machines Corp., 2002
- *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * 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.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) 2026 Andrea Cervesato <andrea.cervesato@suse.com>
*/
-/* ported from SPIE, section2/filesuite/stream.c, by Airong Zhang */
-
-/*======================================================================
- =================== TESTPLAN SEGMENT ===================
->KEYS: < ferror() feof() clearerr() fileno()
->WHAT: < 1) check that ferror returns zero
- < 2) check fileno returns valid file descriptor
- < 3) check that feof returns zero (nonzero) appropriately
- < 4) check that clearerr resets EOF indicator.
->HOW: < 1) open a stream and immediately execute ferror
- < 2) use the file des returned from fileno to read a file
- < written with stream - compare actual vs expected.
- < 3) open stream and ensure feof returns zero, read to end of
- < file and ensure feof returns non-zero.
- < 4) after 3) above use clearerr and then use feof to ensure
- < clearerr worked
->BUGS: <
-======================================================================*/
-
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-#include "test.h"
+/*\
+ * Verify that it's possible to read/write on a file descriptor returned by
+ * `fileno()` and to close it, leaving `fclose()` to raise EBADF error.
+ */
-char *TCID = "stream05";
-int TST_TOTAL = 1;
-int local_flag;
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "tst_rand_data.h"
-#define PASSED 1
-#define FAILED 0
+#define FILENAME "ltp_file"
+#define DATASIZE 3
+#define BUFFSIZE (2 * DATASIZE)
-char progname[] = "stream05()";
-char tempfile[40] = "";
+static char *data;
+static char *buff;
-/*--------------------------------------------------------------------*/
-int main(int ac, char *av[])
+static void run(void)
{
+ int fd;
FILE *stream;
- char buf[10];
- int nr, fd;
-
- int lc;
-
- /*
- * parse standard options
- */
- tst_parse_opts(ac, av, NULL, NULL);
- tst_tmpdir();
- local_flag = PASSED;
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- local_flag = PASSED;
-
- sprintf(tempfile, "stream05.%d", getpid());
- /*--------------------------------------------------------------------*/
- //block0:
- if ((stream = fopen(tempfile, "a+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
- tempfile,
- strerror(errno));
- }
- fprintf(stream, "a");
- fclose(stream);
-
- if ((stream = fopen(tempfile, "r+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s",
- tempfile,
- strerror(errno));
- }
-
- /* check that ferror returns zero */
- if (ferror(stream) != 0) {
- tst_resm(TFAIL, "ferror did not return zero: %s",
- strerror(errno));
- local_flag = FAILED;
- }
-
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed in block0.");
- } else {
- tst_resm(TFAIL, "Test failed in block0.");
- }
- local_flag = PASSED;
+ memset(buff, 0, BUFFSIZE);
- /*--------------------------------------------------------------------*/
- //block1:
+ stream = SAFE_FOPEN(FILENAME, "a+");
+ SAFE_FWRITE(data, 1, DATASIZE, stream);
+ SAFE_FFLUSH(stream);
- /* check that fileno returns valid file descriptor */
- fd = fileno(stream);
- if ((nr = read(fd, buf, 1)) < 0) {
- tst_brkm(TFAIL, NULL, "read failed: %s",
- strerror(errno));
- }
- if (nr != 1) {
- tst_resm(TFAIL, "read did not read right number");
- local_flag = FAILED;
- }
- if (buf[0] != 'a') {
- tst_resm(TFAIL, "read returned bad values");
- local_flag = FAILED;
- }
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed in block1.");
- } else {
- tst_resm(TFAIL, "Test failed in block1.");
- }
+ fd = SAFE_FILENO(stream);
+ SAFE_WRITE(SAFE_WRITE_ALL, fd, data, DATASIZE);
- local_flag = PASSED;
- /*--------------------------------------------------------------------*/
- //block2:
+ fsync(fd);
+ SAFE_FSEEK(stream, 0, SEEK_SET);
+ SAFE_READ(1, fd, buff, BUFFSIZE);
- /* read to EOF and ensure feof returns non-zero */
- fclose(stream);
+ TST_EXP_EQ_STRN(data, buff, DATASIZE);
+ TST_EXP_EQ_STRN(data, buff + 3, DATASIZE);
- if ((stream = fopen(tempfile, "r+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s",
- tempfile,
- strerror(errno));
- }
- if (feof(stream) != 0) {
- tst_resm(TFAIL,
- "feof returned non-zero when it should not: %s",
- strerror(errno));
- local_flag = FAILED;
- }
- fread(buf, 1, 2, stream); /* read to EOF */
- if (feof(stream) == 0) {
- tst_resm(TFAIL,
- "feof returned zero when it should not: %s",
- strerror(errno));
- local_flag = FAILED;
- }
+ SAFE_CLOSE(fd);
+ TST_EXP_FAIL(fclose(stream), EBADF);
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed in block2.");
- } else {
- tst_resm(TFAIL, "Test failed in block2.");
- }
-
- local_flag = PASSED;
- /*--------------------------------------------------------------------*/
- //block3:
- /* ensure clearerr works */
- clearerr(stream);
- if (feof(stream) != 0) {
- tst_resm(TFAIL, "clearerr failed: %s", strerror(errno));
- local_flag = FAILED;
- }
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed in block3.");
- } else {
- tst_resm(TFAIL, "Test failed in block3.");
- }
-
- local_flag = PASSED;
- /*--------------------------------------------------------------------*/
- //block4:
-
- /* test fopen "b" flags -- should be allowed but ignored */
- (void)fclose(stream);
-
- if ((stream = fopen(tempfile, "rb")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) rb failed: %s",
- tempfile,
- strerror(errno));
- }
- (void)fclose(stream);
-
- if ((stream = fopen(tempfile, "wb")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) wb failed: %s",
- tempfile,
- strerror(errno));
- }
- (void)fclose(stream);
-
- if ((stream = fopen(tempfile, "ab")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) ab failed: %s",
- tempfile,
- strerror(errno));
- }
- (void)fclose(stream);
-
- if ((stream = fopen(tempfile, "rb+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) rb+ failed: %s",
- tempfile,
- strerror(errno));
- }
- (void)fclose(stream);
-
- if ((stream = fopen(tempfile, "wb+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) wb+ failed: %s",
- tempfile,
- strerror(errno));
- }
- (void)fclose(stream);
-
- if ((stream = fopen(tempfile, "ab+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) ab+ failed: %s",
- tempfile,
- strerror(errno));
- }
- (void)fclose(stream);
+ SAFE_UNLINK(FILENAME);
+}
- tst_resm(TPASS, "Test passed in block4.");
- /*--------------------------------------------------------------------*/
- unlink(tempfile);
- } /* end for */
- tst_rmdir();
- tst_exit();
+static void setup(void)
+{
+ memcpy(data, tst_rand_data, DATASIZE);
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_tmpdir = 1,
+ .bufs = (struct tst_buffers []) {
+ {&data, .size = DATASIZE},
+ {&buff, .size = BUFFSIZE},
+ {},
+ },
+};
--
2.51.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-03-04 13:28 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-04 13:25 [LTP] [PATCH v2 0/6] Rewrite fs stream testing suite Andrea Cervesato
2026-03-04 13:25 ` [LTP] [PATCH v2 1/6] Add safe macros for " Andrea Cervesato
2026-03-04 13:25 ` [LTP] [PATCH v2 2/6] fs: rewrite stream01 test using new API Andrea Cervesato
2026-04-07 14:57 ` Cyril Hrubis
2026-03-04 13:25 ` [LTP] [PATCH v2 3/6] fs: rewrite stream02 " Andrea Cervesato
2026-04-07 14:59 ` Cyril Hrubis
2026-03-04 13:25 ` [LTP] [PATCH v2 4/6] fs: rewrite stream03 " Andrea Cervesato
2026-03-04 13:25 ` [LTP] [PATCH v2 5/6] fs: rewrite stream04 " Andrea Cervesato
2026-03-04 13:25 ` Andrea Cervesato [this message]
2026-04-07 15:30 ` [LTP] [PATCH v2 6/6] fs: rewrite stream05 " Cyril Hrubis
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=20260304-stream_refactoring-v2-6-3e48ada8ff1a@suse.com \
--to=andrea.cervesato@suse.de \
--cc=ltp@lists.linux.it \
/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