v8_foozzie_test.py 3.88 KB
Newer Older
1
#!/usr/bin/env python
2 3 4 5
# 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.

6 7 8
import os
import subprocess
import sys
9 10
import unittest

11
import v8_foozzie
12
import v8_fuzz_config
13 14
import v8_suppressions

15 16 17 18
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
FOOZZIE = os.path.join(BASE_DIR, 'v8_foozzie.py')
TEST_DATA = os.path.join(BASE_DIR, 'testdata')

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

class ConfigTest(unittest.TestCase):
  def testExperiments(self):
    """Test that probabilities add up to 100 and that all config names exist.
    """
    EXPERIMENTS = v8_fuzz_config.FOOZZIE_EXPERIMENTS
    CONFIGS = v8_foozzie.CONFIGS
    assert sum(x[0] for x in EXPERIMENTS) == 100
    assert all(map(lambda x: x[1] in CONFIGS, EXPERIMENTS))
    assert all(map(lambda x: x[2] in CONFIGS, EXPERIMENTS))
    assert all(map(lambda x: x[3].endswith('d8'), EXPERIMENTS))

  def testConfig(self):
    """Smoke test how to choose experiments.

    When experiment distribution changes this test might change, too.
    """
    class Rng(object):
      def random(self):
        return 0.5
    self.assertEqual(
        [
          '--first-config=ignition',
42
          '--second-config=ignition_turbo',
43 44 45 46 47 48
          '--second-d8=d8',
        ],
        v8_fuzz_config.Config('foo', Rng()).choose_foozzie_flags(),
    )


49
class UnitTest(unittest.TestCase):
50 51 52
  def testDiff(self):
    # TODO(machenbach): Mock out suppression configuration.
    suppress = v8_suppressions.get_suppression(
53
        'x64', 'ignition', 'x64', 'ignition_turbo')
54 55
    one = ''
    two = ''
56
    diff = None, None
57 58 59 60
    self.assertEquals(diff, suppress.diff(one, two))

    one = 'a \n  b\nc();'
    two = 'a \n  b\nc();'
61
    diff = None, None
62 63
    self.assertEquals(diff, suppress.diff(one, two))

64
    # Ignore line before caret, caret position and error message.
65 66 67 68 69 70 71 72 73 74 75 76 77 78
    one = """
undefined
weird stuff
      ^
somefile.js: TypeError: undefined is not a function
  undefined
"""
    two = """
undefined
other weird stuff
            ^
somefile.js: TypeError: baz is not a function
  undefined
"""
79
    diff = None, None
80 81 82 83 84 85 86 87 88
    self.assertEquals(diff, suppress.diff(one, two))

    one = """
Still equal
Extra line
"""
    two = """
Still equal
"""
89
    diff = '- Extra line', None
90 91 92 93 94 95 96 97 98
    self.assertEquals(diff, suppress.diff(one, two))

    one = """
Still equal
"""
    two = """
Still equal
Extra line
"""
99
    diff = '+ Extra line', None
100 101 102 103 104 105 106 107 108 109 110
    self.assertEquals(diff, suppress.diff(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
111
+ otherfile.js: TypeError: undefined is not a constructor""", None
112
    self.assertEquals(diff, suppress.diff(one, two))
113 114


machenbach's avatar
machenbach committed
115 116 117 118
def cut_verbose_output(stdout):
  return '\n'.join(stdout.split('\n')[2:])


119 120 121 122 123 124
def run_foozzie(first_d8, second_d8):
  return subprocess.check_output([
    sys.executable, FOOZZIE,
    '--random-seed', '12345',
    '--first-d8', os.path.join(TEST_DATA, first_d8),
    '--second-d8', os.path.join(TEST_DATA, second_d8),
125
    '--first-config', 'ignition',
126
    '--second-config', 'ignition_turbo',
127
    os.path.join(TEST_DATA, 'fuzz-123.js'),
128 129 130 131 132 133
  ])


class SystemTest(unittest.TestCase):
  def testSyntaxErrorDiffPass(self):
    stdout = run_foozzie('test_d8_1.py', 'test_d8_2.py')
machenbach's avatar
machenbach committed
134
    self.assertEquals('# V8 correctness - pass\n', cut_verbose_output(stdout))
135 136 137 138 139 140 141 142

  def testDifferentOutputFail(self):
    with open(os.path.join(TEST_DATA, 'failure_output.txt')) as f:
      expected_output = f.read()
    with self.assertRaises(subprocess.CalledProcessError) as ctx:
      run_foozzie('test_d8_1.py', 'test_d8_3.py')
    e = ctx.exception
    self.assertEquals(v8_foozzie.RETURN_FAIL, e.returncode)
machenbach's avatar
machenbach committed
143
    self.assertEquals(expected_output, cut_verbose_output(e.output))
144 145 146 147


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