linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Denis Efremov <efremov@linux.com>
To: linux-kselftest@vger.kernel.org
Cc: Denis Efremov <efremov@linux.com>,
	linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
	Jens Axboe <axboe@kernel.dk>, Jiri Kosina <jkosina@suse.cz>,
	Willy Tarreau <w@1wt.eu>
Subject: [RFC PATCH 4/5] selftests: floppy: add basic tests for a readonly disk
Date: Wed, 18 Aug 2021 18:46:45 +0300	[thread overview]
Message-ID: <20210818154646.925351-5-efremov@linux.com> (raw)
In-Reply-To: <20210818154646.925351-1-efremov@linux.com>

Add basic tests for a floppy with a readonly disk. "rdonly" test
works under following assumptions:
 - there is a readonly disk in "/dev/fd0"
 - the disk is VFAT formatted
 - there is "test" file with "TEST" in it on the disk

"run_rdonly.sh" script simulates the conditions and automates the
testing process.

The tests cover the regression:
 - commit f2791e7eadf4 ("Revert "floppy: refactor open() flags handling"")

Signed-off-by: Denis Efremov <efremov@linux.com>
---
 tools/testing/selftests/floppy/.gitignore    |  2 +
 tools/testing/selftests/floppy/Makefile      |  6 +-
 tools/testing/selftests/floppy/lib.sh        |  6 ++
 tools/testing/selftests/floppy/rdonly.c      | 99 ++++++++++++++++++++
 tools/testing/selftests/floppy/run_rdonly.sh | 22 +++++
 5 files changed, 132 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/floppy/rdonly.c
 create mode 100755 tools/testing/selftests/floppy/run_rdonly.sh

diff --git a/tools/testing/selftests/floppy/.gitignore b/tools/testing/selftests/floppy/.gitignore
index a9fdf1dbaa97..7642dc0ef281 100644
--- a/tools/testing/selftests/floppy/.gitignore
+++ b/tools/testing/selftests/floppy/.gitignore
@@ -1,5 +1,7 @@
 init
 cpio_list
 initrd
+testdir/
 
 empty
+rdonly
diff --git a/tools/testing/selftests/floppy/Makefile b/tools/testing/selftests/floppy/Makefile
index 83e18cd63210..ed8fdeb79aea 100644
--- a/tools/testing/selftests/floppy/Makefile
+++ b/tools/testing/selftests/floppy/Makefile
@@ -1,10 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0
 CFLAGS := -static -I../../../../usr/include
 
-TEST_PROGS := run_empty.sh
-TEST_GEN_FILES := init empty
+TEST_PROGS := run_empty.sh run_rdonly.sh
+TEST_GEN_FILES := init empty rdonly
 TEST_FILES := lib.sh
 
 include ../lib.mk
 
-EXTRA_CLEAN := initrd cpio_list
+EXTRA_CLEAN := initrd cpio_list testdir
diff --git a/tools/testing/selftests/floppy/lib.sh b/tools/testing/selftests/floppy/lib.sh
index 22b711ec32ee..9988be187bc9 100644
--- a/tools/testing/selftests/floppy/lib.sh
+++ b/tools/testing/selftests/floppy/lib.sh
@@ -55,3 +55,9 @@ run_qemu_empty() {
   detect_debug "$1"
   $run -drive index=0,if=floppy
 }
