Home | History | Annotate | Download | only in network_WlanHasIP
      1 #!/usr/bin/python
      2 #
      3 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 __author__ = 'kdlucas (at] chromium.org (Kelly Lucas)'
      8 
      9 import fcntl, socket, struct
     10 
     11 from autotest_lib.client.bin import test
     12 from autotest_lib.client.common_lib import error
     13 
     14 
     15 class network_WlanHasIP(test.test):
     16     """
     17     Ensure wlan0 has a valid IP address.
     18     """
     19     version = 1
     20 
     21     def get_ip(self, device):
     22         """
     23         Get the ip address of device. If no IP address is found it will return
     24         None, since socket.inet_ntoa will fail with IOError.
     25 
     26         Args:
     27             device: string, should be a valid network device name.
     28         Returns:
     29             string, represents the IP address.
     30         """
     31 
     32         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     33         try:
     34             ipaddress = socket.inet_ntoa(fcntl.ioctl(
     35                                          s.fileno(),
     36                                          0x8915, # SIOCGIFADDR
     37                                          struct.pack('256s', device[:15])
     38                                          )[20:24])
     39         except IOError:
     40             ipaddress = None
     41 
     42         return ipaddress
     43 
     44 
     45     def run_once(self):
     46         WDEV = 'wlan0'
     47         wlanip = self.get_ip(WDEV)
     48 
     49         if not wlanip:
     50             raise error.TestFail('%s does not have an assigned IP!' % WDEV)
     51