#!/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
#
#
# testtruncate
#
# This script tests LOMAC's handling of the following system calls:
#    sys_truncate()
#
# 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_truncate(). ***\n" );

$lowfilename = "/tmp/testtruncatelowfile";
$highfilename = "/root/testtruncatehighfile";
unlink( $lowfilename );
unlink( $highfilename );

#
# create low and high files
#
open( HIGHFILE, ">$highfilename" ) || die "open $highfilename: $!\n";
print HIGHFILE "garbage";
close( HIGHFILE );
open( LOWFILE, ">$lowfilename" ) || die "open $lowfilename: $!\n";
print LOWFILE "garbage";
close( LOWFILE );

#
# Create a child to try truncating files
#

$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 truncate tests:
    #

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

    print( "Test high process truncate high file:\n" );
    $ret_val = truncate( $highfilename, 0 );
    if( defined $ret_val ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    #
    # demote child and do low process truncate tests
    #

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

    #
    # do low child truncate tests:
    #

    print( "Test low process truncate low file:\n" );
    $ret_val = truncate( $lowfilename, 0 );
    if( defined $ret_val ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process truncate high file:\n" );
    $ret_val = truncate( $highfilename, 0 );
    if( !( defined $ret_val ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );

} # end of child

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

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

exit( 0 );


