#!/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
#
#
# testread1
#
# This script tests LOMAC's handling of the following system calls:
#    sys_read()
# This script tests the use of sys_read on unnamed pipes.
#
# This should be run at level 2.
#
#########################################################################

use lomacpred;

$lowfilename = "/tmp/testread1lowfile";
$message_one = "this is message one";
$message_two = "this is message two";

#
# 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(unnamed pipe). ***\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 first child holding write end of pipe to parent
$child_pid = open( PIPE_FROM_CHILD, "-|" );
if( !defined $child_pid ) {
    die "unable to fork: $!\n";
}

if( $child_pid == 0 ) {

    # first child
    setpgrp( 0, $$ );
    print( $message_one );
    exit( 0 );

} # end of first child

# parent

print( "Test read from pipe written by child of equal level:\n" );
$message_from_child = <PIPE_FROM_CHILD>;
if( ( &ProcAtLevelPred( 2 ) ) &&
    ( $message_from_child eq $message_one ) ) {
    print( "\tOK\n" );
} else {
    print( "\tFAILED\n" );
    exit( -1 );
}
close( PIPE_FROM_CHILD );

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

# Create second child holding write end of pipe to parent
$child_pid = open( PIPE_FROM_CHILD, "-|" );
if( !defined $child_pid ) {
    die "unable to fork: $!\n";
}

if( $child_pid == 0 ) {
    # second child
    # put second child into its own pgrp, get it demoted
    setpgrp( 0, $$ );
    open( LOWFILE, "< $lowfilename" ) ||
	die "child can't open $lowfile: $!\n";
    $garbage = <LOWFILE>;
    close( LOWFILE );
    if( !( &ProcAtLevelPred( 1 ) ) ) {
	die "child not demoted properly\n";
    }

    print( $message_two );
    exit( 0 );
} # end of second child

# should demote
print( "Test read from pipe written by child of lower level:\n" );
$message_from_child = <PIPE_FROM_CHILD>;
if( ( &ProcAtLevelPred( 1 ) ) &&
    ( $message_from_child eq $message_two ) ) {
    print( "\tOK\n" );
} else {
    print( "\tFAILED\n" );
    exit( -1 );
}    

close( PIPE_FROM_CHILD );

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

# clean up
unlink( $lowfilename );
exit( 0 );
