[LTP] [PATCH v2 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality

Xiao Yang yangx.jy@cn.fujitsu.com
Wed Feb 27 07:01:43 CET 2019


Register a new binary type and then check if binfmt_misc
recognises the binary type in some conditions.

Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
---
 runtest/fs                                         |   1 +
 testcases/kernel/fs/binfmt_misc/Makefile           |   4 +-
 testcases/kernel/fs/binfmt_misc/binfmt_misc02.sh   | 107 +++++++++++++++++++++
 testcases/kernel/fs/binfmt_misc/datafiles/Makefile |  13 +++
 .../kernel/fs/binfmt_misc/datafiles/file.extension |   1 +
 .../kernel/fs/binfmt_misc/datafiles/file.magic     |   1 +
 6 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc02.sh
 create mode 100644 testcases/kernel/fs/binfmt_misc/datafiles/Makefile
 create mode 100755 testcases/kernel/fs/binfmt_misc/datafiles/file.extension
 create mode 100755 testcases/kernel/fs/binfmt_misc/datafiles/file.magic

diff --git a/runtest/fs b/runtest/fs
index 227c186..ef2e848 100644
--- a/runtest/fs
+++ b/runtest/fs
@@ -84,3 +84,4 @@ isofs isofs.sh
 fs_fill fs_fill
 
 binfmt_misc01 binfmt_misc01.sh
+binfmt_misc02 binfmt_misc02.sh
diff --git a/testcases/kernel/fs/binfmt_misc/Makefile b/testcases/kernel/fs/binfmt_misc/Makefile
index f9819ad..14437b5 100644
--- a/testcases/kernel/fs/binfmt_misc/Makefile
+++ b/testcases/kernel/fs/binfmt_misc/Makefile
@@ -7,6 +7,6 @@ top_srcdir              ?= ../../../..
 
 include $(top_srcdir)/include/mk/env_pre.mk
 
-INSTALL_TARGETS         := binfmt_misc01.sh binfmt_misc_lib.sh
+INSTALL_TARGETS         := binfmt_misc01.sh binfmt_misc02.sh binfmt_misc_lib.sh
 
-include $(top_srcdir)/include/mk/generic_leaf_target.mk
+include $(top_srcdir)/include/mk/generic_trunk_target.mk
diff --git a/testcases/kernel/fs/binfmt_misc/binfmt_misc02.sh b/testcases/kernel/fs/binfmt_misc/binfmt_misc02.sh
new file mode 100755
index 0000000..8d0ecd8
--- /dev/null
+++ b/testcases/kernel/fs/binfmt_misc/binfmt_misc02.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
+#
+# Description:
+# Register a new binary type and then check if binfmt_misc
+# recognises the binary type in some conditions.
+# 1) binfmt_misc should recognise the binary type when extension
+#    or magic is matched.
+# 2) binfmt_misc should not recognise the binary type when extension
+#    or magic is mismatched.
+# 3) binfmt_misc should not recognise the binary type when it is
+#    disabled.
+#
+# Note:
+# We use various delimiteris to register a new binary type.
+
+TST_CNT=6
+TST_TESTFUNC=do_test
+TST_NEEDS_CMDS="which cat head"
+
+. binfmt_misc_lib.sh
+
+recognised_unrecognised()
+{
+	local file=$1
+	local string=$2
+
+	eval $file >temp 2>&1
+	if [ $? -ne 0 ] || ! grep -q "$string" temp; then
+		tst_res TFAIL "Fail to recognise a binary type"
+		return
+	fi
+
+	(echo 0 >"$mntpoint/$name") 2>/dev/null
+	if [ $? -ne 0 ] || grep -q enable "$mntpoint/$name"; then
+		tst_res TFAIL "Fail to disable a binary type"
+		return
+	fi
+
+	eval $file >temp 2>&1
+	if [ $? -eq 0 ] || grep -q "$string" temp; then
+		tst_res TFAIL "Recognise a disabled binary type successfully"
+		return
+	fi
+
+	tst_res TPASS "Recognise and unrecognise a binary type as expected"
+}
+
+unrecognised()
+{
+	local file=$1
+	local string=$2
+
+	eval $file >temp 2>&1
+	if [ $? -eq 0 ] || grep -q "$string" temp; then
+		tst_res TFAIL "Recognise a binary type successfully"
+	else
+		tst_res TPASS "Fail to recognise a binary type"
+	fi
+}
+
+verify_binfmt_misc()
+{
+	local delimiter=$(echo "$1" | head -c1)
+	local name=$(echo "$1" | awk -F $delimiter '{print $2}')
+	local ttype=$(echo "$1" | awk -F $delimiter '{print $3}')
+	local tfile=$2
+	local valid=$3
+	local mntpoint=$(get_binfmt_misc_mntpoint)
+
+	(echo "$1" >"$mntpoint/register") 2>/dev/null
+	if [ $? -ne 0 -o ! -f "$mntpoint/$name" ]; then
+		tst_res TFAIL "Fail to register a binary type"
+		return
+	fi
+
+	[ "$ttype" = "E" ] && local tstring="This is test for extension"
+	[ "$ttype" = "M" ] && local tstring="This is test for magic"
+
+	[ "$valid" = "1" ] && recognised_unrecognised "$tfile" "$tstring"
+	[ "$valid" = "0" ] && unrecognised "$tfile" "$tstring"
+
+	remove_binary_type "$mntpoint/$name"
+}
+
+do_test()
+{
+	case $1 in
+	1) verify_binfmt_misc ":textension:E::extension::$(which cat):" \
+			      "$TST_DATAROOT/file.extension" "1";;
+	2) verify_binfmt_misc ":tmagic:M:1:This::$(which cat):" \
+			      "$TST_DATAROOT/file.magic" "1";;
+	3) verify_binfmt_misc ".textension.E..extension..$(which cat)." \
+			      "$TST_DATAROOT/file.extension" "1";;
+	4) verify_binfmt_misc ",tmagic,M,1,This,,$(which cat)," \
+			      "$TST_DATAROOT/file.magic" "1";;
+	5) verify_binfmt_misc ":textension:E::ltp::$(which cat):" \
+			      "$TST_DATAROOT/file.extension" "0";;
+	6) verify_binfmt_misc ":tmagic:M:0:This::$(which cat):" \
+			      "$TST_DATAROOT/file.magic" "0";;
+	esac
+}
+
+tst_run
diff --git a/testcases/kernel/fs/binfmt_misc/datafiles/Makefile b/testcases/kernel/fs/binfmt_misc/datafiles/Makefile
new file mode 100644
index 0000000..e6d1487
--- /dev/null
+++ b/testcases/kernel/fs/binfmt_misc/datafiles/Makefile
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
+
+top_srcdir              ?= ../../../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_DIR		:= testcases/data/binfmt_misc02
+INSTALL_TARGETS		:= file.extension file.magic
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/fs/binfmt_misc/datafiles/file.extension b/testcases/kernel/fs/binfmt_misc/datafiles/file.extension
new file mode 100755
index 0000000..8468a8f
--- /dev/null
+++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.extension
@@ -0,0 +1 @@
+This is test for extension
diff --git a/testcases/kernel/fs/binfmt_misc/datafiles/file.magic b/testcases/kernel/fs/binfmt_misc/datafiles/file.magic
new file mode 100755
index 0000000..f2823b6
--- /dev/null
+++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.magic
@@ -0,0 +1 @@
+ This is test for magic
-- 
1.8.3.1





More information about the ltp mailing list