#!/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
#
#
# testread4
#
# 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
# non-UNIX (non-local) domain sockets.
#
# This should be run at level 2.
#
#########################################################################

use Socket;
use lomacpred;

$serverport = 28376;
$proto = getprotobyname( 'tcp' );
$message = "this is the message";


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

# make parent a server on localhost $serverport
socket( ServerSocket, PF_INET, SOCK_STREAM, $proto ) || die "socket: $!\n";
bind( ServerSocket, sockaddr_in($serverport,INADDR_ANY) ) || die "bind: $!\n";

#
# create a child to be a client
#
$child_pid = fork;
if( !defined $child_pid ) {
    die "can't fork: $!\n";
}

if( $child_pid == 0 ) {
    # beginning of 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 );
    }

    # create an INET socket
    $serverhostname = 'localhost';
    $iaddr = inet_aton( $serverhostname ) || die "can't lookup IP: $!\n";
    $paddr = sockaddr_in( $serverport, $iaddr );
    socket( SOCK, PF_INET, SOCK_STREAM, $proto ) || die "socket: $!\n";
    connect( SOCK, $paddr ) || die "connect: $!\n";

    print "Test read from INET socket:\n";
    $fromsock = <SOCK>;
    if( ( $fromsock = $message ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    close( SOCK );
    exit( 0 );

} # end of child

#
# parent here
#

listen( ServerSocket, SOMAXCONN ) || die "listen: $!\n";
accept( ClientSocket, ServerSocket ) || die "accept: $!\n";
print ClientSocket $message;
close( ClientSocket );
close( ServerSocket );

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

exit( 0 );
