Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 1 | # $Id$ |
Benny Prijono | e9a8224 | 2008-07-07 20:14:41 +0000 | [diff] [blame] | 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 | for t in cfg_file.recvfrom_cfg.transaction: |
| 24 | # Print transaction title |
| 25 | if t.title != "": |
| 26 | dlg.trace(t.title) |
| 27 | # Run command and expect patterns |
| 28 | for c in t.cmds: |
| 29 | if c[0] and c[0] != "": |
| 30 | pjsua.send(c[0]) |
| 31 | if len(c)>1 and c[1] and c[1] != "": |
| 32 | pjsua.expect(c[1]) |
| 33 | # Wait for request |
| 34 | if t.check_cseq: |
| 35 | # Absorbs retransmissions |
| 36 | cseq = 0 |
| 37 | method = last_method |
| 38 | while cseq <= last_cseq and method == last_method: |
| 39 | request, src_addr = dlg.wait_msg_from(10) |
| 40 | if request==None or request=="": |
| 41 | raise TestError("Timeout waiting for request") |
| 42 | method = request.split(" ", 1)[0] |
| 43 | cseq_hval = sip.get_header(request, "CSeq") |
| 44 | cseq_hval = cseq_hval.split(" ")[0] |
| 45 | cseq = int(cseq_hval) |
| 46 | last_cseq = cseq |
| 47 | last_method = method |
| 48 | else: |
| 49 | request, src_addr = dlg.wait_msg_from(10) |
| 50 | if request==None or request=="": |
| 51 | raise TestError("Timeout waiting for request") |
| 52 | |
| 53 | # Check for include patterns |
| 54 | for pat in t.include: |
| 55 | if re.search(pat, request, re.M | re.I)==None: |
| 56 | if t.title: |
| 57 | tname = " in " + t.title + " transaction" |
| 58 | else: |
| 59 | tname = "" |
| 60 | raise TestError("Pattern " + pat + " not found" + tname) |
| 61 | # Check for exclude patterns |
| 62 | for pat in t.exclude: |
| 63 | if re.search(pat, request, re.M | re.I)!=None: |
| 64 | if t.title: |
| 65 | tname = " in " + t.title + " transaction" |
| 66 | else: |
| 67 | tname = "" |
| 68 | raise TestError("Excluded pattern " + pat + " found" + tname) |
| 69 | # Create response |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 70 | if t.resp_code!=0: |
| 71 | response = dlg.create_response(request, t.resp_code, "Status reason") |
| 72 | # Add headers to response |
| 73 | for h in t.resp_hdr: |
| 74 | response = response + h + "\r\n" |
| 75 | # Add message body if required |
| 76 | if t.body: |
| 77 | response = response + t.body |
| 78 | # Send response |
| 79 | dlg.send_msg(response, src_addr) |
| 80 | |
Benny Prijono | e9a8224 | 2008-07-07 20:14:41 +0000 | [diff] [blame] | 81 | # Expect something to happen in pjsua |
| 82 | if t.expect != "": |
| 83 | pjsua.expect(t.expect) |
| 84 | # Sync |
| 85 | pjsua.sync_stdout() |
| 86 | |
| 87 | # Replace "$PORT" with server port in pjsua args |
| 88 | cfg_file.recvfrom_cfg.inst_param.arg = cfg_file.recvfrom_cfg.inst_param.arg.replace("$PORT", str(srv_port)) |
| 89 | |
| 90 | # Here where it all comes together |
| 91 | test = TestParam(cfg_file.recvfrom_cfg.name, |
| 92 | [cfg_file.recvfrom_cfg.inst_param], |
| 93 | test_func) |
| 94 | |