+
+run_qemu_rdonly_fat() {
+  detect_debug "$2"
+  $run -drive file=fat:floppy:"$1",index=0,if=floppy,readonly
+}
+
diff --git a/tools/testing/selftests/floppy/rdonly.c b/tools/testing/selftests/floppy/rdonly.c
new file mode 100644
index 000000000000..fa60d069540b
--- /dev/null
+++ b/tools/testing/selftests/floppy/rdonly.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <sys/ioctl.h>
+#include <linux/fd.h>
+#include "../kselftest_harness.h"
+
+FIXTURE(floppy) {
+	const char *dev;
+};
+
+FIXTURE_SETUP(floppy)
+{
+	int fd;
+	struct floppy_drive_params params;
+
+	self->dev = "/dev/fd0";
+	if (access(self->dev, F_OK))
+		ksft_exit_skip("No floppy device found\n");
+	if (access(self->dev, R_OK))
+		ksft_exit_skip("Floppy is not read accessible\n");
+
+	fd = open(self->dev, O_ACCMODE|O_NDELAY);
+	EXPECT_EQ(0, ioctl(fd, FDGETDRVPRM, &params));
+	params.flags |= FTD_MSG|FD_DEBUG;
+	EXPECT_EQ(0, ioctl(fd, FDSETDRVPRM, &params));
+	close(fd);
+}
+
+FIXTURE_TEARDOWN(floppy)
+{
+}
+
+TEST_F(floppy, read)
+{
+	int fd, test;
+
+	fd = open(self->dev, O_RDONLY);
+	ASSERT_GT(fd, 0);
+	ASSERT_EQ(read(fd, &test, sizeof(test)), sizeof(test));
+	ASSERT_EQ(close(fd), 0);
+}
+
+TEST_F(floppy, open_write_fail)
+{
+	ASSERT_LT(open(self->dev, O_WRONLY), 0);
+}
+
+TEST_F(floppy, open_rdwr_fail)
+{
+	ASSERT_LT(open(self->dev, O_RDWR), 0);
+}
+
+TEST_F(floppy, ioctl_disk_writable)
+{
+	int fd;
+	struct floppy_drive_struct drive;
+
+	fd = open(self->dev, O_RDONLY|O_NDELAY);
+	ASSERT_GT(fd, 0);
+	ASSERT_EQ(0, ioctl(fd, FDGETDRVSTAT, &drive));
+	ASSERT_FALSE(drive.flags & FD_DISK_WRITABLE);
+	ASSERT_EQ(close(fd), 0);
+}
+
+TEST_F(floppy, mount)
+{
+	int fd;
+	char test[5] = {};
+
+	mount(self->dev, "/mnt", "vfat", MS_RDONLY, NULL);
+	ASSERT_EQ(0, errno);
+
+	fd = open("/mnt/test", O_RDONLY);
+	read(fd, &test, sizeof(test));
+	ASSERT_EQ(0, strncmp(test, "TEST", 4));
+}
+
+TEST_F(floppy, open_ndelay_write_fail)
+{
+#define TEST_DATA "TEST_FAIL_WRITE"
+	int fd;
+	char test[] = TEST_DATA;
+
+	fd = open(self->dev, O_RDWR|O_NDELAY);
+	ASSERT_GT(fd, 0);
+
+	write(fd, test, sizeof(test));
+	read(fd, test, sizeof(test));
+	ASSERT_NE(0, strncmp(TEST_DATA, test, sizeof(TEST_DATA)));
+
+	ASSERT_EQ(close(fd), 0);
+#undef TEST_DATA
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/floppy/run_rdonly.sh b/tools/testing/selftests/floppy/run_rdonly.sh
new file mode 100755
index 000000000000..a358a1b6e58d
--- /dev/null
+++ b/tools/testing/selftests/floppy/run_rdonly.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+source "$(dirname $0)"/lib.sh
+
+while getopts d flag; do
+  case "${flag}" in
+    d) debug=1;;
+  esac
+done
+
+if [ -z $debug ]; then
+  trap "rm -rf testdir" EXIT
+fi
+mkdir -p testdir
+echo -n TEST > testdir/test
+
+gen_cpio_list rdonly
+gen_initrd rdonly
+run_qemu_rdonly_fat testdir $debug
-- 
2.31.1


  parent reply	other threads:[~2021-08-18 15:47 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-18 15:46 [RFC PATCH 0/5] selftests: floppy: basic tests Denis Efremov
2021-08-18 15:46 ` [RFC PATCH 1/5] checkpatch: improve handling of revert commits Denis Efremov
2021-08-18 16:00   ` Joe Perches
2021-08-18 16:21     ` Denis Efremov
2021-08-18 20:21       ` Joe Perches
2021-08-18 20:35         ` Andrew Morton
2021-08-18 21:22       ` Joe Perches
2021-08-19 19:52         ` Denis Efremov
2021-08-19 21:44           ` Joe Perches
2021-08-19 22:17           ` Joe Perches
2021-08-21  6:47             ` Denis Efremov
2021-08-21  7:12               ` Joe Perches
2021-08-21  7:35                 ` Denis Efremov
2021-08-18 15:46 ` [RFC PATCH 2/5] gen_initramfs.sh: use absolute path for gen_init_cpio Denis Efremov
2021-08-19  0:24   ` Masahiro Yamada
2021-08-19 20:51     ` Denis Efremov
2021-08-20  2:19       ` Masahiro Yamada
2021-08-18 15:46 ` [RFC PATCH 3/5] selftests: floppy: add basic tests for opening an empty device Denis Efremov
2021-08-18 15:46 ` Denis Efremov [this message]
2021-08-18 15:46 ` [RFC PATCH 5/5] selftests: floppy: add basic rdwr tests Denis Efremov

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=20210818154646.925351-5-efremov@linux.com \
    --to=efremov@linux.com \
    --cc=axboe@kernel.dk \
    --cc=jkosina@suse.cz \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=w@1wt.eu \
    /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;
as well as URLs for NNTP newsgroup(s).