121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart# Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart# Use of this source code is governed by a BSD-style license that can be
321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart# found in the LICENSE file.
421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartfrom autotest_lib.client.common_lib import error
721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartfrom autotest_lib.client.common_lib.cros.network import interface
821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartfrom autotest_lib.client.cros import dhcp_handling_rule
921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartfrom autotest_lib.client.cros import dhcp_packet
1021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartfrom autotest_lib.client.cros import dhcp_test_base
1121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartfrom autotest_lib.client.cros.networking import shill_proxy
1221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
1321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart# Length of time the lease from the DHCP server is valid.
1421529ce4b0851467ae2ea18c93776b90d1b3a986Paul StewartLEASE_TIME_SECONDS = 60
1521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart# We'll fill in the subnet and give this address to the client.
1621529ce4b0851467ae2ea18c93776b90d1b3a986Paul StewartINTENDED_IP_SUFFIX = "0.0.0.101"
1721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart# We should be able to complete a DHCP negotiation in this amount of time.
1821529ce4b0851467ae2ea18c93776b90d1b3a986Paul StewartDHCP_NEGOTIATION_TIMEOUT_SECONDS = 10
1921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
2021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewartclass network_DhcpMTU(dhcp_test_base.DhcpTestBase):
2121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart    """Test implemenation of MTU including confirming the interface state."""
2221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
2321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart    def check_mtu_config(self, mtu):
2421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        """Check that the ipconfig and interface in the client has correct MTU.
2521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
2621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        @param mtu int expected MTU value.
2721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
2821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        """
2921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        proxy = shill_proxy.ShillProxy()
3021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        device = proxy.find_object(
3121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                'Device',
3221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                {'Name': self.ethernet_pair.peer_interface_name})
3321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        if device is None:
3421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart            raise error.TestFail('Device was not found.')
3521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        device_properties = device.GetProperties(utf8_strings=True)
3621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        ipconfig_path = device_properties['IPConfigs'][0]
3721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        ipconfig = proxy.get_dbus_object('org.chromium.flimflam.IPConfig',
3821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                                         ipconfig_path)
3921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        ipconfig_properties = ipconfig.GetProperties(utf8_strings=True)
4021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        ipconfig_mtu = ipconfig_properties['Mtu']
4121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        if ipconfig_mtu != mtu:
4221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart            raise error.TestFail('Shill MTU %d does not match expected %d.' %
4321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                                 (ipconfig_mtu, mtu))
4421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
4521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        interface_mtu = interface.Interface(
4621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                self.ethernet_pair.peer_interface_name).mtu
4721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        if interface_mtu != mtu:
4821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart            raise error.TestFail('Interface MTU %d does not match '
4921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                                 'expected %d.' % (interface_mtu, ipconfig_mtu))
5021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
5121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart    def test_body(self):
5221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        """Main body of the test."""
5321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        subnet_mask = self.ethernet_pair.interface_subnet_mask
5421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        intended_ip = dhcp_test_base.DhcpTestBase.rewrite_ip_suffix(
5521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                subnet_mask,
5621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                self.server_ip,
5721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                INTENDED_IP_SUFFIX)
5821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        # Two real name servers, and a bogus one to be unpredictable.
5921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        dns_servers = ["8.8.8.8", "8.8.4.4", "192.168.87.88"]
6021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        interface_mtu = 1234
6121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        # This is the pool of information the server will give out to the client
6221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        # upon request.
6321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        dhcp_options = {
6421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_packet.OPTION_SERVER_ID : self.server_ip,
6521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_packet.OPTION_SUBNET_MASK : subnet_mask,
6621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_packet.OPTION_IP_LEASE_TIME : LEASE_TIME_SECONDS,
6721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_packet.OPTION_REQUESTED_IP : intended_ip,
6821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_packet.OPTION_DNS_SERVERS : dns_servers,
6921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_packet.OPTION_INTERFACE_MTU : interface_mtu
7021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                }
7121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        rules = [
7221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_handling_rule.DhcpHandlingRule_RespondToDiscovery(
7321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                        intended_ip, self.server_ip, dhcp_options, {}),
7421529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                dhcp_handling_rule.DhcpHandlingRule_RespondToRequest(
7521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                        intended_ip, self.server_ip, dhcp_options, {})
7621529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                ]
7721529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        rules[-1].is_final_handler = True
7821529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        self.server.start_test(rules, DHCP_NEGOTIATION_TIMEOUT_SECONDS)
7921529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        self.server.wait_for_test_to_finish()
8021529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        if not self.server.last_test_passed:
8121529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart            raise error.TestFail("Test server didn't get all the messages it "
8221529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart                                 "was told to expect during negotiation.")
8321529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart
84722a445caeb3b31938157fa2dc66690643a4f4b2Paul Stewart        self.wait_for_dhcp_propagation()
8521529ce4b0851467ae2ea18c93776b90d1b3a986Paul Stewart        self.check_mtu_config(interface_mtu)
86