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