Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | # $Id$ |
| 2 | import imp |
| 3 | import sys |
| 4 | import inc_sip as sip |
| 5 | import inc_const as const |
| 6 | import re |
| 7 | from inc_cfg import * |
| 8 | |
| 9 | # Read configuration |
| 10 | cfg_file = imp.load_source("cfg_file", ARGS[1]) |
| 11 | |
| 12 | # Default server port (should we randomize?) |
| 13 | srv_port = 50070 |
| 14 | |
| 15 | def test_func(test): |
| 16 | pjsua = test.process[0] |
| 17 | dlg = sip.Dialog("127.0.0.1", pjsua.inst_param.sip_port, |
| 18 | local_port=srv_port, |
| 19 | tcp=cfg_file.recvfrom_cfg.tcp) |
| 20 | |
| 21 | last_cseq = 0 |
| 22 | last_method = "" |
| 23 | last_call_id = "" |
| 24 | for t in cfg_file.recvfrom_cfg.transaction: |
| 25 | # Print transaction title |
| 26 | if t.title != "": |
| 27 | dlg.trace(t.title) |
| 28 | # Run command and expect patterns |
| 29 | for c in t.cmds: |
| 30 | if c[0] and c[0] != "": |
| 31 | pjsua.send(c[0]) |
| 32 | if len(c)>1 and c[1] and c[1] != "": |
| 33 | pjsua.expect(c[1]) |
| 34 | # Wait for request |
| 35 | if t.check_cseq: |
| 36 | # Absorbs retransmissions |
| 37 | cseq = 0 |
| 38 | method = last_method |
| 39 | call_id = last_call_id |
| 40 | while cseq <= last_cseq and method == last_method and call_id == last_call_id: |
| 41 | request, src_addr = dlg.wait_msg_from(30) |
| 42 | if request==None or request=="": |
| 43 | raise TestError("Timeout waiting for request") |
| 44 | method = request.split(" ", 1)[0] |
| 45 | cseq_hval = sip.get_header(request, "CSeq") |
| 46 | cseq_hval = cseq_hval.split(" ")[0] |
| 47 | cseq = int(cseq_hval) |
| 48 | call_id = sip.get_header(request, "Call-ID") |
| 49 | last_cseq = cseq |
| 50 | last_method = method |
| 51 | else: |
| 52 | request, src_addr = dlg.wait_msg_from(30) |
| 53 | if request==None or request=="": |
| 54 | raise TestError("Timeout waiting for request") |
| 55 | |
| 56 | # Check for include patterns |
| 57 | for pat in t.include: |
| 58 | if re.search(pat, request, re.M | re.I)==None: |
| 59 | if t.title: |
| 60 | tname = " in " + t.title + " transaction" |
| 61 | else: |
| 62 | tname = "" |
| 63 | raise TestError("Pattern " + pat + " not found" + tname) |
| 64 | # Check for exclude patterns |
| 65 | for pat in t.exclude: |
| 66 | if re.search(pat, request, re.M | re.I)!=None: |
| 67 | if t.title: |
| 68 | tname = " in " + t.title + " transaction" |
| 69 | else: |
| 70 | tname = "" |
| 71 | raise TestError("Excluded pattern " + pat + " found" + tname) |
| 72 | # Create response |
| 73 | if t.resp_code!=0: |
| 74 | response = dlg.create_response(request, t.resp_code, "Status reason") |
| 75 | # Add headers to response |
| 76 | for h in t.resp_hdr: |
| 77 | response = response + h + "\r\n" |
| 78 | # Add message body if required |
| 79 | if t.body: |
| 80 | response = response + t.body |
| 81 | # Send response |
| 82 | dlg.send_msg(response, src_addr) |
| 83 | |
| 84 | # Expect something to happen in pjsua |
| 85 | if t.expect != "": |
| 86 | pjsua.expect(t.expect) |
| 87 | # Sync |
| 88 | pjsua.sync_stdout() |
| 89 | |
| 90 | # Replace "$PORT" with server port in pjsua args |
| 91 | cfg_file.recvfrom_cfg.inst_param.arg = cfg_file.recvfrom_cfg.inst_param.arg.replace("$PORT", str(srv_port)) |
| 92 | |
| 93 | # Here where it all comes together |
| 94 | test = TestParam(cfg_file.recvfrom_cfg.name, |
| 95 | [cfg_file.recvfrom_cfg.inst_param], |
| 96 | test_func) |
| 97 | |