From: Andrea Cervesato <andrea.cervesato@suse.de>
To: Linux Test Project <ltp@lists.linux.it>
Subject: [LTP] [PATCH 2/6] fs: rewrite stream01 test using new API
Date: Fri, 23 Jan 2026 17:18:52 +0100 [thread overview]
Message-ID: <20260123-stream_refactoring-v1-2-281b85f6ab02@suse.com> (raw)
In-Reply-To: <20260123-stream_refactoring-v1-0-281b85f6ab02@suse.com>
From: Andrea Cervesato <andrea.cervesato@suse.com>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/fs/stream/stream01.c | 153 ++++++++++------------------------
1 file changed, 46 insertions(+), 107 deletions(-)
diff --git a/testcases/kernel/fs/stream/stream01.c b/testcases/kernel/fs/stream/stream01.c
index af56bca3b916c080328c3356681efbd869b961d2..6d111dc36dcfbab8e0ba8d66d3e901ef36a71400 100644
--- a/testcases/kernel/fs/stream/stream01.c
+++ b/testcases/kernel/fs/stream/stream01.c
@@ -1,127 +1,66 @@
+// 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"
-
-char *TCID = "stream01";
-int TST_TOTAL = 1;
-int local_flag;
+static void read_file(const char *file)
+{
+ char buf[2];
+ FILE *stream;
-#define PASSED 1
-#define FAILED 0
+ memset(buf, 0, sizeof(buf));
-/* XXX: add setup and cleanup. */
+ stream = SAFE_FOPEN(file, "r");
+ SAFE_FREAD(buf, 1, 1, stream);
+ SAFE_FCLOSE(stream);
-char progname[] = "stream01()";
-char tempfile1[40] = "";
-char tempfile2[40] = "";
+ TST_EXP_EXPR((buf[0] == 'a') && (buf[1] == 0),
+ "%s file contains the correct data", file);
+}
-/*--------------------------------------------------------------------*/
-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("a", 1, 1, 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("a", 1, 1, 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);
+ read_file(FILENAME2);
- /*--------------------------------------------------------------------*/
- 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-01-23 16:19 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-23 16:18 [LTP] [PATCH 0/6] Rewrite fs stream testing suite Andrea Cervesato
2026-01-23 16:18 ` [LTP] [PATCH 1/6] Add safe macros for " Andrea Cervesato
2026-02-19 12:31 ` Cyril Hrubis
2026-03-04 9:49 ` Andrea Cervesato via ltp
2026-03-04 10:06 ` Cyril Hrubis
2026-03-04 10:11 ` Andrea Cervesato via ltp
2026-03-04 10:30 ` Cyril Hrubis
2026-03-04 12:20 ` Andrea Cervesato via ltp
2026-01-23 16:18 ` Andrea Cervesato [this message]
2026-02-19 13:05 ` [LTP] [PATCH 2/6] fs: rewrite stream01 test using new API Cyril Hrubis
2026-01-23 16:18 ` [LTP] [PATCH 3/6] fs: rewrite stream02 " Andrea Cervesato
2026-03-02 12:01 ` Cyril Hrubis
2026-01-23 16:18 ` [LTP] [PATCH 4/6] fs: rewrite stream03 " Andrea Cervesato
2026-03-02 13:02 ` Cyril Hrubis
2026-01-23 16:18 ` [LTP] [PATCH 5/6] fs: rewrite stream04 " Andrea Cervesato
2026-03-02 13:14 ` Cyril Hrubis
2026-01-23 16:18 ` [LTP] [PATCH 6/6] fs: rewrite stream05 " Andrea Cervesato
2026-03-02 13:52 ` 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=20260123-stream_refactoring-v1-2-281b85f6ab02@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