#!/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.
#
#
# testsocketrw3
#
# This script tests LOMAC's handling of the following system calls:
#    sys_socketcall(SOCKETPAIR)
#    sys_read(socket)
#    sys_write(socket)
# This script tests the use of socketpair for unix-domain stream sockets.
# It also tests read and write
# on the socket, mainly to verify that the socketpair worked.
#
# This should be run at level 2.
#
#########################################################################

use Socket;
use lomacpred;

$message1 = "he'sdeadjim";
$message_length = 11;

#
# 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_socketcall(STREAM UNIX sockets). ***\n" );

socketpair( CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC ) ||
    die "socketpair: $!";

#
# Fork off child
#

if( 0 == ( $child_pid = fork ) ) {
    # child
    setpgrp( 0, $$ );  # Put child into its own new pgrp.

    read( CHILD, $temp, $message_length );
    syswrite( CHILD, $temp, $message_length );
    close( CHILD );
    exit( 0 );

} # child


print( "Test W/R over socketpair:\n" );
syswrite PARENT, $message1, $message_length;
read( PARENT, $back, $message_length );
close( PARENT );
if( $message1 eq $back ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

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

exit( $ret_val );
