| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import ctypes |
| 21 |
import time |
| 22 |
import sys |
| 23 |
import os |
| 24 |
import re |
| 25 |
import subprocess |
| 26 |
from inc_cfg import * |
| 27 |
import inc_const |
| 28 |
import inc_util as util |
| 29 |
|
| 30 |
|
| 31 |
G_INUNIX = False |
| 32 |
if sys.platform.lower().find("win32")!=-1 or sys.platform.lower().find("microsoft")!=-1: |
| 33 |
G_INUNIX = False |
| 34 |
else: |
| 35 |
G_INUNIX = True |
| 36 |
|
| 37 |
|
| 38 |
FDEVNULL = None |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
SIPP_PATH = 'sipp' |
| 43 |
SIPP_PORT = 50070 |
| 44 |
SIPP_PARAM = "-m 1 -i 127.0.0.1 -p " + str(SIPP_PORT) |
| 45 |
SIPP_TIMEOUT = 60 |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
SIPP_BG_MODE = False |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
PJSUA_INST_PARAM = [] |
| 54 |
PJSUA_EXPECTS = [] |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
PJSUA_DEF_PARAM = "--null-audio --max-calls=1 --no-tcp --id=sip:a@localhost --username=a --realm=*" |
| 60 |
|
| 61 |
|
| 62 |
SIPP_SCEN_XML = "" |
| 63 |
if ARGS[1].endswith('.xml'): |
| 64 |
SIPP_SCEN_XML = ARGS[1] |
| 65 |
else: |
| 66 |
exit(-99) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
def resolve_pjsua_port(mo): |
| 71 |
return str(PJSUA_INST_PARAM[int(mo.group(1))].sip_port) |
| 72 |
|
| 73 |
def resolve_pjsua_uri(mo): |
| 74 |
return PJSUA_INST_PARAM[int(mo.group(1))].uri[1:-1] |
| 75 |
|
| 76 |
def resolve_driver_macros(st): |
| 77 |
st = re.sub("\$SIPP_PORT", str(SIPP_PORT), st) |
| 78 |
st = re.sub("\$SIPP_URI", "sip:[email protected]:"+str(SIPP_PORT), st) |
| 79 |
st = re.sub("\$PJSUA_PORT\[(\d+)\]", resolve_pjsua_port, st) |
| 80 |
st = re.sub("\$PJSUA_URI\[(\d+)\]", resolve_pjsua_uri, st) |
| 81 |
return st |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if os.access(SIPP_SCEN_XML[:-4]+".py", os.R_OK): |
| 86 |
|
| 87 |
cfg_file = util.load_module_from_file("cfg_file", SIPP_SCEN_XML[:-4]+".py") |
| 88 |
for ua_idx, ua_param in enumerate(cfg_file.PJSUA): |
| 89 |
ua_param = resolve_driver_macros(ua_param) |
| 90 |
PJSUA_INST_PARAM.append(InstanceParam("pjsua"+str(ua_idx), ua_param)) |
| 91 |
if DEFAULT_TELNET and hasattr(cfg_file, 'PJSUA_CLI_EXPECTS'): |
| 92 |
PJSUA_EXPECTS = cfg_file.PJSUA_CLI_EXPECTS |
| 93 |
else: |
| 94 |
PJSUA_EXPECTS = cfg_file.PJSUA_EXPECTS |
| 95 |
else: |
| 96 |
|
| 97 |
if os.path.basename(SIPP_SCEN_XML)[0:3] == "uas": |
| 98 |
|
| 99 |
ua_param = PJSUA_DEF_PARAM + " sip:127.0.0.1:" + str(SIPP_PORT) |
| 100 |
else: |
| 101 |
|
| 102 |
ua_param = PJSUA_DEF_PARAM + " --auto-answer=200" |
| 103 |
PJSUA_INST_PARAM.append(InstanceParam("pjsua", ua_param)) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def start_sipp(): |
| 108 |
global SIPP_BG_MODE |
| 109 |
sipp_proc = None |
| 110 |
|
| 111 |
sipp_param = SIPP_PARAM + " -sf " + SIPP_SCEN_XML |
| 112 |
if SIPP_BG_MODE: |
| 113 |
sipp_param = sipp_param + " -bg" |
| 114 |
if SIPP_TIMEOUT: |
| 115 |
sipp_param = sipp_param + " -timeout "+str(SIPP_TIMEOUT)+"s -timeout_error" + " -deadcall_wait "+str(SIPP_TIMEOUT)+"s" |
| 116 |
|
| 117 |
|
| 118 |
sipp_param = sipp_param + " 127.0.0.1:" + str(PJSUA_INST_PARAM[0].sip_port) |
| 119 |
|
| 120 |
|
| 121 |
fullcmd = os.path.normpath(SIPP_PATH) + " " + sipp_param |
| 122 |
print("Running SIPP: " + fullcmd) |
| 123 |
if SIPP_BG_MODE: |
| 124 |
sipp_proc = subprocess.Popen(fullcmd, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=G_INUNIX, universal_newlines=False) |
| 125 |
else: |
| 126 |
|
| 127 |
global FDEVNULL |
| 128 |
|
| 129 |
FDEVNULL = open("logs/sipp_output.tmp", 'w') |
| 130 |
sipp_proc = subprocess.Popen(fullcmd, shell=G_INUNIX, stdout=FDEVNULL, stderr=FDEVNULL) |
| 131 |
|
| 132 |
if not SIPP_BG_MODE: |
| 133 |
if sipp_proc == None or sipp_proc.poll(): |
| 134 |
return None |
| 135 |
return sipp_proc |
| 136 |
|
| 137 |
else: |
| 138 |
|
| 139 |
pid = 0 |
| 140 |
r = re.compile("PID=\[(\d+)\]", re.I) |
| 141 |
|
| 142 |
while True: |
| 143 |
line = sipp_proc.stdout.readline() |
| 144 |
pid_r = r.search(line) |
| 145 |
if pid_r: |
| 146 |
pid = int(pid_r.group(1)) |
| 147 |
break |
| 148 |
if not sipp_proc.poll(): |
| 149 |
break |
| 150 |
|
| 151 |
if pid != 0: |
| 152 |
|
| 153 |
if (sys.platform == "win32"): |
| 154 |
SYNCHRONIZE = 0x00100000 |
| 155 |
PROCESS_QUERY_INFORMATION = 0x0400 |
| 156 |
hnd = ctypes.windll.kernel32.OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, False, pid) |
| 157 |
pid = hnd |
| 158 |
|
| 159 |
return pid |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
def wait_sipp(sipp): |
| 164 |
if not SIPP_BG_MODE: |
| 165 |
global FDEVNULL |
| 166 |
sipp.wait() |
| 167 |
FDEVNULL.close() |
| 168 |
return sipp.returncode |
| 169 |
|
| 170 |
else: |
| 171 |
print("Waiting SIPp (PID=" + str(sipp) + ") to exit..") |
| 172 |
wait_cnt = 0 |
| 173 |
while True: |
| 174 |
try: |
| 175 |
wait_cnt = wait_cnt + 1 |
| 176 |
[pid_, ret_code] = os.waitpid(sipp, 0) |
| 177 |
if sipp == pid_: |
| 178 |
|
| 179 |
ret_code = ret_code >> 8 |
| 180 |
|
| 181 |
|
| 182 |
if (sys.platform == "win32"): |
| 183 |
ctypes.windll.kernel32.CloseHandle(sipp) |
| 184 |
|
| 185 |
return ret_code |
| 186 |
except os.error: |
| 187 |
if wait_cnt <= 5: |
| 188 |
print("Retry ("+str(wait_cnt)+") waiting SIPp..") |
| 189 |
else: |
| 190 |
return -99 |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
def exec_pjsua_expects(t, sipp): |
| 195 |
|
| 196 |
ua = [] |
| 197 |
for ua_idx in range(len(PJSUA_INST_PARAM)): |
| 198 |
ua.append(t.process[ua_idx]) |
| 199 |
|
| 200 |
ua_err_st = "" |
| 201 |
while len(PJSUA_EXPECTS): |
| 202 |
expect = PJSUA_EXPECTS.pop(0) |
| 203 |
ua_idx = expect[0] |
| 204 |
expect_st = expect[1] |
| 205 |
send_cmd = resolve_driver_macros(expect[2]) |
| 206 |
timeout = expect[3] if len(expect)>=4 else 0 |
| 207 |
|
| 208 |
try: |
| 209 |
if expect_st != "": |
| 210 |
if timeout > 0: |
| 211 |
ua[ua_idx].expect(expect_st, raise_on_error = True, timeout = timeout) |
| 212 |
else: |
| 213 |
ua[ua_idx].expect(expect_st, raise_on_error = True) |
| 214 |
if send_cmd != "": |
| 215 |
ua[ua_idx].send(send_cmd) |
| 216 |
except TestError as e: |
| 217 |
ua_err_st = e.desc |
| 218 |
break; |
| 219 |
except: |
| 220 |
ua_err_st = "Unknown error" |
| 221 |
break; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
return ua_err_st |
| 238 |
|
| 239 |
|
| 240 |
def sipp_err_to_str(err_code): |
| 241 |
if err_code == 0: |
| 242 |
return "All calls were successful" |
| 243 |
elif err_code == 1: |
| 244 |
return "At least one call failed" |
| 245 |
elif err_code == 97: |
| 246 |
return "exit on internal command. Calls may have been processed" |
| 247 |
elif err_code == 99: |
| 248 |
return "Normal exit without calls processed" |
| 249 |
elif err_code == -1: |
| 250 |
return "Fatal error (timeout)" |
| 251 |
elif err_code == -2: |
| 252 |
return "Fatal error binding a socket" |
| 253 |
else: |
| 254 |
return "Unknown error" |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
def TEST_FUNC(t): |
| 259 |
|
| 260 |
sipp_ret_code = 0 |
| 261 |
ua_err_st = "" |
| 262 |
|
| 263 |
sipp = start_sipp() |
| 264 |
if not sipp: |
| 265 |
raise TestError("Failed starting SIPp") |
| 266 |
|
| 267 |
ua_err_st = exec_pjsua_expects(t, sipp) |
| 268 |
|
| 269 |
sipp_ret_code = wait_sipp(sipp) |
| 270 |
|
| 271 |
if ua_err_st != "": |
| 272 |
raise TestError(ua_err_st) |
| 273 |
|
| 274 |
if sipp_ret_code: |
| 275 |
rc = ctypes.c_byte(sipp_ret_code).value |
| 276 |
raise TestError("SIPp returned error " + str(rc) + ": " + sipp_err_to_str(rc)) |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
test = TestParam(SIPP_SCEN_XML[:-4], |
| 281 |
PJSUA_INST_PARAM, |
| 282 |
TEST_FUNC) |