UDPSendRecv.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from TCAction import TCActionBase
  2. from NativeLog import NativeLog
  3. import time
  4. import random
  5. import string
  6. TEST_COUNT_ONE_ROUND = 1000
  7. class TestCase(TCActionBase.CommonTCActionBase):
  8. def __init__(self, test_case, test_env, timeout=30, log_path=TCActionBase.LOG_PATH):
  9. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  10. # load param from excel
  11. cmd_set = test_case["cmd set"]
  12. for i in range(1, len(cmd_set)):
  13. if cmd_set[i][0] != "dummy":
  14. cmd_string = "self." + cmd_set[i][0]
  15. exec cmd_string
  16. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  17. pass
  18. def cleanup(self):
  19. # step 0 turn on recv print
  20. checker_stings = ["R SSC1 C +RECVPRINT:1"]
  21. test_action_string = ["SSC SSC1 soc -R -o 1"]
  22. fail_string = "Fail, Fail to turn on recv print"
  23. self.load_and_exe_one_step(checker_stings, test_action_string, fail_string)
  24. pass
  25. def execute(self):
  26. TCActionBase.TCActionBase.execute(self)
  27. self.result_cntx.start()
  28. try:
  29. # configurable params
  30. send_len = self.send_len
  31. test_time = self.test_time * 60
  32. duplex = self.duplex
  33. conn_num = self.conn_num
  34. send_delay = self.send_delay
  35. # configurable params
  36. except StandardError, e:
  37. NativeLog.add_trace_critical("Error configuration for UDPSendRecv script, error is %s" % e)
  38. raise StandardError("Error configuration")
  39. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  40. password = "".join([random.choice(string.lowercase) for m in range(10)])
  41. # step 0 set ap
  42. checker_stings = ["R SSC1 C +SAP:OK"]
  43. test_action_string = ["SSC SSC1 ap -S -s %s -p %s -t 3" % (ssid, password)]
  44. fail_string = "Fail, Fail to set ap"
  45. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  46. return
  47. # step 1 connect to ap and turn off recv print
  48. checker_stings = ["R SSC2 C +JAP:CONNECTED"]
  49. test_action_string = ["SSC SSC2 sta -C -s %s -p %s" % (ssid, password)]
  50. fail_string = "Fail, Fail to connect to server"
  51. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=200) is False:
  52. return
  53. checker_stings = ["R SSC2 A <sta_ip>:\+STAIP:(\d+\.\d+\.\d+\.\d+)\r"]
  54. test_action_string = ["SSC SSC2 ip -Q -o 1"]
  55. fail_string = "Fail, Fail to connect to server"
  56. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=200) is False:
  57. return
  58. checker_stings = ["P SSC1 C +RECVPRINT:0", "P SSC2 C +RECVPRINT:0"]
  59. test_action_string = ["SSC SSC1 soc -R -o 0", "SSC SSC2 soc -R -o 0"]
  60. fail_string = "Fail, Fail to turn off recv print"
  61. self.load_and_exe_one_step(checker_stings, test_action_string, fail_string)
  62. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=200) is False:
  63. return
  64. # step 2 create conn_num udp socket
  65. for i in range(1, conn_num+1):
  66. checker_stings = ["R SSC1 A <t1_sock%d>:\+BIND:(\d+),OK" % i,
  67. "R SSC2 A <t2_sock%d>:\+BIND:(\d+),OK" % i]
  68. test_action_string = ["SSC SSC1 soc -B -t UDP -p <test_udp_port%i>" % i,
  69. "SSC SSC2 soc -B -t UDP -p <test_udp_port%i>" % i]
  70. fail_string = "Fail, Fail to create socket"
  71. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  72. return
  73. start_time = time.time()
  74. # step 3, do send/recv
  75. while time.time()-start_time < test_time:
  76. checker_stings = ["P SSC1 NC ERROR NC CLOSE NC ERROR"]
  77. for i in range(1, conn_num+1):
  78. test_action_string = ["SSC SSC2 soc -S -s <t2_sock%d> -l %d -n %d -j %d "
  79. "-i <target_ap_ip> -p <test_udp_port%d>" %
  80. (i, send_len, TEST_COUNT_ONE_ROUND, send_delay, i)]
  81. checker_stings.append("P SSC2 RE \"\+SEND:%%%%s,OK\"%%%%(<t2_sock%d>) NC ERROR NC CLOSE NC ERROR" % i)
  82. if duplex is True:
  83. test_action_string.append("SSC SSC1 soc -S -s <t1_sock%d> -l %d -n %d -j %d"
  84. " -i <sta_ip> -p <test_udp_port%d>" %
  85. (i, send_len, TEST_COUNT_ONE_ROUND, send_delay, i))
  86. checker_stings.append("P SSC1 RE \"\+SEND:%%%%s,OK\"%%%%(<t1_sock%d>)" % i)
  87. fail_string = "Fail, Failed on send command"
  88. if self.load_and_exe_one_step([], test_action_string, fail_string) is False:
  89. break
  90. time.sleep(1)
  91. fail_string = "Fail, Failed to send/recv data"
  92. if self.load_and_exe_one_step(checker_stings, ["DELAY 0.1"], fail_string,
  93. check_freq=1, check_time=300) is False:
  94. break
  95. pass
  96. NativeLog.add_prompt_trace("time escape: %s" % (time.time() - start_time))
  97. if time.time() - start_time >= test_time:
  98. self.result_cntx.set_result("Succeed")
  99. else:
  100. self.result_cntx.set_result("Fail")
  101. # finally, execute done
  102. def result_check(self, port_name, data):
  103. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  104. self.result_cntx.append_data(port_name, data)
  105. def main():
  106. pass
  107. if __name__ == '__main__':
  108. main()