File: captured_sites_commands.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (578 lines) | stat: -rw-r--r-- 21,177 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Runs captured sites framework recording and tests.

  $ tools/captured_sites/control.py [command] [arguments]
Commands:
  build   Builds a captured_sites_interactive_tests target
  chrome  Starts a Chrome instance with autofill hooks
  wpr     Starts a WPR server instance to record or replay
  run     Starts a test for a single site or "*" for all sites
  refresh Starts a test for a single site or "*" for all sites, and records new
          server prediction responses.

Use "[command] -h" for more information about each command.',

This script attempts to simplify the various configuration and override options
that are available in creating and executing the Captured Sites Framework for
Autofill and Password Manager.

This script assumes execution location is the src folder of the chromium
checkout. Commands should be run from chromium/src directory.

Also assumes that built targets are in :
  out/Default for is_debug = true
  out/Release for is_debug = false

Some environment variables should be set in order to use this script to its
full potential.

  CAPTURED_SITES_USER_DATA_DIR - a location to store local information about the
  chromium profile. This allows the tester to pull back the address and credit
  card profile information without restarting it each time.
  CAPTURED_SITES_LOG_DATA_DIR - a location to store log data for easier parsing
  after a test run has been completed.

Common tasks:
Recording a new test for site 'youtube' (requires two terminal windows):
  Window 1$: tools/captured_sites/control.py wpr record youtube
  Window 2$: tools/captured_sites/control.py chrome -w -r

Checking a recorded test for site 'youtube' (requires two terminal windows):
  Window 1$ tools/captured_sites/control.py wpr replay youtube
  Window 2$ tools/captured_sites/control.py chrome -w -r -u youtube

Running all 'sign_in_pass' tests and saving the logs:
  $ tools/captured_sites/control.py run -s sign_in_pass *

Running disabled autofill test 'rei':
  $ tools/captured_sites/control.py run -d rei

Running autofill test 'rei' with ability to pause at each step:
  $ tools/captured_sites/control.py run -q path/to/pipe rei

