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 errno
6import logging
7import os
8import time
9
10from autotest_lib.client.bin import test
11from autotest_lib.client.common_lib import error, utils
12from autotest_lib.client.cros.networking import shill_context
13from autotest_lib.client.cros.networking import shill_proxy
14
15class network_DefaultProfileCreation(test.test):
16    """The Default Profile Creation class.
17
18    Wipe the default profile, start shill, and check that a default
19    profile has been created.
20
21    Test that the default profile contains default values for properties
22    that should have them.
23
24    """
25    DEFAULT_PROFILE_PATH = '/var/cache/shill/default.profile'
26    EXPECTED_SETTINGS = [
27        # From DefaultProfile::LoadManagerProperties
28        'CheckPortalList=ethernet,wifi,cellular',
29        'IgnoredDNSSearchPaths=gateway.2wire.net',
30        'LinkMonitorTechnologies=wifi',
31        'PortalURL=http://www.gstatic.com/generate_204',
32        'PortalCheckInterval=30',
33        ]
34    PROFILE_LOAD_TIMEOUT_SECONDS = 5
35    version = 1
36
37
38    def run_once(self):
39        """Test main loop."""
40        # TODO: Remove the following block once the bug(crbug.com/594336)
41        # is fixed.
42        boards_to_skip = ['cyan-cheets']
43        dut_board = utils.get_current_board()
44        if dut_board in boards_to_skip:
45            logging.info("Skipping test run on this board.")
46            return
47
48        with shill_context.stopped_shill():
49            try:
50                os.remove(self.DEFAULT_PROFILE_PATH)
51            except OSError as e:
52                if e.errno != errno.ENOENT:
53                    raise e
54        shill = shill_proxy.ShillProxy.get_proxy()
55        start_time = time.time()
56        profile = None
57        while time.time() - start_time < self.PROFILE_LOAD_TIMEOUT_SECONDS:
58            if shill.get_profiles():
59                with open(self.DEFAULT_PROFILE_PATH) as f:
60                    profile = f.read()
61                    if profile:
62                        break
63
64            time.sleep(1)
65        else:
66            if profile is None:
67                raise error.TestFail('shill should load a profile within '
68                                     '%d seconds.' %
69                                     self.PROFILE_LOAD_TIMEOUT_SECONDS)
70            else:
71                raise error.TestFail('shill profile is still empty after '
72                                     '%d seconds.' %
73                                     self.PROFILE_LOAD_TIMEOUT_SECONDS)
74
75        logging.info('Profile contents after %d seconds:\%s',
76                     time.time() - start_time, profile)
77        for setting in self.EXPECTED_SETTINGS:
78            if setting not in profile:
79                logging.error('Did not find setting %s', setting)
80                logging.error('Full profile contents are:\n%s', profile)
81                raise error.TestFail('Missing setting(s) in default profile.')
82