Home | History | Annotate | Download | only in chromeos
      1 # Copyright (c) 2012 The Chromium 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 import json
      5 import os
      6 import unittest
      7 
      8 from telemetry.core import browser_finder
      9 from telemetry.core import exceptions
     10 from telemetry.core import extension_to_load
     11 from telemetry.core import util
     12 from telemetry.core.chrome import cros_interface
     13 from telemetry.core.chrome import cros_util
     14 from telemetry.test import options_for_unittests
     15 
     16 class CrOSAutoTest(unittest.TestCase):
     17   def setUp(self):
     18     options = options_for_unittests.GetCopy()
     19     self._cri = cros_interface.CrOSInterface(options.cros_remote,
     20                                              options.cros_ssh_identity)
     21     self._is_guest = options.browser_type == 'cros-chrome-guest'
     22     self._email = '' if self._is_guest else 'test (at] test.test'
     23 
     24   def _IsCryptohomeMounted(self):
     25     """Returns True if cryptohome is mounted"""
     26     cryptohomeJSON, _ = self._cri.RunCmdOnDevice(['/usr/sbin/cryptohome',
     27                                                  '--action=status'])
     28     cryptohomeStatus = json.loads(cryptohomeJSON)
     29     return (cryptohomeStatus['mounts'] and
     30             cryptohomeStatus['mounts'][0]['mounted'])
     31 
     32   def _CreateBrowser(self, with_autotest_ext):
     33     """Finds and creates a browser for tests. if with_autotest_ext is True,
     34     also loads the autotest extension"""
     35     options = options_for_unittests.GetCopy()
     36 
     37     if with_autotest_ext:
     38       extension_path = os.path.join(os.path.dirname(__file__), 'autotest_ext')
     39       self._load_extension = extension_to_load.ExtensionToLoad(extension_path,
     40                                                                True)
     41       options.extensions_to_load = [self._load_extension]
     42 
     43     browser_to_create = browser_finder.FindBrowser(options)
     44     self.assertTrue(browser_to_create)
     45     return browser_to_create.Create()
     46 
     47   def _GetAutotestExtension(self, browser):
     48     """Returns the autotest extension instance"""
     49     extension = browser.extensions[self._load_extension]
     50     self.assertTrue(extension)
     51     return extension
     52 
     53   def testCryptohomeMounted(self):
     54     """Verifies cryptohome mount status for regular and guest user and when
     55     logged out"""
     56     with self._CreateBrowser(False) as b:
     57       self.assertEquals(1, len(b.tabs))
     58       self.assertTrue(b.tabs[0].url)
     59       self.assertTrue(self._IsCryptohomeMounted())
     60 
     61       chronos_fs = self._cri.FilesystemMountedAt('/home/chronos/user')
     62       self.assertTrue(chronos_fs)
     63       if self._is_guest:
     64         self.assertEquals(chronos_fs, 'guestfs')
     65       else:
     66         home, _ = self._cri.RunCmdOnDevice(['/usr/sbin/cryptohome-path',
     67                                             'user', self._email])
     68         self.assertEquals(self._cri.FilesystemMountedAt(home.rstrip()),
     69                           chronos_fs)
     70 
     71     self.assertFalse(self._IsCryptohomeMounted())
     72     self.assertEquals(self._cri.FilesystemMountedAt('/home/chronos/user'),
     73                       '/dev/mapper/encstateful')
     74 
     75   def testLoginStatus(self):
     76     """Tests autotestPrivate.loginStatus"""
     77     with self._CreateBrowser(True) as b:
     78       extension = self._GetAutotestExtension(b)
     79       extension.ExecuteJavaScript('''
     80         chrome.autotestPrivate.loginStatus(function(s) {
     81           window.__autotest_result = s;
     82         });
     83       ''')
     84       login_status = extension.EvaluateJavaScript('window.__autotest_result')
     85       self.assertEquals(type(login_status), dict)
     86 
     87       self.assertEquals(not self._is_guest, login_status['isRegularUser'])
     88       self.assertEquals(self._is_guest, login_status['isGuest'])
     89       self.assertEquals(login_status['email'], self._email)
     90       self.assertFalse(login_status['isScreenLocked'])
     91 
     92   def testLogout(self):
     93     """Tests autotestPrivate.logout"""
     94     with self._CreateBrowser(True) as b:
     95       extension = self._GetAutotestExtension(b)
     96       try:
     97         extension.ExecuteJavaScript('chrome.autotestPrivate.logout();')
     98       except (exceptions.BrowserConnectionGoneException,
     99               exceptions.BrowserGoneException):
    100         pass
    101       util.WaitFor(lambda: not self._IsCryptohomeMounted(), 20)
    102