def test_fetch(self): mock = MockDevice() with self.assertRaises(ValueError): mock.fetch('foo', 'not-likely-to-be-a-directory') mock.fetch('remote-path', '/tmp') self.assertIn(' '.join( mock.get_ssh_cmd(['scp', '[::1]:remote-path', '/tmp'])), mock.history) mock.fetch('corpus/*', '/tmp') self.assertIn(' '.join(mock.get_ssh_cmd(['scp', '[::1]:corpus/*', '/tmp'])), mock.history)
def test_set_ssh_verbosity(self): mock = MockDevice() mock.set_ssh_verbosity(3) cmd = ' '.join(mock.get_ssh_cmd(['ssh', '::1', 'some-command'])) self.assertIn(' -vvv ', cmd) mock.set_ssh_verbosity(1) cmd = ' '.join(mock.get_ssh_cmd(['ssh', '::1', 'some-command'])) self.assertIn(' -v ', cmd) self.assertNotIn(' -vvv ', cmd) mock.set_ssh_verbosity(0) cmd = ' '.join(mock.get_ssh_cmd(['ssh', '::1', 'some-command'])) self.assertNotIn(' -v ', cmd)
def test_rm(self): mock = MockDevice() mock.rm('path-to-some-file') mock.rm('path-to-some-directory', recursive=True) self.assertIn( ' '.join( mock.get_ssh_cmd( ['ssh', '::1', 'rm', '-f', 'path-to-some-file'])), mock.host.history) self.assertIn( ' '.join( mock.get_ssh_cmd( ['ssh', '::1', 'rm', '-rf', 'path-to-some-directory'])), mock.host.history)
def test_store(self): mock = MockDevice() mock.store('local-path', 'remote-path') self.assertIn( ' '.join( mock.get_ssh_cmd(['scp', 'local-path', '[::1]:remote-path'])), mock.history)
def test_set_ssh_option(self): mock = MockDevice() mock.set_ssh_option('StrictHostKeyChecking no') mock.set_ssh_option('UserKnownHostsFile=/dev/null') cmd = ' '.join(mock.get_ssh_cmd(['ssh'])) self.assertIn(' -o StrictHostKeyChecking no', cmd) self.assertIn(' -o UserKnownHostsFile=/dev/null', cmd)
def test_debug(self): mock_device = MockDevice() base_dir = tempfile.mkdtemp() try: fuzzer = Fuzzer( mock_device, u'mock-package1', u'mock-target2', output=base_dir, debug=True) fuzzer.start(['-some-lf-arg=value']) finally: shutil.rmtree(base_dir) self.assertIn( ' '.join( mock_device.get_ssh_cmd( [ 'ssh', '::1', 'run', fuzzer.url(), '-artifact_prefix=data/', '-some-lf-arg=value', '-jobs=1', '-dict=pkg/data/mock-target2/dictionary', 'data/corpus/', '-handle_segv=0', '-handle_bus=0', '-handle_ill=0', '-handle_fpe=0', '-handle_abrt=0', ])), mock_device.host.history)
def test_ssh(self): mock = MockDevice() mock.ssh(['some-command', '--with', 'some-argument']).check_call() self.assertIn( ' '.join( mock.get_ssh_cmd( ['ssh', '::1', 'some-command', '--with some-argument'])), mock.host.history)
def test_ls(self): mock = MockDevice() files = mock.ls('path-to-some-corpus') self.assertIn(' '.join( mock.get_ssh_cmd(['ssh', '::1', 'ls', '-l', 'path-to-some-corpus'])), mock.history) self.assertTrue('feac37187e77ff60222325cf2829e2273e04f2ea' in files) self.assertEqual(files['feac37187e77ff60222325cf2829e2273e04f2ea'], 1796)
def test_run(self): mock_device = MockDevice() fuzzer = Fuzzer(mock_device, u'mock-package1', u'mock-target2') fuzzer.run(['-some-lf-arg=value']) self.assertIn( ' '.join( mock_device.get_ssh_cmd([ 'ssh', '::1', 'run', fuzzer.url(), '-artifact_prefix=data', '-some-lf-arg=value' ])), mock_device.history)
def test_merge(self): mock_device = MockDevice() fuzzer = Fuzzer(mock_device, u'mock-package1', u'mock-target2') fuzzer.merge(['-some-lf-arg=value']) self.assertIn( ' '.join( mock_device.get_ssh_cmd([ 'ssh', '::1', 'run', fuzzer.url(), '-artifact_prefix=data/', '-merge=1', '-merge_control_file=data/.mergefile', '-some-lf-arg=value data/corpus/', 'data/corpus.prev/' ])), mock_device.history)
def test_stop(self): mock_device = MockDevice() pids = mock_device.getpids() fuzzer1 = Fuzzer(mock_device, u'mock-package1', u'mock-target1') fuzzer1.stop() self.assertIn( ' '.join( mock_device.get_ssh_cmd( ['ssh', '::1', 'kill', str(pids[fuzzer1.tgt])])), mock_device.host.history) fuzzer3 = Fuzzer(mock_device, u'mock-package1', u'mock-target3') fuzzer3.stop()
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 test_pull(self): mock = MockDevice() fuzzer = Fuzzer(mock, u'mock-package1', u'mock-target3') parser = Args.make_parser('description') args = parser.parse_args(['1/3']) corpus = Corpus.from_args(fuzzer, args) corpus.pull() self.assertIn( ' '.join( mock.get_ssh_cmd([ 'scp', '[::1]:' + fuzzer.data_path('corpus/*'), corpus.root ])), mock.host.history)
def test_store(self): mock = MockDevice() mock.store(tempfile.gettempdir(), 'remote-path') self.assertIn( ' '.join( mock.get_ssh_cmd( ['scp', tempfile.gettempdir(), '[::1]:remote-path'])), mock.host.history) # Ensure globbing works mock.host.history = [] with tempfile.NamedTemporaryFile() as f: mock.store('{}/*'.format(tempfile.gettempdir()), 'remote-path') for cmd in mock.host.history: if cmd.startswith('scp'): self.assertIn(f.name, cmd) # No copy if glob comes up empty mock.store(os.path.join(tempfile.gettempdir(), '*'), 'remote-path') self.assertNotIn( ' '.join( mock.get_ssh_cmd( ['scp', tempfile.gettempdir(), '[::1]:remote-path'])), mock.host.history)
def test_repro(self): mock_device = MockDevice() fuzzer = Fuzzer(mock_device, u'mock-package1', u'mock-target2') artifacts = ['data/' + artifact for artifact in fuzzer.list_artifacts()] fuzzer.repro(['-some-lf-arg=value']) self.assertIn( ' '.join( mock_device.get_ssh_cmd( [ 'ssh', '::1', 'run', fuzzer.url(), '-artifact_prefix=data/', '-some-lf-arg=value' ] + artifacts)), mock_device.host.history)
def test_push(self): mock = MockDevice() fuzzer = Fuzzer(mock, u'mock-package1', u'mock-target3') parser = Args.make_parser('description') args = parser.parse_args(['1/3']) corpus = Corpus.from_args(fuzzer, args) with tempfile.NamedTemporaryFile(dir=corpus.root) as f: corpus.push() self.assertIn( ' '.join( mock.get_ssh_cmd([ 'scp', f.name, '[::1]:' + fuzzer.data_path('corpus') ])), mock.host.history)
def test_fetch(self): mock = MockDevice() with self.assertRaises(ValueError): mock.fetch('foo', 'not-likely-to-be-a-directory') mock.fetch('remote-path', '/tmp') self.assertIn( ' '.join(mock.get_ssh_cmd(['scp', '[::1]:remote-path', '/tmp'])), mock.host.history) mock.fetch('corpus/*', '/tmp') self.assertIn( ' '.join(mock.get_ssh_cmd(['scp', '[::1]:corpus/*', '/tmp'])), mock.host.history) mock.delay = 2 with self.assertRaises(subprocess.CalledProcessError): mock.fetch('delayed', '/tmp') mock.delay = 2 with self.assertRaises(subprocess.CalledProcessError): mock.fetch('delayed', '/tmp', retries=1) mock.delay = 2 mock.fetch('delayed', '/tmp', retries=2) self.assertIn( ' '.join(mock.get_ssh_cmd(['scp', '[::1]:delayed', '/tmp'])), mock.host.history)
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 test_symbolize_log_pid_from_deadly_signal(self): mock_device = MockDevice() base_dir = tempfile.mkdtemp() fuzzer = Fuzzer( mock_device, u'mock-package1', u'mock-target2', output=base_dir) os.mkdir(fuzzer.results()) with tempfile.TemporaryFile() as tmp_out: with tempfile.TemporaryFile() as tmp_in: tmp_in.write( """ A line Another line ==67890== ERROR: libFuzzer: deadly signal MS: 1 SomeMutation; base unit: foo Yet another line artifact_prefix='data/'; Test unit written to data/crash-cccc """) tmp_in.flush() tmp_in.seek(0) fuzzer.symbolize_log(tmp_in, tmp_out) tmp_out.flush() tmp_out.seek(0) self.assertIn( ' '.join( mock_device.get_ssh_cmd( [ 'scp', '[::1]:' + fuzzer.data_path('crash-cccc'), fuzzer.results() ])), mock_device.host.history) self.assertEqual( tmp_out.read(), """ A line Another line ==67890== ERROR: libFuzzer: deadly signal Symbolized line 1 Symbolized line 2 Symbolized line 3 MS: 1 SomeMutation; base unit: foo Yet another line artifact_prefix='data/'; Test unit written to data/crash-cccc """)
def test_start(self): mock_device = MockDevice() base_dir = tempfile.mkdtemp() try: fuzzer = Fuzzer(mock_device, u'mock-package1', u'mock-target2', output=base_dir) fuzzer.start(['-some-lf-arg=value']) finally: shutil.rmtree(base_dir) self.assertIn( ' '.join( mock_device.get_ssh_cmd([ 'ssh', '::1', 'run', fuzzer.url(), '-artifact_prefix=data/', '-some-lf-arg=value', '-jobs=1', 'data/corpus/', ])), mock_device.host.history)
def test_init(self): mock = MockDevice(port=51823) cmd = ' '.join(mock.get_ssh_cmd(['ssh', '::1', 'some-command'])) self.assertIn(' -p 51823 ', cmd)