# Tests of error conditions of DL_CONNECT_RES
# Tested:
#	1) DL_BADCORR -- pass wrong dl_correlation
#	2) DL_BADTOKEN -- pass invalid token value
#	3) DL_OUTSTATE -- attempt to send DL_CONNECT_RES on a stream that
#		didn't receive a DL_CONNECT_IND
#	4) DL_OUTSTATE -- attempt to accept connection on a second stream
#		that is not in the idle state
#	5) DL_ACCESS -- attempt to accept on a stream bound to a different SAP
#	6) DL_PENDING -- attempt to accept connection on conn mgmt stream
#	7) DL_PENDING -- attempt to accept connection on listen stream, when
#		there is more than one outstanding connect indication on the
#		stream
#	8) No connect indication received if already have 'max_conind'
#		outstanding indications
#	9) No connect indication received on listen stream if have already
#		accepted a connection on it

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

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

poll $1 $2 $1

connect_ind $1 0 corr3

# Send DL_CONNECT_RES, but with wrong dl_correlation = 99
connect_res $1 99 0		dl_error_ack dl_badcorr

close $1
close $2


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

# Open 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

poll $1 $3 $1

connect_ind $1 0 corr3
token $2 token4

# Send DL_CONNECT_RES, but with wrong dl_token = 2
connect_res $1 $corr3 2		dl_error_ack dl_badtoken

close $1
close $2
close $3


###### Test case #3 ######
open 1
connect_res $1 0 0		dl_error_ack dl_outstate

attach $1 43
connect_res $1 0 0		dl_error_ack dl_outstate

close $1

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

# Open accepting stream (but don't attach or bind it!)
open 2

# 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

poll $1 $3 $1

connect_ind $1 0 corr3
token $2 token4

# Now try to accept connection on stream that isn't even attached
# XXX Error should be DL_OUTSTATE, but to fix this would require modifying
# the llc2 driver to attach its private data to q_ptr when the stream
# is opened instead of when it is attached.
connect_res $1 $corr3 $token4	dl_error_ack dl_badtoken

attach $2 43

# Try to accept connection again, on a stream that hasn't been bound
connect_res $1 $corr3 $token4	dl_error_ack dl_outstate

bind $2 48 2 0 0 1

# Try to accept connection again, on a properly-bound stream
connect_res $1 $corr3 $token4

poll $1 $3 $3

connect_con $3

close $1
close $2
close $3


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

# Open accepting stream, bound to the wrong SAP
open 2
attach $2 43
bind $2 50 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

poll $1 $3 $1

connect_ind $1 0 corr3

token $2 token4
# Attempt to accept on stream with wrong SAP
connect_res $1 $corr3 $token4	dl_error_ack dl_access

close $1
close $2
close $3


###### Test case #6 ######
# 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 conn4

# Attempt to accept the call on the conn mgmt stream
connect_res $2 $conn4 0                       dl_error_ack dl_pending

close $1
close $2
close $3


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

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

poll $1 $2 $1

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

poll $1 $3 $1

connect_ind $1 0 corr3

# Try to accept first cxn on the listen stream, while the second cxn
# is still pending.
connect_res $1 $corr3 0			dl_error_ack dl_pending

poll $1 $3 $1

# Receive second connect indication.
connect_ind $1 0 corr4

# Try to accept second cxn on listen stream, while first cxn still
# pending.
connect_res $1 $corr4 0			dl_error_ack dl_pending

# As a sanity check, open a stream to accept the second cxn.
open 4
attach $4 43
bind $4 48 2 0 0 1
token $4 token6

# Accept the second cxn on the accept stream
connect_res $1 $corr4 $token6

poll $1 $4 $3

connect_con $3

# Should now be OK to accept the first cxn on the listen stream
connect_res $1 $corr3 0

poll $1 $4 $2

connect_con $2

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


###### Test case #8 ######
# Open listen stream, with 'max_conind' = 2
open 1
attach $1 43
bind $1 48 2 2 0 1

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

poll $1 $2 $1

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

poll $1 $3 $1

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

poll $1 $3 $1

# Receive connect indication #1
connect_ind $1 0 corr3

poll $1 $3 $1

# Receive connect indication #2
connect_ind $1 0 corr3

# Should be no more indications on the listen stream
poll $1 $3 0

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


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

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

poll $1 $2 $1

# Accept the connection on the listen stream
connect_ind $1 1 -

poll $1 $2 $2

connect_con $2

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

# Should be no indication on the listen stream
poll $1 $2 0

close $1
close $2
close $3
