# Tests of DL_CONNECT_IND and DL_CONNECT_RES (max_conind, conn_mgmt tests)
# Tested:
#	1) cxn indication not received if stream not bound w/ max_conind > 0
#	2) cxn indication recvd on stream w/ max_conind > 0, if there are
#	    multiple streams bound to the same SAP
#	3) can reply to connect indication on another stream (using DL_TOKEN)
#	4) cxn indication recvd on stream with matching SAP if there is a
#	    conn mgmt stream
#	5) cxn indication recvd on conn mgmt stream if SAP doesn't match
#	    open stream.  (Accept connection on another stream.)
#	6) Receive two cxn indications simultaneously

###### Test case #1 ######
open 1
attach $1 43
bind $1 48 2 0 0 1

open 2
attach $2 43
bind $2 48 2 0 0 1

open 3
attach $3 43
bind $3 40 2 0 0 1
connect $3 0:0:0:0:0:0,48 0

# Have bound two streams to the same SAP, but neither with max_conind > 0
# Should not see events on either stream.
poll $1 $2 0

close $1
close $2
close $3

###### Test case #2 ######
open 1
attach $1 43
bind $1 48 2 0 0 1

open 2
attach $2 43
bind $2 48 2 0 0 1

open 3
attach $3 43
bind $3 48 2 3 0 1

open 4
attach $4 43
bind $4 40 2 0 0 1
connect $4 0:0:0:0:0:0,48 0

# Have bound three streams to the same SAP, and one of these streams (5)
# has max_conind > 0.  Should see an event on this stream.
poll $1 $4 $3

# Receive the incoming call, and respond to it
connect_ind $3 1 -

connect_con $4

send_data $4 '1 2 3 4 5'

poll $1 $4 $3
recv_data $3 '1 2 3 4 5'

close $1
close $2
close $3
close $4

###### Test case #3 ######
# Open listen stream
open 1
attach $1 43
bind $1 48 2 2 0 1

# Open the accepting stream
open 2
attach $2 43
bind $2 48 2 0 0 1

# Open connecting stream
open 3
attach $3 43
bind $3 40 2 0 0 1
connect $3 0:0:0:0:0:0,48 0

# Should see an event on the listen stream (3)
poll $1 $3 $1

# Receive the incoming call
connect_ind $1 0 corr3

check_state $1		dl_incon_pending

# Respond to the call, giving it to stream $2
token $2 token4
connect_res $1 $corr3 $token4

check_state $1		dl_idle

# Should see an event on the connecting stream (5)
poll $1 $3 $3

# Receive the connect confirmation
connect_con $3

# Now send data on stream $3, make sure that it arrives on
# stream $2 and not on stream $1

send_data $3 '1 2 3 4 5'

poll $1 $3 $2

recv_data $2 '1 2 3 4 5'

close $1
close $2
close $3


###### Test case #4 ######
# Open listen stream
open 1
attach $1 43
bind $1 48 2 2 0 1

# Open conn mgmt stream
open 2
attach $2 43
bind $2 0 2 2 1 1

# Open connecting stream
open 3
attach $3 43
bind $3 40 2 0 0 1
connect $3 0:0:0:0:0:0,48 0

# Should see an event on the listen stream (3)
poll $1 $3 $1

# Receive the incoming call, and respond to it
connect_ind $1 1 -

connect_con $3

close $1
close $2
close $3


###### Test case #5 ######
# Open listen stream
open 1
attach $1 43
bind $1 46 2 2 0 1

# Open conn mgmt stream
open 2
attach $2 43
bind $2 0 2 2 1 1

# Open connecting stream
open 3
attach $3 43
bind $3 40 2 0 0 1
connect $3 0:0:0:0:0:0,48 0

# Should see an event on the conn mgmt stream (4)
poll $1 $3 $2

# Receive the incoming call on the conn mgmt stream
connect_ind $2 0 corr4

# Open a stream to accept the connection on
open 4
attach $4 43
bind $4 48 2 0 0 1
token $4 token6

# Accept the call on the second stream
connect_res $2 $corr4 $token6

# Should see event on connecting stream
poll $1 $4 $3

connect_con $3

send_data $3 '1 2 3 4 5'

# Should see event on accepting stream
poll $1 $4 $4

recv_data $4 '1 2 3 4 5'

close $1
close $2
close $3
close $4


###### Test case #6 ######
# Open listen stream
open 1
attach $1 43
bind $1 48 2 2 0 1

# Open accepting stream #1
open 2
attach $2 43
bind $2 48 2 0 0 1

# Open accepting stream #2
open 3
attach $3 43
bind $3 48 2 0 0 1

# Open connecting stream #1
open 4
attach $4 43
bind $4 40 2 0 0 1
connect $4 0:0:0:0:0:0,48 0

poll $1 $4 $1

# Open connecting stream #2 (bound to a different SAP)
open 5
attach $5 43
bind $5 44 2 0 0 1
connect $5 0:0:0:0:0:0,48 0

poll $1 $5 $1

connect_ind $1 0 corr3

check_state $1		dl_incon_pending

# Accept the first connect request on accept stream #1
token $2 token4
connect_res $1 $corr3 $token4

# The listen stream should still be in state DL_INCON_PENDING, since there
# is still a DL_CONNECT_IND outstanding.
check_state $1		dl_incon_pending

connect_con $4

# Should still be an event on the listen stream (the second DL_CONNECT_IND)
poll $1 $5 $1

# Receive the second DL_CONNECT_IND
connect_ind $1 0 corr3

# Now accept the cxn on accept stream #2
token $3 token5
connect_res $1 $corr3 $token5

# Now the listen stream should be back in state DL_IDLE.
check_state $1		dl_idle

poll $1 $5 $5

connect_con $5

# Now double-check to ensure that data can be transferred on the two
# connections.
send_data $2 '1 2 3 4 5 6'

poll $1 $5 $4

recv_data $4 '1 2 3 4 5 6'

send_data $3 '7 8 9 a b c'

poll $1 $5 $5

recv_data $5 '7 8 9 a b c'

close $1
close $2
close $3
close $4
close $5
