WifiJAP.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import os
  2. import random
  3. import time
  4. import WifiConnUtility
  5. from NativeLog import NativeLog
  6. from TCAction import TCActionBase
  7. from Utility import Encoding
  8. from Utility import MakeFolder
  9. STEPS = {"SCAN1": 0x01, "JAP": 0x02, "SCAN2": 0x04, "RECONNECT": 0x08}
  10. AP_PROP = ("ssid", "ssid_len", "pwd",
  11. "pwd_len", "channel", "enc", "apc")
  12. JAP_TEST_METHOD = ("Normal", "OFF_ON", "OFF", "WRONG_PROP")
  13. RECONNECT_TEST_METHOD = ("OFF_ON", "OFF")
  14. LOG_FOLDER = os.path.join("Performance", "JAP")
  15. SSID_LEN_RANGE = (1, 32) # in bytes
  16. ENC_TYPE = (0, 2, 3, 4) # do not support WEP for 8266 soft AP
  17. PWD_RANGE = {0: [0, 0],
  18. 1: [5, 5],
  19. 2: [8, 63],
  20. 3: [8, 63],
  21. 4: [8, 63],
  22. }
  23. class TestCase(TCActionBase.CommonTCActionBase):
  24. def __init__(self, test_case, test_env, timeout=30, log_path=TCActionBase.LOG_PATH):
  25. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  26. # default value for optional configurable params
  27. self.performance_folder_path = log_path
  28. self.pwd_len = [8, 64]
  29. self.step_config = [0x03, 0x01, 0x02, 0x0B, 0x0F]
  30. self.join_test_method = ["Normal"]
  31. self.join_delay = [[1.5, 5], [1.5, 5]]
  32. self.reconnect_test_method = ["OFF_ON"]
  33. self.reconnect_delay = [[1.5, 5], [1.5, 6]]
  34. # load param from excel
  35. cmd_set = test_case["cmd set"]
  36. for i in range(1, len(cmd_set)):
  37. if cmd_set[i][0] != "dummy" and cmd_set[i][0] != "":
  38. cmd_string = "self." + cmd_set[i][0]
  39. exec cmd_string
  40. # read AP list
  41. self.ap_list = []
  42. for i in range(1, len(cmd_set)):
  43. for j in range(len(cmd_set[i][1])):
  44. if cmd_set[i][1][j] != "":
  45. cmd_string = "self.ap_list.append(dict(zip(AP_PROP, " + cmd_set[i][1][j] + ")))"
  46. exec cmd_string
  47. folder_path = MakeFolder.make_folder(self.performance_folder_path + "\\" + LOG_FOLDER)
  48. file_name = "JAP_log_%s.log" % (time.strftime("%m%d%H%M%S", time.localtime()))
  49. self._performance_log_file = os.path.join(folder_path, file_name)
  50. # test statistics
  51. self._succeed_count = self._fail_count = self._time_cost_count = 0
  52. self._total_time = self._longest_time = 0
  53. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  54. # get target type "SSC" or "AT"
  55. self.target_type = ["SSC" if test_env.get_port_by_name("AT1") is None else "AT"]
  56. self.target_type.append("SSC" if test_env.get_port_by_name("AT2") is None else "AT")
  57. self._utility = WifiConnUtility.WifiConnUtility(self)
  58. pass
  59. def _generate_random_ap_prop(self):
  60. ap_prop = dict.fromkeys(AP_PROP)
  61. # generate target ap_value
  62. ap_prop["ssid_len"] = random.randint(SSID_LEN_RANGE[0], SSID_LEN_RANGE[1])
  63. ap_prop["channel"] = random.choice(range(1, 14))
  64. ap_prop["enc"] = random.choice(ENC_TYPE)
  65. ap_prop["pwd_len"] = random.randint(PWD_RANGE[ap_prop["enc"]][0], PWD_RANGE[ap_prop["enc"]][1])
  66. # generate string
  67. if self.target_type[0] == self.target_type[1] == "AT":
  68. ap_prop["ssid"] = Encoding.generate_random_utf8_str(ap_prop["ssid_len"])
  69. ap_prop["pwd"] = Encoding.generate_random_utf8_str(ap_prop["pwd_len"])
  70. # NativeLog.add_trace_info("ssid hex is : %x" % ap_prop["ssid"])
  71. # NativeLog.add_trace_info("pwd hex is : %x" % ap_prop["pwd"])
  72. else:
  73. ap_prop["ssid"] = Encoding.generate_random_printable_str(ap_prop["ssid_len"])
  74. ap_prop["pwd"] = Encoding.generate_random_printable_str(ap_prop["pwd_len"])
  75. return ap_prop
  76. def _logging_performance(self, ssid, join_method="Normal", time_cost=0):
  77. # log performance to performance log file
  78. with open(self._performance_log_file, "ab+") as f:
  79. # log time and ssid
  80. f.write("\r\n[%s]:\r\n[AP name] %s\r\n" %
  81. (time.strftime("%m-%d %H:%M:%S", time.localtime()), ssid))
  82. if join_method == "Normal" or join_method == "OFF_ON":
  83. if time_cost is not False:
  84. self._succeed_count += 1
  85. if join_method == "Normal":
  86. f.write("[Succeed][%f]\r\n" % time_cost)
  87. self._longest_time = (time_cost > self._longest_time and
  88. [time_cost] or [self._longest_time])[0]
  89. self._time_cost_count += 1
  90. self._total_time += time_cost
  91. else:
  92. f.write("[Succeed][%s]\r\n" % join_method)
  93. else:
  94. self._fail_count += 1
  95. f.write("[Fail][%s]\r\n" % join_method)
  96. pass
  97. def _logging_fail_step(self, ssid, step):
  98. with open(self._performance_log_file, "ab+") as f:
  99. f.write("\r\n[%s]:\r\n[AP name] %s\r\n" %
  100. (time.strftime("%m-%d %H:%M:%S", time.localtime()), ssid))
  101. f.write("[Fail][%s]\r\n" % step)
  102. pass
  103. def _generate_performance_report(self):
  104. with open(self._performance_log_file, "ab+") as f:
  105. f.write("[Test report] Succeed: %d\r\n" % self._succeed_count)
  106. f.write("[Test report] Failed: %d\r\n" % self._fail_count)
  107. if self._succeed_count > 0 or self._fail_count > 0:
  108. f.write("[Test report] Pass Rate: %f\r\n" %
  109. (self._succeed_count/(self._fail_count+self._succeed_count)))
  110. if self._time_cost_count > 0:
  111. f.write("[Test report] Average time: %f\r\n" % (self._total_time/self._time_cost_count))
  112. f.write("[Test report] Longest time: %f\r\n" % self._longest_time)
  113. def execute(self):
  114. TCActionBase.TCActionBase.execute(self)
  115. self.result_cntx.start()
  116. # mandatory configurable params
  117. try:
  118. target_ap_num = self.target_ap_num
  119. test_count = self.test_count
  120. except StandardError, e:
  121. NativeLog.add_trace_critical("Error configuration for WifiJAP script, error is %s" % e)
  122. raise StandardError("Error configuration")
  123. # prepare ap list
  124. _ap_list = [["target", None]] * target_ap_num
  125. for _ap_prop in self.ap_list:
  126. _ap_list.append(["AP", _ap_prop])
  127. # set to correct mode first
  128. self._utility.set_mode([1, 2])
  129. for i in xrange(test_count):
  130. _ap = random.choice(_ap_list)
  131. # arrange ap
  132. _ap_type = _ap[0]
  133. _ap_prop = _ap[1]
  134. if _ap_type == "target":
  135. _ap_prop = self._generate_random_ap_prop()
  136. pass
  137. # step 1 : mandatory step, set up AP
  138. if self._utility.setup_ap(_ap_type, _ap_prop) is False:
  139. self._logging_fail_step(_ap_prop["ssid"], "Set AP")
  140. NativeLog.add_prompt_trace("[Step1] setup AP Fail")
  141. continue
  142. step_config = random.choice(self.step_config)
  143. NativeLog.add_prompt_trace("[Step1] setup AP succeed")
  144. # step 2 : optional step, do scan before connect
  145. if step_config & STEPS["SCAN1"] != 0: # check option
  146. if self._utility.do_scan(_ap_prop) is False:
  147. self._logging_fail_step(_ap_prop["ssid"], "Scan before JAP")
  148. NativeLog.add_prompt_trace("[Step2] Scan Done")
  149. # step 3 : mandatory step, join AP
  150. if step_config & STEPS["JAP"] != 0: # check option
  151. _join_test_method = random.choice(self.join_test_method)
  152. time_cost = self._utility.join_ap(_join_test_method, _ap_type, _ap_prop, self.join_delay)
  153. # log performance to performance log file
  154. self._logging_performance(_ap_prop["ssid"], _join_test_method, time_cost)
  155. if time_cost is False:
  156. # do scan once to check if AP exist
  157. self._utility.do_scan(_ap_prop)
  158. continue
  159. NativeLog.add_prompt_trace("[Step3] Join AP done")
  160. # step 4 : optional step, scan after join AP
  161. if step_config & STEPS["SCAN2"] != 0: # check option
  162. if self._utility.do_scan(_ap_prop) is False:
  163. self._logging_fail_step(_ap_prop["ssid"], "Scan after JAP")
  164. NativeLog.add_prompt_trace("[Step4] Scan done")
  165. # step 5 : optional step, reconnect test
  166. if step_config & STEPS["RECONNECT"] != 0: # check option
  167. _reconnect_test_method = random.choice(self.reconnect_test_method)
  168. if self._utility.do_reconnect(_reconnect_test_method,
  169. _ap_type, _ap_prop, self.reconnect_delay) is False:
  170. self._logging_fail_step(_ap_prop["ssid"], "Reconnect")
  171. NativeLog.add_prompt_trace("[Step5] Reconnect done")
  172. # continue to next loop
  173. NativeLog.add_prompt_trace("[WifiJAP] Test count %d done" % i)
  174. # generate report and cleanup
  175. self._generate_performance_report()
  176. self.result_cntx.set_result("Succeed")
  177. def result_check(self, port_name, data):
  178. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  179. self.result_cntx.append_data(port_name, data)
  180. def main():
  181. pass
  182. if __name__ == '__main__':
  183. main()