#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (C) 2000 NAI Labs.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.  This program
# 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 this program; if not, write
# to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#
# testwrite1
#
# This script tests LOMAC's handling of the following system calls:
#    sys_write()
# This script tests the use of sys_write on files.
#
# This should be run at level 2.
#
#########################################################################

use POSIX;
use lomacpred;

$lowfilename = "/tmp/testwrite1lowfile";
$highfilename = "/root/testwrite1highfile";
$message = "this is the message";
$message_length = 19;

#
# 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_write(file). ***\n" );

unlink( $lowfilename );
unlink( $highfilename );

#
# create first child
#
$child_pid = fork;
if( ! defined $child_pid ) {
    die "fork: $!\n";
}
if( $child_pid == 0 ) {
    # first child
    setpgrp( 0, $$ );  # put child into its own pgrp
    
    # Test high process write to a high-level file   
    print( "Test high process write to high file:\n" );
    $highfd = POSIX::open( $highfilename, 
			   ( POSIX::O_CREAT | POSIX::O_WRONLY ) );
    if( ! ( defined $highfd ) ) {
	die( "can't open $highfilename: $!\n" );
    }
    $bytes = POSIX::write( $highfd, $message, $message_length );
    if( defined $bytes ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::close( $highfd );

    #
    # test high process write to a low-level file    
    #

    # Test high process write to a low-level file   
    print( "Test high process write to low file:\n" );
    $lowfd = POSIX::open( $lowfilename, 
			   ( POSIX::O_CREAT | POSIX::O_WRONLY ) );
    if( ! ( defined $lowfd ) ) {
	die( "can't open $lowfilename: $!\n" );
    }
    $bytes = POSIX::write( $lowfd, $message, $message_length );
    if( defined $bytes ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::close( $lowfd );

    exit( 0 );

} # end of first child

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


#
# create second child
#
$child_pid = fork;
if( ! defined $child_pid ) {
    die "fork: $!\n";
}
if( $child_pid == 0 ) {
    # second child
    setpgrp( 0, $$ );  # put child into its own pgrp

    # open files before we are demoted
    $highfd = POSIX::open( $highfilename, POSIX::O_WRONLY );
    if( ! ( defined $highfd ) ) {
	die( "can't open $highfilename: $!\n" );
    }

    $lowfd = POSIX::open( $lowfilename, POSIX::O_RDWR );
    if( ! ( defined $lowfd ) ) {
	die( "can't open $lowfilename: $!\n" );
    }

    # demote child
    POSIX::read( $lowfd, $garbage, $message_length );
    if( !( &ProcAtLevelPred( 1 ) ) ) {
	die "child not demoted properly\n";
    }

    # Test low process write to a low-level file   
    print( "Test high process write to low file:\n" );
    $bytes = POSIX::write( $lowfd, $message, $message_length );
    if( defined $bytes ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::close( $lowfd );


    # Test low process write to a high-level file   
    print( "Test high process write to high file:\n" );
    $bytes = POSIX::write( $highfd, $message, $message_length );
    if( !( defined $bytes ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::close( $highfd );

    exit( 0 );
   
} # end of second child


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

# clean up
unlink( $lowfilename );
unlink( $highfilename );

exit( 0 );
