| #!/usr/bin/env python3 | |
| # Copyright 2026 The Chromium Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Tests for fuzz_target_runner.py.""" | |
| import argparse | |
| import contextlib | |
| import io | |
| import os | |
| import pathlib | |
| import tempfile | |
| import unittest | |
| from unittest import mock | |
| import engines | |
| import fuzz_target_runner as runner | |
| _ARGV = [ | |
| '--engine=libfuzzer', | |
| '--build-dir=out/fuzz', | |
| '--target=foo_fuzzer', | |
| '--duration=15m', | |
| '--output-dir=o', | |
| ] | |
| def _sh(*lines): | |
| """Returns a shell script that stands in for the target.""" | |
| return '\n'.join(('#!/bin/sh', *lines)) + '\n' | |
| class DurationTest(unittest.TestCase): | |
| def test_parse(self): | |
| self.assertEqual(runner.parse_duration('900'), 900) | |
| self.assertEqual(runner.parse_duration('900s'), 900) | |
| self.assertEqual(runner.parse_duration('15m'), 900) | |
| self.assertEqual(runner.parse_duration('1h'), 3600) | |
| self.assertEqual(runner.parse_duration('1h30m'), 5400) | |
| for bad in ('', 'abc', '15x', '1h 2m', '-5', '0', '0s', '0h0m', '\u00b2'): | |
| with self.assertRaises(argparse.ArgumentTypeError): | |
| runner.parse_duration(bad) | |
| class SessionFromArgvTest(unittest.TestCase): | |
| def test_minimal(self): | |
| session = runner.session_from_argv(_ARGV) | |
| self.assertIsInstance(session.adapter, engines.LibFuzzerAdapter) | |
| self.assertEqual( | |
| session.target, | |
| engines.Target(pathlib.Path(os.path.abspath('out/fuzz')), 'foo_fuzzer'), | |
| ) | |
| self.assertEqual( | |
| session.engine_options, engines.EngineOptions(duration_s=900, seed=None) | |
| ) | |
| self.assertEqual(session.layout.root, pathlib.Path(os.path.abspath('o'))) | |
| def test_seed(self): | |
| session = runner.session_from_argv(_ARGV + ['--seed=7']) | |
| self.assertEqual(session.engine_options.seed, 7) | |
| def test_rejects_seed_the_engine_would_not_use(self): | |
| for seed in ('0', '-1'): | |
| with self.subTest(seed=seed): | |
| with self.assertRaises(SystemExit) as e: | |
| with contextlib.redirect_stderr(io.StringIO()) as err: | |
| runner.session_from_argv(_ARGV + [f'--seed={seed}']) | |
| self.assertEqual(e.exception.code, runner.ExitCode.BAD_SPEC) | |
| self.assertIn('seed must be a positive integer', err.getvalue()) | |
| def test_strips_whitespace(self): | |
| session = runner.session_from_argv( | |
| [ | |
| '--engine=libfuzzer', | |
| '--build-dir= out/fuzz ', | |
| '--target= foo_fuzzer ', | |
| '--duration=15m', | |
| '--output-dir= o ', | |
| ] | |
| ) | |
| expected = runner.session_from_argv(_ARGV) | |
| self.assertEqual(session.target, expected.target) | |
| self.assertEqual(session.layout, expected.layout) | |
| def test_empty_flags(self): | |
| for flag in ('--build-dir', '--target', '--output-dir'): | |
| for value in ('', ' '): | |
| argv = { | |
| '--engine': 'libfuzzer', | |
| '--build-dir': 'out/fuzz', | |
| '--target': 'foo_fuzzer', | |
| '--duration': '15m', | |
| '--output-dir': 'o', | |
| flag: value, | |
| } | |
| with self.subTest(flag=flag, value=value): | |
| with self.assertRaises(SystemExit) as e: | |
| with contextlib.redirect_stderr(io.StringIO()) as err: | |
| runner.session_from_argv([f'{k}={v}' for k, v in argv.items()]) | |
| self.assertEqual(e.exception.code, runner.ExitCode.BAD_SPEC) | |
| self.assertIn(f'{flag}: must not be empty', err.getvalue()) | |
| class SessionRunTest(unittest.TestCase): | |
| """Runs real processes, with a shell script standing in for the target.""" | |
| def setUp(self): | |
| tmp = tempfile.TemporaryDirectory() | |
| self.addCleanup(tmp.cleanup) | |
| self.build_dir = pathlib.Path(tmp.name, 'out') | |
| self.build_dir.mkdir() | |
| self.output_dir = pathlib.Path(tmp.name, 'o') | |
| def create_target_and_session(self, target=None): | |
| """Returns a Session whose target binary has the given file content.""" | |
| if target is not None: | |
| binary = self.build_dir / 'foo_fuzzer' | |
| binary.write_text(target, encoding='utf-8') | |
| binary.chmod(0o755) | |
| return runner.session_from_argv( | |
| [ | |
| '--engine=libfuzzer', | |
| f'--build-dir={self.build_dir}', | |
| '--target=foo_fuzzer', | |
| '--duration=1', | |
| f'--output-dir={self.output_dir}', | |
| ] | |
| ) | |
| def assert_setup_error(self, session, exit_code): | |
| with self.assertRaises(runner.SetupError) as e: | |
| session.run() | |
| self.assertEqual(e.exception.exit_code, exit_code) | |
| def test_runs_engine(self): | |
| session = self.create_target_and_session( | |
| _sh( | |
| 'echo "args: $*"', | |
| 'echo "stderr" >&2', | |
| 'touch in_cwd "$HOME/in_home"', | |
| ) | |
| ) | |
| self.assertEqual(session.run(), runner.ExitCode.OK) | |
| log = session.layout.engine_log.read_text(encoding='utf-8') | |
| self.assertIn('-max_total_time=1', log) | |
| self.assertIn('stderr', log) | |
| self.assertTrue((session.layout.work / 'in_cwd').exists()) | |
| self.assertTrue((session.layout.home / 'in_home').exists()) | |
| def test_engine_exit_code_is_not_a_failure(self): | |
| # libFuzzer exits with a non-zero code when it finds a crash. | |
| self.assertEqual( | |
| self.create_target_and_session(_sh('exit 1')).run(), runner.ExitCode.OK | |
| ) | |
| def test_missing_binary(self): | |
| self.assert_setup_error( | |
| self.create_target_and_session(), runner.ExitCode.MISSING_BINARY | |
| ) | |
| self.assertFalse(self.output_dir.exists()) | |
| def test_empty_output_dir(self): | |
| self.output_dir.mkdir() | |
| self.assertEqual( | |
| self.create_target_and_session(_sh()).run(), runner.ExitCode.OK | |
| ) | |
| def test_creates_output_dir_parents(self): | |
| self.output_dir = self.output_dir / 'a' / 'b' | |
| self.assertEqual( | |
| self.create_target_and_session(_sh()).run(), runner.ExitCode.OK | |
| ) | |
| def test_nonempty_output_dir(self): | |
| self.output_dir.mkdir() | |
| (self.output_dir / 'old').touch() | |
| self.assert_setup_error( | |
| self.create_target_and_session(_sh()), runner.ExitCode.BAD_SPEC | |
| ) | |
| def test_uncreatable_output_dir(self): | |
| # A regular file is where the output dir's parent would be. | |
| self.output_dir.touch() | |
| self.output_dir = self.output_dir / 'o' | |
| self.assert_setup_error( | |
| self.create_target_and_session(_sh()), runner.ExitCode.BAD_SPEC | |
| ) | |
| def test_exec_failure(self): | |
| self.assert_setup_error( | |
| self.create_target_and_session('not an executable format'), | |
| runner.ExitCode.ENGINE_DID_NOT_START, | |
| ) | |
| def test_loader_error(self): | |
| self.assert_setup_error( | |
| self.create_target_and_session(_sh('exit 127')), | |
| runner.ExitCode.ENGINE_DID_NOT_START, | |
| ) | |
| class MainTest(unittest.TestCase): | |
| def test_setup_error_becomes_exit_code(self): | |
| error = runner.SetupError('boom', runner.ExitCode.MISSING_BINARY) | |
| with mock.patch.object(runner.Session, 'run', side_effect=error): | |
| with contextlib.redirect_stderr(io.StringIO()) as err: | |
| exit_code = runner.main(_ARGV) | |
| self.assertEqual(exit_code, runner.ExitCode.MISSING_BINARY) | |
| self.assertIn('boom', err.getvalue()) | |
| if __name__ == '__main__': | |
| unittest.main() |