Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 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 | |
| 5 """ Script that exercises the Smart Lock setup flow, testing that a nearby phone | |
| 6 can be found and used to unlock a Chromebook. | |
| 7 | |
| 8 Note: This script does not currently automate Android phones, so make sure that | |
| 9 a phone is properly configured and online before starting the test. | |
| 10 | |
| 11 Usage: | |
| 12 python setup_test.py --remote_address REMOTE_ADDRESS | |
| 13 --username USERNAME | |
| 14 --password PASSWORD | |
| 15 [--app_path APP_PATH] | |
| 16 | |
| 17 If |--app_path| is provided, then a copy of the Smart Lock app on the local | |
| 18 machine will be used instead of the app on the ChromeOS device. | |
| 19 """ | |
| 20 | |
| 21 import argparse | |
| 22 import cros | |
| 23 import cryptauth | |
| 24 import logging | |
| 25 import os | |
| 26 import subprocess | |
| 27 import sys | |
| 28 import tempfile | |
| 29 | |
| 30 logger = logging.getLogger('proximity_auth.%s' % __name__) | |
| 31 | |
| 32 class SmartLockSetupError(Exception): | |
| 33 pass | |
| 34 | |
| 35 def pingable_address(address): | |
| 36 try: | |
| 37 subprocess.check_output(['ping', '-c', '1', '-W', '1', address]) | |
| 38 except subprocess.CalledProcessError: | |
| 39 raise argparse.ArgumentError('%s cannot be reached.' % address) | |
| 40 return address | |
| 41 | |
| 42 def email(arg): | |
| 43 tokens = arg.lower().split('@') | |
| 44 if len(tokens) != 2 or '.' not in tokens[1]: | |
| 45 raise argparse.ArgumentError('%s is not a valid email address' % arg) | |
| 46 name, domain = tokens | |
| 47 if domain == 'gmail.com': | |
| 48 name = name.replace('.', '') | |
| 49 return '@'.join([name, domain]) | |
| 50 | |
| 51 def directory(path): | |
| 52 if not os.path.isdir(path): | |
| 53 raise argparse.ArgumentError('%s is not a directory' % path) | |
| 54 return path | |
| 55 | |
| 56 def ParseArgs(): | |
| 57 parser = argparse.ArgumentParser(prog='python setup_test.py') | |
| 58 parser.add_argument('--remote_address', required=True, type=pingable_address) | |
| 59 parser.add_argument('--username', required=True, type=email) | |
| 60 parser.add_argument('--password', required=True) | |
| 61 parser.add_argument('--app_path', type=directory) | |
| 62 args = parser.parse_args() | |
| 63 return args | |
| 64 | |
| 65 def CheckCryptAuthState(access_token): | |
| 66 cryptauth_client = cryptauth.CryptAuthClient(access_token) | |
| 67 | |
| 68 # Check if we can make CryptAuth requests. | |
| 69 if cryptauth_client.GetMyDevices() is None: | |
| 70 logger.error('Cannot reach CryptAuth on test machine.') | |
| 71 return False | |
| 72 | |
| 73 if cryptauth_client.GetUnlockKey() is not None: | |
| 74 logger.info('Smart Lock currently enabled, turning off on Cryptauth...') | |
| 75 if not cryptauth_client.ToggleEasyUnlock(False): | |
| 76 logger.error('ToggleEasyUnlock request failed.') | |
| 77 return False | |
| 78 | |
| 79 result = cryptauth_client.FindEligibleUnlockDevices() | |
| 80 if result is None: | |
| 81 logger.error('FindEligibleUnlockDevices request failed') | |
| 82 return False | |
| 83 eligibleDevices, _ = result | |
| 84 if len(eligibleDevices) == 0: | |
| 85 logger.warn('No eligible phones found, trying to ping phones...') | |
| 86 result = cryptauth_client.PingPhones() | |
| 87 if result is None or not len(result[0]): | |
| 88 logger.error('Pinging phones failed :(') | |
| 89 return False | |
| 90 else: | |
| 91 logger.inro('Pinging phones succeeded!') | |
|
Ilya Sherman
2015/03/24 01:25:14
nit: logger.info. This is why I prefer compiled l
Tim Song
2015/03/25 23:08:44
Done. I agree!
| |
| 92 else: | |
| 93 logger.info('Found eligible device: %s' % \ | |
| 94 eligibleDevices[0]['friendlyDeviceName']) | |
| 95 return True | |
| 96 | |
| 97 def _NavigateSetupDialog(chromeos, app): | |
| 98 logger.info('Scanning for nearby phones...') | |
| 99 btmon = chromeos.RunBtmon() | |
| 100 find_phone_success = app.FindPhone() | |
| 101 btmon.terminate() | |
| 102 | |
| 103 if not find_phone_success: | |
| 104 fd, filepath = tempfile.mkstemp(prefix='btmon-') | |
| 105 os.write(fd, btmon.stdout.read()) | |
| 106 os.close(fd) | |
| 107 logger.info('Logs for btmon can be found at %s' % filepath) | |
| 108 raise SmartLockSetupError("Failed to find nearby phone.") | |
| 109 | |
| 110 logger.info('Phone found! Starting pairing...') | |
| 111 if not app.PairPhone(): | |
| 112 raise SmartLockSetupError("Failed to pair with phone.") | |
| 113 logger.info('Pairing success! Starting trial run...') | |
| 114 app.StartTrialRun() | |
| 115 | |
| 116 logger.info('Unlocking for trial run...') | |
| 117 lock_screen = chromeos.GetAccountPickerScreen() | |
| 118 lock_screen.WaitForSmartLockState( | |
| 119 lock_screen.SmartLockState.AUTHENTICATED) | |
| 120 lock_screen.UnlockWithClick() | |
| 121 | |
| 122 logger.info('Trial run success! Dismissing app...') | |
| 123 app.DismissApp() | |
| 124 | |
| 125 def RunSetupTest(args): | |
| 126 logger.info('Starting test for %s at %s' % \ | |
| 127 (args.username, args.remote_address)) | |
| 128 if args.app_path is not None: | |
| 129 logger.info('Replacing Smart Lock app with %s' % args.app_path) | |
| 130 | |
| 131 chromeos = cros.ChromeOS(args.remote_address, args.username, args.password) | |
| 132 with chromeos.Start(local_app_path=args.app_path): | |
| 133 logger.info('Chrome initialized') | |
| 134 | |
| 135 # TODO(tengs): The access token is currently fetched from the Smart Lock | |
| 136 # app's background page. To be more robust, we should instead mint the | |
| 137 # access token ourselves. | |
| 138 if not CheckCryptAuthState(chromeos.cryptauth_access_token): | |
| 139 raise SmartLockSetupError('Failed to check CryptAuth state') | |
| 140 | |
| 141 logger.info('Opening Smart Lock settings...') | |
| 142 settings = chromeos.GetSmartLockSettings() | |
| 143 assert(not settings.smart_lock_enabled) | |
| 144 logger.info('Starting Smart Lock setup flow...') | |
| 145 app = settings.StartSetupAndReturnApp() | |
| 146 | |
| 147 _NavigateSetupDialog(chromeos, app) | |
| 148 | |
| 149 def main(): | |
| 150 logging.basicConfig() | |
| 151 logging.getLogger('proximity_auth').setLevel(logging.INFO) | |
| 152 args = ParseArgs() | |
| 153 RunSetupTest(args) | |
| 154 | |
| 155 if __name__ == '__main__': | |
| 156 main() | |
| OLD | NEW |