TCPSendRecv.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from TCAction import TCActionBase
  2. from NativeLog import NativeLog
  3. import time
  4. import random
  5. import string
  6. TEST_COUNT_ONE_ROUND = 1000
  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. # 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 cleanup(self):
  19. # step 0 turn on recv print
  20. checker_stings = ["P SSC1 C +RECVPRINT:1", "P SSC2 C +RECVPRINT:1"]
  21. test_action_string = ["SSC SSC1 soc -R -o 1", "SSC SSC2 soc -R -o 1"]
  22. fail_string = "Fail, Fail to turn on recv print"
  23. self.load_and_exe_one_step(checker_stings, test_action_string, fail_string)
  24. pass
  25. def execute(self):
  26. TCActionBase.TCActionBase.execute(self)
  27. self.result_cntx.start()
  28. try:
  29. # configurable params
  30. send_len = self.send_len
  31. test_time = self.test_time * 60
  32. duplex = self.duplex
  33. conn_num = self.conn_num
  34. send_delay = self.send_delay
  35. # configurable params
  36. except StandardError, e:
  37. NativeLog.add_trace_critical("Error configuration for TCPSendRecv script, error is %s" % e)
  38. raise StandardError("Error configuration")
  39. ssid = "".join([random.choice(string.lowercase) for m in range(10)])
  40. password = "".join([random.choice(string.lowercase) for m in range(10)])
  41. tcp_port = random.randint(10000, 50000)
  42. # step 0 set ap
  43. checker_stings = ["R SSC1 C +SAP:OK"]
  44. test_action_string = ["SSC SSC1 ap -S -s %s -p %s -t 3" % (ssid, password)]
  45. fail_string = "Fail, Fail to set ap"
  46. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  47. self.result_cntx.set_result("Fail")
  48. return
  49. # step 1 connect to ap and turn off recv print
  50. checker_stings = ["R SSC2 C +JAP:CONNECTED"]
  51. test_action_string = ["SSC SSC2 sta -C -s %s -p %s" % (ssid, password)]
  52. fail_string = "Fail, Fail to connect to server"
  53. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=200) is False:
  54. self.result_cntx.set_result("Fail")
  55. return
  56. checker_stings = ["P SSC1 C +RECVPRINT:0", "P SSC2 C +RECVPRINT:0"]
  57. test_action_string = ["SSC SSC1 soc -R -o 0", "SSC SSC2 soc -R -o 0"]
  58. fail_string = "Fail, Fail to turn off recv print"
  59. self.load_and_exe_one_step(checker_stings, test_action_string, fail_string)
  60. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string, check_time=200) is False:
  61. self.result_cntx.set_result("Fail")
  62. return
  63. # step 2 create server on AP
  64. checker_stings = ["R SSC1 A <server_sock>:\+BIND:(\d+),OK"]
  65. test_action_string = ["SSC SSC1 soc -B -t TCP -p %s" % tcp_port]
  66. fail_string = "Fail, Fail to create server"
  67. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  68. self.result_cntx.set_result("Fail")
  69. return
  70. checker_stings = ["R SSC1 A <server_sock>:\+LISTEN:(\d+),OK"]
  71. test_action_string = ["SSC SSC1 soc -L -s <server_sock>"]
  72. fail_string = "Fail, Fail to create server"
  73. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  74. self.result_cntx.set_result("Fail")
  75. return
  76. # step 3 create conn_num tcp connections
  77. for i in range(conn_num):
  78. checker_stings = ["R SSC2 A <client_sock%s>:\+BIND:(\d+),OK" % i]
  79. test_action_string = ["SSC SSC2 soc -B -t TCP"]
  80. fail_string = "Fail, Fail to bind"
  81. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  82. self.result_cntx.set_result("Fail")
  83. return
  84. checker_stings = ["P SSC1 A <accept_sock%s>:\+ACCEPT:(\d+),\d+" % i,
  85. "P SSC2 RE \+CONNECT:\d+,OK"]
  86. test_action_string = ["SSC SSC2 soc -C -s <client_sock%s> -i <target_ap_ip> -p %s" % (i, tcp_port)]
  87. fail_string = "Fail, Fail to connect"
  88. if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
  89. self.result_cntx.set_result("Fail")
  90. return
  91. start_time = time.time()
  92. # step 4, do send/recv
  93. while time.time()-start_time < test_time:
  94. checker_stings = ["P SSC1 NC ERROR NC CLOSE NC ERROR"]
  95. for i in range(conn_num):
  96. test_action_string = ["SSC SSC2 soc -S -s <client_sock%d> -l %d -n %d -j %d" %
  97. (i, send_len, TEST_COUNT_ONE_ROUND, send_delay)]
  98. checker_stings.append("P SSC2 RE \"\+SEND:%%%%s,OK\"%%%%(<client_sock%d>) NC ERROR NC CLOSE" % i)
  99. if duplex is True:
  100. checker_stings.append("P SSC1 RE \"\+SEND:%%%%s,OK\"%%%%(<accept_sock%d>)" % i)
  101. test_action_string.append("SSC SSC1 soc -S -s <accept_sock%d> -l %d -n %d -j %d" %
  102. (i, send_len, TEST_COUNT_ONE_ROUND, send_delay))
  103. fail_string = "Fail, Failed on send command"
  104. if self.load_and_exe_one_step([], test_action_string, fail_string) is False:
  105. self.result_cntx.set_result("Fail")
  106. break
  107. fail_string = "Fail, Failed to send/recv data"
  108. if self.load_and_exe_one_step(checker_stings, ["DELAY 0.1"], fail_string,
  109. check_freq=1, check_time=300) is False:
  110. NativeLog.add_prompt_trace("time escape: %s" % (time.time() - start_time))
  111. self.result_cntx.set_result("Fail")
  112. return
  113. NativeLog.add_prompt_trace("time escape: %s" % (time.time() - start_time))
  114. if (time.time() - start_time) >= test_time:
  115. self.result_cntx.set_result("Succeed")
  116. else:
  117. self.result_cntx.set_result("Failed")
  118. # finally, execute done
  119. def result_check(self, port_name, data):
  120. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  121. self.result_cntx.append_data(port_name, data)
  122. def main():
  123. pass
  124. if __name__ == '__main__':
  125. main()