#
# fcntl.test
#
# Tests for the fcntl command.
#---------------------------------------------------------------------------
# Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies.  Karl Lehenbauer and
# Mark Diekhans make no representations about the suitability of this
# software for any purpose.  It is provided "as is" without express or
# implied warranty.
#------------------------------------------------------------------------------
# $Id: fcntl.test,v 1.1.1.1 1999/06/28 20:35:24 damon Exp $
#------------------------------------------------------------------------------
#

if [cequal [info procs Test] {}] {source testlib.tcl}

close [open FCNTL1.TMP w]
close [open FCNTL2.TMP w]
close [open FCNTL3.TMP w]

set testRWFH [open FCNTL1.TMP r+]
set testRFH [open FCNTL2.TMP r]
set testWFH [open FCNTL3.TMP w]

pipe testRPH testWPH

Test fcntl-1.1 {fcntl tests} {
    fcntl $testRWFH RDONLY
} 0 0

Test fcntl-1.2 {fcntl tests} {
    fcntl $testRWFH WRONLY
} 0 0

Test fcntl-1.3 {fcntl tests} {
    fcntl $testRWFH RDWR
} 0 1

Test fcntl-1.4 {fcntl tests} {
    fcntl $testRWFH READ
} 0 1

Test fcntl-1.5 {fcntl tests} {
    fcntl $testRWFH WRITE
} 0 1


Test fcntl-1.6 {fcntl tests} {
    fcntl $testRFH RDONLY
} 0 1

Test fcntl-1.7 {fcntl tests} {
    fcntl $testRFH WRONLY
} 0 0

Test fcntl-1.8 {fcntl tests} {
    fcntl $testRFH RDWR
} 0 0

Test fcntl-1.9 {fcntl tests} {
    fcntl $testRFH READ
} 0 1

Test fcntl-1.10 {fcntl tests} {
    fcntl $testRFH WRITE
} 0 0


Test fcntl-1.11 {fcntl tests} {
    fcntl $testWFH RDONLY
} 0 0

Test fcntl-1.12 {fcntl tests} {
    fcntl $testWFH WRONLY
} 0 1

Test fcntl-1.13 {fcntl tests} {
    fcntl $testWFH RDWR
} 0 0

Test fcntl-1.14 {fcntl tests} {
    fcntl $testWFH READ
} 0 0

Test fcntl-1.15 {fcntl tests} {
    fcntl $testWFH WRITE
} 0 1


Test fcntl-2.1 {fcntl tests} {
    fcntl $testRWFH CLOEXEC 1
    fcntl $testRWFH CLOEXEC
} 0 1

Test fcntl-2.2 {fcntl tests} {
    fcntl $testRWFH CLOEXEC 0
    fcntl $testRWFH CLOEXEC
} 0 0 

Test fcntl-2.3 {fcntl tests} {
    fcntl $testRPH NONBLOCK 1
    list [fcntl $testRPH NONBLOCK] [fconfigure $testRPH -blocking]
} 0 {1 0}

Test fcntl-2.4 {fcntl tests} {
    fcntl $testRWFH append 1
    fcntl $testRWFH append
} 0 1 unixOnly

Test fcntl-2.5 {fcntl tests} {
    fcntl $testRWFH APPEND 0
    fcntl $testRWFH append
} 0 0 unixOnly

Test fcntl-2.5.1 {fcntl tests} {
    fcntl $testRWFH APPEND 0
} 1 {append mode is not available on MS Windows} pcOnly

Test fcntl-3.1 {fcntl tests} {
    fcntl $testRPH NONBLOCK 0
    list [fcntl $testRPH nonBlock] [fconfigure $testRPH -blocking]
} 0 {0 1}

Test fcntl-3.2 {fcntl tests} {
    fcntl $testRWFH NOBUF 1
    list [fcntl $testRWFH NObuf] [fconfigure $testRWFH -buffering]
} 0 {1 none}

close $testRWFH
close $testRFH
close $testWFH

# Reopen, can not have both nobuf and linebuf
set testRWFH [open FCNTL1.TMP w]  

Test fcntl-4.1 {fcntl tests} {
    fcntl $testRWFH LINEBUF 1
    list [fcntl $testRWFH LINEBUF] [fconfigure $testRWFH -buffering]
} 0 {1 line}

Test fcntl-5.1 {fcntl tests} {
    fcntl $testRWFH a b c
} 1 {wrong # args: fcntl handle attribute ?value?}

Test fcntl-5.2 {fcntl tests} {
    fcntl $testRWFH BAZ 1
} 1 {unknown attribute name "BAZ", expected one of RDONLY, WRONLY, RDWR, READ, WRITE, APPEND, CLOEXEC, NONBLOCK, LINEBUF, NOBUF, or KEEPALIVE}

Test fcntl-5.3 {fcntl tests} {
    fcntl $testRWFH APPEND FOO
} 1 {expected boolean value but got "FOO"}

close $testRWFH

Test fcntl-5.4 {fcntl tests} {
    fcntl $testRWFH RDONLY
} 1 "can not find channel named \"$testRWFH\""

close $testRPH
close $testWPH

#
# Tests for socket related controls.
#
set doSocketTests 0
# FIX: Should be an explict test for NT.
if {[cequal $tcl_platform(platform) unix] ||
    ![cequal $tcl_platform(os) "Windows 95"]} {
    if [catch {
        close [socket [id host] echo]
        set doSocketTests 1
    } msg] {
        puts "*************************************************************"
        puts "Unable to connect to the \"echo\" server:"
        puts "$msg."
        puts "Some tests skipped."
        puts "*************************************************************"
    }
}
if $doSocketTests {
    set sockFH [socket [id host] echo]

    Test fcntl-6.1 {fcntl socket tests} {
        fcntl $sockFH KEEPALIVE 1
        fcntl $sockFH KEEPALIVE
    } 0 1

    Test fcntl-6.2 {fcntl socket tests} {
        fcntl $sockFH KEEPALIVE 0
        fcntl $sockFH KEEPALIVE
    } 0 0

    close $sockFH
}

TestRemove FCNTL1.TMP FCNTL2.TMP FCNTL3.TMP


