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_CookiesBlockedForUrls(enterprise_policy_base.EnterprisePolicyTest):
14    """Test effect of the CookiesBlockedForUrls policy on Chrome OS behavior.
15
16    This test implicitly verifies one value of the DefaultCookiesSetting
17    policy as well. When DefaultCookiesSetting is set to 1, cookies for all
18    URLs shall be stored (i.e., shall be not blocked), except for the URL
19    patterns specified by the CookiesBlockedForUrls policy value.
20
21    The test verifies ChromeOS behaviour for different values of the
22    CookiesBlockedForUrls 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 allowed for urls that
25    are not part of the policy value.
26
27    The corresponding three test cases are NotSet_CookiesAllowed,
28    SingleUrl_CookiesBlocked, MultipleUrls_CookiesBlocked and
29    MultipleUrls_CookiesAllowed.
30
31    """
32    version = 1
33
34    def initialize(self, **kwargs):
35        """Initialize this test."""
36        self._initialize_test_constants()
37        super(policy_CookiesBlockedForUrls, self).initialize(**kwargs)
38        self.start_webserver()
39
40
41    def _initialize_test_constants(self):
42        """Initialize test-specific constants, some from class constants."""
43        self.POLICY_NAME = 'CookiesBlockedForUrls'
44        self.COOKIE_NAME = 'cookie1'
45        self.TEST_FILE = 'cookie_status.html'
46        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
47        self.COOKIE_BLOCKED_SINGLE_FILE = [self.WEB_HOST]
48        self.COOKIE_BLOCKED_MULTIPLE_FILES = ['http://google.com',
49                                              self.WEB_HOST,
50                                              'http://doesnotmatter.com']
51        self.COOKIE_ALLOWED_MULTIPLE_FILES = ['https://testingwebsite.html',
52                                              'https://somewebsite.com',
53                                              'http://doesnotmatter.com']
54        self.TEST_CASES = {
55            'NotSet_Allow': None,
56            'SingleUrl_Block': self.COOKIE_BLOCKED_SINGLE_FILE,
57            'MultipleUrls_Block': self.COOKIE_BLOCKED_MULTIPLE_FILES,
58            'MultipleUrls_Allow': self.COOKIE_ALLOWED_MULTIPLE_FILES
59        }
60        self.SUPPORTING_POLICIES = {'DefaultCookiesSetting': 1}
61
62
63    def _is_cookie_blocked(self, url):
64        """Return True if cookie is blocked for the URL else return False.
65
66        @param url: Url of the page which is loaded to check whether it's
67                    cookie is blocked or stored.
68        """
69        tab = self.navigate_to_url(url)
70        return tab.GetCookieByName(self.COOKIE_NAME) is None
71
72
73    def _test_cookies_blocked_for_urls(self, policy_value):
74        """Verify CrOS enforces CookiesBlockedForUrls policy value.
75
76        When the CookiesBlockedForUrls policy is set to one or more urls/hosts,
77        check that cookies are blocked for the urls/urlpatterns listed in
78        the policy value. When set to None, check that cookies are allowed for
79        all URLs.
80
81        @param policy_value: policy value expected.
82
83        @raises: TestFail if cookies are blocked/not blocked based on the
84                 corresponding policy values.
85        """
86        cookie_is_blocked = self._is_cookie_blocked(self.TEST_URL)
87
88        if policy_value and self.WEB_HOST in policy_value:
89            if not cookie_is_blocked:
90                raise error.TestFail('Cookies should be blocked.')
91        else:
92            if cookie_is_blocked:
93                raise error.TestFail('Cookies should be allowed.')
94
95
96    def run_once(self, case):
97        """Setup and run the test configured for the specified test case.
98
99        @param case: Name of the test case to run.
100        """
101        case_value = self.TEST_CASES[case]
102        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
103        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
104        self._test_cookies_blocked_for_urls(case_value)
105