UDPSTAMuitiSocketSendRecv.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from TCAction import TCActionBase
  2. from NativeLog import NativeLog
  3. import time
  4. import random
  5. import string
  6. class TestCase(TCActionBase.CommonTCActionBase):
  7. def __init__(self, test_case, test_env, timeout=45, log_path=TCActionBase.LOG_PATH):
  8. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  9. # load param from excel
  10. cmd_set = test_case["cmd set"]
  11. for i in range(1, len(cmd_set)):
  12. if cmd_set[i][0] != "dummy":
  13. cmd_string = "self." + cmd_set[i][0]
  14. exec cmd_string
  15. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  16. pass
  17. def execute(self):
  18. TCActionBase.TCActionBase.execute(self)
  19. self.result_cntx.start()
  20. try:
  21. # configurable params
  22. send_len = self.send_len
  23. test_time = self.test_time * 60
  24. server_echo = self.server_echo
  25. conn_number = self.conn_number
  26. sta_number = self.sta_number
  27. send_delay = self.send_delay
  28. # configurable params
  29. except StandardError, e:
  30. NativeLog.add_trace_critical("Error configuration for TCPTransparent script, error is %s" % e)
  31. raise StandardError("Error configuration")
  32. # step0 reboot
  33. for i in range(sta_number + 1):
  34. checker_stings = ["P SSC%d C !!!ready!!!" % (i + 1)]
  35. test_action_string = ["SSCC SSC%d reboot" % (i + 1)]
  36. fail_string = "Fail, Fail to reboot"
  37. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  38. return
  39. # turn off recv print
  40. for i in range(sta_number + 1):
  41. checker_stings = ["P SSC%d C +RECVPRINT:0" % (i + 1)]
  42. test_action_string = ["SSCC SSC%d soc -R -o 0" % (i + 1)]
  43. fail_string = "Fail, Fail to turn off recv print"
  44. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  45. return
  46. # step1, set softAP mode on SSC1
  47. checker_stings = ["R SSC1 C +MODE:OK"]
  48. test_action_string = ["SSCC SSC1 op -S -o 2"]
  49. fail_string = "Fail, Fail to set mode on SSC1"
  50. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  51. return
  52. # step2, set STA mode on SSC2-SSCn
  53. for i in range(sta_number):
  54. checker_stings = ["R SSC%d C +MODE:OK" % (i + 2)]
  55. test_action_string = ["SSCC SSC%d op -S -o 1" % (i + 2)]
  56. fail_string = "Fail, Fail to set mode on SSC%d" % (i + 2)
  57. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  58. return
  59. # step3, set ssid/password on SSC1
  60. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  61. password = "".join([random.choice(string.lowercase) for m in range(10)])
  62. checker_stings = ["R SSC1 C +SAP:OK"]
  63. test_action_string = ["SSCC SSC1 ap -S -s %s -p %s -n 10 -t 0 -m 8" % (ssid, password)]
  64. fail_string = "Fail, Fail to set ssid/password on SSC1"
  65. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  66. return
  67. # step4, all STA join SSC1(soft AP)
  68. for i in range(sta_number):
  69. checker_stings = ["R SSC%d C +JAP:CONNECTED,%s" % (i + 2, ssid)]
  70. test_action_string = ["SSCC SSC%d ap -C -s %s -p %s" % (i + 2, ssid, password)]
  71. fail_string = "Fail, Fail to connect to SSC1"
  72. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=450) is False:
  73. return
  74. # step5, get all the STA ip
  75. for i in range(sta_number):
  76. checker_stings = ["R SSC%d A <SSC%d_IP>:\+STAIP:192.168.4.(\d+)" % (i + 2, i + 2)]
  77. test_action_string = ["SSCC SSC%d ip -Q" % (i + 2)]
  78. fail_string = "Fail, Fail to get SSC%d ip" % (i + 2)
  79. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  80. return
  81. else:
  82. print "SSC%d ip is:" % (i + 2), self.get_parameter("SSC%d_IP" % (i + 2))
  83. udp_port_list = []
  84. # step6, create multi UDP socket on all targets
  85. for i in range(conn_number):
  86. udp_port = random.randint(10000, 20000)
  87. udp_port_list.append(udp_port)
  88. checker_stings = ["R SSC2 A <SSC2_sock%d>:\+BIND:(\d+),OK" % i]
  89. test_action_string = ["SSCC SSC2 soc -B -t UDP -p %s" % udp_port]
  90. fail_string = "Fail, SSC2 Fail to bind"
  91. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  92. return
  93. checker_stings = ["R SSC3 A <SSC3_sock%d>:\+BIND:(\d+),OK" % i]
  94. test_action_string = ["SSCC SSC3 soc -B -t UDP -p %s" % (udp_port + 1)]
  95. fail_string = "Fail, SSC3 Fail to bind"
  96. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  97. return
  98. start_time = time.time()
  99. # step7, do send/recv, SSC2<---->other STAs
  100. while time.time() - start_time < test_time:
  101. # drop off the delay time if it's greater than 20ms
  102. if send_delay > 20:
  103. send_delay -= 10
  104. checker_stings = []
  105. test_action_string = []
  106. if server_echo is True:
  107. # SSC2 send packets to SSC3
  108. ssc3_ip = "192.168.4." + self.get_parameter("SSC3_IP")
  109. for i in range(conn_number):
  110. test_action_string.append("SSC SSC2 soc -S -s <SSC2_sock%s> -i %s -p %s -l %d -n 1000 -j %d" % (
  111. i, ssc3_ip, udp_port_list[i] + 1, send_len, send_delay))
  112. checker_stings.append("P SSC2 RE \+SEND:\d+,OK NC CLOSED NC ERROR")
  113. # SSC3 send packets to SSC2
  114. ssc2_ip = "192.168.4." + self.get_parameter("SSC2_IP")
  115. for i in range(conn_number):
  116. test_action_string.append("SSC SSC3 soc -S -s <SSC3_sock%d> -i %s -p %s -l %d -n 1000 -j %d" % (
  117. i, ssc2_ip, udp_port_list[i], send_len, send_delay))
  118. checker_stings.append("P SSC3 RE \+SEND:\d+,OK NC CLOSED NC ERROR")
  119. fail_string = "Fail, Failed to send/recv data"
  120. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  121. check_freq=1, check_time=300) is False:
  122. break
  123. NativeLog.add_trace_critical("time escape: %s" % (time.time() - start_time))
  124. if (time.time() - start_time) >= test_time:
  125. self.result_cntx.set_result("Succeed")
  126. else:
  127. self.result_cntx.set_result("Failed")
  128. # finally, execute done
  129. def result_check(self, port_name, data):
  130. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  131. self.result_cntx.append_data(port_name, data)
  132. def main():
  133. pass
  134. if __name__ == '__main__':
  135. main()