• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3#
4# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
5# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
6#
7# Description:
8# Register a new binary type and then check if binfmt_misc
9# recognises the binary type in some conditions.
10# 1) binfmt_misc should recognise the binary type when extension
11#    or magic is matched.
12# 2) binfmt_misc should not recognise the binary type when extension
13#    or magic is mismatched.
14# 3) binfmt_misc should not recognise the binary type when it is
15#    disabled.
16#
17# Note:
18# We use various delimiteris to register a new binary type.
19
20TST_CNT=6
21TST_TESTFUNC=do_test
22TST_NEEDS_CMDS="cat head"
23
24. binfmt_misc_lib.sh
25
26recognised_unrecognised()
27{
28	local file=$1
29	local string="$2"
30
31	eval $file >temp 2>&1
32	if [ $? -ne 0 ] || ! grep -q "$string" temp; then
33		tst_res TFAIL "Fail to recognise a binary type"
34		return
35	fi
36
37	(echo 0 >"$mntpoint/$name") 2>/dev/null
38	if [ $? -ne 0 ] || grep -q enable "$mntpoint/$name"; then
39		tst_res TFAIL "Fail to disable a binary type"
40		return
41	fi
42
43	eval $file >temp 2>&1
44	if [ $? -eq 0 ] || grep -q "$string" temp; then
45		tst_res TFAIL "Recognise a disabled binary type successfully"
46		return
47	fi
48
49	tst_res TPASS "Recognise and unrecognise a binary type as expected"
50}
51
52unrecognised()
53{
54	local file=$1
55	local string="$2"
56
57	eval $file >temp 2>&1
58	if [ $? -eq 0 ] || grep -q "$string" temp; then
59		tst_res TFAIL "Recognise a binary type successfully"
60	else
61		tst_res TPASS "Fail to recognise a binary type"
62	fi
63}
64
65verify_binfmt_misc()
66{
67	local delimiter=$(echo "$1" | head -c1)
68	local name=$(echo "$1" | awk -F $delimiter '{print $2}')
69	local ttype=$(echo "$1" | awk -F $delimiter '{print $3}')
70	local tfile=$2
71	local valid=$3
72	local mntpoint=$(get_binfmt_misc_mntpoint)
73
74	(echo "$1" >"$mntpoint/register") 2>/dev/null
75	if [ $? -ne 0 -o ! -f "$mntpoint/$name" ]; then
76		tst_res TFAIL "Fail to register a binary type"
77		return
78	fi
79
80	[ "$ttype" = "E" ] && local tstring="This is test for extension"
81	[ "$ttype" = "M" ] && local tstring="This is test for magic"
82
83	[ "$valid" = "1" ] && recognised_unrecognised "$tfile" "$tstring"
84	[ "$valid" = "0" ] && unrecognised "$tfile" "$tstring"
85
86	remove_binary_type "$mntpoint/$name"
87}
88
89do_test()
90{
91	local cat="$(command -v cat)"
92
93	case $1 in
94	1) verify_binfmt_misc ":textension:E::extension::$cat:" \
95			      "$TST_DATAROOT/file.extension" "1";;
96	2) verify_binfmt_misc ":tmagic:M:1:This::$cat:" \
97			      "$TST_DATAROOT/file.magic" "1";;
98	3) verify_binfmt_misc ".textension.E..extension..$cat." \
99			      "$TST_DATAROOT/file.extension" "1";;
100	4) verify_binfmt_misc ",tmagic,M,1,This,,$cat," \
101			      "$TST_DATAROOT/file.magic" "1";;
102	5) verify_binfmt_misc ":textension:E::ltp::$cat:" \
103			      "$TST_DATAROOT/file.extension" "0";;
104	6) verify_binfmt_misc ":tmagic:M:0:This::$cat:" \
105			      "$TST_DATAROOT/file.magic" "0";;
106	esac
107}
108
109tst_run
110