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_ImagesBlockedForUrls(enterprise_policy_base.EnterprisePolicyTest): 12 """Test ImagesBlockedForUrls 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 ImagesBlockedForUrls user policies. These values are covered by 16 four test cases, named: NotSet_Allow, 1Url_Block, 2Urls_Allow, and 17 3Urls_Block. 18 19 When the policy value is None (as in case=NotSet_Allow), then images are 20 not blocked on any page. When the value is set to a single domain (such as 21 case=1Url_Block), then images are blocked on any page with that domain. 22 When set to multiple domains (as in case=2Urls_Allow or 3Urls_Block), 23 then images are blocked on any page with a domain that matches any of the 24 listed domains. 25 26 Two test cases (1Url_Block, 3Urls_Block) are designed to block images 27 on the test page. The other two test cases (NotSet_Allow, 2Urls_Allow) 28 are designed to allow images to be shown 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_ImagesAllowedForUrls. 32 For this test, we set DefaultImagesSetting=1 (or null). This allows images 33 on all pages except those with a domain listed in ImagesBlockedForUrls. 34 For the test of ImagesAllowedForUrls, we set DefaultImagesSetting=2. 35 That blocks images on all pages except those with domains listed in 36 ImagesAllowedForUrls. 37 38 """ 39 version = 1 40 41 def initialize(self, **kwargs): 42 """Initialize this test.""" 43 self._initialize_test_constants() 44 super(policy_ImagesBlockedForUrls, 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 = 'ImagesBlockedForUrls' 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 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_Allow': None, 60 '1Url_Block': self.URL1_DATA, 61 '2Urls_Allow': self.URL2_DATA, 62 '3Urls_Block': self.URL3_DATA 63 } 64 self.STARTUP_URLS = ['chrome://policy', 'chrome://settings'] 65 self.SUPPORTING_POLICIES = { 66 'DefaultImagesSetting': 1, 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_blocked_for_urls(self, policy_value): 86 """Verify CrOS enforces the ImagesBlockedForUrls policy. 87 88 When ImagesBlockedForUrls is undefined, images shall be allowed on 89 all pages. When ImagesBlockedForUrls contains one or more URLs, images 90 shall be blocked on any page whose domain matches any of the listed 91 domains. 92 93 @param policy_value: policy value for this case. 94 """ 95 tab = self.navigate_to_url(self.TEST_URL) 96 self._wait_for_page_ready(tab) 97 image_is_blocked = self._is_image_blocked(tab) 98 99 # String |WEB_HOST| shall be found in string |policy_value| for test 100 # cases 1Url_Block and 3Urls_Block, but not for NotSet_Allow and 101 # 2Urls_Allow. 102 if policy_value is not None and self.WEB_HOST in policy_value: 103 if not image_is_blocked: 104 raise error.TestFail('Image should be blocked.') 105 else: 106 if image_is_blocked: 107 raise error.TestFail('Image should not be blocked.') 108 tab.Close() 109 110 111 def run_once(self, case): 112 """Setup and run the test configured for the specified test case. 113 114 @param case: Name of the test case to run. 115 """ 116 case_value = self.TEST_CASES[case] 117 self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value 118 self.setup_case(user_policies=self.SUPPORTING_POLICIES) 119 self._test_images_blocked_for_urls(case_value) 120