#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (c) 1999-2001 NAI Labs, Inc.  All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#   Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# 
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 
#   Neither the name of NAI Labs, Inc. nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# 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 );
