mod_recvfrom.py 3.7 KB

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