xref: /aosp_15_r20/external/ltp/testcases/commands/eject/eject-tests.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) International Business Machines Corp., 2001
4# Copyright (c) 2016 Cyril Hrubis <[email protected]>
5# Author: Manoj Iyer <[email protected]>
6#
7# Tests basic functionality of eject command.
8
9TST_CNT=4
10TST_SETUP=setup
11TST_CLEANUP=cleanup
12TST_TESTFUNC=test
13TST_NEEDS_TMPDIR=1
14TST_NEEDS_ROOT=1
15TST_NEEDS_CMDS="eject"
16
17setup()
18{
19	CD_DRIVE="/dev/cdrom"
20
21	if ! [ -e "$CD_DRIVE" ]; then
22		tst_brk TCONF "There is no "$CD_DRIVE""
23	fi
24
25	if grep -q "$CD_DRIVE" /proc/mounts; then
26		tst_brk TCONF "$CD_DRIVE is already mounted"
27	fi
28
29	ROD mkdir "cdrom"
30}
31
32cleanup()
33{
34	# We have to use the mount point since /dev/cdrom may be link to
35	# /dev/sr0 and because of that /dev/cdrom is not listed in /proc/mounts
36	tst_umount "$PWD/cdrom"
37}
38
39test1()
40{
41	EXPECT_PASS eject -d \> eject.out
42
43	if grep -q "eject: default device:" eject.out; then
44		tst_res TPASS "Eject listed default device"
45	else
46		tst_res TFAIL "Eject failed to list default device"
47		cat eject.out
48	fi
49}
50
51test2()
52{
53	EXPECT_PASS eject -v $CD_DRIVE \> eject.out
54
55	if grep -q "CD-ROM eject command succeeded" eject.out; then
56		# Close the tray if it is supported.
57	        eject -t $CD_DRIVE > /dev/null 2>&1
58		tst_res TPASS "Drive successfully ejected"
59	else
60		tst_res TFAIL "Eject failed"
61		cat eject.out
62	fi
63}
64
65mount_cdrom()
66{
67	local tries=100
68
69	# Wait for the drive to spin up the disk
70	while [ $tries -gt 0 ]; do
71		eject_check_tray $CD_DRIVE
72		if [ $? -eq 4 ]; then
73			break
74		fi
75		tst_sleep 100ms
76		tries=$((tries-1))
77	done
78
79	mount "$CD_DRIVE" cdrom/ > mount.out 2>&1
80	if [ $? -eq 32 ]; then
81		tst_res TCONF "Failed to mount $CD_DRIVE, no disk in drive?"
82		cat mount.out
83		return 0
84	fi
85
86	tst_res TINFO "$CD_DRIVE mounted sucessfully"
87
88	return 1
89}
90
91test3()
92{
93	if mount_cdrom; then
94		return
95	fi
96
97	test2
98
99	if grep -q "$CD_DRIVE" /proc/mounts; then
100		tst_res TFAIL "$CD_DRIVE is stil moutned"
101	else
102		tst_res TPASS "$CD_DRIVE umounted successfully"
103	fi
104}
105
106test4()
107{
108	if mount_cdrom; then
109		return
110	fi
111
112	EXPECT_PASS eject -a on $CD_DRIVE
113
114	eject_check_tray $CD_DRIVE
115	if [ $? -eq 2 ]; then
116		tst_brk TBROK "$CD_DRIVE is mounted but tray is open"
117	fi
118
119	EXPECT_PASS umount $CD_DRIVE
120
121	eject_check_tray $CD_DRIVE
122	if [ $? -eq 2 ]; then
123		tst_res TPASS "$CD_DRIVE was auto-ejected"
124	else
125		tst_res TFAIL "$CD_DRIVE was not auto-ejected"
126	fi
127
128	EXPECT_PASS eject -a off $CD_DRIVE
129
130	eject -t $CD_DRIVE > /dev/null 2>&1
131
132	if mount_cdrom; then
133		return
134	fi
135
136	EXPECT_PASS umount $CD_DRIVE
137
138	eject_check_tray $CD_DRIVE
139	if [ $? -eq 2 ]; then
140		tst_res TFAIL "$CD_DRIVE was auto-ejected"
141	else
142		tst_res TPASS "$CD_DRIVE was not auto-ejected"
143	fi
144}
145
146. tst_test.sh
147tst_run
148