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 <[email protected]> 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 25recognised_unrecognised() 26{ 27 local file=$1 28 local string="$2" 29 30 eval $file >temp 2>&1 31 if [ $? -ne 0 ] || ! grep -q "$string" temp; then 32 tst_res TFAIL "Fail to recognise a binary type" 33 return 34 fi 35 36 (echo 0 >"$mntpoint/$name") 2>/dev/null 37 if [ $? -ne 0 ] || grep -q enable "$mntpoint/$name"; then 38 tst_res TFAIL "Fail to disable a binary type" 39 return 40 fi 41 42 eval $file >temp 2>&1 43 if [ $? -eq 0 ] || grep -q "$string" temp; then 44 tst_res TFAIL "Recognise a disabled binary type successfully" 45 return 46 fi 47 48 tst_res TPASS "Recognise and unrecognise a binary type as expected" 49} 50 51unrecognised() 52{ 53 local file=$1 54 local string="$2" 55 56 eval $file >temp 2>&1 57 if [ $? -eq 0 ] || grep -q "$string" temp; then 58 tst_res TFAIL "Recognise a binary type successfully" 59 else 60 tst_res TPASS "Fail to recognise a binary type" 61 fi 62} 63 64verify_binfmt_misc() 65{ 66 local delimiter=$(echo "$1" | head -c1) 67 local name=$(echo "$1" | awk -F $delimiter '{print $2}') 68 local ttype=$(echo "$1" | awk -F $delimiter '{print $3}') 69 local tfile=$2 70 local valid=$3 71 local mntpoint=$(get_binfmt_misc_mntpoint) 72 73 (echo "$1" >"$mntpoint/register") 2>/dev/null 74 if [ $? -ne 0 -o ! -f "$mntpoint/$name" ]; then 75 tst_res TFAIL "Fail to register a binary type" 76 return 77 fi 78 79 [ "$ttype" = "E" ] && local tstring="This is test for extension" 80 [ "$ttype" = "M" ] && local tstring="This is test for magic" 81 82 [ "$valid" = "1" ] && recognised_unrecognised "$tfile" "$tstring" 83 [ "$valid" = "0" ] && unrecognised "$tfile" "$tstring" 84 85 remove_binary_type "$mntpoint/$name" 86} 87 88do_test() 89{ 90 local cat="$(command -v cat)" 91 92 case $1 in 93 1) verify_binfmt_misc ":textension:E::extension::$cat:" \ 94 "$TST_DATAROOT/file.extension" "1";; 95 2) verify_binfmt_misc ":tmagic:M:1:This::$cat:" \ 96 "$TST_DATAROOT/file.magic" "1";; 97 3) verify_binfmt_misc ".textension.E..extension..$cat." \ 98 "$TST_DATAROOT/file.extension" "1";; 99 4) verify_binfmt_misc ",tmagic,M,1,This,,$cat," \ 100 "$TST_DATAROOT/file.magic" "1";; 101 5) verify_binfmt_misc ":textension:E::ltp::$cat:" \ 102 "$TST_DATAROOT/file.extension" "0";; 103 6) verify_binfmt_misc ":tmagic:M:0:This::$cat:" \ 104 "$TST_DATAROOT/file.magic" "0";; 105 esac 106} 107 108. binfmt_misc_lib.sh 109tst_run 110