File: v8_foozzie_test.py

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (674 lines) | stat: -rwxr-xr-x 25,486 bytes parent folder | download | duplicates (6)
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/usr/bin/env python3
# Copyright 2016 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import random
import re
import subprocess
import sys
import textwrap
import unittest
import unittest.mock

from pathlib import Path

import v8_commands
import v8_foozzie
import v8_fuzz_config
import v8_suppressions

BASE_DIR = Path(__file__).parent.resolve()
FOOZZIE = BASE_DIR / 'v8_foozzie.py'
TEST_DATA = BASE_DIR / 'testdata'

KNOWN_BUILDS = [
  'd8',
  'clang_x86/d8',
  'clang_x86_v8_arm/d8',
  'clang_x64_v8_arm64/d8',
  'clang_x64_fuzzer_experiments/d8',
]


def output(stdout, is_crash):
  exit_code = -1 if is_crash else 0
  return v8_commands.Output(
      exit_code=exit_code, stdout_bytes=stdout.encode('utf-8'), pid=0)


class ConfigTest(unittest.TestCase):
  def testExperiments(self):
    """Test integrity of probabilities and configs."""
    CONFIGS = v8_foozzie.CONFIGS
    EXPERIMENTS = v8_fuzz_config.FOOZZIE_EXPERIMENTS
    FLAGS = v8_fuzz_config.ADDITIONAL_FLAGS
    # Probabilities add up to 100%.
    first_is_int = lambda x: type(x[0]) == int
    assert all(map(first_is_int, EXPERIMENTS))
    assert sum(x[0] for x in EXPERIMENTS) == 100
    # Configs used in experiments are defined.
    assert all(map(lambda x: x[1] in CONFIGS, EXPERIMENTS))
    assert all(map(lambda x: x[2] in CONFIGS, EXPERIMENTS))
    # The last config item points to a known build configuration.
    assert all(map(lambda x: x[3] in KNOWN_BUILDS, EXPERIMENTS))
    # All flags have a probability.
    first_is_float = lambda x: type(x[0]) == float
    assert all(map(first_is_float, FLAGS))
    first_between_0_and_1 = lambda x: x[0] > 0 and x[0] < 1
    assert all(map(first_between_0_and_1, FLAGS))
    # Test consistent flags.
    second_is_string = lambda x: isinstance(x[1], str)
    assert all(map(second_is_string, FLAGS))
    # We allow spaces to separate more flags. We don't allow spaces in the flag
    # value.
    is_flag = lambda x: x.startswith('--')
    all_parts_are_flags = lambda x: all(map(is_flag, x[1].split()))
    assert all(map(all_parts_are_flags, FLAGS))

  def testConfig(self):
    """Smoke test how to choose experiments."""
    config = v8_fuzz_config.Config('foo', random.Random(42))
    experiments = [
      [25, 'ignition', 'jitless', 'd8'],
      [75, 'ignition', 'ignition', 'clang_x86/d8'],
    ]
    flags = [
      [0.1, '--flag'],
      [0.3, '--baz'],
      [0.3, '--foo --bar'],
    ]
    self.assertEqual(
        [
          '--first-config=ignition',
          '--second-config=jitless',
          '--second-d8=d8',
          '--second-config-extra-flags=--baz',
          '--second-config-extra-flags=--foo',
          '--second-config-extra-flags=--bar',
        ],
        config.choose_foozzie_flags(experiments, flags),
    )
    self.assertEqual(
        [
          '--first-config=ignition',
          '--second-config=jitless',
          '--second-d8=d8',
        ],
        config.choose_foozzie_flags(experiments, flags),
    )


