#!/bin/sh
#
# ASSIGN3.TEST
#
# To create a test script of your data base library put this file,
# assign3.test, your source files, and your compiled shell smallsh
# together in a directory.  Then run the command
#    sh assign3.test >& test_script
# Note this assumes you are running the C shell; the C shell is the default
# for itlabs accounts.  If you are running the Bourne or Korn shell the
# command would be "sh assign3.test > test_script 2>&1".  You must use the
# right command to capture both standard output and standard error in the
# test_script.
#
# This test should only take a minutes or so to run.
#
# When the test completes, edit the test_script output to include your
# name, student id, and email address at the top; if you work in a group
# be sure to include this information for all members of your group. 
#
# % sh assign3.test >& test_script
# % emacs test_script                # add your name, email address, and id
#
# This script assumes your source code is in files named:
# main.c procline.c runcommand.c and smallsh.h.  If you have added new
# files or use different file names for your source files, you will have
# to either modify this script below, where it says SPECIFY SOURCE FILES HERE,
# or you will have to edit the output of the test script so that it includes
# all your sources.
#
# Be sure to check the test_script file before you turn it in.  Be sure
# it has your name, id, and email address; be sure it includes a copy
# of all your source code, and be sure all the tests have run correctly.
# Note some of the tests will report errors even for a completely correct
# shell.  Don't depend on this script to test your program.
#

RM=/bin/rm
TESTDIR=assign3.test.$$

echo ==========================================================================
echo
echo CS 311 - Assignment III
echo
echo Name:
echo Student ID:
echo Email:
echo
echo Name:
echo Student ID:
echo Email:
echo
echo Name:
echo Student ID:
echo Email:
echo
echo
date
uname -a
whoami
echo
echo ==========================================================================
echo
echo
echo

# SPECIFY SOURCE FILES HERE
for file in smallsh.h main.c procline.c runcommand.c 
do

echo --------------------------------------------------------------------------
echo Source File: $file
echo
cat $file 2>&1
echo
echo

done

if test -d $TESTDIR
	then
		echo Please rename or remove $TESTDIR
		exit 1
fi

mkdir $TESTDIR

cp smallsh $TESTDIR

cd $TESTDIR

echo --------------------------------------------------------------------------
echo Building Test Program
cat > minitest.c <<___EOF___
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>


int main( int argc, char *argv[] )

{

	char c;
	int i,status;


	switch( atoi( argv[1] ) ) {

	case 1:
		for( i=0; i<argc; i++ )
			printf( "arg %d: |%s|\n", i, argv[i] );
		if( argv[argc] != NULL )
			printf( "arg %d is NOT NULL!: |%s|\n", argc, argv[argc] );
		else
			printf( "arg %d is NULL\n", argc );
		break;

	case 2:
		for( i=3; i<20; i++ ) {
			if( close( i ) == 0 )
				printf( "fd %d was open\n", i );
		}
		if( (status=fcntl( 0, F_GETFL )) == -1 ) {
			printf( "error getting file flags on stdin (%d)\n", errno );
		}
		else if( (status & O_ACCMODE) == O_RDONLY
		|| (status & O_ACCMODE) == O_RDWR ) {
			printf( "stdin is readable\n" );
		}
		else {
			printf( "stdin is NOT readable\n" );
		}
printf("fputs that herlocker thinks is 15 is %i\n", fputs( "this is stdout\n", stdout ));
		if( fputs( "this is stdout\n", stdout ) != 15 )
			printf( "write to stdout failed (%d)\n", errno );
		if( fputs( "this is stderr\n", stderr ) != 15 )
			printf( "write to stderr failed (%d)\n", errno );
		break;

	case 3:
		if( argc >= 3 ) {
			printf( "exiting with value %d\n", atoi( argv[2] ) );
			exit( atoi( argv[2] ) );
		}
		break;

	case 5:
		if( argc >= 3 ) {
			printf( "sending signal %d\n", atoi( argv[2] ) );
			fflush( stdout );
			kill( getpid( ), atoi( argv[2] ) );
			printf( "kill %s returned\n", argv[2] );
		}
		break;

	case 6:
		printf( "sending SIGINT (%d)\n", SIGINT );
		fflush( stdout );
		kill( getpid( ), SIGINT );
		printf( "kill SIGINT returned\n" );
		break;

	case 7:
		c = 'X';
		i = read( 0, &c, 1 );
		printf( "read returns %d, char read is %c\n", i, c );
		break;
	}

	return( 0 );

}
___EOF___
echo
cc -o minitest minitest.c
echo
cat > junk <<___EOF___
smallsh
is a miniature shell
___EOF___
echo
cat > bkgjunk <<___EOF___
smallsh
is a miniature shell
that can run commands
in the background
___EOF___
echo
echo

