"""
<Program Name>
  z_test_state_machine_recv.repy

<Started>
  February 8, 2009

<Author>
  Richard Jordan

<Purpose>
  To test that TcpStateMachine's recv() function works.

  Passes quietly
"""
include state_machine.repy

def test_estab_recv():
  mycontext['sm'].state = mycontext['sm'].STATE_ESTAB
  mycontext['sm'].recv()

def test_else_recv_not_established():
  try_recv(mycontext['sm'].STATE_CLOSED)
  try_recv(mycontext['sm'].STATE_LISTEN)
  try_recv(mycontext['sm'].STATE_SYN_SENT)
  try_recv(mycontext['sm'].STATE_SYN_RCVD)

####################
# Test Helper Functions
####################

# Sets state to given state
# And makes sure error is raised.
def try_recv(state):
  mycontext['sm'].state = state
  try:
    mycontext['sm'].recv()
  except NotEstablishedError:
    pass
  else:
    raise Exception("should have raised error")
  
if callfunc == 'initialize':
  # setup
  mycontext['sm'] = TcpStateMachine()
  
  # Run tests
  test_estab_recv()
  test_else_recv_not_established()

  # teardown
  exitall()
