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