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 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_join(self): fuchsia_dir = os.getenv('FUCHSIA_DIR') os.unsetenv('FUCHSIA_DIR') del os.environ['FUCHSIA_DIR'] with self.assertRaises(Host.ConfigError): Host.join('foo') if not fuchsia_dir: return os.environ['FUCHSIA_DIR'] = fuchsia_dir self.assertTrue(Host.join('foo').endswith('foo'))
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_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 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 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 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_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_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 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 main(): parser = ArgParser('Lists corpus instances in CIPD for the named fuzzer') args = parser.parse_args() host = Host.from_build() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) with Corpus.from_args(fuzzer, args) as corpus: cipd = Cipd(corpus) print(cipd.instances()) return 0
def test_set_ssh_identity(self): mock = MockDevice() with self.assertRaises(Host.ConfigError): mock.set_ssh_identity('no_such_identity') if not os.getenv('FUCHSIA_DIR'): return identity_file = Host.join('.ssh', 'pkey') if not os.path.exists(identity_file): return mock.set_ssh_identity(identity_file) cmd = ' '.join(mock.get_ssh_cmd(['scp'])) self.assertIn('scp', cmd) self.assertIn(' -i ' + identity_file, cmd)
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 test_set_ssh_config(self): mock = MockDevice() with self.assertRaises(Host.ConfigError): mock.set_ssh_config('no_such_config') if not os.getenv('FUCHSIA_DIR'): return build_dir = mock.host.find_build_dir() ssh_config = Host.join(build_dir, 'ssh-keys', 'ssh_config') if not os.path.exists(ssh_config): return mock.set_ssh_config(ssh_config) cmd = ' '.join(mock.get_ssh_cmd(['ssh'])) self.assertIn('ssh', cmd) self.assertIn(' -F ' + ssh_config, cmd)
def __init__(self, fuzzer, disabled=False, root=None, label=None): self.disabled = disabled if disabled: return self.device = fuzzer.device self.fuzzer = fuzzer self._bin = Host.join('.jiri_root', 'bin', 'cipd') if root: self.root = root self._is_tmp = False else: self.root = tempfile.mkdtemp() self._is_tmp = True self.label = label
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('Stops the named fuzzer.') args = parser.parse_args() host = Host.from_build() 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_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 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.from_build() 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 main(): parser = Args.make_parser( description='Lists fuzzers matching NAME if provided, or all fuzzers.', name_required=False) args = parser.parse_args() host = Host.from_build() fuzzers = Fuzzer.filter(host.fuzzers, args.name) if len(fuzzers) == 0: print('No matching fuzzers.') return 1 print('Found %d matching fuzzers:' % len(fuzzers)) for fuzzer in fuzzers: print(' %s/%s' % fuzzer) 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 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 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 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 main(): parser = ArgParser( 'Transfers a fuzzing corpus for a named fuzzer from a device to CIPD') args = parser.parse_args() host = Host.from_build() 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 Corpus.from_args(fuzzer, args) as corpus: corpus.pull() cipd = Cipd(corpus) if not args.no_cipd: cipd.create() return 0
def main(): parser = ArgParser( 'Starts the named fuzzer. Additional arguments are passed through.') args, fuzzer_args = parser.parse_known_args() host = Host.from_build() device = Device.from_args(host, args) fuzzer = Fuzzer.from_args(device, args) if not args.monitor: with Corpus.from_args(fuzzer, args) as corpus: cipd = Cipd(corpus) if not args.no_cipd: cipd.install('latest') corpus.push() 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) if not args.foreground: subprocess.Popen(['python', sys.argv[0], '--monitor', str(fuzzer)]) else: fuzzer.monitor() title = str(fuzzer) + ' has stopped.' body = 'Output written to ' + fuzzer.results() + '.' print(title) print(body) host.notify_user(title, body) return 0