David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 1 | # Copyright 2018 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 | import json |
| 6 | import logging |
| 7 | import time |
| 8 | |
| 9 | from autotest_lib.client.bin import test |
| 10 | from autotest_lib.client.bin import utils |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib.cros import chrome |
sdantuluri | 4974977 | 2019-02-01 16:52:49 -0800 | [diff] [blame] | 13 | from autotest_lib.client.cros import cryptohome |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 14 | from autotest_lib.client.cros.input_playback import input_playback |
| 15 | |
| 16 | |
| 17 | class desktopui_CheckRlzPingSent(test.test): |
| 18 | """Tests creating a new user, doing a google search, checking RLZ Data.""" |
| 19 | version = 1 |
| 20 | |
| 21 | _RLZ_DATA_FILE = "/home/chronos/user/RLZ Data" |
| 22 | |
| 23 | def _verify_rlz_data(self): |
| 24 | """Checks that the CAF event is in the RLZ Data file.""" |
| 25 | def rlz_data_exists(): |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 26 | """Check rlz data exists.""" |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 27 | rlz_data = json.loads(utils.run('cat "%s"' % |
| 28 | self._RLZ_DATA_FILE).stdout) |
| 29 | logging.debug('rlz data: %s', rlz_data) |
| 30 | if 'stateful_events' in rlz_data: |
| 31 | return 'CAF' in rlz_data['stateful_events']['C']['_'] |
| 32 | return False |
| 33 | |
| 34 | utils.poll_for_condition(rlz_data_exists, timeout=120) |
| 35 | |
| 36 | |
| 37 | def _check_url_for_rlz(self, cr): |
| 38 | """ |
| 39 | Does a Google search and ensures there is an rlz parameter. |
| 40 | |
| 41 | @param cr: Chrome instance. |
| 42 | |
| 43 | """ |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 44 | timeout_minutes = 2 |
| 45 | timeout = time.time() + 60 * timeout_minutes |
| 46 | |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 47 | # Setup a keyboard emulator to open new tabs and type a search. |
Daniel Erat | a87122c | 2018-05-19 07:43:59 -0700 | [diff] [blame] | 48 | with input_playback.InputPlayback() as player: |
| 49 | player.emulate(input_type='keyboard') |
| 50 | player.find_connected_inputs() |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 51 | |
Daniel Erat | a87122c | 2018-05-19 07:43:59 -0700 | [diff] [blame] | 52 | while True: |
| 53 | # Open a new tab, search in the omnibox. |
| 54 | player.blocking_playback_of_default_file( |
| 55 | input_type='keyboard', filename='keyboard_ctrl+t') |
| 56 | player.blocking_playback_of_default_file( |
| 57 | input_type='keyboard', filename='keyboard_b+a+d+enter') |
| 58 | logging.info(cr.browser.tabs[-1].url) |
| 59 | if 'rlz=' in cr.browser.tabs[-1].url: |
| 60 | break |
| 61 | else: |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 62 | if time.time() > timeout: |
| 63 | raise error.TestFail('RLZ ping did not send in %d ' |
David Haddock | e2eb20f | 2018-12-11 08:28:39 +0000 | [diff] [blame] | 64 | 'minutes.' % timeout_minutes) |
Daniel Erat | a87122c | 2018-05-19 07:43:59 -0700 | [diff] [blame] | 65 | time.sleep(10) |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 66 | |
| 67 | |
sdantuluri | 4974977 | 2019-02-01 16:52:49 -0800 | [diff] [blame] | 68 | def _wait_for_rlz_lock(self): |
| 69 | """Waits for the DUT to get into locked state after login.""" |
| 70 | def get_install_lockbox_finalized_status(): |
| 71 | status = cryptohome.get_tpm_more_status() |
| 72 | return status.get('install_lockbox_finalized') |
| 73 | |
sdantuluri | 3029e9d | 2019-02-20 14:47:38 -0800 | [diff] [blame] | 74 | try: |
| 75 | utils.poll_for_condition( |
| 76 | lambda: get_install_lockbox_finalized_status(), |
| 77 | exception=utils.TimeoutError(), |
| 78 | timeout=120) |
| 79 | except utils.TimeoutError: |
| 80 | raise error.TestFail('Timed out trying to lock the device') |
sdantuluri | 4974977 | 2019-02-01 16:52:49 -0800 | [diff] [blame] | 81 | |
| 82 | |
Kyle Shimabukuro | b3014bc | 2020-01-24 13:26:40 -0800 | [diff] [blame^] | 83 | def run_once(self, ping_timeout=30, logged_in=True): |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 84 | """ |
| 85 | Main entry to the test. |
| 86 | |
Kyle Shimabukuro | b3014bc | 2020-01-24 13:26:40 -0800 | [diff] [blame^] | 87 | @param logged_in: True for real login or False for guest mode. |
| 88 | @param ping_timeout: Delay time (seconds) before rlz ping is sent. |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 89 | |
| 90 | """ |
Kyle Shimabukuro | b3014bc | 2020-01-24 13:26:40 -0800 | [diff] [blame^] | 91 | # Browser arg to make DUT send rlz ping after a short delay. |
| 92 | rlz_flag = '--rlz-ping-delay=%d' % ping_timeout |
| 93 | |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 94 | # If we are testing the ping is sent in guest mode (logged_in=False), |
| 95 | # we need to first do a real login and wait for the DUT to become |
| 96 | # 'locked' for rlz. Then logout and enter guest mode. |
| 97 | if not logged_in: |
Kyle Shimabukuro | b3014bc | 2020-01-24 13:26:40 -0800 | [diff] [blame^] | 98 | with chrome.Chrome(logged_in=True, extra_browser_args=rlz_flag): |
sdantuluri | 4974977 | 2019-02-01 16:52:49 -0800 | [diff] [blame] | 99 | self._wait_for_rlz_lock() |
David Haddock | 7f26288 | 2018-11-08 11:53:52 -0800 | [diff] [blame] | 100 | |
Kyle Shimabukuro | b3014bc | 2020-01-24 13:26:40 -0800 | [diff] [blame^] | 101 | with chrome.Chrome(logged_in=logged_in, |
| 102 | extra_browser_args=rlz_flag) as cr: |
David Haddock | dd35fb5e | 2018-05-03 14:52:33 -0700 | [diff] [blame] | 103 | self._check_url_for_rlz(cr) |
| 104 | self._verify_rlz_data() |