Home | History | Annotate | Download | only in eject
      1 #!/bin/sh
      2 ################################################################################
      3 ##                                                                            ##
      4 ## Copyright (c) International Business Machines  Corp., 2001                 ##
      5 ##  Author: Manoj Iyer, manjo (at] mail.utexas.edu                                 ##
      6 ## Copyright (c) 2016 Cyril Hrubis <chrubis (at] suse.cz>                          ##
      7 ##                                                                            ##
      8 ## This program is free software;  you can redistribute it and#or modify      ##
      9 ## it under the terms of the GNU General Public License as published by       ##
     10 ## the Free Software Foundation; either version 2 of the License, or          ##
     11 ## (at your option) any later version.                                        ##
     12 ##                                                                            ##
     13 ## This program is distributed in the hope that it will be useful, but        ##
     14 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
     15 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
     16 ## for more details.                                                          ##
     17 ##                                                                            ##
     18 ## You should have received a copy of the GNU General Public License          ##
     19 ## along with this program;  if not, write to the Free Software Foundation,   ##
     20 ## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           ##
     21 ##                                                                            ##
     22 ################################################################################
     23 #
     24 # Tests basic functionality of eject command.
     25 #
     26 
     27 TST_CNT=4
     28 TST_SETUP=setup
     29 TST_CLEANUP=cleanup
     30 TST_TESTFUNC=test
     31 TST_NEEDS_TMPDIR=1
     32 TST_NEEDS_ROOT=1
     33 TST_NEEDS_CMDS="eject"
     34 . tst_test.sh
     35 
     36 setup()
     37 {
     38 	CD_DRIVE="/dev/cdrom"
     39 
     40 	if ! [ -e "$CD_DRIVE" ]; then
     41 		tst_brk TCONF "There is no "$CD_DRIVE""
     42 	fi
     43 
     44 	if grep -q "$CD_DRIVE" /proc/mounts; then
     45 		tst_brk TCONF "$CD_DRIVE is already mounted"
     46 	fi
     47 
     48 	ROD mkdir "cdrom"
     49 }
     50 
     51 cleanup()
     52 {
     53 	# We have to use the mount point since /dev/cdrom may be link to
     54 	# /dev/sr0 and because of that /dev/cdrom is not listed in /proc/mounts
     55 	tst_umount "$PWD/cdrom"
     56 }
     57 
     58 test1()
     59 {
     60 	EXPECT_PASS eject -d \> eject.out
     61 
     62 	if grep -q "eject: default device:" eject.out; then
     63 		tst_res TPASS "Eject listed default device"
     64 	else
     65 		tst_res TFAIL "Eject failed to list default device"
     66 		cat eject.out
     67 	fi
     68 }
     69 
     70 test2()
     71 {
     72 	EXPECT_PASS eject -v $CD_DRIVE \> eject.out
     73 
     74 	if grep -q "CD-ROM eject command succeeded" eject.out; then
     75 		# Close the tray if it is supported.
     76 	        eject -t $CD_DRIVE > /dev/null 2>&1
     77 		tst_res TPASS "Drive successfully ejected"
     78 	else
     79 		tst_res TFAIL "Eject failed"
     80 		cat eject.out
     81 	fi
     82 }
     83 
     84 mount_cdrom()
     85 {
     86 	local tries=100
     87 
     88 	# Wait for the drive to spin up the disk
     89 	while [ $tries -gt 0 ]; do
     90 		eject_check_tray $CD_DRIVE
     91 		if [ $? -eq 4 ]; then
     92 			break
     93 		fi
     94 		tst_sleep 100ms
     95 		tries=$((tries-1))
     96 	done
     97 
     98 	mount "$CD_DRIVE" cdrom/ > mount.out 2>&1
     99 	if [ $? -eq 32 ]; then
    100 		tst_res TCONF "Failed to mount $CD_DRIVE, no disk in drive?"
    101 		cat mount.out
    102 		return 0
    103 	fi
    104 
    105 	tst_res TINFO "$CD_DRIVE mounted sucessfully"
    106 
    107 	return 1
    108 }
    109 
    110 test3()
    111 {
    112 	if mount_cdrom; then
    113 		return
    114 	fi
    115 
    116 	test2
    117 
    118 	if grep -q "$CD_DRIVE" /proc/mounts; then
    119 		tst_res TFAIL "$CD_DRIVE is stil moutned"
    120 	else
    121 		tst_res TPASS "$CD_DRIVE umounted succesfully"
    122 	fi
    123 }
    124 
    125 test4()
    126 {
    127 	if mount_cdrom; then
    128 		return
    129 	fi
    130 
    131 	EXPECT_PASS eject -a on $CD_DRIVE
    132 
    133 	eject_check_tray $CD_DRIVE
    134 	if [ $? -eq 2 ]; then
    135 		tst_brk TBROK "$CD_DRIVE is mounted but tray is open"
    136 	fi
    137 
    138 	EXPECT_PASS umount $CD_DRIVE
    139 
    140 	eject_check_tray $CD_DRIVE
    141 	if [ $? -eq 2 ]; then
    142 		tst_res TPASS "$CD_DRIVE was auto-ejected"
    143 	else
    144 		tst_res TFAIL "$CD_DRIVE was not auto-ejected"
    145 	fi
    146 
    147 	EXPECT_PASS eject -a off $CD_DRIVE
    148 
    149 	eject -t $CD_DRIVE > /dev/null 2>&1
    150 
    151 	if mount_cdrom; then
    152 		return
    153 	fi
    154 
    155 	EXPECT_PASS umount $CD_DRIVE
    156 
    157 	eject_check_tray $CD_DRIVE
    158 	if [ $? -eq 2 ]; then
    159 		tst_res TFAIL "$CD_DRIVE was auto-ejected"
    160 	else
    161 		tst_res TPASS "$CD_DRIVE was not auto-ejected"
    162 	fi
    163 }
    164 
    165 tst_run
    166