"""

import argparse
from collections import namedtuple
import json
import os
import subprocess
import sys

# Checking for environment variables.
_HOME_DIR = os.environ['HOME']
_DEFAULT_USER_DATA_DIR = os.path.join(_HOME_DIR, 'data/userdir')
_DEFAULT_LOG_DATA_DIR = os.path.join(_HOME_DIR, 'data/local_test_results')

# Long text chunks that will be used in command constructions.
_EXTRA_BROWSER_AUTOFILL = ('autofill_download_manager=1,form_cache=1,'
                           'autofill_agent=1,autofill_handler=1,'
                           'form_structure=1,cache_replayer=2')
_WPR_INJECT_SCRIPTS = ('--inject_scripts=third_party/catapult/web_page_replay_g'
                       'o/deterministic.js,chrome/test/data/web_page_replay_go_'
                       'helper_scripts/automation_helper.js')
_NORMAL_BROWSER_AUTOFILL = 'cache_replayer=1'
_RUN_BACKGROUND = 'testing/xvfb.py'
_RUN_DISABLED_TESTS = '--gtest_also_run_disabled_tests'
_RUN_DEBUGGING_TESTS = '--gtest_break_on_failure'

_AUTOFILL_ARTIFACTS_PATH = 'chrome/test/data/autofill/captured_sites/artifacts'
_AUTOFILL_TEST = '*/AutofillCapturedSitesInteractiveTest'
_AUTOFILL_REFRESH = '*/AutofillCapturedSitesRefresh'
_PASSWORD_ARTIFACTS_PATH = 'chrome/test/data/password/captured_sites/artifacts'
_PASSWORD_MANAGER_TEST = '*/CapturedSitesPasswordManagerBrowserTest'
_PASSWORD_MANAGER_REFRESH = '*/CapturedSitesPasswordManagerRefresh'
_VMODULE_AUTOFILL_FILE = 'autofill_captured_sites_interactive_uitest'
_VMODULE_PASSWORD_FILE = 'password_manager_captured_sites_interactive_uitest'

_WPR_SUPPORT_FILES_PATH = ('components/test/data/autofill/'
                           'web_page_replay_support_files')

_STABLE_GOOGLE_CHROME = '/usr/bin/google-chrome'
_RELEASE_BUILD_CHROME = 'out/Release/chrome'

_HOOK_CHROME_TO_WPR = ('--host-resolver-rules="MAP *:80 127.0.0.1:8080,'
                       'MAP *:443 127.0.0.1:8081,EXCLUDE localhost"')

_AUTOFILL_CACHE_TYPE_LOOKUP = {
    'SavedCache': 'SavedCache',
    'ProductionServer': 'ProductionServer',
    'OnlyLocalHeuristics': 'OnlyLocalHeuristics',
    'c': 'SavedCache',
    'p': 'ProductionServer',
    'n': 'OnlyLocalHeuristics'
}

WprInfo = namedtuple('WprInfo', ['cert', 'key', 'hash'])
_WPR_CERT_LOOKUP = {
    'ecdsa':
    WprInfo('ecdsa_cert.pem', 'ecdsa_key.pem',
            '2HcXCSKKJS0lEXLQEWhpHUfGuojiU0tiT5gOF9LP6IQ='),
    'rsa':
    WprInfo('wpr_cert.pem', 'wpr_key.pem',
            'PoNnQAwghMiLUPg1YNFtvTfGreNT8r9oeLEyzgNCJWc='),
}


def available_commands():
  commands = {
      'build': BuildCommand,
      'chrome': ChromeCommand,
      'refresh': RefreshCommand,
      'run': RunCommand,
      'wpr': WprCommand,
  }
  return commands


def parse_command():
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawTextHelpFormatter)
  parser.usage = __doc__
  parser.add_argument('name', choices=available_commands().keys())
  parser.add_argument('args', nargs=argparse.REMAINDER)
  name_args = parser.parse_args()
  return name_args.name, name_args.args


def initiate_command(command_name, local_environ=os.environ.copy()):
  class_object = available_commands()[command_name]
  return class_object(local_environ)


def initiate_and_build_command():
  command_name, command_args = parse_command()
  instance = initiate_command(command_name)
  instance.build(command_args)
  return instance


class Command():

  def __init__(self, description, local_environ):
    self.description = description
    self.setup_environ(local_environ)

  def setup_environ(self, local_environ):
    self.local_environ = local_environ

    if 'CAPTURED_SITES_USER_DATA_DIR' in self.local_environ:
      self.user_data_dir_path = self.local_environ[
          'CAPTURED_SITES_USER_DATA_DIR']
    else:
      self.user_data_dir_path = _DEFAULT_USER_DATA_DIR

    if 'CAPTURED_SITES_LOG_DATA_DIR' in self.local_environ:
      self.log_data_dir_path = self.local_environ['CAPTURED_SITES_LOG_DATA_DIR']
    else:
      self.log_data_dir_path = _DEFAULT_LOG_DATA_DIR

  def build(self, args):
    parser = argparse.ArgumentParser(description=self.description)
    self.add_args(parser)
    parse_result = parser.parse_known_args(args)

    self.options = parse_result[0]
    self.command_args = []
    self.additional_args = parse_result[1]

  def print(self):
    command_text = ' '.join(self.command_args + self.additional_args)
    print(command_text)
    return command_text

  def launch(self):
    self.print()
    if self.options.print_only:
      return
    self.make_process_call(self.command_args + self.additional_args)

  def insert_cert_info(self, cert_type):
    cert_paths = []
    key_paths = []
    hashes = []
    for cert_name, cert_info in _WPR_CERT_LOOKUP.items():
      if cert_type == 'all' or cert_type == cert_name:
        cert_paths.append(f'{_WPR_SUPPORT_FILES_PATH}/{cert_info.cert}')
        key_paths.append(f'{_WPR_SUPPORT_FILES_PATH}/{cert_info.key}')
        hashes.append(cert_info.hash)
    cert_arg = '--https_cert_file=' + ','.join(cert_paths)
    self.command_args.append(cert_arg)
    key_arg = '--https_key_file=' + ','.join(key_paths)
    self.command_args.append(key_arg)

  def make_process_call(self, args):
    command_text = ' '.join(args)

    if not os.path.exists(args[0]):
      raise EnvironmentError('Cannot locate binary to execute. '
                             'Ensure that working directory is chromium/src')
    subprocess.call(command_text, shell=True)

  def add_args(self, parser):
    parser.add_argument(
        '-p',
        '--print-only',
        dest='print_only',
        action='store_true',
        help='Build the command and print it but do not execute.')

  def add_cert_args(self, parser):
    parser.add_argument(
        '-c',
        '--cert-type',
        dest='cert_type',
        action='store',
        default='all',
        choices=['ecdsa', 'rsa', 'all'],
        help='Define tls certificate type for the session. Defaults to `all`.')

  def add_scenario_site_args(self, parser):
    parser.add_argument(
        'scenario_dir',
        nargs='?',
        default='',
        choices=[
            'sign_in_pass', 'sign_up_pass', 'sign_up_fill',
            'capture_update_pass', '*', ''
        ],
        help=('Only for password tests to designate the specific '
              'test scenario. Use * to indicate all password test'
              ' scenarios.'))
    parser.add_argument(
        'site_name',
        help=('The site name which should have a match in '
              'testcases.json. Use * to indicate all enumerated '
              'sites in that file.'))

  def retrieve_cert_info(self, cert_type):
    cert_paths = []
    key_paths = []
    hashes = []
    for cert_name, cert_info in _WPR_CERT_LOOKUP.items():
      if cert_type == 'all' or cert_type == cert_name:
        cert_paths.append(f'{_WPR_SUPPORT_FILES_PATH}/{cert_info.cert}')
        key_paths.append(f'{_WPR_SUPPORT_FILES_PATH}/{cert_info.key}')
        hashes.append(cert_info.hash)
    cert_arg = '--https_cert_file=' + ','.join(cert_paths)
    key_arg = '--https_key_file=' + ','.join(key_paths)
    ignore_cert_list_arg = '--ignore-certificate-errors-spki-list=' + ','.join(
        hashes)
    return cert_arg, key_arg, ignore_cert_list_arg


class BuildCommand(Command):

  def __init__(self, local_environ):
    super().__init__('Build the test target.', local_environ)

  def build(self, args):
    super().build(args)
    self.command_args = [
        'autoninja', '-C', 'out/' + self.options.binary_folder,
        'captured_sites_interactive_tests'
    ]

  def make_process_call(self, args):
    command_text = ' '.join(args)
    subprocess.call(command_text, shell=True)

  def add_args(self, parser):
    super().add_args(parser)
    parser.add_argument(
        '-r',
        '--release',
        dest='binary_folder',
        default='Default',
        const='Release',
        help=
        'Start a build of captured_sites_interactive_tests in Release folder.',
        action='store_const')


class ChromeCommand(Command):

  def __init__(self, local_environ):
    super().__init__('Start a Chrome instance with autofill hooks.',
                     local_environ)

  def build(self, args):
    super().build(args)

    _, _, ignore_cert_list_arg = self.retrieve_cert_info(self.options.cert_type)

    self.command_args = [self.options.build_target, ignore_cert_list_arg]

    if not os.path.isdir(self.user_data_dir_path):
      print('Required CAPTURED_SITES_USER_DATA_DIR "%s" cannot be found' %
            self.user_data_dir_path)
      raise ValueError(
          'Must set environment variable $CAPTURED_SITES_USER_DATA_D'
          'IR or ensure default _DEFAULT_USER_DATA_DIR exists')
    else:
      self.command_args.append('--user-data-dir="%s"' % self.user_data_dir_path)

    self.command_args.extend([
        '--disable-application-cache', '--show-autofill-signatures',
        '--enable-features=AutofillShowTypePredictions',
        '--disable-features=AutofillCacheQueryResponses'
    ])

    if self.options.wpr_selection:
      self.command_args.append(_HOOK_CHROME_TO_WPR)

    if self.options.start_url:
      startURL = self._print_starting_url(self.options.start_url)
      if startURL:
        self.command_args.append(startURL)

  def add_args(self, parser):
    super().add_args(parser)
    parser.add_argument('-r',
                        '--release',
                        dest='build_target',
                        default=_STABLE_GOOGLE_CHROME,
                        const=_RELEASE_BUILD_CHROME,
                        help='Start Release build of chrome.',
                        action='store_const')
    parser.add_argument('-w',
                        '--wpr',
                        dest='wpr_selection',
                        action='store_true',
                        help='Point chrome instance at wpr service.')
    parser.add_argument('-u',
                        '--url',
                        dest='start_url',
                        action='store',
                        help='Grab starting URL from test recipe.')
    self.add_cert_args(parser)

  def _print_starting_url(self, url):
    base_path = _AUTOFILL_ARTIFACTS_PATH
    if '-' in url:
      base_path = _PASSWORD_ARTIFACTS_PATH
      url = url.replace('-', '/')
    archive_path = f'{base_path}/{url}.test'

    if not os.path.exists(archive_path):
      print('No file found for "%s"' % url, file=sys.stderr)
      return
    with open(archive_path, 'r') as read_file:
      data = json.load(read_file)
    if not 'startingURL' in data:
      print('No startingURL found in file for "%s"' % url, file=sys.stderr)
      return
    print('%s test starts at:' % url, file=sys.stderr)
    startURL = data['startingURL']
    print(startURL)
    print('')
    return startURL


class TestCommand(Command):

  def __init__(self, description, gtest_filter_autofill, gtest_filter_password,
               local_environ):
    super().__init__(description, local_environ)
    self.gtest_filter_autofill = gtest_filter_autofill
    self.gtest_filter_password = gtest_filter_password

  def extract_parameter(self):
    if self.options.scenario_dir != '':
      self.gtest_parameter = '%s_%s' % (self.options.scenario_dir,
                                        self.options.site_name)
      self.vmodule_name = _VMODULE_PASSWORD_FILE
      self.gtest_filter = self.gtest_filter_password
    else:
      self.gtest_parameter = self.options.site_name
      self.vmodule_name = _VMODULE_AUTOFILL_FILE
      self.gtest_filter = self.gtest_filter_autofill

  def build(self, args):
    super().build(args)

    self.extract_parameter()

    self.command_args = [
        'out/%s/captured_sites_interactive_tests' % self.options.target,
        '--gtest_filter="%s.Recipe/%s"' %
        (self.gtest_filter, self.gtest_parameter),
        '--enable-pixel-output-in-tests',
    ]

    if self.options.use_bot_timeout:
      self.command_args.extend([
          '--ui-test-action-max-timeout=180000',
          '--test-launcher-timeout=180000'
      ])
    else:
      self.command_args.append('--test-launcher-interactive')

    self.command_args.append('--vmodule=captured_sites_test_utils=2,%s,%s=1' %
                             (self.options.verbose_logging, self.vmodule_name))

    if self.options.background:
      self.command_args.insert(0, _RUN_BACKGROUND)

    if self.options.add_disabled:
      self.command_args.append(_RUN_DISABLED_TESTS)

    if self.options.add_break_on_failure:
      self.command_args.append(_RUN_DEBUGGING_TESTS)

    if self.options.wpr_verbose:
      self.command_args.append('--wpr_verbose')

    if self.options.retry_count > 0:
      self.command_args.append('--test-launcher-retry-limit=%d' %
                               self.options.retry_count)

    if self.options.autofill_cache_type:
      full_cache_type = _AUTOFILL_CACHE_TYPE_LOOKUP[
          self.options.autofill_cache_type]
      self.command_args.append('--autofill-server-type=%s ' % full_cache_type)

    if self.options.command_file:
      self.command_args.append('--command_file=%s' %
                               os.path.expanduser(self.options.command_file))

    if self.options.store_log:
      if not os.path.isdir(self.log_data_dir_path):
        print('Required LOG_DATA_DIR "%s" cannot be found' %
              self.log_data_dir_path)
        raise ValueError('Must set environment variable $LOG_DATA_DIR or '
                         'ensure default _DEFAULT_LOG_DATA_DIR exists')
      logging_scenario_site_param = self.gtest_parameter.replace('*', 'all')
      self.command_args.append(
          '--test-launcher-summary-output={}/{}_output.json'.format(
              self.log_data_dir_path, logging_scenario_site_param))
      self.command_args.extend(self.additional_args)
      self.additional_args = []
      self.command_args.append('2>&1 | tee {}/{}_capture.log'.format(
          self.log_data_dir_path, logging_scenario_site_param))

  def add_args(self, parser):
    super().add_args(parser)
    parser.add_argument('-r',
                        '--release',
                        dest='target',
                        action='store_const',
                        default='Default',
                        const='Release',
                        help='Run tests on Release build of chrome.')
    parser.add_argument(
        '-s',
        '--store-log',
        dest='store_log',
        action='store_true',
        help=f'Store the log and output in {self.log_data_dir_path}.')
    parser.add_argument('-b',
                        '--background',
                        dest='background',
                        action='store_true',
                        help='Run the test in background with xvfb.py.')
    parser.add_argument('-d',
                        '--disabled',
                        dest='add_disabled',
                        action='store_true',
                        help='Also run disabled tests that match the filter.')
    parser.add_argument(
        '-u',
        '--use_bot_timeout',
        dest='use_bot_timeout',
        action='store_true',
        help=('Use the same timeout settings as exists on the bot (3 minutes) '
              'instead of the `test-launcher-interactive` setting. This is '
              'particularly useful when bisecting.'))
    parser.add_argument('-f',
                        '--break_on_failure',
                        dest='add_break_on_failure',
                        action='store_true',
                        help=('Run tests in single-process mode and brings the '
                              'debugger on an assertion failure.'))
    parser.add_argument('-v',
                        '--verbose',
                        dest='verbose_logging',
                        action='store_const',
                        default=_NORMAL_BROWSER_AUTOFILL,
                        const=_EXTRA_BROWSER_AUTOFILL,
                        help='Log verbose Autofill Server information.')
    parser.add_argument('-t',
                        '--test-retry',
                        dest='retry_count',
                        action='store',
                        default=0,
                        type=int,
                        help='How many times to retry failed tests.')
    parser.add_argument('-a',
                        '--autofill-cache-type',
                        dest='autofill_cache_type',
                        choices=_AUTOFILL_CACHE_TYPE_LOOKUP.keys(),
                        action='store',
                        help='Control the autofill cache behavior.')
    parser.add_argument('-q',
                        '--command_file',
                        dest='command_file',
                        action='store',
                        default='',
                        type=str,
                        help='Location of "pipe: file')
    parser.add_argument('-w',
                        '--wpr_verbose',
                        dest='wpr_verbose',
                        action='store_true',
                        help='Also include verbose WPR output.')
    self.add_scenario_site_args(parser)


class RefreshCommand(TestCommand):

  def __init__(self, local_environ):
    super().__init__(
        'Refresh the Server Predictions of an autofill or password test.',
        _AUTOFILL_REFRESH, _PASSWORD_MANAGER_REFRESH, local_environ)


class RunCommand(TestCommand):

  def __init__(self, local_environ):
    super().__init__('Start an autofill or password test run.', _AUTOFILL_TEST,
                     _PASSWORD_MANAGER_TEST, local_environ)


class WprCommand(Command):

  def __init__(self, local_environ):
    super().__init__('Start WPR to replay or record.', local_environ)

  def build(self, args):
    super().build(args)
    self.command_args = [
        'third_party/catapult/telemetry/telemetry/bin/linux/x86_64/wpr',
        self.options.subhead, '--http_port=8080', '--https_port=8081'
    ]

    self.command_args.append(_WPR_INJECT_SCRIPTS)

    if self.options.subhead == 'replay':
      self.command_args.append('--serve_response_in_chronological_sequence')

    self.insert_cert_info(self.options.cert_type)

    if self.options.scenario_dir == '':
      wpr_path = f'{_AUTOFILL_ARTIFACTS_PATH}/{self.options.site_name}.wpr'
    else:
      wpr_path = f'{_PASSWORD_ARTIFACTS_PATH}/'\
               + f'{self.options.scenario_dir}/{self.options.site_name}.wpr'
    self.command_args.append(wpr_path)

  def add_args(self, parser):
    parser.add_argument('subhead',
                        choices=['record', 'replay'],
                        help=('Whether to record new traffic to an archive, '
                              'or replay from an existing archive.'))
    self.add_cert_args(parser)
    self.add_scenario_site_args(parser)
    super().add_args(parser)