1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 from autotest_lib.client.bin import test, utils 6 from autotest_lib.client.common_lib import error 7 from autotest_lib.client.cros import rtc, sys_power 8 9 import logging 10 11 class network_WiFiResume(test.test): 12 version = 1 13 14 def run_once(self, reachable=None, wifi_timeout=5, dev=None): 15 '''Check that WiFi is working after a resume 16 17 If test is successful, the performance key "secs_iface_up_delay" is 18 reported as seconds the interface needed to be up 19 20 @param reachable: ip address (string) to try to ping as a network test 21 @param dev: device (eg 'wlan0') to use for wifi tests. 22 autodetected if unset 23 @param wifi_timeout: number of seconds with in which a WiFi association 24 must be (re)established after a suspend/resume cycle 25 ''' 26 27 if not dev: 28 dev = get_wifi_dev() 29 30 if not dev: 31 raise error.TestError('No WiFi device found') 32 33 logging.info('checking wifi interface %s', dev) 34 check_wifi_dev(dev) 35 36 if network_is_up(reachable=reachable, dev=dev): 37 suspend_to_ram() 38 start = rtc.get_seconds() 39 deadline = start + wifi_timeout 40 have_network = network_is_up(reachable=reachable, dev=dev) 41 42 while (not have_network) and (deadline > rtc.get_seconds()): 43 have_network = network_is_up(reachable=reachable, dev=dev) 44 45 if have_network: 46 delay = rtc.get_seconds() - start 47 logging.info('Network came up at %d seconds', delay) 48 self.write_perf_keyval({'secs_iface_up_delay': delay}) 49 return 50 51 delay = rtc.get_seconds() - start 52 raise error.TestFail('Network down after %d seconds' % delay) 53 54 raise error.TestFail('Network down at start of test - cannot continue') 55 56 57 def suspend_to_ram(secs_to_suspend=5): 58 logging.info('Scheduling wakeup in %d seconds\n', secs_to_suspend) 59 sys_power.do_suspend(secs_to_suspend) 60 logging.info('Woke up at %d', rtc.get_seconds()) 61 62 63 def get_wifi_dev(): 64 return utils.system_output('iwconfig 2>/dev/null | (read i x; echo $i)') 65 66 67 def check_wifi_dev(dev): 68 cmd = 'iwconfig %s 2>/dev/null | grep "^%s"' % (dev, dev) 69 ret = utils.system(cmd, ignore_status=True) 70 if dev and ret == 0: 71 return 72 raise error.TestError('"%s" is not a valid WiFi device' % dev) 73 74 75 def get_pingable_address(dev=None): 76 if not dev: 77 dev = get_wifi_dev() 78 cmd = 'ip route show dev %s to match 0/0|if read X X G X; then echo $G; fi' 79 return utils.system_output(cmd % dev) or None 80 81 82 def network_is_up(reachable=None, dev=None): 83 if not reachable: 84 reachable = get_pingable_address(dev=dev) 85 if not reachable: 86 return False 87 if utils.ping(reachable, tries=1) == 0: 88 return True 89 return False 90