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