[LTP] [PATCH 3/6] fs: rewrite stream02 test using new API
Andrea Cervesato
andrea.cervesato@suse.de
Fri Jan 23 17:18:53 CET 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/fs/stream/stream02.c | 133 ++++++++--------------------------
1 file changed, 29 insertions(+), 104 deletions(-)
diff --git a/testcases/kernel/fs/stream/stream02.c b/testcases/kernel/fs/stream/stream02.c
index 98473d86aab686eea2212c0e005a46e950f74ae8..0c6f3737dda69623e5e2bdf35ee427de0ad24586 100644
--- a/testcases/kernel/fs/stream/stream02.c
+++ b/testcases/kernel/fs/stream/stream02.c
@@ -1,119 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
+ * Copyright (c) International Business Machines Corp., 2002
+ * ported from SPIE section2/filesuite/stream2.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/stream2.c, by Airong Zhang */
-
-/*======================================================================
- =================== TESTPLAN SEGMENT ===================
->KEYS: < fseek() mknod() fopen()
->WHAT: < 1)
->HOW: < 1)
->BUGS: <
-======================================================================*/
-
-#include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "test.h"
-char *TCID = "stream02";
-int TST_TOTAL = 1;
-int local_flag;
+/*\
+ * Verify that it's possible to `fopen()` a file that has been created by
+ * `mknod()` using different modes.
+ */
-#define PASSED 1
-#define FAILED 0
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
-char progname[] = "stream02()";
-char tempfile1[40] = "";
+#define FILENAME "ltp_file_node"
-/* XXX: add cleanup + setup. */
+static const char *const modes[] = {
+ "r+",
+ "w+",
+ "a+",
+};
-/*--------------------------------------------------------------------*/
-int main(int ac, char *av[])
+static void run(void)
{
FILE *stream;
- int fd;
- int lc;
-
- /*
- * parse standard options
- */
- tst_parse_opts(ac, av, NULL, NULL);
- local_flag = PASSED;
- tst_tmpdir();
+ SAFE_MKNOD(FILENAME, (S_IFIFO | 0666), 0);
- for (lc = 0; TEST_LOOPING(lc); lc++) {
+ for (size_t i = 0; i < ARRAY_SIZE(modes); i++) {
+ stream = SAFE_FOPEN(FILENAME, modes[i]);
+ SAFE_FCLOSE(stream);
- sprintf(tempfile1, "stream1.%d", getpid());
- /*--------------------------------------------------------------------*/
- //block0:
- if (mknod(tempfile1, (S_IFIFO | 0666), 0) != 0) {
- tst_resm(TFAIL, "mknod failed in block0: %s",
- strerror(errno));
- local_flag = FAILED;
- goto block1;
- }
- if ((stream = fopen(tempfile1, "w+")) == NULL) {
- tst_resm(TFAIL, "fopen(%s) w+ failed for pipe file: %s",
- tempfile1, strerror(errno));
- local_flag = FAILED;
- } else {
- fclose(stream);
- }
- if ((stream = fopen(tempfile1, "a+")) == NULL) {
- tst_resm(TFAIL, "fopen(%s) a+ failed: %s", tempfile1,
- strerror(errno));
- local_flag = FAILED;
- } else {
- fclose(stream);
- unlink(tempfile1);
- }
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed in block0.");
- } else {
- tst_resm(TFAIL, "Test failed in block0.");
- }
- local_flag = PASSED;
+ tst_res(TPASS, "Opened file using '%s' mode", modes[i]);
+ }
- /*--------------------------------------------------------------------*/
-block1:
- if ((fd = open("/dev/tty", O_WRONLY)) >= 0) {
- close(fd);
- if ((stream = fopen("/dev/tty", "w")) == NULL) {
- tst_resm(TFAIL | TERRNO,
- "fopen(/dev/tty) write failed");
- local_flag = FAILED;
- } else {
- fclose(stream);
- }
- }
- if (local_flag == PASSED) {
- tst_resm(TPASS, "Test passed in block1.");
- } else {
- tst_resm(TFAIL, "Test failed in block1.");
- }
-
- /*--------------------------------------------------------------------*/
- } /* end for */
- tst_rmdir();
- tst_exit();
+ SAFE_UNLINK(FILENAME);
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .needs_tmpdir = 1,
+};
--
2.51.0
More information about the ltp
mailing list