binfmt_misc02.sh 2.9 KB

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