#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux
# Copyright (c) 1999, 2000, 2001, 2002 Networks Associates
# Technology, Inc.  All rights reserved.
# 
# This file is part of LOMAC.
#
# LOMAC is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# LOMAC is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LOMAC; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# testread3
#
# This script tests LOMAC's handling of the following system calls:
#    sys_read()
# This script tests the use of sys_read when used to read from fifos.
#
# This should be run at level 2.
#
#########################################################################

use lomacpred;

$lowfilename = "/tmp/testread3lowfile";
$lowfifoname  = "/tmp/testread3lowfifo";
$highfifoname = "/root/testread3highfifo";
$messageone   = "this is message one";
$messagetwo   = "this is message two";
$messagethree = "this is message three";
$messagefour  = "this is message four";

unlink( $lowfilename );
unlink( $lowfifoname );
unlink( $highfifoname );

#
# Ensure that LOMAC is running and that we are running at level 2.
#
&ProcAtLevelPred( 2 ) || die "ERROR: You must run this test at level 2.\n";
print( "*** Testing sys_read(fifo). ***\n" );

#
# create a low-level file
#
print( "Creating low-level file:\n" );
open( LOWFILE, "> $lowfilename" ) || die "can't create $lowfilename: $!\n";
print LOWFILE "garbage\n";
close LOWFILE;
if( FileAtLevelPred( $lowfilename, 1 ) ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}
    
# create high fifo
print( "Create high FIFO:\n" );
if( system( "mkfifo $highfifoname" ) == 0 ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

# create low FIFO
print( "Create low FIFO:\n" );
if( system( 'mkfifo', $lowfifoname ) == 0 ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

#
# Create first child.
#
$child_pid = fork;
if( !defined $child_pid ) {
    die "can't fork: $!\n";
}

if( $child_pid == 0 ) {
    # beginning of first child

    # Put child in its own new pgrp.
    print( "Put child in its own job:\n" );
    setpgrp( 0, $$ );                   # put child in its own pgrp
    if( ( $$ == getpgrp( 0 ) ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # Test read from high fifo
    print "Test high process read from high fifo:\n";
    open( MYFIFO, "<$highfifoname" ) || die "can't open fifo: $!\n";
    $fromfifo = <MYFIFO>;
    if( ( $fromfifo eq $messageone ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( MYFIFO );

    # Test read from low fifo
    print "Test high process read from low fifo:\n";
    open( MYFIFO, "<$lowfifoname" ) || die "can't open fifo: $!\n";
    $fromfifo = <MYFIFO>;
    if( ( $fromfifo eq $messagetwo ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( MYFIFO );

    exit( 0 );
} # end of first child


# parent here
open( MYFIFO, ">>$highfifoname" ) || die "can't open fifo $!\n";
print MYFIFO $messageone;
close( MYFIFO );
open( MYFIFO, ">>$lowfifoname" ) || die "can't open fifo $!\n";
print MYFIFO $messagetwo;
close( MYFIFO );

# wait for child to exit
waitpid( $child_pid, 0 );

#
# Create second child.
#
$child_pid = fork;
if( !defined $child_pid ) {
    die "can't fork: $!\n";
}

if( $child_pid == 0 ) {
    # beginning of second child

    # Put child in its own new pgrp.
    print( "Put child in its own job:\n" );
    setpgrp( 0, $$ );                   # put child in its own pgrp
    if( ( $$ == getpgrp( 0 ) ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # demote child
    print( "Demoting child:\n" );
    open( LOWFILE, "< $lowfilename" ) ||
	die "child can't open $lowfilename: $!\n";
    $garbage = <LOWFILE>;
    close( LOWFILE );
    if( &ProcAtLevelPred( 1 ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # Test read from high fifo
    print "Test low process read from high fifo:\n";
    open( MYFIFO, "<$highfifoname" ) || die "can't open fifo: $!\n";
    $fromfifo = <MYFIFO>;
    if( ( $fromfifo eq $messagethree ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( MYFIFO );

    # Test read from low fifo
    print "Test low process read from low fifo:\n";
    open( MYFIFO, "<$lowfifoname" ) || die "can't open fifo: $!\n";
    $fromfifo = <MYFIFO>;
    if( ( $fromfifo eq $messagefour ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( MYFIFO );

    exit( 0 );
} # end of second child

# parent here
open( MYFIFO, ">>$highfifoname" ) || die "can't open fifo $!\n";
print MYFIFO $messagethree;
close( MYFIFO );
open( MYFIFO, ">>$lowfifoname" ) || die "can't open fifo $!\n";
print MYFIFO $messagefour;
close( MYFIFO );

# wait for child to exit
waitpid( $child_pid, 0 );

# clean up
unlink( $lowfilename );
unlink( $highfifoname );
unlink( $lowfifoname );

exit( 0 );

