• 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
5# Copyright 2015 The Chromium OS Authors. All rights reserved.
6# Use of this source code is governed by a BSD-style license that can be
7# found in the LICENSE file.
8
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros.enterprise import enterprise_policy_base
11
12
13class policy_CookiesAllowedForUrls(enterprise_policy_base.EnterprisePolicyTest):
14    """Test effect of the CookiesAllowedForUrls policy on Chrome OS behavior.
15
16    This test implicitly verifies one value of the DefaultCookiesSetting
17    policy as well. When the DefaultCookiesSetting policy value is set to 2,
18    cookies for all URLs shall not be stored (ie, shall be blocked), except
19    for the URL patterns specified by the CookiesAllowedForUrls policy.
20
21    The test verifies ChromeOS behaviour for different values of the
22    CookiesAllowedForUrls policy, i.e., for the policy value set to Not Set,
23    set to a single url/host pattern, or when the policy is set to multiple
24    url/host patterns. It also verifies that cookies are blocked for urls that
25    are not part of the policy value.
26
27    The corresponding three test cases are NotSet_CookiesBlocked,
28    SingleUrl_CookiesAllowed, MultipleUrls_CookiesAllowed, and
29    MultipleUrls_CookiesBlocked.
30    """
31    version = 1
32
33    def initialize(self, **kwargs):
34        """Initialize this test."""
35        self._initialize_test_constants()
36        super(policy_CookiesAllowedForUrls, self).initialize(**kwargs)
37        self.start_webserver()
38
39
40    def _initialize_test_constants(self):
41        """Initialize test-specific constants, some from class constants."""
42        self.POLICY_NAME = 'CookiesAllowedForUrls'
43        self.COOKIE_NAME = 'cookie1'
44        self.TEST_FILE = 'cookie_status.html'
45        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
46        self.COOKIE_ALLOWED_SINGLE_FILE = [self.WEB_HOST]
47        self.COOKIE_ALLOWED_MULTIPLE_FILES = ['http://google.com',
48                                              self.WEB_HOST,
49                                              'http://doesnotmatter.com']
50        self.COOKIE_BLOCKED_MULTIPLE_FILES = ['https://testingwebsite.html',
51                                              'https://somewebsite.com',
52                                              'http://doesnotmatter.com']
53        self.TEST_CASES = {
54            'NotSet_Block': None,
55            'SingleUrl_Allow': self.COOKIE_ALLOWED_SINGLE_FILE,
56            'MultipleUrls_Allow': self.COOKIE_ALLOWED_MULTIPLE_FILES,
57            'MultipleUrls_Block': self.COOKIE_BLOCKED_MULTIPLE_FILES
58        }
59        self.SUPPORTING_POLICIES = {'DefaultCookiesSetting': 2}
60
61
62    def _is_cookie_blocked(self, url):
63        """Return True if cookie is blocked for the URL else return False.
64
65        @param url: Url of the page which is loaded to check whether it's
66                    cookie is blocked or stored.
67        """
68        tab = self.navigate_to_url(url)
69        return tab.GetCookieByName(self.COOKIE_NAME) is None
70
71
72    def _test_cookies_allowed_for_urls(self, policy_value):
73        """Verify CrOS enforces CookiesAllowedForUrls policy value.
74
75        When the CookiesAllowedForUrls policy is set to one or more urls/hosts,
76        check that cookies are not blocked for the urls/urlpatterns listed in
77        the policy value. When set to None, check that cookies are blocked for
78        all URLs.
79
80        @param policy_value: policy value for this case.
81        @raises: TestFail if cookies are blocked/not blocked based on the
82                 corresponding policy values.
83        """
84        cookie_is_blocked = self._is_cookie_blocked(self.TEST_URL)
85
86        if policy_value and self.WEB_HOST in policy_value:
87            if cookie_is_blocked:
88                raise error.TestFail('Cookies should be allowed.')
89        else:
90            if not cookie_is_blocked:
91                raise error.TestFail('Cookies should be blocked.')
92
93
94    def run_once(self, case):
95        """Setup and run the test configured for the specified test case.
96
97        Set the expected |policy_value| and |policies_dict| data defined for
98        the specified test |case|, and run the test.
99
100        @param case: Name of the test case to run.
101        """
102        case_value = self.TEST_CASES[case]
103        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
104        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
105        self._test_cookies_allowed_for_urls(case_value)
106