1# Copyright (c) 2015 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
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.common_lib.cros.network import interface
8from autotest_lib.client.cros import dhcp_handling_rule
9from autotest_lib.client.cros import dhcp_packet
10from autotest_lib.client.cros import dhcp_test_base
11from autotest_lib.client.cros.networking import shill_proxy
12
13# Length of time the lease from the DHCP server is valid.
14LEASE_TIME_SECONDS = 60
15# We'll fill in the subnet and give this address to the client.
16INTENDED_IP_SUFFIX = "0.0.0.101"
17# We should be able to complete a DHCP negotiation in this amount of time.
18DHCP_NEGOTIATION_TIMEOUT_SECONDS = 10
19
20class network_DhcpMTU(dhcp_test_base.DhcpTestBase):
21    """Test implemenation of MTU including confirming the interface state."""
22
23    def check_mtu_config(self, mtu):
24        """Check that the ipconfig and interface in the client has correct MTU.
25
26        @param mtu int expected MTU value.
27
28        """
29        proxy = shill_proxy.ShillProxy()
30        device = proxy.find_object(
31                'Device',
32                {'Name': self.ethernet_pair.peer_interface_name})
33        if device is None:
34            raise error.TestFail('Device was not found.')
35        device_properties = device.GetProperties(utf8_strings=True)
36        ipconfig_path = device_properties['IPConfigs'][0]
37        ipconfig = proxy.get_dbus_object('org.chromium.flimflam.IPConfig',
38                                         ipconfig_path)
39        ipconfig_properties = ipconfig.GetProperties(utf8_strings=True)
40        ipconfig_mtu = ipconfig_properties['Mtu']
41        if ipconfig_mtu != mtu:
42            raise error.TestFail('Shill MTU %d does not match expected %d.' %
43                                 (ipconfig_mtu, mtu))
44
45        interface_mtu = interface.Interface(
46                self.ethernet_pair.peer_interface_name).mtu
47        if interface_mtu != mtu:
48            raise error.TestFail('Interface MTU %d does not match '
49                                 'expected %d.' % (interface_mtu, ipconfig_mtu))
50
51    def test_body(self):
52        """Main body of the test."""
53        subnet_mask = self.ethernet_pair.interface_subnet_mask
54        intended_ip = dhcp_test_base.DhcpTestBase.rewrite_ip_suffix(
55                subnet_mask,
56                self.server_ip,
57                INTENDED_IP_SUFFIX)
58        # Two real name servers, and a bogus one to be unpredictable.
59        dns_servers = ["8.8.8.8", "8.8.4.4", "192.168.87.88"]
60        interface_mtu = 1234
61        # This is the pool of information the server will give out to the client
62        # upon request.
63        dhcp_options = {
64                dhcp_packet.OPTION_SERVER_ID : self.server_ip,
65                dhcp_packet.OPTION_SUBNET_MASK : subnet_mask,
66                dhcp_packet.OPTION_IP_LEASE_TIME : LEASE_TIME_SECONDS,
67                dhcp_packet.OPTION_REQUESTED_IP : intended_ip,
68                dhcp_packet.OPTION_DNS_SERVERS : dns_servers,
69                dhcp_packet.OPTION_INTERFACE_MTU : interface_mtu
70                }
71        rules = [
72                dhcp_handling_rule.DhcpHandlingRule_RespondToDiscovery(
73                        intended_ip, self.server_ip, dhcp_options, {}),
74                dhcp_handling_rule.DhcpHandlingRule_RespondToRequest(
75                        intended_ip, self.server_ip, dhcp_options, {})
76                ]
77        rules[-1].is_final_handler = True
78        self.server.start_test(rules, DHCP_NEGOTIATION_TIMEOUT_SECONDS)
79        self.server.wait_for_test_to_finish()
80        if not self.server.last_test_passed:
81            raise error.TestFail("Test server didn't get all the messages it "
82                                 "was told to expect during negotiation.")
83
84        self.wait_for_dhcp_propagation()
85        self.check_mtu_config(interface_mtu)
86