blob: 73b5e2dfa766b615ca7f423f74d9e9f3f7d726f9 [file] [log] [blame]
Jonathan Metzmand5ee1c62018-11-05 10:33:08 -08001# -*- coding: utf-8 -*-
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
6"""Unit tests for cros_fuzz."""
7
8from __future__ import print_function
9
10import os
11
12from chromite.lib import cros_logging as logging
13from chromite.lib import cros_test_lib
14from chromite.scripts import cros_fuzz
15
16DEFAULT_MAX_TOTAL_TIME_OPTION = cros_fuzz.GetLibFuzzerOption(
17 cros_fuzz.MAX_TOTAL_TIME_OPTION_NAME,
18 cros_fuzz.MAX_TOTAL_TIME_DEFAULT_VALUE)
19
20FUZZ_TARGET = 'fuzzer'
21FUZZER_COVERAGE_PATH = (
22 '/build/amd64-generic/tmp/fuzz/coverage-report/%s' % FUZZ_TARGET)
23
24BOARD = 'amd64-generic'
25
26
27class SysrootPathTest(cros_test_lib.TestCase):
28 """Tests the SysrootPath class."""
29
30 def setUp(self):
31 self.path_to_sysroot = _SetPathToSysroot()
32 self.sysroot_relative_path = '/dir'
33 self.basename = os.path.basename(self.sysroot_relative_path)
34 # Chroot relative path of a path that is in the sysroot.
35 self.path_in_sysroot = os.path.join(self.path_to_sysroot, self.basename)
36
37 def testSysroot(self):
38 """Tests that SysrootPath.sysroot returns expected result."""
39 sysroot_path = cros_fuzz.SysrootPath(self.sysroot_relative_path)
40 self.assertEqual(self.sysroot_relative_path, sysroot_path.sysroot)
41
42 def testChroot(self):
43 """Tests that SysrootPath.chroot returns expected result."""
44 sysroot_path = cros_fuzz.SysrootPath(self.sysroot_relative_path)
45 expected = os.path.join(self.path_to_sysroot, self.basename)
46 self.assertEqual(expected, sysroot_path.chroot)
47
48 def testIsSysrootPath(self):
49 """Tests that the IsSysrootPath can tell what is in the sysroot."""
50 self.assertTrue(cros_fuzz.SysrootPath.IsPathInSysroot(self.path_to_sysroot))
51 self.assertTrue(cros_fuzz.SysrootPath.IsPathInSysroot(self.path_in_sysroot))
52 path_not_in_sysroot_1 = os.path.join(
53 os.path.dirname(self.path_to_sysroot), self.basename)
54 self.assertFalse(
55 cros_fuzz.SysrootPath.IsPathInSysroot(path_not_in_sysroot_1))
56 path_not_in_sysroot_2 = os.path.join('/dir/build/amd64-generic')
57 self.assertFalse(
58 cros_fuzz.SysrootPath.IsPathInSysroot(path_not_in_sysroot_2))
59
60 def testFromChrootPathInSysroot(self):
61 """Tests that FromChrootPathInSysroot converts paths properly."""
62 # Test that it raises an assertion error when the path is not in the
63 # sysroot.
64 path_not_in_sysroot_1 = os.path.join(
65 os.path.dirname(self.path_to_sysroot), 'dir')
66 with self.assertRaises(AssertionError):
67 cros_fuzz.SysrootPath.FromChrootPathInSysroot(path_not_in_sysroot_1)
68
69 sysroot_path = cros_fuzz.SysrootPath.FromChrootPathInSysroot(
70 self.path_in_sysroot)
71 self.assertEqual(self.sysroot_relative_path, sysroot_path)
72
73
74class GetPathForCopyTest(cros_test_lib.TestCase):
75 """Tests GetPathForCopy."""
76
77 def testGetPathForCopy(self):
78 """Test that GetPathForCopy gives us the correct sysroot directory."""
79 _SetPathToSysroot()
80 directory = '/path/to/directory'
81 parent = 'parent'
82 child = os.path.basename(directory)
83 path_to_sysroot = cros_fuzz.SysrootPath.path_to_sysroot
84 storage_directory = cros_fuzz.SCRIPT_STORAGE_DIRECTORY
85
86 sysroot_path = cros_fuzz.GetPathForCopy(parent, child)
87
88 expected_chroot_path = os.path.join(path_to_sysroot, 'tmp',
89 storage_directory, parent, child)
90 self.assertEqual(expected_chroot_path, sysroot_path.chroot)
91
92 expected_sysroot_path = os.path.join('/', 'tmp', storage_directory, parent,
93 child)
94 self.assertEqual(expected_sysroot_path, sysroot_path.sysroot)
95
96
97class GetLibFuzzerOptionTest(cros_test_lib.TestCase):
98 """Tests GetLibFuzzerOption."""
99
100 def testGetLibFuzzerOption(self):
101 """Tests that GetLibFuzzerOption returns a correct libFuzzer option."""
102 expected = '-max_total_time=60'
103 self.assertEqual(expected, cros_fuzz.GetLibFuzzerOption(
104 'max_total_time', 60))
105
106
107class LimitFuzzingTest(cros_test_lib.TestCase):
108 """Tests LimitFuzzing."""
109
110 def setUp(self):
111 self.fuzz_command = ['./fuzzer', '-rss_limit_mb=4096']
112 self.corpus = None
113
114 def _Helper(self, expected_command=None):
115 """Calls LimitFuzzing and asserts fuzz_command equals |expected_command|.
116
117 If |expected| is None, then it is set to self.fuzz_command before calling
118 LimitFuzzing.
119 """
120 if expected_command is None:
121 expected_command = self.fuzz_command[:]
122 cros_fuzz.LimitFuzzing(self.fuzz_command, self.corpus)
123 self.assertEqual(expected_command, self.fuzz_command)
124
125 def testCommandHasMaxTotalTime(self):
126 """Tests that no limit is added when user specifies -max_total_time."""
127 self.fuzz_command.append('-max_total_time=60')
128 self._Helper()
129
130 def testCommandHasRuns(self):
131 """Tests that no limit is added when user specifies -runs"""
132 self.fuzz_command.append('-runs=1')
133 self._Helper()
134
135 def testCommandHasCorpus(self):
136 """Tests that a limit is added when user specifies a corpus."""
137 self.corpus = 'corpus'
138 expected = self.fuzz_command + ['-runs=0']
139 self._Helper(expected)
140
141 def testNoLimitOrCorpus(self):
142 """Tests that a limit is added when user specifies no corpus or limit."""
143 expected = self.fuzz_command + [DEFAULT_MAX_TOTAL_TIME_OPTION]
144 self._Helper(expected)
145
146
147class RunSysrootCommandMockTestCase(cros_test_lib.MockTestCase):
148 """Class for TestCases that call RunSysrootCommand."""
149
150 def setUp(self):
151 _SetPathToSysroot()
152 self.expected_command = None
153 self.expected_env = None
154 self.PatchObject(
155 cros_fuzz,
156 'RunSysrootCommand',
157 side_effect=self.MockedRunSysrootCommand)
158
159
160 def MockedRunSysrootCommand(
161 self, command, env=None, **kwargs): # pylint: disable=unused-argument
162 """The mocked version of RunSysrootCommand.
163
164 Asserts |command| and |env| are what is expected.
165 """
166 self.assertEqual(self.expected_command, command)
167 self.assertEqual(self.expected_env, env)
168
169
170class RunFuzzerTest(RunSysrootCommandMockTestCase):
171 """Tests RunFuzzer."""
172
173 def setUp(self):
174 self.corpus_path = None
175 self.fuzz_args = ''
176 self.testcase_path = None
177 self.expected_command = [
178 cros_fuzz.GetFuzzerSysrootPath(FUZZ_TARGET).sysroot,
179 ]
180 self.expected_env = {'ASAN_OPTIONS': 'log_path=stderr'}
181
182 def _Helper(self):
183 """Calls RunFuzzer."""
184 cros_fuzz.RunFuzzer(FUZZ_TARGET, self.corpus_path, self.fuzz_args,
185 self.testcase_path)
186
187 def testNoOptional(self):
188 """Tests correct command and env used when not specifying optional."""
189 self.expected_command.append(DEFAULT_MAX_TOTAL_TIME_OPTION)
190 self._Helper()
191
192 def testFuzzArgs(self):
193 """Tests that the correct command is used when fuzz_args is specified."""
194 fuzz_args = [DEFAULT_MAX_TOTAL_TIME_OPTION, '-fake_arg=fake_value']
195 self.expected_command.extend(fuzz_args)
196 self.fuzz_args = ' '.join(fuzz_args)
197 self._Helper()
198
199 def testTestCase(self):
200 """Tests a testcase is used when specified."""
201 self.testcase_path = '/path/to/testcase'
202 self.expected_command.append(self.testcase_path)
203 self._Helper()
204
205
206class MergeProfrawTest(RunSysrootCommandMockTestCase):
207 """Tests MergeProfraw."""
208
209 def testMergeProfraw(self):
210 """Tests that MergeProfraw works as expected."""
211 # Parent class will assert that these commands are used.
212 profdata_path = cros_fuzz.GetProfdataPath(FUZZ_TARGET)
213 self.expected_command = [
214 'llvm-profdata',
215 'merge',
216 '-sparse',
217 cros_fuzz.DEFAULT_PROFRAW_PATH,
218 '-o',
219 profdata_path.sysroot,
220 ]
221 cros_fuzz.MergeProfraw(FUZZ_TARGET)
222
223
224class GenerateCoverageReportTest(cros_test_lib.RunCommandTestCase):
225 """Tests GenerateCoverageReport."""
226
227 def setUp(self):
228 _SetPathToSysroot()
229 self.fuzzer_path = cros_fuzz.GetFuzzerSysrootPath(FUZZ_TARGET).chroot
230 self.profdata_path = cros_fuzz.GetProfdataPath(FUZZ_TARGET)
231
232 def testWithSharedLibraries(self):
233 """Tests that right command is used when specifying shared libraries."""
234 shared_libraries = ['shared_lib.so']
235 cros_fuzz.GenerateCoverageReport(FUZZ_TARGET, shared_libraries)
236 instr_profile_option = '-instr-profile=%s' % self.profdata_path.chroot
237 output_dir_option = '-output-dir=%s' % FUZZER_COVERAGE_PATH
238 expected_command = [
239 'llvm-cov',
240 'show',
241 '-object',
242 self.fuzzer_path,
243 '-object',
244 'shared_lib.so',
245 '-format=html',
246 instr_profile_option,
247 output_dir_option,
248 ]
249 self.assertCommandCalled(
250 expected_command, redirect_stderr=True, debug_level=logging.DEBUG)
251
252 def testNoSharedLibraries(self):
253 """Tests the right coverage command is used without shared libraries."""
254 shared_libraries = []
255 cros_fuzz.GenerateCoverageReport(FUZZ_TARGET, shared_libraries)
256 instr_profile_option = '-instr-profile=%s' % self.profdata_path.chroot
257 output_dir_option = '-output-dir=%s' % FUZZER_COVERAGE_PATH
258 expected_command = [
259 'llvm-cov', 'show', '-object', self.fuzzer_path, '-format=html',
260 instr_profile_option, output_dir_option
261 ]
262 self.assertCommandCalled(
263 expected_command, redirect_stderr=True, debug_level=logging.DEBUG)
264
265
266class RunSysrootCommandTest(cros_test_lib.RunCommandTestCase):
267 """Tests RunSysrootCommand."""
268
269 def testRunSysrootCommand(self):
270 """Tests RunSysrootCommand creates a proper command to run in sysroot."""
271 command = ['./fuzz', '-rss_limit_mb=4096']
272 cros_fuzz.RunSysrootCommand(command)
273 sysroot_path = _SetPathToSysroot()
274 expected_command = ['sudo', '--', 'chroot', sysroot_path]
275 expected_command.extend(command)
276 self.assertCommandCalled(expected_command, debug_level=logging.DEBUG)
277
278
279class GetBuildExtraEnvTest(cros_test_lib.TestCase):
280 """Tests GetBuildExtraEnv."""
281
282 TEST_ENV_VAR = 'TEST_VAR'
283
284 def testUseAndFeaturesNotClobbered(self):
285 """Tests that values of certain environment variables are appended to."""
286 vars_and_values = {'FEATURES': 'foo', 'USE': 'bar'}
287 for var, value in vars_and_values.iteritems():
288 os.environ[var] = value
289 extra_env = cros_fuzz.GetBuildExtraEnv(cros_fuzz.BuildType.COVERAGE)
290 for var, value in vars_and_values.iteritems():
291 self.assertIn(value, extra_env[var])
292
293 def testCoverageBuild(self):
294 """Tests that a proper environment is returned for a coverage build."""
295 extra_env = cros_fuzz.GetBuildExtraEnv(cros_fuzz.BuildType.COVERAGE)
296 for expected_flag in ['fuzzer', 'coverage', 'asan']:
297 self.assertIn(expected_flag, extra_env['USE'])
298 self.assertIn('noclean', extra_env['FEATURES'])
299
300
301def _SetPathToSysroot():
302 """Calls SysrootPath.SetPathToSysroot and returns result."""
303 return cros_fuzz.SysrootPath.SetPathToSysroot(BOARD)