1# Copyright (c) 2013 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
5import logging
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import dhcp_test_base
10
11class network_DhcpFailureWithStaticIP(dhcp_test_base.DhcpTestBase):
12    """The DHCP Negotiation Timeout class.
13
14    Sets up a virtual ethernet pair, and stops the DHCP server on the
15    pair.  Static IP parameters are configured on the interface using
16    shill's StaticIP configuration.  Ensure that these parameters are
17    immediately applied to the ipconfig.
18
19    After the DHCP timeout interval, check to make sure that the same
20    IP config remains applied.
21
22    """
23    SHILL_DHCP_TIMEOUT_SECONDS = 30
24
25
26    def check_static_ip_config(self, ipconfig, static_ip_address, name_servers):
27        """Checks that the static IP configuration is applied to the
28        interface ipconfig.
29
30        @param ipconfig object representing the DBus IPConfig entity to check.
31        @param static_ip_address string IP address we expect to be configured.
32        @param name_servers list of string name servers we expect to be
33                configured on the interface.
34
35        """
36        ipconfig_properties = self.shill_proxy.dbus2primitive(
37                ipconfig.GetProperties(utf8_strings=True))
38
39        logging.info('IPConfig properties are %r', ipconfig_properties)
40        if static_ip_address != ipconfig_properties['Address']:
41            raise error.TestFail('Expected address %r but got %r' %
42                                 (static_ip_address,
43                                  ipconfig_properties['Address']))
44
45        if name_servers != ipconfig_properties['NameServers']:
46            raise error.TestFail('Expected name servers %r but got %r' %
47                                 (name_servers,
48                                  ipconfig_properties['NameServers']))
49
50
51    def get_ipconfig(self):
52        """Returns the first IPConfig object associated with the peer device."""
53        ipconfig_objects = (
54                self.get_interface_ipconfig_objects(
55                        self.ethernet_pair.peer_interface_name))
56        if len(ipconfig_objects) == 0:
57            raise error.TestFail('Failed to retrieve DHCP ipconfig object '
58                                 'from shill.')
59        return ipconfig_objects[0]
60
61
62    def test_body(self):
63        """Test main loop."""
64        self.server.stop()
65        service = self.find_ethernet_service(
66                self.ethernet_pair.peer_interface_name)
67
68        static_ip_address = '192.168.1.101'
69        prefix_len = 23
70        service.SetProperty('StaticIP.Address', static_ip_address)
71        service.SetProperty('StaticIP.Prefixlen', prefix_len)
72        name_servers = [ '10.10.10.10', '10.10.11.11' ]
73        service.SetProperty('StaticIP.NameServers',
74                            ','.join(name_servers))
75        ipconfig = self.get_ipconfig()
76        ipconfig.Refresh()
77
78        self.check_static_ip_config(ipconfig, static_ip_address, name_servers)
79
80        # Make sure configuration is still correct after DHCP timeout.
81        time.sleep(self.SHILL_DHCP_TIMEOUT_SECONDS + 2)
82        ipconfig = self.get_ipconfig()
83        self.check_static_ip_config(ipconfig, static_ip_address, name_servers)
84