public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/2] fs/binfmt_misc_lib.sh: Add common remove_binary_type()
@ 2019-02-22  9:59 Xiao Yang
  2019-02-22  9:59 ` [LTP] [PATCH 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality Xiao Yang
  0 siblings, 1 reply; 6+ messages in thread
From: Xiao Yang @ 2019-02-22  9:59 UTC (permalink / raw)
  To: ltp

1) Add common remove_binary_type() so that we can remove the
   registered binary type by it.
2) Take use of remove_binary_type() in binfmt_misc01.sh.

Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
---
 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh   | 7 ++-----
 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh | 9 +++++++++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh b/testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh
index 6c9aa64..06106e8 100755
--- a/testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh
+++ b/testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh
@@ -42,11 +42,8 @@ verify_binfmt_misc()
 	cat "$mntpoint/$name" >/dev/null 2>&1
 	tst_res TFAIL "Register a binary type successfully"
 
-	if [ -f "$mntpoint/$name" ]; then
-		(echo -1 >"$mntpoint/$name") 2>/dev/null
-		[ $? -ne 0 -o -f "$mntpoint/$name" ] && \
-			tst_res TWARN "Failed to remove a binary type"
-	fi
+	[ -f "$mntpoint/$name" ] && \
+		remove_binary_type "$mntpoint/$name"
 }
 
 do_test()
diff --git a/testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh b/testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh
index 5d00ab6..4976825 100755
--- a/testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh
+++ b/testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh
@@ -17,6 +17,15 @@ rmod_binfmt_misc=0
 umount_binfmt_misc=0
 binfmt_misc_mntpoint="ltp_binfmt_misc"
 
+remove_binary_type()
+{
+	local name=$1
+
+	(echo -1 >"$name") 2>/dev/null
+	[ $? -ne 0 -o -f "$name" ] && \
+		tst_res TWARN "Fail to remove a binary type"
+}
+
 get_binfmt_misc_mntpoint()
 {
 	local mntpoint
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [LTP] [PATCH 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality
  2019-02-22  9:59 [LTP] [PATCH 1/2] fs/binfmt_misc_lib.sh: Add common remove_binary_type() Xiao Yang
@ 2019-02-22  9:59 ` Xiao Yang
  2019-02-26 19:06   ` Cyril Hrubis
  0 siblings, 1 reply; 6+ messages in thread
From: Xiao Yang @ 2019-02-22  9:59 UTC (permalink / raw)
  To: ltp

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..5eb6bb8
--- /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=8
+TST_TESTFUNC=do_test
+TST_NEEDS_CMDS="perl"
+
+. binfmt_misc_lib.sh
+
+expect_recognised()
+{
+	local file=$1
+	local string=$2
+
+	eval $file >temp 2>&1
+	if [ $? -eq 0 ] && grep -q "$string" temp; then
+		tst_res TPASS "Register and recognise a binary type successfully"
+	else
+		tst_res TFAIL "Fail to recognise a binary type"
+	fi
+}
+
+expect_unrecognised()
+{
+	local file=$1
+	local string=$2
+
+	eval $file >temp 2>&1
+	if [ $? -ne 0 ] && ! grep -q "$string" temp; then
+		tst_res TPASS "Fail to recognise a binary type"
+	else
+		tst_res TFAIL "Recognise a binary type successfully"
+	fi
+}
+
+verify_binfmt_misc()
+{
+	local delimiter=${1:0:1}
+	local name=$(echo "$1" | awk -F $delimiter '{print $2}')
+	local ttype=$(echo "$1" | awk -F $delimiter '{print $3}')
+	local tfile=$2
+	local disabled=$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"
+
+	if [ "$disabled" = "1" ]; then
+		(echo 0 >"$mntpoint/$name") 2>/dev/null
+		if [ $? -ne 0 ] || grep -q enable "$mntpoint/$name"; then
+			remove_binary_type "$mntpoint/$name"
+			tst_res TFAIL "Fail to disable a binary type"
+			return
+		fi
+	fi
+
+	[ -z "$disabled" ] && expect_recognised "$tfile" "$tstring"
+	[ -n "$disabled" ] && expect_unrecognised "$tfile" "$tstring"
+
+	remove_binary_type "$mntpoint/$name"
+}
+
+do_test()
+{
+	case $1 in
+	1) verify_binfmt_misc ":textension:E::extension::$(which perl):" \
+			      "$TST_DATAROOT/file.extension";;
+	2) verify_binfmt_misc ":tmagic:M:1:print::$(which perl):" \
+			      "$TST_DATAROOT/file.magic";;
+	3) verify_binfmt_misc ".textension.E..extension..$(which perl)." \
+			      "$TST_DATAROOT/file.extension";;
+	4) verify_binfmt_misc ",tmagic,M,1,print,,$(which perl)," \
+			      "$TST_DATAROOT/file.magic";;
+	5) verify_binfmt_misc ":textension:E::ltp::$(which perl):" \
+			      "$TST_DATAROOT/file.extension" "0";;
+	6) verify_binfmt_misc ":tmagic:M:0:print::$(which perl):" \
+			      "$TST_DATAROOT/file.magic" "0";;
+	7) verify_binfmt_misc ":textension:E::extension::$(which perl):" \
+			      "$TST_DATAROOT/file.extension" "1";;
+	8) verify_binfmt_misc ":tmagic:M:1:print::$(which perl):" \
+			      "$TST_DATAROOT/file.magic" "1";;
+	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..054ee07
--- /dev/null
+++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.extension
@@ -0,0 +1 @@
+print "This is test for extension\n"
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..a018abf
--- /dev/null
+++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.magic
@@ -0,0 +1 @@
+ print "This is test for magic\n"
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [LTP] [PATCH 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality
  2019-02-22  9:59 ` [LTP] [PATCH 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality Xiao Yang
@ 2019-02-26 19:06   ` Cyril Hrubis
  2019-02-27  3:53     ` Xiao Yang
  2019-02-27  6:01     ` [LTP] [PATCH v2 " Xiao Yang
  0 siblings, 2 replies; 6+ messages in thread
From: Cyril Hrubis @ 2019-02-26 19:06 UTC (permalink / raw)
  To: ltp

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

Generally looks good, couple of comments below.

> 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..5eb6bb8
> --- /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=8
> +TST_TESTFUNC=do_test
> +TST_NEEDS_CMDS="perl"

I wonder if we can avoid perl dependency by (mis)using cat instead of
proper interpreter.

> +. binfmt_misc_lib.sh
> +
> +expect_recognised()
> +{
> +	local file=$1
> +	local string=$2
> +
> +	eval $file >temp 2>&1
> +	if [ $? -eq 0 ] && grep -q "$string" temp; then
> +		tst_res TPASS "Register and recognise a binary type successfully"
> +	else
> +		tst_res TFAIL "Fail to recognise a binary type"
> +	fi
> +}
> +
> +expect_unrecognised()
> +{
> +	local file=$1
> +	local string=$2
> +
> +	eval $file >temp 2>&1
> +	if [ $? -ne 0 ] && ! grep -q "$string" temp; then
> +		tst_res TPASS "Fail to recognise a binary type"
> +	else
> +		tst_res TFAIL "Recognise a binary type successfully"
> +	fi
> +}
> +
> +verify_binfmt_misc()
> +{
> +	local delimiter=${1:0:1}
                          ^
			  This is bashism should be replaced for example
			  with $(echo "$a" | head -c1)

> +	local name=$(echo "$1" | awk -F $delimiter '{print $2}')
> +	local ttype=$(echo "$1" | awk -F $delimiter '{print $3}')
> +	local tfile=$2
> +	local disabled=$3

Can't we just run each test twice once with enabled and once with
disabled binfmt handler?

> +	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"
> +
> +	if [ "$disabled" = "1" ]; then
> +		(echo 0 >"$mntpoint/$name") 2>/dev/null
> +		if [ $? -ne 0 ] || grep -q enable "$mntpoint/$name"; then
> +			remove_binary_type "$mntpoint/$name"
> +			tst_res TFAIL "Fail to disable a binary type"
> +			return
> +		fi
> +	fi
> +
> +	[ -z "$disabled" ] && expect_recognised "$tfile" "$tstring"
> +	[ -n "$disabled" ] && expect_unrecognised "$tfile" "$tstring"
> +
> +	remove_binary_type "$mntpoint/$name"
> +}
> +
> +do_test()
> +{
> +	case $1 in
> +	1) verify_binfmt_misc ":textension:E::extension::$(which perl):" \
> +			      "$TST_DATAROOT/file.extension";;
> +	2) verify_binfmt_misc ":tmagic:M:1:print::$(which perl):" \
> +			      "$TST_DATAROOT/file.magic";;
> +	3) verify_binfmt_misc ".textension.E..extension..$(which perl)." \
> +			      "$TST_DATAROOT/file.extension";;
> +	4) verify_binfmt_misc ",tmagic,M,1,print,,$(which perl)," \
> +			      "$TST_DATAROOT/file.magic";;
> +	5) verify_binfmt_misc ":textension:E::ltp::$(which perl):" \
> +			      "$TST_DATAROOT/file.extension" "0";;
> +	6) verify_binfmt_misc ":tmagic:M:0:print::$(which perl):" \
> +			      "$TST_DATAROOT/file.magic" "0";;
> +	7) verify_binfmt_misc ":textension:E::extension::$(which perl):" \
> +			      "$TST_DATAROOT/file.extension" "1";;
> +	8) verify_binfmt_misc ":tmagic:M:1:print::$(which perl):" \
> +			      "$TST_DATAROOT/file.magic" "1";;
> +	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..054ee07
> --- /dev/null
> +++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.extension
> @@ -0,0 +1 @@
> +print "This is test for extension\n"
> 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..a018abf
> --- /dev/null
> +++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.magic
> @@ -0,0 +1 @@
> + print "This is test for magic\n"
> -- 
> 1.8.3.1
> 
> 
> 

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [LTP] [PATCH 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality
  2019-02-26 19:06   ` Cyril Hrubis
@ 2019-02-27  3:53     ` Xiao Yang
  2019-02-27  6:01     ` [LTP] [PATCH v2 " Xiao Yang
  1 sibling, 0 replies; 6+ messages in thread
From: Xiao Yang @ 2019-02-27  3:53 UTC (permalink / raw)
  To: ltp

On 2019/02/27 3:06, Cyril Hrubis wrote:
> Hi!
>> Register a new binary type and then check if binfmt_misc
>> recognises the binary type in some conditions.
> Generally looks good, couple of comments below.
>
>> 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..5eb6bb8
>> --- /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=8
>> +TST_TESTFUNC=do_test
>> +TST_NEEDS_CMDS="perl"
> I wonder if we can avoid perl dependency by (mis)using cat instead of
> proper interpreter.

Hi Cyril,

Yes, we can use cat instead. :-)

>> +. binfmt_misc_lib.sh
>> +
>> +expect_recognised()
>> +{
>> +	local file=$1
>> +	local string=$2
>> +
>> +	eval $file>temp 2>&1
>> +	if [ $? -eq 0 ]&&  grep -q "$string" temp; then
>> +		tst_res TPASS "Register and recognise a binary type successfully"
>> +	else
>> +		tst_res TFAIL "Fail to recognise a binary type"
>> +	fi
>> +}
>> +
>> +expect_unrecognised()
>> +{
>> +	local file=$1
>> +	local string=$2
>> +
>> +	eval $file>temp 2>&1
>> +	if [ $? -ne 0 ]&&  ! grep -q "$string" temp; then
>> +		tst_res TPASS "Fail to recognise a binary type"
>> +	else
>> +		tst_res TFAIL "Recognise a binary type successfully"
>> +	fi
>> +}
>> +
>> +verify_binfmt_misc()
>> +{
>> +	local delimiter=${1:0:1}
>                            ^
> 			  This is bashism should be replaced for example
> 			  with $(echo "$a" | head -c1)
>
>> +	local name=$(echo "$1" | awk -F $delimiter '{print $2}')
>> +	local ttype=$(echo "$1" | awk -F $delimiter '{print $3}')
>> +	local tfile=$2
>> +	local disabled=$3
> Can't we just run each test twice once with enabled and once with
> disabled binfmt handler?

For matched binary types, we can run test twice by enabled and disabled 
binfmt handler.
For mismatched binary types, it  seems enough to run test once by 
enabled binfmt handler.

Best Regards,
Xiao Yang

>> +	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"
>> +
>> +	if [ "$disabled" = "1" ]; then
>> +		(echo 0>"$mntpoint/$name") 2>/dev/null
>> +		if [ $? -ne 0 ] || grep -q enable "$mntpoint/$name"; then
>> +			remove_binary_type "$mntpoint/$name"
>> +			tst_res TFAIL "Fail to disable a binary type"
>> +			return
>> +		fi
>> +	fi
>> +
>> +	[ -z "$disabled" ]&&  expect_recognised "$tfile" "$tstring"
>> +	[ -n "$disabled" ]&&  expect_unrecognised "$tfile" "$tstring"
>> +
>> +	remove_binary_type "$mntpoint/$name"
>> +}
>> +
>> +do_test()
>> +{
>> +	case $1 in
>> +	1) verify_binfmt_misc ":textension:E::extension::$(which perl):" \
>> +			      "$TST_DATAROOT/file.extension";;
>> +	2) verify_binfmt_misc ":tmagic:M:1:print::$(which perl):" \
>> +			      "$TST_DATAROOT/file.magic";;
>> +	3) verify_binfmt_misc ".textension.E..extension..$(which perl)." \
>> +			      "$TST_DATAROOT/file.extension";;
>> +	4) verify_binfmt_misc ",tmagic,M,1,print,,$(which perl)," \
>> +			      "$TST_DATAROOT/file.magic";;
>> +	5) verify_binfmt_misc ":textension:E::ltp::$(which perl):" \
>> +			      "$TST_DATAROOT/file.extension" "0";;
>> +	6) verify_binfmt_misc ":tmagic:M:0:print::$(which perl):" \
>> +			      "$TST_DATAROOT/file.magic" "0";;
>> +	7) verify_binfmt_misc ":textension:E::extension::$(which perl):" \
>> +			      "$TST_DATAROOT/file.extension" "1";;
>> +	8) verify_binfmt_misc ":tmagic:M:1:print::$(which perl):" \
>> +			      "$TST_DATAROOT/file.magic" "1";;
>> +	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..054ee07
>> --- /dev/null
>> +++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.extension
>> @@ -0,0 +1 @@
>> +print "This is test for extension\n"
>> 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..a018abf
>> --- /dev/null
>> +++ b/testcases/kernel/fs/binfmt_misc/datafiles/file.magic
>> @@ -0,0 +1 @@
>> + print "This is test for magic\n"
>> -- 
>> 1.8.3.1
>>
>>
>>




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [LTP] [PATCH v2 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality
  2019-02-26 19:06   ` Cyril Hrubis
  2019-02-27  3:53     ` Xiao Yang
@ 2019-02-27  6:01     ` Xiao Yang
  2019-03-05 13:24       ` Cyril Hrubis
  1 sibling, 1 reply; 6+ messages in thread
From: Xiao Yang @ 2019-02-27  6:01 UTC (permalink / raw)
  To: ltp

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




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [LTP] [PATCH v2 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality
  2019-02-27  6:01     ` [LTP] [PATCH v2 " Xiao Yang
@ 2019-03-05 13:24       ` Cyril Hrubis
  0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2019-03-05 13:24 UTC (permalink / raw)
  To: ltp

Hi!
Applied, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-03-05 13:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-22  9:59 [LTP] [PATCH 1/2] fs/binfmt_misc_lib.sh: Add common remove_binary_type() Xiao Yang
2019-02-22  9:59 ` [LTP] [PATCH 2/2] fs/binfmt_misc02.sh: Add new test for basic functionality Xiao Yang
2019-02-26 19:06   ` Cyril Hrubis
2019-02-27  3:53     ` Xiao Yang
2019-02-27  6:01     ` [LTP] [PATCH v2 " Xiao Yang
2019-03-05 13:24       ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox