TCPSoftAPSTASendRecv.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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,
  10. timeout=timeout, log_path=log_path)
  11. self.send_len = 1460
  12. self.server_port = random.randint(10000, 50000)
  13. self.server_port_2 = random.randint(10000, 50000)
  14. self.server_echo = True
  15. self.test_time = 12 * 60
  16. self.sta_number = 3
  17. self.send_delay = 50
  18. # load param from excel
  19. cmd_set = test_case["cmd set"]
  20. for i in range(1, len(cmd_set)):
  21. if cmd_set[i][0] != "dummy":
  22. cmd_string = "self." + cmd_set[i][0]
  23. exec cmd_string
  24. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  25. pass
  26. def cleanup(self):
  27. checker_stings = []
  28. test_action_strings = []
  29. for i in range(self.sta_number + 2):
  30. checker_stings.append("R SSC%s C +RECVPRINT:1" % (i+1))
  31. test_action_strings.append("SSC SSC%s soc -R -o 1" % (i+1))
  32. fail_string = "Fail, Fail to turn on recv print"
  33. self.load_and_exe_one_step(checker_stings, test_action_strings, fail_string)
  34. pass
  35. def execute(self):
  36. TCActionBase.TCActionBase.execute(self)
  37. self.result_cntx.start()
  38. try:
  39. # configurable params
  40. send_len = self.send_len
  41. test_time = self.test_time * 60
  42. # server port
  43. server_port = self.server_port
  44. server_port_t = self.server_port_2
  45. # ap ip
  46. # ap_ip = self.ap_ip
  47. # server echo
  48. server_echo = self.server_echo
  49. # station number
  50. sta_number = self.sta_number
  51. # send delay
  52. send_delay = self.send_delay
  53. # configurable params
  54. except StandardError, e:
  55. NativeLog.add_trace_critical("Error configuration for TCPTransparent script, error is %s" % e)
  56. raise StandardError("Error configuration")
  57. # step0 reboot
  58. checker_stings = []
  59. test_action_string = []
  60. for i in range(sta_number + 2):
  61. checker_stings.append("P SSC%d C !!!ready!!!" % (i + 1))
  62. test_action_string.append("SSCC SSC%d reboot" % (i + 1))
  63. fail_string = "Fail, Fail to reboot"
  64. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  65. return
  66. # switch off recv print
  67. checker_stings = []
  68. test_action_strings = []
  69. for i in range(self.sta_number + 2):
  70. checker_stings.append("R SSC%s C +RECVPRINT:0" % (i+1))
  71. test_action_strings.append("SSC SSC%s soc -R -o 0" % (i+1))
  72. fail_string = "Fail, Fail to turn off recv print"
  73. self.load_and_exe_one_step(checker_stings, test_action_strings, fail_string)
  74. # step1, set ap/STA mode on all target
  75. for i in range(sta_number + 2):
  76. checker_stings = ["R SSC%d C +MODE:OK" % (i + 1)]
  77. test_action_string = ["SSCC SSC%d op -S -o 3" % (i + 1)]
  78. fail_string = "Fail, Fail to set mode on SSC%d" % (i + 1)
  79. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  80. return
  81. # set different getway for SSC1 softAP
  82. checker_stings = ["R SSC1 C +DHCP:AP,OK"]
  83. test_action_string = ["SSCC SSC1 dhcp -E -o 2"]
  84. fail_string = "Fail, SSC1 Fail to disable DHCP"
  85. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  86. return
  87. checker_stings = ["R SSC1 C +IP:OK"]
  88. test_action_string = ["SSCC SSC1 ip -S -o 2 -i 192.168.6.1"]
  89. fail_string = "Fail, SSC1 Fail to set IP"
  90. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  91. return
  92. checker_stings = ["R SSC1 C +DHCP:AP,OK"]
  93. test_action_string = ["SSCC SSC1 dhcp -S -o 2"]
  94. fail_string = "Fail, SSC1 Fail to enable DHCP"
  95. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  96. return
  97. # set different getway for SSC2 softAP
  98. checker_stings = ["R SSC2 C +DHCP:AP,OK"]
  99. test_action_string = ["SSCC SSC2 dhcp -E -o 2"]
  100. fail_string = "Fail, SSC2 Fail to disable DHCP"
  101. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  102. return
  103. checker_stings = ["R SSC2 C +IP:OK"]
  104. test_action_string = ["SSCC SSC2 ip -S -o 2 -i 192.168.5.1"]
  105. fail_string = "Fail, SSC2 Fail to set IP"
  106. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  107. return
  108. checker_stings = ["R SSC2 C +DHCP:AP,OK"]
  109. test_action_string = ["SSCC SSC2 dhcp -S -o 2"]
  110. fail_string = "Fail, SSC2 Fail to enable DHCP"
  111. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  112. return
  113. # step2, set ssid/password on SSC1
  114. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  115. password = "".join([random.choice(string.lowercase) for m in range(10)])
  116. checker_stings = ["R SSC1 C +SAP:OK"]
  117. test_action_string = ["SSCC SSC1 ap -S -s %s -p %s -n 10 -t 0 -m 8" % (ssid, password)]
  118. fail_string = "Fail, Fail to set ssid/password on SSC1"
  119. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  120. return
  121. # step3, set ssid/password on SSC2
  122. ssid_1 = "".join([random.choice(string.lowercase) for m in range(10)])
  123. password_1 = "".join([random.choice(string.lowercase) for m in range(10)])
  124. checker_stings = ["R SSC2 C +SAP:OK"]
  125. test_action_string = ["SSCC SSC2 ap -S -s %s -p %s -n 10 -t 0 -m 8" % (ssid_1, password_1)]
  126. fail_string = "Fail, Fail to set ap ssid/password on SSC2"
  127. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  128. return
  129. # step4, SSC2 join SSC1(soft AP)
  130. checker_stings = []
  131. test_action_string = []
  132. checker_stings.append("P SSC2 C +JAP:CONNECTED,%s" % ssid)
  133. test_action_string.append("SSCC SSC2 ap -C -s %s -p %s" % (ssid, password))
  134. fail_string = "Fail, Fail to connect to SSC1 SoftAP"
  135. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  136. return
  137. # step5, create server on SSC1
  138. checker_stings = ["R SSC1 A <server_sock>:BIND:(\d+),OK"]
  139. test_action_string = ["SSCC SSC1 soc -B -t TCP -p %s" % server_port]
  140. fail_string = "Fail, Fail to create server on SSC1 while binding"
  141. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  142. return
  143. checker_stings = ["R SSC1 RE LISTEN:(\d+),OK"]
  144. test_action_string = ["SSCC SSC1 soc -L -s <server_sock>"]
  145. fail_string = "Fail, Fail to create server on SSC1 while listening"
  146. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  147. return
  148. # step6, create client on SSC2
  149. checker_stings = []
  150. test_action_string = []
  151. checker_stings.append("P SSC2 A <client_sock>:BIND:(\d+),OK")
  152. test_action_string.append("SSCC SSC2 soc -B -t TCP")
  153. fail_string = "Fail, SSC2 Fail to connect to server while binding"
  154. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  155. return
  156. checker_stings = ["P SSC2 RE CONNECT:(\d+),OK", "P SSC1 A <accept_sock>:ACCEPT:(\d+),.+"]
  157. test_action_string = ["SSCC SSC2 soc -C -s <client_sock> -i %s -p %s" % ("192.168.6.1", server_port)]
  158. fail_string = "Fail, SSC2 Fail to connect to server while connecting"
  159. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  160. return
  161. # step7, SSC3 - SSC5 join SSC2
  162. checker_stings = []
  163. test_action_string = []
  164. for i in range(sta_number):
  165. checker_stings.append("P SSC%d C +JAP:CONNECTED,%s" % (i + 3, ssid_1))
  166. test_action_string.append("SSCC SSC%d ap -C -s %s -p %s" % (i + 3, ssid_1, password_1))
  167. fail_string = "Fail, SSC%d Fail to connect to SSC2" % (i + 3)
  168. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=450) is False:
  169. return
  170. # step8, create server on SSC2
  171. checker_stings = ["R SSC2 A <server_sock_t>:BIND:(\d+),OK"]
  172. test_action_string = ["SSCC SSC2 soc -B -t TCP -p %s -i 192.168.5.1" % server_port_t]
  173. fail_string = "Fail, Fail to create server one SSC2 while binding"
  174. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  175. return
  176. checker_stings = ["R SSC2 RE LISTEN:(\d+),OK"]
  177. test_action_string = ["SSCC SSC2 soc -L -s <server_sock_t>"]
  178. fail_string = "Fail, Fail to create server one SSC2 while listening"
  179. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  180. return
  181. # step9, create client on SSC3 - SSC5
  182. checker_stings = []
  183. test_action_string = []
  184. for i in range(sta_number):
  185. checker_stings.append("P SSC%d A <client_sock%d>:BIND:(\d+),OK" % (i + 3, i + 3))
  186. test_action_string.append("SSCC SSC%d soc -B -t TCP" % (i + 3))
  187. fail_string = "Fail, Fail to connect to SSC2 server while binding"
  188. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  189. return
  190. for i in range(sta_number):
  191. checker_stings = ["P SSC%d RE CONNECT:(\d+),OK" % (i + 3),
  192. "P SSC2 A <accept_sock%d>:ACCEPT:(\d+),.+" % (i + 3)]
  193. test_action_string = ["SSCC SSC%d soc -C -s <client_sock%d> -i %s -p %s" %
  194. (i + 3, i + 3, "192.168.5.1", server_port_t)]
  195. fail_string = "Fail, Fail to connect to SSC2 server while connecting"
  196. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  197. return
  198. start_time = time.time()
  199. # step 10, do send/recv
  200. while time.time() - start_time < test_time:
  201. checker_stings = []
  202. test_action_string = []
  203. if server_echo is True:
  204. test_action_string.append("SSC SSC1 soc -S -s <accept_sock> -l %d -n %d -j %d" %
  205. (send_len, TEST_COUNT_ONE_ROUND, send_delay))
  206. checker_stings.append("P SSC1 RE \+SEND:\d+,OK NC CLOSED")
  207. test_action_string.append("SSC SSC2 soc -S -s <server_sock> -l %d -n %d -j %d" %
  208. (send_len, TEST_COUNT_ONE_ROUND, send_delay))
  209. checker_stings.append("P SSC2 RE \+SEND:\d+,OK NC CLOSED")
  210. for i in range(sta_number):
  211. checker_stings.append("P SSC%d RE \+SEND:\d+,OK NC CLOSED" % (i + 3))
  212. test_action_string.append("SSC SSC%d soc -S -s <client_sock%d> -l %d -n %d -j %d" %
  213. (i + 3, i + 3, send_len, TEST_COUNT_ONE_ROUND, send_delay))
  214. for i in range(sta_number):
  215. test_action_string.append("SSC SSC2 soc -S -s <accept_sock%d> -l %d -n %d -j %d" %
  216. (i + 3, send_len, TEST_COUNT_ONE_ROUND, send_delay))
  217. checker_stings.append("P SSC2 RE \+SEND:\d+,OK NC CLOSED")
  218. fail_string = "Fail, Failed to send/recv data"
  219. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  220. check_freq=1, check_time=300) is False:
  221. break
  222. pass
  223. NativeLog.add_prompt_trace("time escape: %s" % (time.time() - start_time))
  224. if (time.time() - start_time) > test_time:
  225. self.result_cntx.set_result("Succeed")
  226. else:
  227. self.result_cntx.set_result("Failed")
  228. checker_stings = []
  229. test_action_string = []
  230. for i in range(sta_number + 2):
  231. checker_stings.append("P SSC%d C CLOSEALL" % (i + 1))
  232. test_action_string.append("SSCC SSC%d soc -T" % (i + 1))
  233. fail_string = "Fail, Fail to close socket"
  234. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  235. return
  236. # re-set server on SSC1
  237. server_port = random.randint(20000, 30000)
  238. checker_stings = ["R SSC1 A <server_sock>:BIND:(\d+),OK"]
  239. test_action_string = ["SSCC SSC1 soc -B -t TCP -p %s" % server_port]
  240. fail_string = "Fail, Fail to bind socket"
  241. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  242. return
  243. checker_stings = ["R SSC1 RE LISTEN:(\d+),OK"]
  244. test_action_string = ["SSCC SSC1 soc -L -s <server_sock>"]
  245. fail_string = "Fail, Fail to listen"
  246. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  247. return
  248. # SSC2 connnect SSC1
  249. checker_stings = []
  250. test_action_string = []
  251. checker_stings.append("P SSC2 A <client_sock>:BIND:(\d+),OK")
  252. test_action_string.append("SSCC SSC2 soc -B -t TCP")
  253. fail_string = "Fail, SSC2 Fail to bind sock"
  254. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  255. return
  256. checker_stings = ["P SSC2 RE CONNECT:(\d+),OK", "P SSC1 A <accept_sock>:ACCEPT:(\d+),.+"]
  257. test_action_string = ["SSCC SSC2 soc -C -s <client_sock> -i %s -p %s" % ("192.168.6.1", server_port)]
  258. fail_string = "Fail, SSC2 Fail to connect to SSC1 server"
  259. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  260. return
  261. # create server on SSC2
  262. checker_stings = []
  263. test_action_string = []
  264. checker_stings.append("P SSC2 A <server_sock>:BIND:(\d+),OK")
  265. test_action_string.append("SSCC SSC2 soc -B -t TCP -p %s -i 192.168.5.1" % server_port_t)
  266. fail_string = "Fail, SSC2 Fail to bind"
  267. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  268. return
  269. checker_stings = ["R SSC2 RE LISTEN:(\d+),OK"]
  270. test_action_string = ["SSCC SSC2 soc -L -s <server_sock>"]
  271. fail_string = "Fail, SSC2 Fail to listen"
  272. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  273. return
  274. # create client on SSC3-SSC5
  275. checker_stings = []
  276. test_action_string = []
  277. for i in range(sta_number):
  278. checker_stings.append("P SSC%d A <client_sock%d>:BIND:(\d+),OK" % (i + 3, i + 3))
  279. test_action_string.append("SSCC SSC%d soc -B -t TCP" % (i + 3))
  280. fail_string = "Fail, Fail to connect to SSC2 server while binding"
  281. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  282. return
  283. for i in range(sta_number):
  284. checker_stings = ["P SSC%d RE CONNECT:(\d+),OK" % (i + 3),
  285. "P SSC2 A <accept_sock%d>:ACCEPT:(\d+),.+" % (i + 3)]
  286. test_action_string = ["SSCC SSC%d soc -C -s <client_sock%d> -i %s -p %s" %
  287. (i + 3, i + 3, "192.168.5.1", server_port_t)]
  288. fail_string = "Fail, Fail to connect to server"
  289. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  290. return
  291. def result_check(self, port_name, data):
  292. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  293. self.result_cntx.append_data(port_name, data)
  294. def main():
  295. pass
  296. if __name__ == '__main__':
  297. main()