archie/tcl-dp/tests/udp.test
2024-05-27 16:13:40 +02:00

355 lines
7.4 KiB
Plaintext

# udp.test
#
# This file tests the udp protocol
#
if {[string compare test [info procs test]] == 1} then {source ../tests/defs}
# set VERBOSE 1
test udp-1.1.1 {dp_connect command} {
list [catch {
dp_connect udp -bar
} msg] $msg
} {1 {unknown option "-bar", must be -host, -myaddr, -myport or -port}}
test udp-1.1.2 {dp_connect command} {
list [catch {
dp_connect udp -bar foo
} msg] $msg
} {1 {unknown option "-bar", must be -host, -myaddr, -myport or -port}}
#
# Test arg missing checks
#
test udp-1.2.1 {dp_connect command} {
list [catch {
dp_connect udp -host
} msg] $msg
} {1 {value for "-host" missing}}
test udp-1.2.2 {dp_connect command} {
list [catch {
dp_connect udp -port
} msg] $msg
} {1 {value for "-port" missing}}
test udp-1.2.3 {dp_connect command} {
list [catch {
dp_connect udp -myaddr
} msg] $msg
} {1 {value for "-myaddr" missing}}
test udp-1.2.4 {dp_connect command} {
list [catch {
dp_connect udp -myport
} msg] $msg
} {1 {value for "-myport" missing}}
#
# Test type checking of args
#
test udp-1.3.1 {dp_connect command} {
list [catch {
dp_connect udp -host ""
} msg] $msg
} {1 {Unknown host ""}}
# This test must only be run if the client can communicate with
# the DNS
#test udp-1.3.2 {dp_connect command} {
# list [catch {
# dp_connect udp -host foo
# } msg] $msg
#} {1 {Unknown host "foo"}}
test udp-1.3.3 {dp_connect command} {
list [catch {
dp_connect udp -port foo
} msg] $msg
} {1 {expected integer but got "foo"}}
# Only run if we're on a network
#test udp-1.3.4 {dp_connect command} {
# list [catch {
# dp_connect udp -myaddr foo
# } msg] $msg
#} {1 {Illegal value for -myaddr "foo"}}
test udp-1.3.5 {dp_connect command} {
list [catch {
dp_connect udp -myport foo
} msg] $msg
} {1 {expected integer but got "foo"}}
set sock1 {}
set sock2 {}
test udp-1.3.6 {dp_connect command} {
catch {
set sock1 [dp_connect udp -host localhost -port 19065 -myport 19056];
set sock2 [dp_connect udp -host localhost -port 19056 -myport 19065];
}
} 0
# At this point, sock1 is either the null string or a handle to a udp
# socket. Only do remaining tests if we could create the socket.
if {$sock1 != ""} {
#
# fconfigure tests
#
test udp-1.4.1 {fconfigure udp} {
list [catch {
fconfigure $sock1
} msg] $msg
} {0 {-blocking 1 -buffering none -buffersize 4096 -eofchar {{} {}} -translation {lf lf} -sendBuffer 8192 -recvBuffer 8192 -peek 0 -host 127.0.0.1 -port 19065 -myport 19056}}
test udp-1.4.2 {fconfigure udp} {
list [catch {
fconfigure $sock1 -badopt
} msg] $msg
} {1 {bad option "-badopt": must be -blocking, -buffering, -buffersize, -eofchar, -translation, or a channel type specific option}}
test udp-1.4.3 {fconfigure udp} {
list [catch {
fconfigure $sock1 -myport 1000
} msg] $msg
} {1 {Can't set port after socket is opened}}
test udp-1.4.3 {fconfigure udp} {
list [catch {
fconfigure $sock2 -blocking 0
} msg] $msg
} {0 {}}
test udp-1.4.4 {fconfigure udp} {
list [catch {
fconfigure $sock2 -blocking
} msg] $msg
} {0 0}
test udp-1.4.5.1 {fconfigure udp} {
list [catch {
fconfigure $sock1 -sendBuffer 4096
} msg] $msg
} {0 {}}
test udp-1.4.5.2 {fconfigure udp} {
list [catch {
fconfigure $sock1 -sendBuffer
} msg] $msg
} {0 4096}
test udp-1.4.5.3 {fconfigure udp} {
list [catch {
fconfigure $sock1 -sendBuffer -1
} msg] $msg
} {1 {Buffer size must be > 0}}
test udp-1.4.5.4 {fconfigure udp} {
list [catch {
fconfigure $sock1 -sendBuffer foo
} msg] $msg
} {1 {expected integer but got "foo"}}
test udp-1.4.6.1 {fconfigure udp} {
list [catch {
fconfigure $sock1 -recvBuffer 4096
} msg] $msg
} {0 {}}
test udp-1.4.6.2 {fconfigure udp} {
list [catch {
fconfigure $sock1 -recvBuffer
} msg] $msg
} {0 4096}
test udp-1.4.6.3 {fconfigure udp} {
list [catch {
fconfigure $sock1 -recvBuffer -1
} msg] $msg
} {1 {Buffer size must be > 0}}
test udp-1.4.6.4 {fconfigure udp} {
list [catch {
fconfigure $sock1 -recvBuffer foo
} msg] $msg
} {1 {expected integer but got "foo"}}
test udp-1.4.7.1 {fconfigure udp} {
list [catch {
fconfigure $sock1 -peek 1
} msg] $msg
} {0 {}}
test udp-1.4.7.2 {fconfigure udp} {
list [catch {
fconfigure $sock1 -peek
} msg] $msg
} {0 1}
test udp-1.4.7.3 {fconfigure udp} {
list [catch {
fconfigure $sock1 -peek foo
} msg] $msg
} {1 {expected boolean value but got "foo"}}
test udp-1.4.7.4 {fconfigure udp} {
list [catch {
fconfigure $sock1 -peek 0
} msg] $msg
} {0 {}}
test udp-1.4.8.1 {fconfigure udp} {
list [catch {
fconfigure $sock1 -host 127.0.0.1
} msg] $msg
} {0 {}}
test udp-1.4.8.2 {fconfigure udp} {
list [catch {
fconfigure $sock1 -host
} msg] $msg
} {0 127.0.0.1}
test udp-1.4.8.3 {fconfigure udp} {
list [catch {
fconfigure $sock1 -host ftp.cs.cornell.edu
} msg1] $msg1 [catch {
fconfigure $sock1 -host
} msg2] $msg2
} {0 {} 0 128.84.154.10}
test udp-1.4.8.4 {fconfigure udp} {
list [catch {
fconfigure $sock1 -host localhost
} msg1] $msg1 [catch {
fconfigure $sock1 -host
} msg2] $msg2
} {0 {} 0 127.0.0.1}
test udp-1.4.9.1 {fconfigure udp} {
list [catch {
fconfigure $sock1 -port 2048
} msg1] $msg1
} {0 {}}
test udp-1.4.9.2 {fconfigure udp} {
list [catch {
fconfigure $sock1 -port
} msg1] $msg1
} {0 2048}
test udp-1.4.9.3 {fconfigure udp} {
list [catch {
fconfigure $sock1 -port -1
} msg1] $msg1
} {1 {Port number must be > 0}}
test udp-1.4.9.4 {fconfigure udp} {
list [catch {
fconfigure $sock1 -port foo
} msg1] $msg1
} {1 {expected integer but got "foo"}}
test udp-1.4.9.5 {fconfigure udp} {
list [catch {
fconfigure $sock1 -port 19065
} msg1] $msg1 [catch {
fconfigure $sock1 -port
} msg2] $msg2
} {0 {} 0 19065}
test udp-1.4.10 {fconfigure udp} {
list [catch {
fconfigure $sock1 -myport 1
} msg1] $msg1 [catch {
fconfigure $sock1 -myport
} msg2] $msg2
} {1 {Can't set port after socket is opened} 0 19056}
#
# Test send/receive
#
test udp-1.5.1 {send udp} {
list [catch {
puts -nonewline $sock1 "Testing 1 2 3"
flush $sock1
} msg] $msg
} {0 {}}
test udp-1.5.2 {read udp} {
list [catch {
read $sock2
} msg] $msg
} {0 {Testing 1 2 3}}
#
# Try out the peek option. We'll put 2 messages on the wire,
# and we should get the same one twice. If so, we clear the
# peek and we should get the two messages on the next two
# reads.
#
test udp-1.5.3 {peek udp} {
puts -nonewline $sock1 "12345678901"
flush $sock1
puts -nonewline $sock1 "12345678902"
flush $sock1
fconfigure $sock2 -peek 1
set x [list [read $sock2] [read $sock2]]
} {12345678901 12345678901}
test udp-1.5.4 {send udp} {
fconfigure $sock2 -peek 0
if {![string compare [lindex $x 1] "12345678901"]} {
set x [list [read $sock2] [read $sock2]]
} else {
set x {12345678901 12345678902}
}
} {12345678901 12345678902}
test udp-1.5.5 {dp_from variable} {
list [catch {
set a $dp_from
} msg] $msg
} {0 {{127.0.0.1 19056}}}
#
# Shut 'em down
#
test udp-1.9.0 {close udp socket} {
list [catch {
close $sock1
} msg] $msg
} {0 {}}
test udp-1.9.2 {close udp socket} {
list [catch {
close $sock2
} msg] $msg
} {0 {}}
test udp-1.9.3 {close udp socket} {
list [catch {
fconfigure $sock2
} msg] $msg
} [list 1 "can not find channel named \"$sock2\""]
# The following close brace matches the line above:
# if {$sock1 != ""}
}