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