#!/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
#
#
# testrename
#
# This script tests LOMAC's handling of the following system calls:
#    sys_rename()
#
# This should be run at level 2, as root.
#
#########################################################################

use lomacpred;

#
# 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";
( 0 == $EUID ) || die "ERROR: You must run this test as root.\n";

print( "*** Testing sys_rename(). ***\n" );

$lowfilename_one = "/tmp/testrenamelowfile1";
$highfilename_one = "/root/testrenamehighfile1";
$lowfilename_two = "/tmp/testrenamelowfile2";
$highfilename_two = "/root/testrenamehighfile2";

#
# create low and high files
#
unlink( $lowfilename_one );
unlink( $highfilename_one );
unlink( $lowfilename_two );
unlink( $highfilename_two );
open( HIGHFILE, ">$highfilename_one" ) || die "open $highfilename_one: $!\n";
print HIGHFILE "garbage";
close( HIGHFILE );
open( LOWFILE, ">$lowfilename_one" ) || die "open $lowfilename_one: $!\n";
print LOWFILE "garbage";
close( LOWFILE );

#
# Create a child to run high-process rename tests
#

$child_pid = fork;
if( !( defined $child_pid ) ) {
    die "fork: $!\n";
}

if( $child_pid == 0 ) {
    # child here

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

    #
    # do high child rename tests:
    #

    print( "Test high process rename high file to high file:\n" );
    $ret_val = rename( $highfilename_one, $highfilename_two );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process rename low file to low file:\n" );
    $ret_val = rename( $lowfilename_one, $lowfilename_two );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process rename high file to low file:\n" );
    $ret_val = rename( $highfilename_two, $lowfilename_one );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process rename low file to high file:\n" );
    $ret_val = rename( $lowfilename_two, $highfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );

} # done with child for high-process tests

# parent here

#
# wait for child to exit
#

waitpid( $child_pid, 0 );

#
# create new files for next round of tests
#

unlink( $lowfilename_one );
unlink( $highfilename_one );
unlink( $lowfilename_two );
unlink( $highfilename_two );
open( HIGHFILE, ">$highfilename_one" ) || die "open $highfilename_one: $!\n";
print HIGHFILE "garbage";
close( HIGHFILE );
open( LOWFILE, ">$lowfilename_one" ) || die "open $lowfilename_one: $!\n";
print LOWFILE "garbage";
close( LOWFILE );

#
# fork a child to do low-process rename tests.
#

$child_pid = fork;
if( !( defined $child_pid ) ) {
    die "fork: $!\n";
}

if( $child_pid == 0 ) {
    # child here

    # Put child in its own new pgrp.
    print( "Putting child into its own pgrp:\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_one" ) ||
	die "child can't open $lowfilename_one: $!\n";
    print LOWFILE "garbage";
    $garbage = <LOWFILE>;
    close( LOWFILE );
    if( &ProcAtLevelPred( 1 ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    #
    # do low child rename tests:
    #

    print( "Test low process rename high file to high file:\n" );
    $ret_val = rename( $highfilename_one, $highfilename_two );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process rename low file to low file:\n" );
    $ret_val = rename( $lowfilename_one, $lowfilename_two );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process rename high file to low file:\n" );
    $ret_val = rename( $highfilename_two, $lowfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process rename low file to high file:\n" );
    $ret_val = rename( $lowfilename_two, $highfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );

} # done with child for low-process tests

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

# clean up
unlink( $lowfilename_one );
unlink( $highfilename_one );
unlink( $lowfilename_two );
unlink( $highfilename_two );

exit( 0 );


