• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 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
5import utils
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9
10
11class policy_ImagesAllowedForUrls(enterprise_policy_base.EnterprisePolicyTest):
12    """Test ImagesAllowedForUrls policy effect on CrOS look & feel.
13
14    This test verifies the behavior of Chrome OS with a range of valid values
15    for the ImagesAllowedForUrls user policies. These values are covered by
16    four test cases, named: NotSet_Block, 1Url_Allow, 2Urls_Block, and
17    3Urls_Allow.
18
19    When the policy value is None (as in case=NotSet_Block), then images are
20    blocked on any page. When the value is set to a single domain (such as
21    case=1Url_Allow), images are allowed on any page with that domain. When
22    set to multiple domains (as in case=2Urls_Block or 3Urls_Allow), then
23    images are allowed on any page with a domain that matches any of the
24    listed domains.
25
26    Two test cases (1Url_Allow, 3Urls_Allow) are designed to allow images
27    to be shown on the test page. The other two test cases (NotSet_Block,
28    2Urls_Block) are designed to block images on the test page.
29
30    Note this test has a dependency on the DefaultImagesSetting policy, which
31    is partially tested herein, and by the test policy_ImagesBlockedForUrls.
32    For this test, we set DefaultImagesSetting=2. This blocks images on all
33    pages except those with a domain listed in ImagesAllowedForUrls. For the
34    test policy_ImagesBlockedForUrls, we set DefaultImagesSetting=1. That
35    allows images to be shown on all pages except those with domains listed in
36    ImagesBlockedForUrls.
37
38    """
39    version = 1
40
41    def initialize(self, **kwargs):
42        self._initialize_test_constants()
43        super(policy_ImagesAllowedForUrls, self).initialize(**kwargs)
44        self.start_webserver()
45
46
47    def _initialize_test_constants(self):
48        """Initialize test-specific constants, some from class constants."""
49        self.POLICY_NAME = 'ImagesAllowedForUrls'
50        self.TEST_FILE = 'kittens.html'
51        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
52        self.MINIMUM_IMAGE_WIDTH = 640
53
54        self.URL1_DATA = [self.WEB_HOST]
55        self.URL2_DATA = ['http://www.bing.com', 'https://www.yahoo.com']
56        self.URL3_DATA = ['http://www.bing.com', self.WEB_HOST,
57                          'https://www.yahoo.com']
58        self.TEST_CASES = {
59            'NotSet_Block': None,
60            '1Url_Allow': self.URL1_DATA,
61            '2Urls_Block': self.URL2_DATA,
62            '3Urls_Allow': self.URL3_DATA
63        }
64        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
65        self.SUPPORTING_POLICIES = {
66            'DefaultImagesSetting': 2,
67            'BookmarkBarEnabled': False,
68            'RestoreOnStartupURLs': self.STARTUP_URLS,
69            'RestoreOnStartup': 4
70        }
71
72
73    def _wait_for_page_ready(self, tab):
74        utils.poll_for_condition(
75            lambda: tab.EvaluateJavaScript('pageReady'),
76            exception=error.TestError('Test page is not ready.'))
77
78
79    def _is_image_blocked(self, tab):
80        image_width = tab.EvaluateJavaScript(
81            "document.getElementById('kittens_id').naturalWidth")
82        return image_width < self.MINIMUM_IMAGE_WIDTH
83
84
85    def _test_images_allowed_for_urls(self, policy_value):
86        """Verify CrOS enforces the ImagesAllowedForUrls policy.
87
88        When ImagesAllowedForUrls is undefined, images shall be blocked on
89        all pages. When ImagesAllowedForUrls contains one or more URLs, images
90        shall be shown only on the pages whose domain matches any of the
91        listed domains.
92
93        @param policy_value: policy value for this case.
94
95        """
96        tab = self.navigate_to_url(self.TEST_URL)
97        self._wait_for_page_ready(tab)
98        image_is_blocked = self._is_image_blocked(tab)
99
100        # String |WEB_HOST| shall be found in string |policy_value| for test
101        # cases 1Url_Allow and 3Urls_Allow, but not for NotSet_Block and
102        # 2Urls_Block.
103        if policy_value is not None and self.WEB_HOST in policy_value:
104            if image_is_blocked:
105                raise error.TestFail('Image should not be blocked.')
106        else:
107            if not image_is_blocked:
108                raise error.TestFail('Image should be blocked.')
109        tab.Close()
110
111
112    def run_test_case(self, case):
113        """Setup and run the test configured for the specified test case.
114
115        @param case: Name of the test case to run.
116
117        """
118        case_value = self.TEST_CASES[case]
119        self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES)
120        self._test_images_allowed_for_urls(case_value)
121