TCPMultiSTASendRecv.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from TCAction import TCActionBase
  2. from NativeLog import NativeLog
  3. import time
  4. import random
  5. import string
  6. TEST_COUNT_ONE_ROUND = 500
  7. class TestCase(TCActionBase.CommonTCActionBase):
  8. def __init__(self, test_case, test_env, timeout=45, 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 execute(self):
  19. TCActionBase.TCActionBase.execute(self)
  20. self.result_cntx.start()
  21. try:
  22. # configurable params
  23. send_len = self.send_len
  24. test_time = self.test_time * 60
  25. server_echo = self.server_echo
  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 restore" % (i + 1)]
  36. fail_string = "Fail, Fail to restore"
  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. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  60. password = "".join([random.choice(string.lowercase) for m in range(10)])
  61. tcp_port = random.randint(40000, 50000)
  62. # step3, set ssid/password on SSC1
  63. checker_stings = ["R SSC1 C +SAP:OK"]
  64. test_action_string = ["SSCC SSC1 ap -S -s %s -p %s -n 10 -t 0 -m 10" % (ssid, password)]
  65. fail_string = "Fail, Fail to set ssid/password on SSC1"
  66. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  67. return
  68. # step4, SSC2-SSCn join SSC1(soft AP)
  69. for i in range(sta_number):
  70. checker_stings = ["P SSC%d C +JAP:CONNECTED,%s" % (i + 2, ssid)]
  71. test_action_string = ["SSCC SSC%d ap -C -s %s -p %s" % (i + 2, ssid, password)]
  72. fail_string = "Fail, SSC%d Fail to connect to SSC1" % (i + 2)
  73. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=450) is False:
  74. return
  75. # step5, create server on SSC2
  76. checker_stings = ["R SSC2 A <server_sock>:BIND:(\d+),OK"]
  77. test_action_string = ["SSCC SSC2 soc -B -t TCP -p %s" % tcp_port]
  78. fail_string = "Fail, Fail to create server on SSC2 while binding"
  79. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  80. return
  81. checker_stings = ["R SSC2 RE LISTEN:(\d+),OK"]
  82. test_action_string = ["SSCC SSC2 soc -L -s <server_sock>"]
  83. fail_string = "Fail, Fail to create server on SSC2 while listening"
  84. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  85. return
  86. # step6, create client on SSC3-SSCn
  87. for i in range(sta_number - 1):
  88. checker_stings = ["P SSC%d A <client_sock%d>:BIND:(\d+),OK" % (i + 3, i + 3)]
  89. test_action_string = ["SSCC SSC%d soc -B -t TCP" % (i + 3)]
  90. fail_string = "Fail, SSC%d Fail to connect to TCP server while binding" % (i + 3)
  91. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  92. return
  93. for i in range(sta_number - 1):
  94. checker_stings = ["P SSC%d RE CONNECT:(\d+),OK" % (i + 3), "P SSC2 A <accept_sock%d>:ACCEPT:(\d+),.+" % i]
  95. test_action_string = [
  96. "SSCC SSC%d soc -C -s <client_sock%d> -i 192.168.4.2 -p %s" % (i + 3, i + 3, tcp_port)]
  97. fail_string = "Fail, SSC%d Fail to connect to TCP server while connecting" % (i + 3)
  98. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  99. return
  100. start_time = time.time()
  101. # step7, do send/recv, SSC2<---->other STAs
  102. while time.time() - start_time < test_time:
  103. checker_stings = []
  104. test_action_string = []
  105. # SSC2 send packets to SSC3-SSCn
  106. if server_echo is True:
  107. for i in range(sta_number - 1):
  108. test_action_string.append("SSC SSC2 soc -S -s <accept_sock%d> -l %d -n 1000 -j %d" %
  109. (i, send_len, send_delay))
  110. checker_stings.append(
  111. "P SSC2 RE \+SEND:%s,OK NC CLOSED NC ERROR" % self.get_parameter("accept_sock%d" % (i + 3)))
  112. # SSC3-SSCn send packets to SSC2
  113. for i in range(sta_number - 1):
  114. checker_stings.append(
  115. "P SSC%d RE \+SEND:%s,OK NC CLOSED NC ERROR" % (i + 3, self.get_parameter("client_sock%d" % i)))
  116. test_action_string.append("SSC SSC%d soc -S -s <client_sock%d> -l %d -n 1000 -j %d" %
  117. (i + 3, i + 3, send_len, send_delay))
  118. fail_string = "Fail, Failed to send/recv data"
  119. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  120. check_freq=1, check_time=900) is False:
  121. break
  122. # drop off the delay time if it's greater than 20ms
  123. if send_delay > 20:
  124. send_delay -= 10
  125. NativeLog.add_trace_critical("Time escape: %d" % (time.time() - start_time))
  126. if (time.time() - start_time) >= test_time:
  127. self.result_cntx.set_result("Succeed")
  128. else:
  129. self.result_cntx.set_result("Failed")
  130. # finally, execute done
  131. def result_check(self, port_name, data):
  132. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  133. self.result_cntx.append_data(port_name, data)
  134. def main():
  135. pass
  136. if __name__ == '__main__':
  137. main()