"""
<Program Name>
  z_test_state_machine_accept.repy

<Started>
  February 8, 2009

<Author>
  Richard Jordan

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

  Passes quietly
"""
include state_machine.repy

# no error
def test_syn_rcvd_accept():
  mycontext['sm'].state = mycontext['sm'].STATE_SYN_RCVD
  mycontext['sm'].accept()

def test_else_accept():
  try_accept(mycontext['sm'].STATE_CLOSED)
  try_accept(mycontext['sm'].STATE_LISTEN)
  try_accept(mycontext['sm'].STATE_SYN_SENT)
  try_accept(mycontext['sm'].STATE_ESTAB)


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

# Sets state to given state        
# And makes sure error is raised.
def try_accept(state):
  mycontext['sm'].state = state
  try:
    mycontext['sm'].accept()
  except NotRequestedError:
    pass
  else:  
    raise Exception("should have raised error")
  
if callfunc == 'initialize':
  # setup
  mycontext['sm'] = TcpStateMachine()
  
  # Run tests
  test_syn_rcvd_accept()
  test_else_accept()

  # teardown
  exitall()
