From: Andrea Cervesato <andrea.cervesato@suse.de>
To: Linux Test Project <ltp@lists.linux.it>
Subject: [LTP] [PATCH v2 2/6] fs: rewrite stream01 test using new API
Date: Wed, 04 Mar 2026 14:25:40 +0100 [thread overview]
Message-ID: <20260304-stream_refactoring-v2-2-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/stream01.c | 155 +++++++++++-----------------------
1 file changed, 49 insertions(+), 106 deletions(-)
diff --git a/testcases/kernel/fs/stream/stream01.c b/testcases/kernel/fs/stream/stream01.c
index af56bca3b916c080328c3356681efbd869b961d2..d666e67491e53b1c45dabef5bb6118dbbdc8ad38 100644
--- a/testcases/kernel/fs/stream/stream01.c
+++ b/testcases/kernel/fs/stream/stream01.c
@@ -1,127 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
+ * Copyright (c) International Business Machines Corp., 2002
+ * ported from SPIE section2/filesuite/stream1.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.
+ * Copyright (c) 2026 Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * Verify that `freopen()` substitutes the named file in place of stream.
*
- * 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.
+ * [Algorithm]
*
- * 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
+ * - fopen() a stream
+ * - fwrite() something inside it
+ * - perform freopen() creating a new stream pointing to the first one
+ * - fwrite() data inside the new stream
+ * - check that second write to stream went to the file specified by freopen()
*/
-/* ported from SPIE section2/filesuite/stream1.c, by Airong Zhang */
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
-/*======================================================================
- =================== TESTPLAN SEGMENT ===================
->KEYS: < freopen()
->WHAT: < 1) check that freopen substitutes the named file in place of stream.
->HOW: < 1) open a stream, write something to it, perform freopen and
- < write some more. Check that second write to stream went to
- < the file specified by freopen.
->BUGS: <
-======================================================================*/
+#define FILENAME1 "ltp_file1.txt"
+#define FILENAME2 "ltp_file2.txt"
-#include <stdio.h>
-#include <errno.h>
-#include "test.h"
+static char *buff_file1 = "abc";
+static char *buff_file2 = "def";
-char *TCID = "stream01";
-int TST_TOTAL = 1;
-int local_flag;
+static void read_file(const char *file, const char *str, size_t n)
+{
+ char buf[n];
+ FILE *stream;
+ size_t len;
-#define PASSED 1
-#define FAILED 0
+ memset(buf, 0, sizeof(buf));
-/* XXX: add setup and cleanup. */
+ stream = SAFE_FOPEN(file, "r");
+ len = SAFE_FREAD(buf, n, n, stream);
+ SAFE_FCLOSE(stream);
-char progname[] = "stream01()";
-char tempfile1[40] = "";
-char tempfile2[40] = "";
+ TST_EXP_EXPR(len == n, "Read the entire %s file buffer", file);
+ TST_EXP_EQ_STRN(buf, str, len);
+}
-/*--------------------------------------------------------------------*/
-int main(int ac, char *av[])
+static void run(void)
{
FILE *stream;
- char buf[10];
- int i;
- int lc;
-
- /*
- * parse standard options
- */
- tst_parse_opts(ac, av, NULL, NULL);
- local_flag = PASSED;
- tst_tmpdir();
- for (lc = 0; TEST_LOOPING(lc); lc++) {
+ tst_res(TINFO, "Write %s file", FILENAME1);
+ stream = SAFE_FOPEN(FILENAME1, "a+");
+ SAFE_FWRITE(buff_file1, strlen(buff_file1), 3, stream);
- sprintf(tempfile1, "stream011.%d", getpid());
- sprintf(tempfile2, "stream012.%d", getpid());
- /*--------------------------------------------------------------------*/
- //block0:
- if ((stream = fopen(tempfile1, "a+")) == NULL) {
- tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
- tempfile1,
- strerror(errno));
- }
- fwrite("a", 1, 1, stream);
- if ((stream = freopen(tempfile2, "a+", stream)) == NULL) {
- tst_brkm(TFAIL | TERRNO, NULL, "freopen(%s) a+ failed",
- tempfile2);
- }
- fwrite("a", 1, 1, stream);
- fclose(stream);
+ tst_res(TINFO, "Write %s file streaming into %s file", FILENAME2, FILENAME1);
+ stream = SAFE_FREOPEN(FILENAME2, "a+", stream);
+ SAFE_FWRITE(buff_file2, strlen(buff_file2), 3, stream);
- /* now check that a single "a" is in each file */
- if ((stream = fopen(tempfile1, "r")) == NULL) {
- tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed",
- tempfile1);
- } else {
- for (i = 0; i < 10; i++)
- buf[i] = 0;
- fread(buf, 1, 1, stream);
- if ((buf[0] != 'a') || (buf[1] != 0)) {
- tst_resm(TFAIL, "bad contents in %s",
- tempfile1);
- local_flag = FAILED;
- }
- fclose(stream);
- }
- if ((stream = fopen(tempfile2, "r")) == NULL) {
- tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed",
- tempfile2);
- } else {
- for (i = 0; i < 10; i++)
- buf[i] = 0;
- fread(buf, 1, 1, stream);
- if ((buf[0] != 'a') || (buf[1] != 0)) {
- tst_resm(TFAIL, "bad contents in %s",
- tempfile2);
- local_flag = FAILED;
- }
- fclose(stream);
- }
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed.");
- } else {
- tst_resm(TFAIL, "Test failed.");
- }
+ SAFE_FCLOSE(stream);
- local_flag = PASSED;
+ read_file(FILENAME1, buff_file1, 3);
+ read_file(FILENAME2, buff_file2, 3);
- /*--------------------------------------------------------------------*/
- unlink(tempfile1);
- unlink(tempfile2);
-
- } /* end for */
- tst_rmdir();
- tst_exit();
+ SAFE_UNLINK(FILENAME1);
+ SAFE_UNLINK(FILENAME2);
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .needs_tmpdir = 1,
+};
--
2.51.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-03-04 13:26 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 ` Andrea Cervesato [this message]
2026-04-07 14:57 ` [LTP] [PATCH v2 2/6] fs: rewrite stream01 test using new API 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 ` [LTP] [PATCH v2 6/6] fs: rewrite stream05 " Andrea Cervesato
2026-04-07 15:30 ` 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-2-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