xref: /aosp_15_r20/external/autotest/server/site_tests/rlz_CheckPing/rlz_CheckPing.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import datetime
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import tpm_utils
10from autotest_lib.client.common_lib.cros import vpd_utils
11from autotest_lib.server import autotest
12from autotest_lib.server import test
13
14
15class rlz_CheckPing(test.test):
16    """
17    Tests if we are sending RLZ install (CAI) and first-use (CAF) pings in a
18    variety of different circumstances. Refer to the TestTracker RLZ test plan
19    for more information:
20    https://testtracker.googleplex.com/testplans/details/25301
21
22    """
23    version = 1
24
25    _CLIENT_TEST = 'desktopui_CheckRlzPingSent'
26    _PING_VPD = 'should_send_rlz_ping'
27    _DATE_VPD = 'rlz_embargo_end_date'
28
29    def _check_rlz_brand_code(self):
30        """Checks that we have an rlz brand code."""
31        try:
32            self._host.run('cros_config / brand-code')
33        except error.AutoservRunError as e:
34            raise error.TestFail('DUT is missing brand_code: %s.' %
35                                 e.result_obj.stderr)
36
37
38    def _set_vpd_values(self, should_send_rlz_ping, rlz_embargo_end_date):
39        """
40        Sets the required vpd values for the test.
41
42        @param should_send_rlz_ping: Value to set should_send_rlz_ping. 1 to
43                                     send the ping, 0 to not send it, None to
44                                     clear it from the VPD.
45        @param embargo_date: Date for rlz_embargo_end_date value. This should
46                             be a datetime.date object with the desired date
47                             for rlz_embargo_end_date, or None to clear it and
48                             proceed with rlz_embargo_end_date not set.
49
50        """
51        to_set = {}
52        if should_send_rlz_ping is not None:
53            to_set[self._PING_VPD] = should_send_rlz_ping
54        else:
55            vpd_utils.vpd_delete(host=self._host, key=self._PING_VPD,
56                                 dump=True, force_dump=True)
57
58        if rlz_embargo_end_date:
59            to_set[self._DATE_VPD] = rlz_embargo_end_date.isoformat()
60        else:
61            vpd_utils.vpd_delete(host=self._host, key=self._DATE_VPD,
62                                 dump=True, force_dump=True)
63
64        if to_set:
65            vpd_utils.vpd_set(host=self._host, vpd_dict=to_set, dump=True,
66                              force_dump=True)
67
68
69    def _check_rlz_vpd_settings_post_ping(self, should_send_rlz_ping,
70                                          rlz_embargo_end_date):
71        """
72        Checks that rlz related vpd settings are correct after the test.
73        In the typical case where the first-use event (CAF) ping is sent
74        successfully, both should_send_rlz_ping and rlz_embargo_end_date VPD
75        values should be cleared. If the CAF ping is not sent, they will
76        remain unchanged.
77
78        @param should_send_rlz_ping: Expected value for the
79                                     should_send_rlz_ping VPD setting
80                                     (0 or 1). If None, we expect no setting
81                                     to be present.
82        @param rlz_embargo_end_date: Expected value of the
83                                     rlz_embargo_end_date VPD setting.
84                                     This argument should be None if expecting
85                                     the value to be cleared, and a
86                                     datetime.date object containing the
87                                     expected date otherwise.
88
89        """
90        check_should_send_ping = vpd_utils.vpd_get(host=self._host,
91                                                   key=self._PING_VPD)
92        if check_should_send_ping is not None:
93            check_should_send_ping = int(check_should_send_ping)
94
95        check_date = vpd_utils.vpd_get(host=self._host,
96                                       key=self._DATE_VPD)
97
98        if rlz_embargo_end_date:
99            rlz_embargo_end_date = rlz_embargo_end_date.isoformat()
100
101        if check_date != rlz_embargo_end_date:
102            raise error.TestFail('Unexpected value for rlz_embargo_end_date:'
103                                 ' %s' % check_date)
104
105        if check_should_send_ping != should_send_rlz_ping:
106            raise error.TestFail('Unexpected value for should_send_rlz_ping:'
107                                 ' %s' % check_should_send_ping)
108
109
110    def run_once(self, host, ping_timeout=30, pre_login=None,
111                 rlz_embargo_end_date=None, should_send_rlz_ping=1,
112                 check_ping_not_resent=False, reboot=False):
113        """
114        Configures the DUT to send RLZ pings. Checks for the RLZ client
115        install (CAI) and first-use (CAF) pings.
116
117        @param host: Host to run test on.
118        @param ping_timeout: Delay time (seconds) before both CAI and CAF
119                             pings are sent.
120        @param pre_login: Whether or not to login before the main RLZ ping
121                          test, and for how long. Should be one of
122                          ['lock', 'no_lock', None]. 'lock' is meant for guest
123                          mode testing, where a non-guest user must login to
124                          'lock' the device for RLZ before the ping can be
125                          sent in guest mode. 'no_lock' is to log in with a
126                          different user account and log out immediately to
127                          ensure no ping is sent. Used to verify that the ping
128                          can be sent from subsequent user logins if it has
129                          not already been sent.
130        @param rlz_embargo_end_date: Date for rlz_embargo_end_date VPD
131                                     setting. If None, no value will be set
132                                     for rlz_embargo_end_date, and any
133                                     existing value will be cleared. For a
134                                     specific rlz_embargo_end_date, this
135                                     argument should be a datetime.date
136                                     object containing the desired date.
137        @param should_send_rlz_ping: Value of the should_send_rlz_ping VPD
138                                     setting. 1 to send the first-use (CAF)
139                                     ping, 0 to not send it. The ping should
140                                     not be sent when the VPD value is 0,
141                                     which in the normal use-case occurs after
142                                     the CAF ping has been sent. It is set to
143                                     0 after the CAF ping to ensure it is not
144                                     sent again in the device's lifetime.
145                                     This argument can also be None, in which
146                                     case the should_send_rlz_ping VPD setting
147                                     will be cleared. No CAF ping should be
148                                     sent in this case, either.
149        @param check_ping_not_resent: True to perform a second RLZ check with
150                                      a different user account for tests that
151                                      confirm the first-use (CAF) ping is not
152                                      resent. The second check assumes the CAF
153                                      ping was sent successfully, so the
154                                      should_send_rlz_ping and
155                                      rlz_embargo_end_date parameters should
156                                      be a combination that ensures it was
157                                      sent.
158        @param reboot: True to reboot after the first RLZ check.
159
160        """
161        self._host = host
162        self._check_rlz_brand_code()
163
164        # Set VPD settings that control if the DUT will send a first-use ping.
165        self._set_vpd_values(should_send_rlz_ping=should_send_rlz_ping,
166                             rlz_embargo_end_date=rlz_embargo_end_date)
167        # Clear TPM owner so we have no users on DUT.
168        tpm_utils.ClearTPMOwnerRequest(self._host)
169
170        # We expect first-use (CAF) ping to be sent when:
171        # 1. should_send_rlz_ping exists and is 1
172        # 2. rlz_embargo_end_date is missing or in the past
173        expect_caf = bool(should_send_rlz_ping)
174        if rlz_embargo_end_date:
175            expect_caf = (expect_caf and
176                (datetime.date.today() - rlz_embargo_end_date).days > 0)
177
178        # Login, do a Google search, verify events in RLZ Data file.
179        client_at = autotest.Autotest(self._host)
180        client_at.run_test(self._CLIENT_TEST, ping_timeout=ping_timeout,
181                           expect_caf_ping=expect_caf, pre_login=pre_login,
182                           pre_login_username='rlz_user')
183        client_at._check_client_test_result(self._host, self._CLIENT_TEST)
184
185        if expect_caf:
186            self._check_rlz_vpd_settings_post_ping(
187                should_send_rlz_ping=0, rlz_embargo_end_date=None)
188        else:
189            self._check_rlz_vpd_settings_post_ping(
190                should_send_rlz_ping=should_send_rlz_ping,
191                rlz_embargo_end_date=rlz_embargo_end_date)
192
193        if reboot:
194            self._host.reboot()
195
196        # Log in with another user and verify CAF ping is not resent. This
197        # portion of the test assumes a successful run above where expect_caf
198        # was True.
199        if check_ping_not_resent:
200            client_at.run_test(self._CLIENT_TEST, ping_timeout=ping_timeout,
201                               expect_caf_ping=False, username='rlz_user',
202                               tag="check_ping_not_resent")
203
204            # Confirm VPD settings are also unchanged
205            self._check_rlz_vpd_settings_post_ping(should_send_rlz_ping=0,
206                                                   rlz_embargo_end_date=None)
207