1# Copyright (c) 2013 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 re
6
7from autotest_lib.client.bin import test, utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import chrome
10from telemetry.core import exceptions
11
12class security_SandboxStatus(test.test):
13    """Verify sandbox status."""
14    version = 1
15
16
17    def _EvaluateJavaScript(self, js):
18        '''Evaluates js, returns None if an exception was thrown.'''
19
20        try:
21            return self._tab.EvaluateJavaScript(js)
22        except exceptions.EvaluateException:
23            return None
24
25    def _CheckSandboxPage(self, url, js):
26        self._tab.Navigate(url)
27
28        return utils.poll_for_condition(
29                lambda: self._EvaluateJavaScript(js),
30                exception=error.TestError('Failed to evaluate in %s "%s"'
31                                          % (url, js)),
32                timeout=30)
33
34
35    def _CheckAdequatelySandboxed(self):
36        '''Checks that chrome://sandbox shows "You are adequately sandboxed."'''
37        url = 'chrome://sandbox'
38        res = self._CheckSandboxPage(url,
39                "document.getElementsByTagName('p')[0].textContent")
40
41        text = 'You are adequately sandboxed.'
42        if not re.match(text, res):
43            raise error.TestFail('Could not find "%s" in %s' % (text, url))
44
45
46    def _CheckGPUSandboxed(self):
47        '''
48        Checks that chrome://gpu has "Sandboxed" row, and "Sandboxed" is True.
49        '''
50        url = 'chrome://gpu'
51        res = self._CheckSandboxPage(url,
52                                     "browserBridge.isSandboxedForTesting();")
53        if res is not True:
54            raise error.TestFail('"Sandboxed" not True in %s' % url)
55
56    def run_once(self):
57        '''Open various sandbox-related pages and test that we are sandboxed.'''
58        with chrome.Chrome() as cr:
59            self._tab = cr.browser.tabs[0]
60            self._CheckAdequatelySandboxed()
61            self._CheckGPUSandboxed()
62