TCPSTAMuitiSockSendRecv.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, 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. server_echo = self.server_echo
  24. conn_number = self.conn_number
  25. sta_number = self.sta_number
  26. test_time = self.test_time
  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. # step1, set ap mode on SSC1, set STA mode on SSC2-SSC3
  40. checker_stings = ["R SSC1 C +MODE:OK"]
  41. test_action_string = ["SSCC SSC1 op -S -o 2"]
  42. fail_string = "Fail, Fail to set mode on SSC1"
  43. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  44. return
  45. for i in range(sta_number):
  46. checker_stings = ["R SSC%d C +MODE:OK" % (i + 2)]
  47. test_action_string = ["SSCC SSC%d op -S -o 1" % (i + 2)]
  48. fail_string = "Fail, Fail to set mode on SSC%d" % (i + 2)
  49. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  50. return
  51. # turn off recv print
  52. for i in range(sta_number + 1):
  53. checker_stings = ["P SSC%d C +RECVPRINT:0" % (i + 1)]
  54. test_action_string = ["SSCC SSC%d soc -R -o 0" % (i + 1)]
  55. fail_string = "Fail, Fail to turn off recv print"
  56. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  57. return
  58. # step2, 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. tcp_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 -t 3 -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. # step3, SSC2-SSC3 connect to SSC1
  68. checker_stings = []
  69. test_action_string = []
  70. for i in range(sta_number):
  71. checker_stings.append("P SSC%d C +JAP:CONNECTED,%s" % (i + 2, ssid))
  72. test_action_string.append("SSCC SSC%d ap -C -s %s -p %s" % (i + 2, ssid, password))
  73. fail_string = "Fail, SSC%d Fail to connect to SoftAP" % (i + 2)
  74. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=450) is False:
  75. return
  76. # step4, create tcp server on STA SSC2
  77. checker_stings = ["R SSC2 A <server_sock>:BIND:(\d+),OK"]
  78. test_action_string = ["SSCC SSC2 soc -B -t TCP -p %s" % tcp_port]
  79. fail_string = "Fail, Fail to create server on SSC2 while binding"
  80. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  81. return
  82. checker_stings = ["R SSC2 RE LISTEN:(\d+),OK"]
  83. test_action_string = ["SSCC SSC2 soc -L -s <server_sock>"]
  84. fail_string = "Fail, Fail to create server on SSC2 while listening"
  85. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  86. return
  87. # step5, create multi client on SSC3
  88. for i in range(conn_number):
  89. checker_stings = ["P SSC3 A <client_sock%d>:BIND:(\d+),OK" % i]
  90. test_action_string = ["SSCC SSC3 soc -B -t TCP"]
  91. fail_string = "Fail, Fail to create client on SSC3"
  92. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  93. return
  94. checker_stings = ["P SSC3 RE CONNECT:(\d+),OK", "P SSC2 A <accept_sock%d>:ACCEPT:(\d+),.+" % i]
  95. test_action_string = ["SSCC SSC3 soc -C -s <client_sock%d> -i %s -p %s" % (i, "192.168.4.2", tcp_port)]
  96. fail_string = "Fail, Fail to connect to SSC2 server while connecting"
  97. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  98. return
  99. start_time = time.time()
  100. # step6, do send/recv
  101. while time.time() - start_time < test_time:
  102. checker_stings = []
  103. test_action_string = []
  104. # SSC2 send packets to SSC3
  105. if server_echo is True:
  106. for i in range(conn_number):
  107. checker_stings.append("P SSC2 RE \+SEND:\d+,OK NC CLOSED NC ERROR")
  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. # SSC3 send packets to SSC2
  111. for i in range(conn_number):
  112. test_action_string.append("SSC SSC3 soc -S -s <client_sock%d> -l %d -n 1000 -j %d" %
  113. (i, send_len, send_delay))
  114. checker_stings.append("P SSC3 RE \+SEND:\d+,OK NC CLOSED NC ERROR")
  115. fail_string = "Fail, Failed to send/recv data"
  116. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string,
  117. check_freq=1, check_time=600) is False:
  118. break
  119. if send_delay > 20:
  120. send_delay -= 10
  121. NativeLog.add_trace_critical("Time escape: %d" % (time.time() - start_time))
  122. if (time.time() - start_time) > test_time:
  123. self.result_cntx.set_result("Succeed")
  124. else:
  125. self.result_cntx.set_result("Failed")
  126. # finally, execute done
  127. def result_check(self, port_name, data):
  128. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  129. self.result_cntx.append_data(port_name, data)
  130. def main():
  131. pass
  132. if __name__ == '__main__':
  133. main()