class UnitTest(unittest.TestCase):
  def testCluster(self):
    crash_test_example_path = 'CrashTests/path/to/file.js'
    self.assertEqual(
        v8_foozzie.ORIGINAL_SOURCE_DEFAULT,
        v8_foozzie.cluster_failures(''))
    self.assertEqual(
        v8_foozzie.ORIGINAL_SOURCE_CRASHTESTS,
        v8_foozzie.cluster_failures(crash_test_example_path))
    self.assertEqual(
        '_o_O_',
        v8_foozzie.cluster_failures(
            crash_test_example_path,
            known_failures={crash_test_example_path: '_o_O_'}))
    self.assertEqual(
        '98',
        v8_foozzie.cluster_failures('v8/test/mjsunit/apply.js'))

  def testDiff(self):
    def diff_fun(one, two, skip=False):
      suppress = v8_suppressions.get_suppression(skip)
      return suppress.diff_lines(one.splitlines(), two.splitlines())

    smoke = v8_suppressions.SMOKE_TEST_SOURCE

    one = ''
    two = ''
    diff = None, smoke
    self.assertEqual(diff, diff_fun(one, two))

    one = 'a \n  b\nc();'
    two = 'a \n  b\nc();'
    diff = None, smoke
    self.assertEqual(diff, diff_fun(one, two))

    one = """
Still equal
Extra line
"""
    two = """
Still equal
"""
    diff = '- Extra line', smoke
    self.assertEqual(diff, diff_fun(one, two))

    one = """
Still equal
"""
    two = """
Still equal
Extra line
"""
    diff = '+ Extra line', smoke
    self.assertEqual(diff, diff_fun(one, two))

    one = """
undefined
somefile.js: TypeError: undefined is not a constructor
"""
    two = """
undefined
otherfile.js: TypeError: undefined is not a constructor
"""
    diff = """- somefile.js: TypeError: undefined is not a constructor
+ otherfile.js: TypeError: undefined is not a constructor""", smoke
    self.assertEqual(diff, diff_fun(one, two))

  def testOutputCapping(self):
    def check(stdout1, stdout2, is_crash1, is_crash2, capped_lines1,
              capped_lines2):
      output1 = output(stdout1, is_crash1)
      output2 = output(stdout2, is_crash2)
      self.assertEqual(
          (capped_lines1.encode('utf-8'), capped_lines2.encode('utf-8')),
          v8_suppressions.get_output_capped(output1, output2))

    # No capping, already equal.
    check('1\n2', '1\n2', True, True, '1\n2', '1\n2')
    # No crash, no capping.
    check('1\n2', '1\n2\n3', False, False, '1\n2', '1\n2\n3')
    check('1\n2\n3', '1\n2', False, False, '1\n2\n3', '1\n2')
    # Cap smallest if all runs crash.
    check('1\n2', '1\n2\n3', True, True, '1\n2', '1\n2')
    check('1\n2\n3', '1\n2', True, True, '1\n2', '1\n2')
    check('1\n2', '1\n23', True, True, '1\n2', '1\n2')
    check('1\n23', '1\n2', True, True, '1\n2', '1\n2')
    # Cap the non-crashy run.
    check('1\n2\n3', '1\n2', False, True, '1\n2', '1\n2')
    check('1\n2', '1\n2\n3', True, False, '1\n2', '1\n2')
    check('1\n23', '1\n2', False, True, '1\n2', '1\n2')
    check('1\n2', '1\n23', True, False, '1\n2', '1\n2')
    # The crashy run has more output.
    check('1\n2\n3', '1\n2', True, False, '1\n2\n3', '1\n2')
    check('1\n2', '1\n2\n3', False, True, '1\n2', '1\n2\n3')
    check('1\n23', '1\n2', True, False, '1\n23', '1\n2')
    check('1\n2', '1\n23', False, True, '1\n2', '1\n23')
    # Keep output difference when capping.
    check('1\n2', '3\n4\n5', True, True, '1\n2', '3\n4')
    check('1\n2\n3', '4\n5', True, True, '1\n2', '4\n5')
    check('12', '345', True, True, '12', '34')
    check('123', '45', True, True, '12', '45')

  @unittest.mock.patch(
      'v8_suppressions.IGNORE_LINES',
      [re.compile('^ign1.*\n'.encode('utf-8'), re.M),
       re.compile('^ign2.*\n'.encode('utf-8'), re.M)])
  def testIgnoredLines(self):
    def check(stdout1, stdout2, is_crash1, is_crash2, diff):
      output1 = output(stdout1, is_crash1)
      output2 = output(stdout2, is_crash2)
      suppress = v8_suppressions.get_suppression()
      self.assertEqual(
          (diff, v8_suppressions.SMOKE_TEST_SOURCE),
          suppress.diff(output1, output2))

    # One run has lines to ignore, the other crashes.
    check('ign1X\n111\nign2X\n222\n333', '111\n22', False, True, None)
    check('111\n22', 'ign1X\n111\nign2X\n222\n333', True, False, None)

    # Ignored lines in both runs at different positions.
    check('ign1X\n111\n222\n333', '111\nign2X\n22', False, True, None)
    check('111\nign2X\n22', 'ign1X\n111\n222\n333', True, False, None)

    # Ignored lines at different positions, no crash.
    check('ign2X\n111\n\n222', 'ign1X\n111\nign1X\n\n222', False, False, None)
    check('ign1X\n111\nign1X\n\n222', 'ign2X\n111\n\n222', False, False, None)

    # Ignored lines and a difference, no crash.
    check('1\n2\nign2\n3', 'ign1X\n1\nign1\n2', False, False, '- 3')
    check('ign1X\n1\nign1\n2', '1\n2\nign2\n3', False, False, '+ 3')

    # Ignored lines, a difference and a crash.
    check('\n1\n3\nign1X\n4', '\nign2\n1\nign1\n2', False, True, '- 3\n+ 2')
    check('\nign2\n1\nign1\n2', '\n1\n3\nign1X\n4', True, False, '- 2\n+ 3')

  def testReduceOutput(self):
    suppress = v8_suppressions.get_suppression()
    proper_test_output = textwrap.dedent(f"""\
      Smoke-test output.
      {v8_suppressions.SMOKE_TEST_END_TOKEN}
      Real-test output.
      Some more.""")

    # The source is some test. Don't show smoke-test output.
    result = suppress.reduced_output(proper_test_output, 'some/file')
    expected = textwrap.dedent(f"""\
      Real-test output.
      Some more.""")
    self.assertEqual(expected, result)

    # The source is the smoke test. Only show smoke-test output.
    result = suppress.reduced_output(
        proper_test_output, v8_suppressions.SMOKE_TEST_SOURCE)
    self.assertEqual('Smoke-test output.\n', result)

    # Smoke-test output is not properly wrapped. Check that we print
    # everything.
    invalid_test_output = textwrap.dedent(f"""\
      Smoke-test output.
      Real-test output.
      Some more.""")
    result = suppress.reduced_output(invalid_test_output, 'some/file')
    self.assertEqual(invalid_test_output, result)

  @unittest.mock.patch('v8_foozzie.DISALLOWED_FLAGS', ['A'])
  @unittest.mock.patch('v8_foozzie.CONTRADICTORY_FLAGS',
                       [('B', 'C'), ('B', 'D')])
  def testFilterFlags(self):
    def check(input_flags, expected):
      self.assertEqual(expected, v8_foozzie.filter_flags(input_flags))

    check([], [])
    check(['A'], [])
    check(['D', 'A'], ['D'])
    check(['A', 'D'], ['D'])
    check(['C', 'D'], ['C', 'D'])
    check(['E', 'C', 'D', 'F'], ['E', 'C', 'D', 'F'])
    check(['B', 'D'], ['D'])
    check(['D', 'B'], ['B'])
    check(['C', 'B', 'D'], ['C', 'D'])
    check(['E', 'C', 'A', 'F', 'B', 'G', 'D'], ['E', 'C', 'F', 'G', 'D'])

  @unittest.mock.patch('v8_foozzie.DISALLOWED_FLAG_PREFIXES', ['A', 'B='])
  def testFilterFlagPrefixes(self):
    def check(input_flags, expected):
      self.assertEqual(expected, v8_foozzie.filter_flags(input_flags))

    check([], [])
    check(['A'], [])
    check(['D', 'A1'], ['D'])
    check(['A1', 'D'], ['D'])
    check(['B=42', 'D'], ['D'])
    check(['D', 'B=42'], ['D'])
    check(['A', 'B', 'C=42', 'D', 'B=-1'], ['B', 'C=42', 'D'])

  def _test_content(self, filename):
    with (TEST_DATA / filename).open() as f:
      return f.read()

  def _create_execution_configs(self, *extra_flags, **kwargs):
    """Create three execution configs as in production with a fake config
    called `special`.
    """
    # If we need the configs to be cross-arch with same-arch fallbacks,
    # we use build3 (x86) otherwise we compare with build1 (x64).
    build = 'build3' if kwargs.pop('cross_arch', True) else 'build1'
    argv = create_test_cmd_line(build, 'special', 'fuzz-123.js',
                                *extra_flags)
    options = v8_foozzie.parse_args(argv[2:])
    return v8_foozzie.create_execution_configs(options)

  @unittest.mock.patch('v8_suppressions.DROP_FLAGS_ON_CONTENT',
                       [('--bat', r'\%DontUseThat\(|\%DontUseThis\(')])
  @unittest.mock.patch(
      'v8_foozzie.CONFIGS', {
          'ignition': ['--foo', '--baz'],
          'default': ['--bar', '--baz'],
          'special': ['--bat'],
      })
  def testAdjustConfigsByContent_Matches1(self):
    suppress = v8_suppressions.get_suppression()
    content = self._test_content('fuzz-123.js')
    configs = self._create_execution_configs()
    logs = suppress.adjust_configs_by_content(configs, content)
    self.assertEqual(
        ['Dropped second config using --bat based on content rule.'], logs)
    self.assertEqual(2, len(configs))
    self.assertEqual(['--foo', '--baz'], configs[0].config_flags)
    self.assertEqual(['--bar', '--baz'], configs[1].config_flags)

    configs = self._create_execution_configs('--first-config-extra-flags=--bat')
    logs = suppress.adjust_configs_by_content(configs, content)
    expected_logs = [
        'Dropped --bat from first config based on content rule.',
        'Dropped second config using --bat based on content rule.',
    ]
    self.assertEqual(expected_logs, logs)
    self.assertEqual(2, len(configs))
    self.assertEqual(['--foo', '--baz'], configs[0].config_flags)
    self.assertEqual(['--bar', '--baz'], configs[1].config_flags)

  @unittest.mock.patch('v8_suppressions.DROP_FLAGS_ON_CONTENT',
                       [('--baz', r'\%DontUseThat\(|\%DontUseThis\(')])
  @unittest.mock.patch(
      'v8_foozzie.CONFIGS', {
          'ignition': ['--foo', '--baz'],
          'default': ['--bar', '--baz'],
          'special': ['--bat'],
      })
  def testAdjustConfigsByContent_Matches2(self):
    suppress = v8_suppressions.get_suppression()
    content = self._test_content('fuzz-123.js')
    configs = self._create_execution_configs()
    logs = suppress.adjust_configs_by_content(configs, content)
    expected_logs = [
        'Dropped --baz from first config based on content rule.',
        'Dropped --baz from default config based on content rule.',
    ]
    self.assertEqual(expected_logs, logs)
    self.assertEqual(3, len(configs))
    self.assertEqual(['--foo'], configs[0].config_flags)
    self.assertEqual(['--bar'], configs[1].config_flags)
    self.assertEqual(['--bar'], configs[1].fallback.config_flags)
    self.assertEqual(['--bat'], configs[2].config_flags)
    self.assertEqual(['--bat'], configs[2].fallback.config_flags)

    configs = self._create_execution_configs(
        '--second-config-extra-flags=--baz')
    logs = suppress.adjust_configs_by_content(configs, content)
    expected_logs = [
        'Dropped --baz from first config based on content rule.',
        'Dropped --baz from default config based on content rule.',
        'Dropped second config using --baz based on content rule.',
    ]
    self.assertEqual(expected_logs, logs)
    self.assertEqual(2, len(configs))
    self.assertEqual(['--foo'], configs[0].config_flags)
    self.assertEqual(['--bar'], configs[1].config_flags)
    self.assertEqual(['--bar'], configs[1].fallback.config_flags)

  @unittest.mock.patch('v8_suppressions.DROP_FLAGS_ON_CONTENT',
                       [('--baz', r'\%UnusedFun\(')])
  @unittest.mock.patch('v8_foozzie.CONFIGS', {
      'ignition': ['--foo', '--baz'],
      'default': ['--bar'],
      'special': ['--bat'],
  })
  def testAdjustConfigsByContent_DoesntMatch(self):
    suppress = v8_suppressions.get_suppression()
    content = self._test_content('fuzz-123.js')
    configs = self._create_execution_configs(
        '--second-config-extra-flags=--baz')
    logs = suppress.adjust_configs_by_content(configs, content)
    self.assertEqual([], logs)
    self.assertEqual(3, len(configs))
    self.assertEqual(['--foo', '--baz'], configs[0].config_flags)
    self.assertEqual(['--bar', '--baz'], configs[1].config_flags)
    self.assertEqual(['--bar', '--baz'], configs[1].fallback.config_flags)
    self.assertEqual(['--bat', '--baz'], configs[2].config_flags)
    self.assertEqual(['--bat', '--baz'], configs[2].fallback.config_flags)

  @unittest.mock.patch(
      'v8_foozzie.CONFIGS', {
          'ignition': ['--foo'],
          'default': [],
          'special': ['--bar'],
      })
  def testAdjustConfigsByOutput_Matches(self):
    """Test scenarios where the directive to avoid cross-arch comparison is in
    the output.
    """
    suppress = v8_suppressions.get_suppression()
    matching_output = textwrap.dedent("""\
      Some lines...
      Indentation doesn't matter.
      Warning: This run cannot be compared across architectures.
      That's it.""")

    # Scenario 1: We have a match, but the comparisons are in the same
    # architecture. So there's nothing to do.
    configs = self._create_execution_configs(cross_arch=False)
    baseline_config = configs[0]
    remaining_configs = configs[1:]
    logs = suppress.adjust_configs_by_output(
        remaining_configs, matching_output)
    self.assertEqual([], logs)
    self.assertEqual(2, len(remaining_configs))
    for config in remaining_configs:
      self.assertEqual(baseline_config.arch, config.arch)
      self.assertIsNone(config.fallback)

    # Scenario 2: We have a match and compare cross-arch. Ensure the
    # adjustments turns this into a same-arch comparison.
    configs = self._create_execution_configs()
    baseline_config = configs[0]
    remaining_configs = configs[1:]
    logs = suppress.adjust_configs_by_output(
        remaining_configs, matching_output)
    expected_logs = [
        'Running the default config on the same architecture.',
        'Running the second config on the same architecture.'
    ]
    self.assertEqual(expected_logs, logs)
    self.assertEqual(2, len(remaining_configs))
    for config in remaining_configs:
      self.assertEqual(baseline_config.arch, config.arch)
      self.assertIsNone(config.fallback)

  @unittest.mock.patch(
      'v8_foozzie.CONFIGS', {
          'ignition': ['--foo'],
          'default': [],
          'special': ['--bar'],
      })
  def testAdjustConfigsByOutput_DoesntMatch(self):
    """Test that cross-arch comparisons stay untouched if the directive
    from above is not in the output.
    """
    suppress = v8_suppressions.get_suppression()
    non_matching_output = textwrap.dedent("""\
      Some lines...
      Indentation doesn't matter.
      That's it.""")

    configs = self._create_execution_configs(cross_arch=True)
    baseline_config = configs[0]
    remaining_configs = configs[1:]
    logs = suppress.adjust_configs_by_output(
        remaining_configs, non_matching_output)
    self.assertEqual([], logs)
    self.assertEqual(2, len(remaining_configs))
    for config in remaining_configs:
      self.assertNotEqual(baseline_config.arch, config.arch)
      self.assertEqual(baseline_config.arch, config.fallback.arch)


