blob: 259083f5cd94dda975f0cc7063837081584ca1e6 [file] [log] [blame]
Benny Prijonoe9a82242008-07-07 20:14:41 +00001# $Id:$
2import imp
3import sys
4import inc_sip as sip
5import inc_const as const
6import re
7from inc_cfg import *
8
9# Read configuration
10cfg_file = imp.load_source("cfg_file", ARGS[1])
11
12# Default server port (should we randomize?)
13srv_port = 50070
14
15def 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
70 response = dlg.create_response(request, t.resp_code, "Status reason")
71 # Add headers to response
72 for h in t.resp_hdr:
73 response = response + h + "\r\n"
74 # Add message body if required
75 if t.body:
76 response = response + t.body
77 # Send response
78 dlg.send_msg(response, src_addr)
79 # Expect something to happen in pjsua
80 if t.expect != "":
81 pjsua.expect(t.expect)
82 # Sync
83 pjsua.sync_stdout()
84
85# Replace "$PORT" with server port in pjsua args
86cfg_file.recvfrom_cfg.inst_param.arg = cfg_file.recvfrom_cfg.inst_param.arg.replace("$PORT", str(srv_port))
87
88# Here where it all comes together
89test = TestParam(cfg_file.recvfrom_cfg.name,
90 [cfg_file.recvfrom_cfg.inst_param],
91 test_func)
92