# Tests of source routing
# Tested:
#	1) Connection setup and simple LLC2 data transfer
#	2) Test 
#	3) XID
#	4) LLC1 data transfer

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

# less than 2 bytes of source routing -> error
connect $2 0:0:0:0:0:0,48,1 1	dl_error_ack dl_badaddr

# more than 18 bytes of source routing -> error
connect $2 0:0:0:0:0:0,48,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2,3 1	dl_error_ack dl_badaddr

# valid source routing 
connect $2 0:0:0:0:0:0,48,1,2 0

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

# Receive the incoming call
connect_ind $1 1 -

# Should see an event on the connecting stream (4)
poll $1 $2 $2

# Receive the connect confirmation
connect_con $2

poll $1 $2 0

# Send data from fd $1 to fd $2
send_data $1 '1 2 3 4 5'

# Should see an event on the receiving stream
poll $1 $2 $2
recv_data $2 '1 2 3 4 5'

poll $1 $2 0

# Send data from fd $2 to fd $1
send_data $2 'aa bb cc dd ee ff'

# Should see an event on the receiving stream
poll $1 $2 $1
recv_data $1 'aa bb cc dd ee ff'

close $1
close $2

###### Test case #2 ######
open 1

attach $1 43
bind $1 48 1 0 0 0

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

# send less than 2 bytes of source routing -> error
test_req $2 0:0:0:0:0:0,48,1 0 1 ''	dl_error_ack dl_badaddr

# send more than 18 bytes of source routing -> error
test_req $2 0:0:0:0:0:0,48,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 0 1 ''	dl_error_ack dl_badaddr

# send valid amount of source routing
test_req $2 0:0:0:0:0:0,48,1,2 0 0 ''

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

test_ind $1 0

# send less than 2 bytes of source routing -> error
test_res $1 0:0:0:0:0:0,40,1 0 ''

# shouln't see anything
poll $1 $2 0

# send less than 2 bytes of source routing -> error
test_res $1 0:0:0:0:0:0,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 0 ''

# shouln't see anything
poll $1 $2 0

# send valid amount of source routing
test_res $1 0:0:0:0:0:0,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 0 ''

poll $1 $2 $2

test_con $2 0

poll $1 $2 0

check_state $1 dl_idle
check_state $2 dl_idle

close $1
close $2


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

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

# send less than 2 bytes of source routing
xid_req $2 0:0:0:0:0:0,48,1 0 1 ''	dl_error_ack dl_badaddr

# send more than 18 bytes of source routing
xid_req $2 0:0:0:0:0:0,48,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 0 1 ''	dl_error_ack dl_badaddr

# send valid amount of source routing
xid_req $2 0:0:0:0:0:0,48,1,2 0 0 ''

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

xid_ind $1 0

# send less than 2 bytes of source routing
xid_res $1 0:0:0:0:0:0,40,1 0 ''

# shouldn't see anything
poll $1 $2 0

# send more than 18 bytes of source routing
xid_res $1 0:0:0:0:0:0,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 1 ''

# shouldn't see anything
poll $1 $2 0

# send valid amount of source routing
xid_res $1 0:0:0:0:0:0,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 0 ''

poll $1 $2 $2

xid_con $2 0

poll $1 $2 0

check_state $1 dl_idle
check_state $2 dl_idle

close $1
close $2

###### Test case #4 ######
# Open and bind two LLC1 streams
open 1
attach $1 43
bind $1 40 1 0 0 0

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

# Send less than 2 bytes source routing
send_udata $1 0:0:0:0:0:0,48,1 '1 2 3 4 5'
recv_udata $1			dl_uderror_ind dl_badaddr

# Send more than 18 bytes source routing
send_udata $1 0:0:0:0:0:0,48,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 '1 2 3 4 5'
recv_udata $1			dl_uderror_ind dl_badaddr

# Send correct amount of source routing
send_udata $1 0:0:0:0:0:0,48,1,2 '1 2 3 4 5'

poll $1 $2 $2

recv_udata $2 '1 2 3 4 5'

poll $1 $2 0
check_state $1 dl_idle
check_state $2 dl_idle

# Send less than 2 bytes source routing
send_udata $2 0:0:0:0:0:0,40,1 '1 2 3 4 5'
recv_udata $2			dl_uderror_ind dl_badaddr

# Send more than 18 bytes source routing
send_udata $2 0:0:0:0:0:0,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 '1 2 3 4 5'
recv_udata $2			dl_uderror_ind dl_badaddr

# Now send data from fd $2 to fd $1
send_udata $2 0:0:0:0:0:0,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 'aa bb cc dd ee ff'

poll $1 $2 $1

recv_udata $1 'aa bb cc dd ee ff'

poll $1 $2 0
check_state $1 dl_idle
check_state $2 dl_idle

close $1
close $2

###### Test case #5 ######
# Open and bind two LLC1 streams in Ethernet mode
open 1
attach $1 43
bind $1 2000 1 0 0 0

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

# Send less than 2 bytes source routing
send_udata $1 0:0:0:0:0:0,20,40,1 '1 2 3 4 5'
recv_udata $1			dl_uderror_ind dl_badaddr

# Send more than 18 bytes source routing
send_udata $1 0:0:0:0:0:0,20,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 '1 2 3 4 5'
recv_udata $1			dl_uderror_ind dl_badaddr

# Send correct amount of source routing
send_udata $1 0:0:0:0:0:0,20,40,1,2 '1 2 3 4 5'

poll $1 $2 $2

recv_udata $2 '1 2 3 4 5'

poll $1 $2 0
check_state $1 dl_idle
check_state $2 dl_idle

# Send less than 2 bytes source routing
send_udata $2 0:0:0:0:0:0,20,00,1 '1 2 3 4 5'
recv_udata $2			dl_uderror_ind dl_badaddr

# Send more than 18 bytes source routing
send_udata $2 0:0:0:0:0:0,20,00,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 '1 2 3 4 5'
recv_udata $2			dl_uderror_ind dl_badaddr

# Now send data from fd $2 to fd $1
send_udata $2 0:0:0:0:0:0,20,00,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 'aa bb cc dd ee ff'

poll $1 $2 $1

recv_udata $1 'aa bb cc dd ee ff'

poll $1 $2 0
check_state $1 dl_idle
check_state $2 dl_idle

close $1
close $2
