TCPAPNSTA.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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=30, log_path=TCActionBase.LOG_PATH):
  9. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  10. self.send_len = 1460
  11. self.server_echo = True
  12. self.sta_number = 4
  13. self.test_time = 12 * 60
  14. self.send_delay = 50
  15. # load param from excel
  16. cmd_set = test_case["cmd set"]
  17. for i in range(1, len(cmd_set)):
  18. if cmd_set[i][0] != "dummy":
  19. cmd_string = "self." + cmd_set[i][0]
  20. exec cmd_string
  21. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  22. pass
  23. def cleanup(self):
  24. checker_stings = []
  25. test_action_strings = []
  26. for i in range(self.sta_number + 1):
  27. checker_stings.append("R SSC%s C +RECVPRINT:1" % (i+1))
  28. test_action_strings.append("SSC SSC%s soc -R -o 1" % (i+1))
  29. fail_string = "Fail, Fail to turn on recv print"
  30. self.load_and_exe_one_step(checker_stings, test_action_strings, fail_string)
  31. pass
  32. def execute(self):
  33. TCActionBase.TCActionBase.execute(self)
  34. self.result_cntx.start()
  35. try:
  36. # configurable params
  37. send_len = self.send_len
  38. server_echo = self.server_echo
  39. sta_number = self.sta_number
  40. test_time = self.test_time * 60
  41. send_delay = self.send_delay
  42. ap_ip = self.get_parameter("target_ap_ip")
  43. # configurable params
  44. except StandardError, e:
  45. NativeLog.add_trace_critical("Error configuration for TCPTransparent script, error is %s" % e)
  46. raise StandardError("Error configuration")
  47. # step0 reboot
  48. checker_stings = []
  49. test_action_string = []
  50. for i in range(sta_number+1):
  51. checker_stings.append("P SSC%d C !!!ready!!!" % (i+1))
  52. test_action_string.append("SSCC SSC%d reboot" % (i+1))
  53. fail_string = "Fail, Fail to reboot"
  54. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  55. return
  56. # switch off recv print
  57. checker_stings = []
  58. test_action_strings = []
  59. for i in range(self.sta_number + 1):
  60. checker_stings.append("R SSC%s C +RECVPRINT:0" % (i+1))
  61. test_action_strings.append("SSC SSC%s soc -R -o 0" % (i+1))
  62. fail_string = "Fail, Fail to turn off recv print"
  63. self.load_and_exe_one_step(checker_stings, test_action_strings, fail_string)
  64. # step1 set ap on SSC1, create server
  65. checker_stings = ["R SSC1 C +MODE:OK"]
  66. test_action_string = ["SSCC SSC1 op -S -o 2"]
  67. fail_string = "Fail, Fail set mode"
  68. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  69. return
  70. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  71. password = "".join([random.choice(string.lowercase) for m in range(10)])
  72. tcp_port = random.randint(10000, 20000)
  73. checker_stings = ["R SSC1 C +SAP:OK"]
  74. test_action_string = ["SSCC SSC1 ap -S -s %s -p %s -t 3 -m 8" % (ssid, password)]
  75. fail_string = "Fail, Fail set ap"
  76. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  77. return
  78. checker_stings = ["R SSC1 A <server_sock>:BIND:(\d+),OK"]
  79. test_action_string = ["SSCC SSC1 soc -B -t TCP -p %s" % tcp_port]
  80. fail_string = "Fail, Fail create server"
  81. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  82. return
  83. checker_stings = ["R SSC1 RE LISTEN:(\d+),OK"]
  84. test_action_string = ["SSCC SSC1 soc -L -s <server_sock>"]
  85. fail_string = "Fail, Fail create server"
  86. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  87. return
  88. # step 2, 8 SSC target(SSC2 - SSC9) join SSC1 soft AP
  89. checker_stings = []
  90. test_action_string = []
  91. for i in range(sta_number):
  92. checker_stings.append("P SSC%d C +MODE:OK" % (i+2))
  93. test_action_string.append("SSCC SSC%d op -S -o 1" % (i+2))
  94. fail_string = "Fail, Fail set mode"
  95. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  96. return
  97. checker_stings = []
  98. test_action_string = []
  99. for i in range(sta_number):
  100. checker_stings.append("P SSC%d C +JAP:CONNECTED,%s" % (i+2, ssid))
  101. test_action_string.append("SSCC SSC%d ap -C -s %s -p %s" % (i+2, ssid, password))
  102. fail_string = "Fail, Fail to connect to server"
  103. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  104. return
  105. # step 3, create client on SSC2 - SSC9
  106. checker_stings = []
  107. test_action_string = []
  108. for i in range(sta_number):
  109. checker_stings.append("P SSC%d A <client_sock%d>:BIND:(\d+),OK" % (i+2, i+2))
  110. test_action_string.append("SSCC SSC%d soc -B -t TCP" % (i+2))
  111. fail_string = "Fail, Fail to connect to server"
  112. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  113. return
  114. for i in range(sta_number):
  115. checker_stings = ["P SSC%d RE CONNECT:(\d+),OK" % (i+2),
  116. "P SSC1 A <accept_sock%d>:ACCEPT:(\d+),.+" % (i+2)]
  117. test_action_string = ["SSCC SSC%d soc -C -s <client_sock%d> -i %s -p %s" %
  118. (i+2, i+2, ap_ip, tcp_port)]
  119. fail_string = "Fail, Fail to connect to server"
  120. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  121. return
  122. start_time = time.time()
  123. # step 4, do send/recv
  124. while time.time() - start_time < test_time:
  125. checker_stings = []
  126. test_action_string = []
  127. for i in range(sta_number):
  128. checker_stings.append("P SSC%d RE \+SEND:\d+,OK NC CLOSED" % (i+2))
  129. test_action_string.append("SSC SSC%d soc -S -s <client_sock%d> -l %d -n %d -j %d" %
  130. (i+2, i+2, send_len, TEST_COUNT_ONE_ROUND, send_delay))
  131. if server_echo is True:
  132. test_action_string.append("SSC SSC1 soc -S -s <accept_sock%d> -l %d -n %d -j %d" %
  133. (i+2, send_len, TEST_COUNT_ONE_ROUND, send_delay))
  134. checker_stings.append("P SSC1 RE \"\+SEND:%%%%s,OK\"%%%%(<accept_sock%d>) NC CLOSED)" %
  135. (i+2))
  136. fail_string = "Fail, Failed to send/recv data"
  137. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  138. check_freq=1, check_time=300) is False:
  139. break
  140. NativeLog.add_prompt_trace("time escape: %s" % (time.time() - start_time))
  141. if (time.time() - start_time) >= test_time:
  142. self.result_cntx.set_result("Succeed")
  143. else:
  144. self.result_cntx.set_result("Failed")
  145. # TODO: create a function to create TCP connections. reuse not copy paste code
  146. checker_stings = []
  147. test_action_string = []
  148. for i in range(sta_number + 1):
  149. checker_stings.append("P SSC%d C CLOSEALL" % (i + 1))
  150. test_action_string.append("SSCC SSC%d soc -T" % (i + 1))
  151. fail_string = "Fail, Fail to close socket"
  152. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  153. return
  154. server_port = random.randint(20000, 30000)
  155. checker_stings = ["R SSC1 A <server_sock>:BIND:(\d+),OK"]
  156. test_action_string = ["SSCC SSC1 soc -B -t TCP -p %s" % server_port]
  157. fail_string = "Fail, Fail to bind socket"
  158. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  159. return
  160. checker_stings = ["R SSC1 RE LISTEN:(\d+),OK"]
  161. test_action_string = ["SSCC SSC1 soc -L -s <server_sock>"]
  162. fail_string = "Fail, Fail to listen"
  163. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  164. return
  165. checker_stings = []
  166. test_action_string = []
  167. for i in range(sta_number):
  168. checker_stings.append("P SSC%d A <client_sock%d>:BIND:(\d+),OK" % (i + 2, i + 2))
  169. test_action_string.append("SSCC SSC%d soc -B -t TCP" % (i + 2))
  170. fail_string = "Fail, Fail to connect to server"
  171. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  172. return
  173. for i in range(sta_number):
  174. checker_stings = ["P SSC%d RE CONNECT:(\d+),OK" % (i + 2),
  175. "P SSC1 A <accept_sock%d>:ACCEPT:(\d+),.+" % (i + 2)]
  176. test_action_string = ["SSCC SSC%d soc -C -s <client_sock%d> -i %s -p %s" %
  177. (i + 2, i + 2, ap_ip, server_port)]
  178. fail_string = "Fail, Fail to connect to server"
  179. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  180. return
  181. # finally, execute done
  182. def result_check(self, port_name, data):
  183. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  184. self.result_cntx.append_data(port_name, data)
  185. def main():
  186. pass
  187. if __name__ == '__main__':
  188. main()