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
9import logging
10
11from autotest_lib.client.common_lib import error
12from autotest_lib.client.cros import enterprise_policy_base
13
14POLICY_NAME = 'CookiesAllowedForUrls'
15URL_BASE = 'http://localhost'
16URL_PORT = 8080
17URL_HOST = '%s:%d'%(URL_BASE, URL_PORT)
18URL_RESOURCE = '/test_data/testWebsite1.html'
19TEST_URL = URL_HOST + URL_RESOURCE
20COOKIE_NAME = 'cookie1'
21COOKIE_ALLOWED_SINGLE_FILE_DATA = [URL_HOST]
22COOKIE_ALLOWED_MULTIPLE_FILES_DATA = ['http://google.com', URL_HOST,
23                                      'http://doesnotmatter.com']
24COOKIE_BLOCKED_MULTIPLE_FILES_DATA = ['https://testingwebsite.html',
25                                      'https://somewebsite.com',
26                                      'http://doesnotmatter.com']
27# Setting DefaultCookiesSetting=2 blocks cookies on all sites.
28SUPPORTING_POLICIES = {'DefaultCookiesSetting': 2}
29
30
31class policy_CookiesAllowedForUrls(enterprise_policy_base.EnterprisePolicyTest):
32    """Test effect of the CookiesAllowedForUrls policy on Chrome OS behavior.
33
34    This test implicitly verifies one value of the DefaultCookiesSetting
35    policy as well. When the DefaultCookiesSetting policy value is set to 2,
36    cookies for all URLs shall not be stored (ie, shall be blocked), except
37    for the URL patterns specified by the CookiesAllowedForUrls policy.
38
39    The test verifies ChromeOS behaviour for different values of the
40    CookiesAllowedForUrls policy i.e., for the policy value set to Not Set,
41    Set to a single url/host pattern or when the policy is set to multiple
42    url/host patterns. It also excercises an additional scenario i.e., it
43    tests that cookies are blocked for urls that are not part of the policy
44    value.
45
46    The corresponding three test cases are NotSet_CookiesBlocked,
47    SingleUrl_CookiesAllowed, MultipleUrls_CookiesAllowed, and
48    and MultipleUrls_CookiesBlocked.
49
50    """
51    version = 1
52    TEST_CASES = {
53        'NotSet_CookiesBlocked': '',
54        'SingleUrl_CookiesAllowed': COOKIE_ALLOWED_SINGLE_FILE_DATA,
55        'MultipleUrls_CookiesAllowed': COOKIE_ALLOWED_MULTIPLE_FILES_DATA,
56        'MultipleUrls_CookiesBlocked': COOKIE_BLOCKED_MULTIPLE_FILES_DATA
57    }
58
59    def initialize(self, args=()):
60        super(policy_CookiesAllowedForUrls, self).initialize(args)
61        self.start_webserver(URL_PORT)
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        """
70        tab = self.navigate_to_url(url)
71        return tab.GetCookieByName(COOKIE_NAME) is None
72
73    def _test_CookiesAllowedForUrls(self, policy_value, policies_json):
74        """Verify CrOS enforces CookiesAllowedForUrls policy value.
75
76        When the CookiesAllowedForUrls policy is set to one or more urls/hosts,
77        check that cookies are not blocked for the urls/urlpatterns listed in
78        the policy value.
79        When set to None, check that cookies are blocked for all URLs.
80
81        @param policy_value: policy value expected on chrome://policy page.
82        @param policies_json: policy JSON data to send to the fake DM server.
83        @raises: TestFail if cookies are blocked/not blocked based on the
84                 corresponding policy values.
85
86        """
87        logging.info('Running _test_CookiesAllowedForUrls(%s, %s)',
88                     policy_value, policies_json)
89        self.setup_case(POLICY_NAME, policy_value, policies_json)
90
91        cookie_is_blocked = self._is_cookie_blocked(TEST_URL)
92        logging.info('cookie_is_blocked = %s', cookie_is_blocked)
93
94        if policy_value and URL_HOST in policy_value:
95            if cookie_is_blocked:
96                raise error.TestFail('Cookies should be allowed.')
97        else:
98            if not cookie_is_blocked:
99                raise error.TestFail('Cookies should be blocked.')
100
101    def run_test_case(self, case):
102        """Setup and run the test configured for the specified test case.
103
104        Set the expected |policy_value| and |policies_json| data based on the
105        test |case|. If the user specified an expected |value| in the command
106        line args, then use it to set the |policy_value| and blank out the
107        |policies_json|.
108
109        @param case: Name of the test case to run.
110
111        """
112        policy_value = None
113        policies_json = None
114
115        if self.is_value_given:
116            # If |value| was given in the command line args, then set expected
117            # |policy_value| to the given value, and |policies_json| to None.
118            policy_value = self.value
119            policies_json = None
120        else:
121            # Otherwise, set expected |policy_value| and setup |policies_json|
122            # data to the values required by the specified test |case|.
123            policy_value = ','.join(self.TEST_CASES[case])
124            policy_json = {POLICY_NAME: self.TEST_CASES[case]}
125            policies_json = SUPPORTING_POLICIES.copy()
126            policies_json.update(policy_json)
127
128        # Run test using the values configured for the test case.
129        self._test_CookiesAllowedForUrls(policy_value, policies_json)
130