inc_cfg.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import random
  2. import config_site
  3. import socket
  4. import errno
  5. import time
  6. DEFAULT_ECHO = True
  7. DEFAULT_TRACE = True
  8. DEFAULT_START_SIP_PORT = 50000
  9. DEFAULT_TELNET = True
  10. DEFAULT_START_TELNET_PORT = 60000
  11. # Shared vars
  12. ARGS = [] # arguments containing script module & config
  13. HAS_SND_DEV = config_site.HAS_SND_DEV
  14. # Individual pjsua instance configuration class
  15. class InstanceParam:
  16. # Name to identify this pjsua instance (e.g. "caller", "callee", etc.)
  17. name = ""
  18. # pjsua command line arguments, concatenated in string
  19. arg = ""
  20. # Specify whether pjsua telnet CLI is enabled
  21. telnet_enabled = DEFAULT_TELNET
  22. # Telnet port number, zero to automatically assign
  23. telnet_port = 0
  24. # Specify whether pjsua output should be echoed to stdout
  25. echo_enabled = DEFAULT_ECHO
  26. # Enable/disable test tracing
  27. trace_enabled = DEFAULT_TRACE
  28. # SIP URI to send request to this instance
  29. uri = ""
  30. # SIP port number, zero to automatically assign
  31. sip_port = 0
  32. # Does this have registration? If yes then the test function will
  33. # wait until the UA is registered before doing anything else
  34. have_reg = False
  35. # Does this have PUBLISH?
  36. have_publish = False
  37. # Enable stdout buffer?
  38. enable_buffer = False
  39. def __init__( self,
  40. name, # Instance name
  41. arg, # Cmd-line arguments
  42. uri="", # URI
  43. uri_param="", # Additional URI param
  44. telnet_port=0, # Telnet port
  45. sip_port=0, # SIP port
  46. have_reg=False, # Have registration?
  47. have_publish=False, # Have publish?
  48. echo_enabled=DEFAULT_ECHO,
  49. trace_enabled=DEFAULT_TRACE,
  50. telnet_enabled = DEFAULT_TELNET,
  51. enable_buffer = False):
  52. # Instance name
  53. self.name = name
  54. # Give random telnet_port if it's not specified and telnet CLI is enabled
  55. if telnet_enabled and telnet_port==0:
  56. # avoid port conflict
  57. cnt = 0
  58. port = 0
  59. while cnt < 10:
  60. cnt = cnt + 1
  61. port = random.randint(DEFAULT_START_TELNET_PORT, 65534)
  62. s = socket.socket(socket.AF_INET)
  63. try:
  64. s.bind(("0.0.0.0", port))
  65. except socket.error as serr:
  66. s.close()
  67. if serr.errno == errno.EADDRINUSE or serr.errno == errno.EACCES:
  68. continue
  69. s.close()
  70. break;
  71. self.telnet_port = port
  72. else:
  73. self.telnet_port = telnet_port
  74. # Give random sip_port if it's not specified
  75. if sip_port==0:
  76. # avoid port conflict
  77. cnt = 0
  78. port = 0
  79. while cnt < 10:
  80. port = random.randint(DEFAULT_START_SIP_PORT, 60000)
  81. if port==self.telnet_port:
  82. continue
  83. cnt = cnt + 1
  84. s = socket.socket(socket.AF_INET)
  85. try:
  86. s.bind(("0.0.0.0", port))
  87. except socket.error as serr:
  88. s.close()
  89. if serr.errno == errno.EADDRINUSE or serr.errno == errno.EACCES:
  90. continue
  91. s.close()
  92. break;
  93. self.sip_port = port
  94. # Give some time for socket close
  95. time.sleep(0.5)
  96. else:
  97. self.sip_port = sip_port
  98. # Autogenerate URI if it's empty.
  99. self.uri = uri
  100. if self.uri=="":
  101. self.uri = "sip:pjsip@127.0.0.1:" + str(self.sip_port)
  102. # Add uri_param to the URI
  103. self.uri = self.uri + uri_param
  104. # Add bracket to the URI
  105. if self.uri[0] != "<":
  106. self.uri = "<" + self.uri + ">"
  107. # Add SIP local port to the argument
  108. self.arg = arg + " --local-port=" + str(self.sip_port)
  109. self.have_reg = have_reg
  110. self.have_publish = have_publish
  111. if have_publish and have_reg and not ("--publish" in self.arg):
  112. self.arg = self.arg + " --publish"
  113. self.echo_enabled = echo_enabled
  114. self.trace_enabled = trace_enabled
  115. self.enable_buffer = enable_buffer
  116. ############################################
  117. # Test parameter class
  118. class TestParam:
  119. title = ""
  120. # params is list containing InstanceParams objects
  121. inst_params = []
  122. # flag if this tes should be skipped
  123. skip = None
  124. # list of Expect instances, to be filled at run-time by
  125. # the test program
  126. process = []
  127. # the function for test body
  128. test_func = None
  129. post_func = None
  130. def __init__( self,
  131. title, # Test title
  132. inst_params, # InstanceParam's as list
  133. func=None,
  134. skip=False,
  135. post_func=None,
  136. need_stdout_buffer=False):
  137. self.title = title
  138. self.inst_params = inst_params
  139. self.skip = skip
  140. self.test_func = func
  141. self.post_func = post_func
  142. ###################################
  143. # TestError exception
  144. class TestError(Exception):
  145. desc = ""
  146. def __init__(self, desc):
  147. self.desc = desc