def test_notify_user(self): host = Host() try: host.set_build_dir(host.find_build_dir()) except Host.ConfigError: return host.notify_user('This is a test', 'This is only a test.')
def test_set_fuzzers_json(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_fuzzers_json('no_such_json') if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() json_file = Host.join(build_dir, 'fuzzers.json') # No guarantee of contents; just ensure it parses without crashing. if os.path.exists(json_file): host.set_fuzzers_json(json_file) # Construct and parse both fuchisa and zircon style fuzzer metadata. data = [ { 'fuzz_host': False, 'fuzzers': ['foo_fuzzer'], 'fuzzers_package': 'foo_fuzzers' }, { 'fuzz_host': False, 'fuzzers': ['zx_fuzzer.asan', 'zx_fuzzer.ubsan'], 'fuzzers_package': 'zircon_fuzzers' }, ] with tempfile.NamedTemporaryFile() as f: f.write(json.dumps(data)) f.seek(0) host.set_fuzzers_json(f.name) self.assertIn(('foo_fuzzers', 'foo_fuzzer'), host.fuzzers) self.assertIn(('zircon_fuzzers', 'zx_fuzzer.asan'), host.fuzzers) self.assertIn(('zircon_fuzzers', 'zx_fuzzer.ubsan'), host.fuzzers)
def test_set_build_dir(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_fuzzers_json('no_such_build_dir') if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() ssh_config = Host.join(build_dir, 'ssh-keys', 'ssh_config') zxtools = Host.join(build_dir + '.zircon', 'tools') platform = 'mac-x64' if os.uname()[0] == 'Darwin' else 'linux-x64' executable = Host.join(host.get_host_out_dir(), 'symbolize') symbolizer = Host.join( 'prebuilt', 'third_party', 'clang', platform, 'bin', 'llvm-symbolizer', ) host.set_build_dir(build_dir) self.assertNotEqual(len(host._ids), 0) for id in host._ids: self.assertTrue(os.path.exists(id)) self.assertIsNotNone(host._zxtools) self.assertTrue(os.path.exists(host._symbolizer_exec)) self.assertTrue(os.path.exists(host._llvm_symbolizer))
def main(): parser = Args.make_parser( 'Starts the named fuzzer. Additional arguments are passed through.') args, fuzzer_args = parser.parse_known_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) with Cipd.from_args(fuzzer, args) as cipd: if cipd.install(): device.store(os.path.join(cipd.root, '*'), fuzzer.data_path('corpus')) print('\n****************************************************************') print(' Starting ' + str(fuzzer) + '.') print(' Outputs will be written to:') print(' ' + fuzzer.results()) if not args.foreground: print(' You should be notified when the fuzzer stops.') print(' To check its progress, use `fx fuzz check ' + str(fuzzer) + '`.') print(' To stop it manually, use `fx fuzz stop ' + str(fuzzer) + '`.') print('****************************************************************\n') fuzzer.start(fuzzer_args) title = str(fuzzer) + ' has stopped.' body = 'Output written to ' + fuzzer.results() + '.' print(title) print(body) host.notify_user(title, body) return 0
def test_from_args(self): host = Host() parser = Args.make_parser('description', name_required=False) # netaddr should get called with 'just-four-random-words', and fail with self.assertRaises(RuntimeError): args = parser.parse_args(['--device', 'just-four-random-words']) device = Device.from_args(host, args)
def init_hosts(args): hosts = args['hosts'] output_folder = args['output_folder'] logging.info("Initializing host info") ret_hosts = [] for host_info in hosts: host_info['output_folder'] = output_folder ret_hosts.append(Host(host_info, args['pre-req'])) return ret_hosts
def test_set_platform(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_platform('no_such_platform') if not os.getenv('FUCHSIA_DIR'): return platform = 'mac-x64' if os.uname()[0] == 'Darwin' else 'linux-x64' host.set_platform(platform) self.assertEqual(host._platform, platform)
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 setUp(self): super(IntegrationTest, self).setUp() # Set up hermetic environment. self.host = Host() self.host.fd_out = self._stdout self.host.fd_err = self._stderr self.factory = Factory(host=self.host) self.temp_dir = tempfile.mkdtemp() self.parser = self.factory.parser
def test_set_build_ids(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_build_ids('no_such_ids') if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() build_ids = Host.join(build_dir, 'ids.txt') if not os.path.exists(build_ids): return host.set_build_ids(build_ids) self.assertEqual(host._ids, build_ids)
def test_add_build_ids(self): host = Host() host.add_build_ids('no_such_ids') self.assertNotIn('no_such_ids', host._ids) if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() build_ids = Host.join(build_dir, '.build-id') if not os.path.exists(build_ids): return host.add_build_ids(build_ids) self.assertIn(build_ids, host._ids)
def test_set_zxtools(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_zxtools('no_such_zxtools') if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() zxtools = Host.join(build_dir + '.zircon', 'tools') if not os.path.isdir(zxtools): return host.set_zxtools(zxtools) self.assertEqual(host._zxtools, zxtools)
def test_set_fuzzers_json(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_fuzzers_json('no_such_json') if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() json_file = Host.join(build_dir, 'fuzzers.json') if not os.path.exists(json_file): return # No guarantee of contents; just ensure it parses without crashing host.set_fuzzers_json(json_file)
def test_zircon_tool(self): # If a build tree is available, try using a zircon tool for "real". host = Host() try: host.set_build_dir(host.find_build_dir()) except Host.ConfigError: return with self.assertRaises(Host.ConfigError): host.zircon_tool(['no_such_tool']) path = os.path.abspath(__file__) line = host.zircon_tool(['merkleroot', path]) self.assertRegexpMatches(line, r'[0-9a-f]* - ' + path)
def test_find_build_dir(self): host = Host() fuchsia_dir = os.getenv('FUCHSIA_DIR') os.unsetenv('FUCHSIA_DIR') del os.environ['FUCHSIA_DIR'] with self.assertRaises(Host.ConfigError): host.find_build_dir() if not fuchsia_dir: return os.environ['FUCHSIA_DIR'] = fuchsia_dir print(host.find_build_dir()) self.assertTrue(os.path.isdir(host.find_build_dir()))
def test_snapshot(self): fuchsia_dir = os.getenv('FUCHSIA_DIR') os.unsetenv('FUCHSIA_DIR') del os.environ['FUCHSIA_DIR'] host = Host() with self.assertRaises(Host.ConfigError): host.snapshot() if not fuchsia_dir: return os.environ['FUCHSIA_DIR'] = fuchsia_dir line = host.snapshot() self.assertRegexpMatches(line, r'[0-9a-f]{40}')
def main(): parser = Args.make_parser( 'Lists the fuzzing corpus instances in CIPD for a named fuzzer') args = parser.parse_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) cipd = Cipd(fuzzer) if not cipd.list(): return 1 return 0
def main(): parser = Args.make_parser('Stops the named fuzzer.') args = parser.parse_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) if fuzzer.is_running(): print('Stopping ' + str(fuzzer) + '.') fuzzer.stop() else: print(str(fuzzer) + ' is already stopped.') return 0
def test_symbolize(self): mock = MockHost() mock.symbolize('mock_in', 'mock_out') self.assertIn( 'mock/symbolize-ids-rel -ids mock/ids.txt -llvm-symbolizer mock/llvm_symbolizer', mock.history) # If a build tree is available, try doing symbolize for "real". host = Host() try: host.set_build_dir(host.find_build_dir()) except Host.ConfigError: return tmp_in = tempfile.TemporaryFile() tmp_out = tempfile.TemporaryFile() host.symbolize(tmp_in, tmp_out)
def main(): parser = Args.make_parser( 'Runs the named fuzzer on provided test units, or all current test ' + 'units for the fuzzer. Use \'check-fuzzer\' to see current tests units.' ) args, fuzzer_args = parser.parse_known_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) if fuzzer.repro(fuzzer_args) == 0: print('No matching artifacts found.') return 1 return 0
def test_zircon_tool(self): mock = MockHost() mock.zircon_tool(['mock_tool', 'mock_arg']) self.assertIn('mock/out/default.zircon/tools/mock_tool mock_arg', mock.history) # If a build tree is available, try using a zircon tool for "real". host = Host() try: host.set_build_dir(host.find_build_dir()) except Host.ConfigError: return with self.assertRaises(Host.ConfigError): host.zircon_tool(['no_such_tool']) path = os.path.abspath(__file__) line = host.zircon_tool(['merkleroot', path]) self.assertRegexpMatches(line, r'[0-9a-f]* - ' + path)
def main(): parser = Args.make_parser( 'Transfers a fuzzing corpus for a named fuzzer from a device to CIPD') args = parser.parse_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) if fuzzer.measure_corpus()[0] == 0: print('Ignoring ' + str(fuzzer) + '; corpus is empty.') return 0 with Cipd.from_args(fuzzer, args) as cipd: device.fetch(fuzzer.data_path('corpus/*'), cipd.root) if not cipd.create(): return 1 return 0
def main(): parser = Args.make_parser( 'Transfers corpus for a named fuzzer to a device', label_present=True) args = parser.parse_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) if os.path.isdir(args.label): device.store(os.path.join(args.label, '*'), fuzzer.data_path('corpus')) return 0 with Cipd.from_args(fuzzer, args, label=args.label) as cipd: if not cipd.install(): return 1 device.store(os.path.join(cipd.root, '*'), fuzzer.data_path('corpus')) return 0
def test_set_symbolizer(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_symbolizer('no_such_executable', 'no_such_symbolizer') if not os.getenv('FUCHSIA_DIR'): return platform = 'mac-x64' if os.uname()[0] == 'Darwin' else 'linux-x64' executable = Host.join(host.get_host_out_dir(), 'symbolize') symbolizer = Host.join('prebuilt', 'third_party', 'clang', platform, 'bin', 'llvm-symbolizer') with self.assertRaises(Host.ConfigError): host.set_symbolizer(executable, 'no_such_symbolizer') with self.assertRaises(Host.ConfigError): host.set_symbolizer('no_such_executable', symbolizer) host.set_symbolizer(executable, symbolizer) self.assertEqual(host._symbolizer_exec, executable) self.assertEqual(host._llvm_symbolizer, symbolizer)
def main(): parser = Args.make_parser( description='Reports status for the fuzzer matching NAME if provided, ' + 'or for all running fuzzers. Status includes execution state, corpus ' + 'size, and number of artifacts.', name_required=False) args = parser.parse_args() host = Host() device = Device.from_args(host, args) fuzzers = Fuzzer.filter(host.fuzzers, args.name) pids = device.getpids() silent = True for pkg, tgt in fuzzers: fuzzer = Fuzzer(device, pkg, tgt) if not args.name and tgt not in pids: continue silent = False if tgt in pids: print(str(fuzzer) + ': RUNNING') else: print(str(fuzzer) + ': STOPPED') print(' Output path: ' + fuzzer.data_path()) print(' Corpus size: %d inputs / %d bytes' % fuzzer.measure_corpus()) artifacts = fuzzer.list_artifacts() if len(artifacts) == 0: print(' Artifacts: None') else: print(' Artifacts: ' + artifacts[0]) for artifact in artifacts[1:]: print(' ' + artifact) if silent: print( 'No fuzzers are running. Include \'name\' to check specific fuzzers.' ) return 1 return 0
def main(): parser = Args.make_parser( 'Minimizes the current corpus for the named fuzzer. This should be ' + 'used after running the fuzzer for a while, or after incorporating a ' + 'third-party corpus using \'fetch-corpus\'') args, fuzzer_args = parser.parse_known_args() host = Host() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) with Cipd.from_args(fuzzer, args) as cipd: if cipd.install(): device.store(os.path.join(cipd.root, '*'), fuzzer.data_path('corpus')) if fuzzer.merge(fuzzer_args) == (0, 0): print('Corpus for ' + str(fuzzer) + ' is empty.') return 1 device.fetch(fuzzer.data_path('corpus/*'), cipd.root) if not cipd.create(): return 1 return 0
def test_set_build_dir(self): host = Host() with self.assertRaises(Host.ConfigError): host.set_fuzzers_json('no_such_build_dir') if not os.getenv('FUCHSIA_DIR'): return build_dir = host.find_build_dir() ssh_config = Host.join(build_dir, 'ssh-keys', 'ssh_config') build_ids = Host.join(build_dir, 'ids.txt') zxtools = Host.join(build_dir + '.zircon', 'tools') platform = 'mac-x64' if os.uname()[0] == 'Darwin' else 'linux-x64' executable = Host.join('zircon', 'prebuilt', 'downloads', 'symbolize') symbolizer = Host.join('buildtools', platform, 'clang', 'bin', 'llvm-symbolizer') if not os.path.exists(ssh_config) or not os.path.exists( build_ids) or not os.path.isdir(zxtools): return host.set_build_dir(build_dir) self.assertEqual(host._ids, build_ids) self.assertEqual(host._zxtools, zxtools) self.assertEqual(host._platform, platform) self.assertEqual(host._symbolizer_exec, executable) self.assertEqual(host._llvm_symbolizer, symbolizer)
def init_hosts(self): for hinfo in self.hosts_info: self.machines.append(Host(hinfo, None))
def setUp(self): super(NonHermeticTestCase, self).setUp() self._host = Host() self._host.fd_out = self._stdout self._host.fd_err = self._stderr self._temp_dir = tempfile.mkdtemp()
def init_hosts(self): for hinfo in self.hosts_info: host = Host(hinfo, None) self.machines.append(host) if self.sys_info is not None: host.start_info_server(self.workspace)