1#!/usr/bin/env python3.4 2# 3# Copyright (C) 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may not 6# use this file except in compliance with the License. You may obtain a copy of 7# the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations under 15# the License. 16# 17""" 18This test exercises basic scanning functionality to confirm expected behavior 19related to wlan scanning 20""" 21 22from datetime import datetime 23 24import pprint 25import time 26 27import acts.base_test 28import acts.test_utils.wifi.wifi_test_utils as wutils 29 30from acts import signals 31from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest 32 33class WlanScanTest(WifiBaseTest): 34 """wlan scan test class. 35 36 Test Bed Requirement: 37 * One Fuchsia device 38 * Several Wi-Fi networks visible to the device, including an open Wi-Fi 39 network. 40 """ 41 def __init__(self, controllers): 42 WifiBaseTest.__init__(self, controllers) 43 44 def setup_class(self): 45 self.dut = self.fuchsia_devices[0] 46 47 def teardown_test(self): 48 self.dut.wlan_lib.wlanDisconnect() 49 50 """Helper Functions""" 51 52 def check_connect_response(self, connection_response): 53 if connection_response.get("error") is None: 54 # the command did not get an error response - go ahead and check the 55 # result 56 connection_result = connection_response.get("result") 57 if connection_result: 58 self.log.info("connection to network successful") 59 else: 60 # ideally, we would have the actual error... but logging here to 61 # cover that error case 62 raise signals.TestFailure("Connect call failed, aborting test") 63 else: 64 # the response indicates an error - log and raise failure 65 raise signals.TestFailure("Aborting test - Connect call failed with error: %s" 66 %connection_response.get("error")) 67 68 69 """Tests""" 70 def test_basic_scan_request(self): 71 """Verify a general scan trigger returns at least one result""" 72 start_time = datetime.now() 73 74 scan_response = self.dut.wlan_lib.wlanStartScan() 75 76 # first check if we received an error 77 if scan_response.get("error") is None: 78 # the scan command did not get an error response - go ahead and check 79 # for scan results 80 scan_results = scan_response["result"] 81 else: 82 # the response indicates an error - log and raise failure 83 raise signals.TestFailure("Aborting test - scan failed with error: %s" 84 %scan_response.get("error")) 85 86 self.log.info("scan contained %d results", len(scan_results)) 87 88 total_time_ms = (datetime.now() - start_time).total_seconds() * 1000 89 self.log.info("scan time: %d ms", total_time_ms) 90 91 if len(scan_results) > 0: 92 raise signals.TestPass(details="", extras={"Scan time":"%d" %total_time_ms}) 93 else: 94 raise signals.TestFailure("Scan failed or did not find any networks") 95 96 def test_scan_while_connected_open_network(self): 97 """Verify a general scan trigger returns at least one result when wifi is connected""" 98 99 "first check if we can read params" 100 target_ssid = self.user_params.get("wlan_open_network_ssid").get("SSID") 101 self.log.info("got the ssid! %s", target_ssid) 102 103 connection_response = self.dut.wlan_lib.wlanConnectToNetwork(target_ssid) 104 self.check_connect_response(connection_response) 105 106 self.test_basic_scan_request() 107 108 def test_scan_while_connected_wpa2_network(self): 109 """Verify a general scan trigger returns at least one result when wifi is connected""" 110 111 "first check if we can read params" 112 target_ssid = self.user_params.get("wlan_wpa2_network_ssid").get("SSID") 113 target_pwd = self.user_params.get("wlan_wpa2_network_pwd").get("password") 114 self.log.info("got the ssid! %s", target_ssid) 115 116 connection_response = self.dut.wlan_lib.wlanConnectToNetwork(target_ssid, target_pwd) 117 self.check_connect_response(connection_response) 118 119 self.test_basic_scan_request() 120 121