1# Copyright (c) 2012 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 utils
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import dhcp_packet
10from autotest_lib.client.cros import dhcp_test_base
11
12# Length of time the lease from the DHCP server is valid.
13LEASE_TIME_SECONDS = 60
14# We'll fill in the subnet and give this address to the client.
15INTENDED_IP_SUFFIX = '0.0.0.101'
16# Most ChromeOS devices are configured with this name.
17DEFAULT_HOSTNAME = 'localhost'
18# Hostname we'll provide to the device.
19TEST_HOSTNAME = 'britney-spears'
20
21class network_DhcpRequestHostName(dhcp_test_base.DhcpTestBase):
22    """Tests that we can supply a hostname to the shill over DHCP."""
23    def test_body(self):
24        # Make sure that shill is started with
25        # --accept-hostname-from=pseudoethernet0.
26        required_flag = '--accept-hostname-from=pseudoethernet0'
27        pid = utils.system_output('pgrep shill')
28        process_info = utils.system_output('ps %s' % pid)
29        if required_flag not in process_info:
30            raise error.TestNAError('Invalid Test. '
31                                    'Expected shill to be started with %s' %
32                                    required_flag)
33
34        # Keep track of the original hostname.
35        original_hostname = utils.system_output('hostname')
36        if original_hostname != DEFAULT_HOSTNAME:
37            logging.warning('Unexpected starting hostname %s (expected %s)',
38                            original_hostname, DEFAULT_HOSTNAME)
39            # Set the hostname to something we know.
40            utils.system('hostname %s' % DEFAULT_HOSTNAME)
41
42        subnet_mask = self.ethernet_pair.interface_subnet_mask
43        intended_ip = dhcp_test_base.DhcpTestBase.rewrite_ip_suffix(
44                subnet_mask,
45                self.server_ip,
46                INTENDED_IP_SUFFIX)
47        # Two real name servers, and a bogus one to be unpredictable.
48        dns_servers = ['8.8.8.8', '8.8.4.4', '192.168.87.88']
49        domain_name = 'corp.google.com'
50        dns_search_list = [
51                'corgie.google.com',
52                'lies.google.com',
53                'that.is.a.tasty.burger.google.com',
54                ]
55        # This is the pool of information the server will give out to the client
56        # upon request.
57        dhcp_options = {
58                dhcp_packet.OPTION_SERVER_ID : self.server_ip,
59                dhcp_packet.OPTION_SUBNET_MASK : subnet_mask,
60                dhcp_packet.OPTION_IP_LEASE_TIME : LEASE_TIME_SECONDS,
61                dhcp_packet.OPTION_REQUESTED_IP : intended_ip,
62                dhcp_packet.OPTION_DNS_SERVERS : dns_servers,
63                dhcp_packet.OPTION_DOMAIN_NAME : domain_name,
64                dhcp_packet.OPTION_HOST_NAME : TEST_HOSTNAME,
65                dhcp_packet.OPTION_DNS_DOMAIN_SEARCH_LIST : dns_search_list,
66                }
67
68        try:
69            self.negotiate_and_check_lease(dhcp_options)
70            system_hostname = utils.system_output('hostname')
71        finally:
72            # Set the hostname back to the original to avoid side effects.
73            utils.system_output('hostname %s' % original_hostname)
74
75        # Test that shill updated the system hostname correctly.
76        if system_hostname != TEST_HOSTNAME:
77            raise error.TestFail('Expected system host name to be set to '
78                                 '%s, but got %s instead.' %
79                                 (TEST_HOSTNAME, system_hostname))
80