echo --------------------------------------------------------------------------
echo Testing smallsh
echo
./smallsh <<___EOF___
echo -------------------
echo Single Command Test
echo
echo ls
ls
echo
echo ps
ps
echo
echo
echo -------------------
echo Argument Test
echo
./minitest 1 2 3 4 five six seven eight 9 10 11 12 13 14 15 16 17
echo
echo
echo -------------------
echo Exit Status Test
echo
./minitest 3 0
status
echo
./minitest 3 17
status
echo
./minitest 5 1
status
echo
./minitest 6
status
echo
echo
echo -------------------
echo Redirection Test
echo
echo ls out junk1
ls > junk1
echo
echo ls
ls
echo
echo cat junk1
cat junk1
echo
echo tr m A in junk
tr m A < junk
echo
echo tr m B in junk out junk2
tr m B < junk > junk2
echo cat junk2
cat junk2
echo
echo tr M C out junk3 in junk
tr m C > junk3 < junk
echo cat junk3
cat junk3
echo
echo
echo -------------------
echo Input/Output Test
echo
./minitest 2
echo
echo minitest 2 in junk out junk4
./minitest 2 < junk > junk4
echo cat junk4
cat junk4
echo
echo
echo -------------------
echo Bad Command Test
echo
ps
echo
funnybadcommand
echo
ps
echo
echo
echo -------------------
echo Bad Redirection Test
echo
echo cat in badfile
cat < badfile
echo
echo chmod 0 junk - ls out junk
chmod 0 junk
ls > junk
echo
ps
echo
echo
echo -------------------
echo System Test - should fail
echo
echo ls | wc
ls | wc
echo rm junk*
rm junk*
echo
echo
echo -------------------
echo Background Test
echo
echo sleep 5 background
sleep 5 &
sleep 2
echo
ps
echo
sleep 5
echo
ps
echo
echo
echo -------------------
echo Background Input/Output Test
echo
./minitest 2 &
sleep 2
echo
./minitest 7 &
sleep 2
echo
echo cat in bkgjunk background
cat < bkgjunk &
echo
echo
echo -------------------
echo Background Redirection Test
echo
echo tr m M in bkgjunk out bkgjunk2 background
tr m M < bkgjunk > bkgjunk2 &
sleep 2
echo
echo cat bkgjunk2
cat bkgjunk2
echo
echo
echo -------------------
echo Background Exit Status Test
echo
./minitest 3 0 &
sleep 2
echo
./minitest 3 17 &
sleep 2
echo
./minitest 5 1 &
sleep 2
echo
./minitest 6 &
sleep 2
echo
echo
echo -------------------
echo Background Check PS
echo
ps
echo
echo
echo -------------------
echo CD Test
echo
mkdir subdir
echo cd subdir
cd subdir
pwd
echo
echo cd ..
cd ..
pwd
echo
echo cd
cd
pwd
echo
echo cd `pwd`
cd `pwd`
pwd
echo
echo
echo -------------------
echo Comment Test
echo
# this is a comment
echo
echo
echo -------------------
echo Blank Line Test
echo

    
echo
echo
echo -------------------
echo Exit Test
echo
exit
___EOF___

echo
echo
echo --------------------------------------------------------------------------
echo Shell SIGINT Test
ps
echo "sleep 10" | ./smallsh > /dev/null 2>&1 &
echo PS While Running
ps
echo
kill -2 $!
echo PS After SIGINT
ps
kill -9 $!
echo
echo
cd ..
$RM -rf $TESTDIR
