UDPMultiSTASendRecv.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. sta_number = self.sta_number
  26. send_delay = self.send_delay
  27. # configurable params
  28. except StandardError, e:
  29. NativeLog.add_trace_critical("Error configuration for TCPTransparent script, error is %s" % e)
  30. raise StandardError("Error configuration")
  31. # step0 reboot
  32. for i in range(sta_number + 1):
  33. checker_stings = ["P SSC%d C ready!!!" % (i + 1)]
  34. test_action_string = ["SSCC SSC%d restore" % (i + 1)]
  35. fail_string = "Fail, Fail to restore"
  36. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  37. return
  38. # turn off recv print
  39. for i in range(sta_number + 1):
  40. checker_stings = ["P SSC%d C +RECVPRINT:0" % (i + 1)]
  41. test_action_string = ["SSCC SSC%d soc -R -o 0" % (i + 1)]
  42. fail_string = "Fail, Fail to turn off recv print"
  43. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  44. return
  45. # step1, set softAP mode on SSC1
  46. checker_stings = ["R SSC1 C +MODE:OK"]
  47. test_action_string = ["SSCC SSC1 op -S -o 2"]
  48. fail_string = "Fail, Fail to set mode on SSC1"
  49. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  50. return
  51. # step2, set STA mode on SSC2-SSCn
  52. for i in range(sta_number):
  53. checker_stings = ["R SSC%d C +MODE:OK" % (i + 2)]
  54. test_action_string = ["SSCC SSC%d op -S -o 1" % (i + 2)]
  55. fail_string = "Fail, Fail to set mode on SSC%d" % (i + 2)
  56. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  57. return
  58. # step3, set ssid/password on SSC1
  59. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  60. password = "".join([random.choice(string.lowercase) for m in range(10)])
  61. udp_port = random.randint(10000, 20000)
  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 10" % (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. # step6, create UDP socket on all targets
  84. for i in range(sta_number):
  85. checker_stings = ["R SSC%d A <sock%d>:\+BIND:(\d+),OK" % (i + 2, i + 2)]
  86. test_action_string = ["SSCC SSC%d soc -B -t UDP -p %s" % (i + 2, udp_port + i + 2)]
  87. fail_string = "Fail, SSC%d Fail to bind" % (i + 2)
  88. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  89. return
  90. start_time = time.time()
  91. # step7, do send/recv, SSC2<---->other STAs
  92. while time.time() - start_time < test_time:
  93. checker_stings = []
  94. test_action_string = []
  95. if server_echo is True:
  96. # SSC2 send packets to SSC3-SSCn
  97. for i in range(sta_number - 1):
  98. ip = "192.168.4." + self.get_parameter("SSC%d_IP" % (i + 3))
  99. test_action_string.append(
  100. "SSC SSC2 soc -S -s <sock%d> -i %s -p %s -l %d -n 1000 -j %d" % (
  101. i + 3, ip, udp_port + i + 3, send_len, send_delay))
  102. checker_stings.append(
  103. "P SSC2 RE \+SEND:%s,OK NC CLOSED NC ERROR" % self.get_parameter("sock%d" % (i + 3)))
  104. # SSC3-SSCn send packets to SSC2
  105. ssc2_ip = "192.168.4." + self.get_parameter("SSC2_IP")
  106. for i in range(sta_number - 1):
  107. test_action_string.append(
  108. "SSC SSC%d soc -S -s <sock%d> -i %s -p %s -l %d -n 1000 -j %d" % (
  109. i + 3, i + 3, ssc2_ip, udp_port + 2, send_len, send_delay))
  110. checker_stings.append(
  111. "P SSC%d RE \+SEND:%s,OK NC CLOSED NC ERROR" % (i + 3, self.get_parameter("sock%d" % (i + 3))))
  112. fail_string = "Fail, Failed to send/recv data"
  113. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  114. check_freq=1, check_time=300) is False:
  115. break
  116. # drop off the delay time if it's greater than 20ms
  117. if send_delay > 20:
  118. send_delay -= 10
  119. NativeLog.add_trace_critical("time escape: %s" % (time.time() - start_time))
  120. if (time.time() - start_time) >= test_time:
  121. self.result_cntx.set_result("Succeed")
  122. else:
  123. self.result_cntx.set_result("Failed")
  124. # finally, execute done
  125. def result_check(self, port_name, data):
  126. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  127. self.result_cntx.append_data(port_name, data)
  128. def main():
  129. pass
  130. if __name__ == '__main__':
  131. main()