• 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        """Initialize this test."""
43        self._initialize_test_constants()
44        super(policy_ImagesAllowedForUrls, self).initialize(**kwargs)
45        self.start_webserver()
46
47
48    def _initialize_test_constants(self):
49        """Initialize test-specific constants, some from class constants."""
50        self.POLICY_NAME = 'ImagesAllowedForUrls'
51        self.TEST_FILE = 'kittens.html'
52        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
53        self.MINIMUM_IMAGE_WIDTH = 640
54
55        self.URL1_DATA = [self.WEB_HOST]
56        self.URL2_DATA = ['http://www.bing.com', 'https://www.yahoo.com']
57        self.URL3_DATA = ['http://www.bing.com', self.WEB_HOST,
58                          'https://www.yahoo.com']
59        self.TEST_CASES = {
60            'NotSet_Block': None,
61            '1Url_Allow': self.URL1_DATA,
62            '2Urls_Block': self.URL2_DATA,
63            '3Urls_Allow': self.URL3_DATA
64        }
65        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
66        self.SUPPORTING_POLICIES = {
67            'DefaultImagesSetting': 2,
68            'BookmarkBarEnabled': False,
69            'RestoreOnStartupURLs': self.STARTUP_URLS,
70            'RestoreOnStartup': 4
71        }
72
73
74    def _wait_for_page_ready(self, tab):
75        utils.poll_for_condition(
76            lambda: tab.EvaluateJavaScript('pageReady'),
77            exception=error.TestError('Test page is not ready.'))
78
79
80    def _is_image_blocked(self, tab):
81        image_width = tab.EvaluateJavaScript(
82            "document.getElementById('kittens_id').naturalWidth")
83        return image_width < self.MINIMUM_IMAGE_WIDTH
84
85
86    def _test_images_allowed_for_urls(self, policy_value):
87        """Verify CrOS enforces the ImagesAllowedForUrls policy.
88
89        When ImagesAllowedForUrls is undefined, images shall be blocked on
90        all pages. When ImagesAllowedForUrls contains one or more URLs, images
91        shall be shown only on the pages whose domain matches any of the
92        listed domains.
93
94        @param policy_value: policy value for this case.
95
96        """
97        tab = self.navigate_to_url(self.TEST_URL)
98        self._wait_for_page_ready(tab)
99        image_is_blocked = self._is_image_blocked(tab)
100
101        # String |WEB_HOST| shall be found in string |policy_value| for test
102        # cases 1Url_Allow and 3Urls_Allow, but not for NotSet_Block and
103        # 2Urls_Block.
104        if policy_value is not None and self.WEB_HOST in policy_value:
105            if image_is_blocked:
106                raise error.TestFail('Image should not be blocked.')
107        else:
108            if not image_is_blocked:
109                raise error.TestFail('Image should be blocked.')
110        tab.Close()
111
112
113    def run_once(self, case):
114        """Setup and run the test configured for the specified test case.
115
116        @param case: Name of the test case to run.
117
118        """
119        case_value = self.TEST_CASES[case]
120        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
121        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
122        self._test_images_allowed_for_urls(case_value)
123