Debian Codegraph @

Xpjproject/tests/pjsua/mod_recvfrom.py

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