def cut_verbose_output(stdout, n_comp):
  # This removes the first lines containing d8 commands of `n_comp` comparison
  # runs.
  return '\n'.join(stdout.split('\n')[n_comp * 2:])


def create_test_cmd_line(second_d8_dir, second_config, filename, *extra_flags):
  return list(
      map(str, [
          sys.executable,
          FOOZZIE,
          '--random-seed',
          '12345',
          '--first-d8',
          TEST_DATA / 'baseline' / 'd8.py',
          '--second-d8',
          TEST_DATA / second_d8_dir / 'd8.py',
          '--first-config',
          'ignition',
          '--second-config',
          second_config,
          TEST_DATA / filename,
      ] + list(extra_flags)))


def run_foozzie(second_d8_dir, *extra_flags, **kwargs):
  filename = kwargs.pop('filename', 'fuzz-123.js')
  second_config = kwargs.pop('second_config', 'ignition_turbo')
  cmd = create_test_cmd_line(second_d8_dir, second_config, filename,
                             *extra_flags)
  return subprocess.check_output(cmd, text=True, **kwargs)


class SystemTest(unittest.TestCase):
  """This tests the whole correctness-fuzzing harness with fake build
  artifacts.

  Overview of fakes:
    baseline: Example foozzie output.
    build1: No difference to baseline but ignored lines.
    build2: Output difference causing the script to fail.
    build3: As build1 but with an architecture difference as well.
  """

  def assert_expected(self, file_name, expected):
    if os.environ.get('GENERATE'):
      with (TEST_DATA / file_name).open('w') as f:
        f.write(expected)
    with (TEST_DATA / file_name).open() as f:
      self.assertEqual(f.read(), expected)

  def testPass(self):
    stdout = run_foozzie('build1')
    self.assertEqual('# V8 correctness - pass\n',
                     cut_verbose_output(stdout, 3))
    # Default comparison includes suppressions.
    self.assertIn('v8_suppressions.js', stdout)
    # Default comparison doesn't include any specific mock files.
    self.assertNotIn('v8_mock_archs.js', stdout)
    self.assertNotIn('v8_mock_webassembly.js', stdout)

  def _testDifferentOutputFail(self, expected_path, *args):
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build2',
                  '--first-config-extra-flags=--flag1',
                  '--first-config-extra-flags=--flag2=0',
                  '--second-config-extra-flags=--flag3', *args)
    e = ctx.exception
    self.assertEqual(v8_foozzie.RETURN_FAIL, e.returncode)
    self.assert_expected(expected_path, cut_verbose_output(e.output, 2))

  def testDifferentOutputFail(self):
    self._testDifferentOutputFail('failure_output.txt')

  def testSmokeTest_Fails(self):
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build4')
    e = ctx.exception
    self.assertEqual(v8_foozzie.RETURN_FAIL, e.returncode)
    self.assert_expected(
        'smoke_test_output.txt', cut_verbose_output(e.output, 2))

  def testSmokeTest_Crashes(self):
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build4',
                  '--second-config-extra-flags=--crash-the-smoke-test')
    e = ctx.exception
    self.assertEqual(v8_foozzie.RETURN_FAIL, e.returncode)
    self.assert_expected(
        'smoke_test_crash_output.txt', cut_verbose_output(e.output, 2))

  def testSimulatedCrash(self):
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build5', '--second-config-extra-flags=--simulate-errors')
    e = ctx.exception
    self.assertEqual(v8_foozzie.RETURN_FAIL, e.returncode)
    self.assert_expected(
        'simulated_crash_output.txt', cut_verbose_output(e.output, 2))

  def testDifferentArch(self):
    """Test that the architecture-specific mocks are passed to both runs when
    we use executables with different architectures.
    """
    # Build 3 simulates x86, while the baseline is x64.
    stdout = run_foozzie('build3')
    lines = stdout.split('\n')
    # TODO(machenbach): Don't depend on the command-lines being printed in
    # particular lines.
    self.assertIn('v8_mock_archs.js', lines[1])
    self.assertIn('v8_mock_archs.js', lines[3])

  def testDifferentArchFailFirst(self):
    """Test that we re-test against x64. This tests the path that also fails
    on x64 and then reports the error as x64.
    """
    # Build 3 simulates x86 and produces a difference on --bad-flag, but
    # the baseline build shows the same difference when --bad-flag is passed.
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build3', '--second-config-extra-flags=--bad-flag')
    e = ctx.exception
    self.assertEqual(v8_foozzie.RETURN_FAIL, e.returncode)
    self.assert_expected(
        'failure_output_arch.txt', cut_verbose_output(e.output, 3))

  def testDifferentArchFailSecond(self):
    """As above, but we test the path that only fails in the second (ia32)
    run and not with x64 and then reports the error as ia32.
    """
    # Build 3 simulates x86 and produces a difference on --very-bad-flag,
    # which the baseline build doesn't.
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build3', '--second-config-extra-flags=--very-bad-flag')
    e = ctx.exception
    self.assertEqual(v8_foozzie.RETURN_FAIL, e.returncode)
    self.assert_expected(
        'failure_output_second.txt', cut_verbose_output(e.output, 3))

  def testJitless(self):
    """Test that webassembly is mocked out when comparing with jitless."""
    stdout = run_foozzie(
        'build1', second_config='jitless')
    lines = stdout.split('\n')
    # TODO(machenbach): Don't depend on the command-lines being printed in
    # particular lines.
    self.assertIn('v8_mock_webassembly.js', lines[1])
    self.assertIn('v8_mock_webassembly.js', lines[3])

  def testJitlessAndWasmStruct_FlagPassed(self):
    """We keep passing the --jitless flag when no content rule matches.

    The flag passed to one run of build3 causes an output difference.
    """
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('build3', second_config='jitless')
    self.assertIn('jitless flag passed', ctx.exception.stdout)
    self.assertNotIn('Adjusted flags and experiments based on the test case',
                     ctx.exception.stdout)

  def testJitlessAndWasmStruct_FlagDropped(self):
    """We drop the --jitless flag when the content rule matches."""
    stdout = run_foozzie(
        'build3',
        second_config='jitless',
        filename='fuzz-wasm-struct-123.js')
    self.assertIn('Adjusted flags and experiments based on the test case',
                  stdout)
    self.assertIn(
        'Dropped second config using --jitless based on content rule.', stdout)

  def testAvoidCrossArchComparison(self):
    """We turn a cross-arch into a same-arch comparison if a directive is in
    the baseline output.
    """
    stdout = run_foozzie(
        'build3',
        '--first-config-extra-flags=--avoid-cross-arch',
        second_config='ignition_turbo_opt',
        filename='fuzz-123.js')

    self.assertIn('# Adjusted experiments based on baseline output', stdout)
    self.assertIn(
        'Running the default config on the same architecture.', stdout)
    self.assertIn(
        'Running the second config on the same architecture.', stdout)

  def testSkipSuppressions(self):
    """Test that the suppressions file is not passed when skipping
    suppressions.
    """
    # Compare baseline with baseline. This passes as there is no difference.
    stdout = run_foozzie('baseline', '--skip-suppressions')
    self.assertNotIn('v8_suppressions.js', stdout)


if __name__ == '__main__':
  unittest.main()