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

use Socket;
use lomacpred;

$socketname  = "\000123";

$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" );

#
# Fork off server
#

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

    # bind server socket
    $uaddr = sockaddr_un($socketname);
    socket( Server, PF_UNIX, SOCK_STREAM, 0 ) || die "server socket: $!";
    bind( Server, $uaddr ) || die "server bind: $!";

    # handle connections
    while( 1 ) {
	listen( Server, SOMAXCONN );
	accept( Client, Server ) || die "accept: $!";
	read( Client, $temp, $message_length );
	syswrite( Client, $temp, $message_length );
	close( Client );
    }
    
} # server


# cheesy race-condition non-solution:
sleep( 3 );

#
# Fork off client
#

if( 0 == ( $client_pid = fork ) ) {
    # client

    setpgrp( 0, $$ );  # Put client into its own new pgrp.

    print( "Test client W/R to sever:\n" );
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) || die "client socket: $!";
    connect( SOCK, sockaddr_un( $socketname ) ) ||die "client connect: $!";
    # print SOCK $message1";
    # $back = <SOCK>;
    syswrite SOCK, $message1, $message_length;
    read( SOCK, $back, $message_length );
    close( SOCK );
    if( $message1 eq $back ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );
}

#
# Overall parent program.
#

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


exit( 0 );

# clean up
kill 'TERM', $server_pid;


exit( $ret_val );
