def test_e2e(self): # Set up hermetic environment. host = Host() host.fd_out = self._stdout host.fd_err = self._stderr with host.temp_dir() as temp_dir: # (Re-)parse the command line arguments, a la main.py. factory = Factory(host=host) parser = factory.parser args = parser.parse_args() # Ensure exactly 1 fuzzer is selected. fuzzer = factory.create_fuzzer(args) self.assertNoErrors() args.name = str(fuzzer) list_args = parser.parse_args(['list', args.name]) list_args.command(list_args, factory) self.assertOut( ['Found 1 matching fuzzer for "{}":'.format(str(fuzzer))], n=1) self.assertNoErrors() start_args = parser.parse_args( ['start', '-o', temp_dir.pathname, args.name]) proc = command.start_fuzzer(start_args, factory) self.assertNoErrors() stop_args = parser.parse_args(['stop', args.name]) command.stop_fuzzer(stop_args, factory) self.assertNoErrors() if proc: proc.wait() check_args = parser.parse_args(['check', args.name]) command.check_fuzzer(check_args, factory) self.assertOut(['{}: STOPPED'.format(args.name)], n=1) self.assertNoErrors() unit = os.path.join(temp_dir.pathname, 'unit') with open(unit, 'w') as opened: opened.write('hello world') repro_args = parser.parse_args(['repro', args.name, unit]) command.repro_units(repro_args, factory) self.assertNoErrors() analyze_args = ['analyze', '-max_total_time=10', args.name] if args.local: analyze_args.append('--local') analyze_args = parser.parse_args(analyze_args) command.analyze_fuzzer(analyze_args, factory) self.assertNoErrors()
def test_single_fuzzer(self): # (Re-)parse the command line arguments, a la main.py. args = self.parser.parse_args() # Ensure exactly 1 fuzzer is selected. fuzzer = self.factory.create_fuzzer(args) self.assertNoErrors() args.name = str(fuzzer) list_args = self.parser.parse_args(['list', args.name]) list_args.command(list_args, self.factory) self.assertOut( ['Found 1 matching fuzzer for "{}":'.format(str(fuzzer))], n=1) self.assertNoErrors() start_args = self.parser.parse_args( ['start', '-o', self.temp_dir, args.name]) proc = command.start_fuzzer(start_args, self.factory) self.assertNoErrors() stop_args = self.parser.parse_args(['stop', args.name]) command.stop_fuzzer(stop_args, self.factory) self.assertNoErrors() if proc: proc.wait() check_args = self.parser.parse_args(['check', args.name]) command.check_fuzzer(check_args, self.factory) self.assertOut(['{}: STOPPED'.format(args.name)], n=1) self.assertNoErrors() unit = os.path.join(self.temp_dir, 'unit') with open(unit, 'w') as opened: opened.write('hello world') repro_args = self.parser.parse_args(['repro', args.name, unit]) command.repro_units(repro_args, self.factory) self.assertNoErrors() analyze_args = ['analyze', '-max_total_time=10', args.name] if args.local: analyze_args.append('--local') analyze_args = self.parser.parse_args(analyze_args) command.analyze_fuzzer(analyze_args, self.factory) self.assertNoErrors()
def test_analyze_fuzzer(self): args = self.parse_args('analyze', '-l', str(self.fuzzer)) command.analyze_fuzzer(args, self.factory) # We shouldn't have copied anything self.assertFalse([cmd for cmd in self.host.processes if 'gs' in cmd]) # Invalid corpus corpus1 = 'corpus1' corpus2 = 'corpus2' local_dict = 'local_dict' args = self.parse_args( 'analyze', '-c', corpus1, '-c', corpus2, '-d', local_dict, '{}/{}'.format(self.fuzzer.package, self.fuzzer.executable)) self.assertError( lambda: command.analyze_fuzzer(args, self.factory), 'No such directory: {}'.format(corpus1)) self.host.mkdir(corpus1) foo = os.path.join(corpus1, 'foo') bar = os.path.join(corpus1, 'bar') self.host.touch(foo) self.host.touch(bar) self.assertError( lambda: command.analyze_fuzzer(args, self.factory), 'No such directory: {}'.format(corpus2)) # Invalid dictionary self.host.mkdir(corpus2) baz = os.path.join(corpus2, 'baz') self.host.touch(baz) self.assertError( lambda: command.analyze_fuzzer(args, self.factory), 'No such file: {}'.format(local_dict)) self.host.touch(local_dict) with self.host.temp_dir() as temp_dir: # Make it appear as if something was retrieved from GCS. qux = os.path.join(temp_dir.pathname, 'qux') self.host.touch(qux) command.analyze_fuzzer(args, self.factory) gcs_url = 'gs://corpus.internal.clusterfuzz.com/libFuzzer/fuchsia_{}-{}'.format( self.fuzzer.package, self.fuzzer.executable) with self.host.temp_dir() as temp_dir: cmd = ['gsutil', '-m', 'cp', gcs_url + '/*', temp_dir.pathname] self.assertRan(*cmd) abspath = self.ns.data_abspath(self.corpus.nspaths[0]) self.assertScpTo(bar, foo, abspath) self.assertScpTo(baz, abspath) self.assertEqual( self.dictionary.nspath, self.fuzzer.ns.data(local_dict))