def run_bug_parse(content): with libear.TemporaryDirectory() as tmpdir: file_name = os.path.join(tmpdir, 'test.html') with open(file_name, 'w') as handle: handle.writelines(content) for bug in sut.parse_bug_html(file_name): return bug
def test_creates_directory(self): dirname = None with sut.TemporaryDirectory() as tmpdir: self.assertTrue(os.path.isdir(tmpdir)) dirname = tmpdir self.assertIsNotNone(dirname) self.assertFalse(os.path.exists(dirname))
def assert_command_creates_entry(self, command, expected): with libear.TemporaryDirectory() as tmpdir: filename = os.path.join(tmpdir, command[-1]) create_empty_file(filename) cmd = ['sh', '-c', ' '.join(command)] cdb = self.run_intercept(tmpdir, cmd) self.assertEqual(' '.join(expected), cdb[0]['command'])
def test_append_to_existing_cdb(self): with libear.TemporaryDirectory() as tmpdir: result = self.run_intercept(tmpdir, 'build_clean', []) self.assertTrue(os.path.isfile(result)) result = self.run_intercept(tmpdir, 'build_regular', ['--append']) self.assertTrue(os.path.isfile(result)) self.assertEqual(5, self.count_entries(result))
def test_intercept_cc_works(self): with libear.TemporaryDirectory() as tmpdir: outdir = check_call_and_report([ 'scan-build-py', '--plist', '-o', tmpdir, '--override-compiler', '--intercept-first' ], self.compile_empty_source_file(tmpdir, False)) self.assertEqual(self.get_plist_count(outdir), 1)
def test_broken_does_not_creates_failure_reports(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('broken', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, ['--no-failure-reports']) self.assertFalse(os.path.isdir(os.path.join(reportdir, 'failures')))
def test_sip(self): def create_status_report(filename, message): content = """#!/usr/bin/env sh echo 'sa-la-la-la' echo 'la-la-la' echo '{0}' echo 'sa-la-la-la' echo 'la-la-la' """.format(message) lines = [line.strip() for line in content.split('\n')] with open(filename, 'w') as handle: handle.write('\n'.join(lines)) handle.close() os.chmod(filename, 0x1ff) def create_csrutil(dest_dir, status): filename = os.path.join(dest_dir, 'csrutil') message = 'System Integrity Protection status: {0}'.format(status) return create_status_report(filename, message) def create_sestatus(dest_dir, status): filename = os.path.join(dest_dir, 'sestatus') message = 'SELinux status:\t{0}'.format(status) return create_status_report(filename, message) ENABLED = 'enabled' DISABLED = 'disabled' OSX = 'darwin' LINUX = 'linux' with libear.TemporaryDirectory() as tmpdir: try: saved = os.environ['PATH'] os.environ['PATH'] = tmpdir + ':' + saved create_csrutil(tmpdir, ENABLED) self.assertTrue(sut.is_preload_disabled(OSX)) create_csrutil(tmpdir, DISABLED) self.assertFalse(sut.is_preload_disabled(OSX)) create_sestatus(tmpdir, ENABLED) self.assertTrue(sut.is_preload_disabled(LINUX)) create_sestatus(tmpdir, DISABLED) self.assertFalse(sut.is_preload_disabled(LINUX)) finally: os.environ['PATH'] = saved try: saved = os.environ['PATH'] os.environ['PATH'] = '' # shall be false when it's not in the path self.assertFalse(sut.is_preload_disabled(OSX)) self.assertFalse(sut.is_preload_disabled(LINUX)) self.assertFalse(sut.is_preload_disabled('unix')) finally: os.environ['PATH'] = saved
def test_parse_real_crash(self): import libscanbuild.analyze as sut2 import re with libear.TemporaryDirectory() as tmpdir: filename = os.path.join(tmpdir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') # produce failure report opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'file': filename, 'output_dir': tmpdir, 'language': 'c', 'error_type': 'other_error', 'error_output': 'some output', 'exit_code': 13 } sut2.report_failure(opts) # find the info file pp_file = None for root, _, files in os.walk(tmpdir): keys = [os.path.join(root, name) for name in files] for key in keys: if re.match(r'^(.*/)+clang(.*)\.i$', key): pp_file = key self.assertIsNot(pp_file, None) # read the failure report back result = sut.parse_crash(pp_file + '.info.txt') self.assertEqual(result['source'], filename) self.assertEqual(result['problem'], 'Other Error') self.assertEqual(result['file'], pp_file) self.assertEqual(result['info'], pp_file + '.info.txt') self.assertEqual(result['stderr'], pp_file + '.stderr.txt')
def test_directory_name_comparison(self): with libear.TemporaryDirectory() as tmpdir, \ sut.report_directory(tmpdir, False) as report_dir1, \ sut.report_directory(tmpdir, False) as report_dir2, \ sut.report_directory(tmpdir, False) as report_dir3: self.assertLess(report_dir1, report_dir2) self.assertLess(report_dir2, report_dir3)
def test_not_successful_build(self): with libear.TemporaryDirectory() as tmpdir: result = os.path.join(tmpdir, 'cdb.json') make = make_args(tmpdir) + ['build_broken'] silent_call(['intercept-build', '--cdb', result] + make) self.assertTrue(os.path.isfile(result)) self.assertEqual(2, self.count_entries(result))
def test_successful_build_on_empty_env(self): with libear.TemporaryDirectory() as tmpdir: result = os.path.join(tmpdir, 'cdb.json') make = make_args(tmpdir) + ['CC=clang', 'build_regular'] silent_check_call( ['intercept-build', '--cdb', result, 'env', '-'] + make) self.assertTrue(os.path.isfile(result)) self.assertEqual(5, self.count_entries(result))
def assert_creates_number_of_entries(self, command, count): with libear.TemporaryDirectory() as tmpdir: filename = os.path.join(tmpdir, 'test.c') create_empty_file(filename) command.append(filename) cmd = ['sh', '-c', ' '.join(command)] cdb = self.run_intercept(tmpdir, cmd) self.assertEqual(count, len(cdb))
def test_plist_does_not_creates_html_report(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('regular', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, ['--plist']) self.assertFalse( os.path.exists(os.path.join(reportdir, 'index.html'))) self.assertEqual(self.get_html_count(reportdir), 0) self.assertEqual(self.get_plist_count(reportdir), 5)
def test_successful_build_with_wrapper(self): with libear.TemporaryDirectory() as tmpdir: result = os.path.join(tmpdir, 'cdb.json') make = make_args(tmpdir) + ['build_regular'] silent_check_call( ['intercept-build', '--cdb', result, '--override-compiler'] + make) self.assertTrue(os.path.isfile(result)) self.assertEqual(5, self.count_entries(result))
def test_intercept_library_works(self): with libear.TemporaryDirectory() as tmpdir: make = make_args(tmpdir) + ['build_regular'] outdir = check_call_and_report([ 'scan-build-py', '--plist', '-o', tmpdir, '--intercept-first' ], make) self.assertTrue(os.path.isdir(outdir)) self.assertEqual(self.get_plist_count(outdir), 5)
def test_default_only_creates_html_report(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('regular', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, []) self.assertTrue( os.path.exists(os.path.join(reportdir, 'index.html'))) self.assertEqual(self.get_html_count(reportdir), 2) self.assertEqual(self.get_plist_count(reportdir), 0) self.assertEqual(self.get_sarif_count(reportdir), 0)
def test_interposition_works(self): with libear.TemporaryDirectory() as tmpdir: make = make_args(tmpdir) + ['build_regular'] outdir = check_call_and_report( ['scan-build', '--plist', '-o', tmpdir, '--override-compiler'], make) self.assertTrue(os.path.isdir(outdir)) self.assertEqual(self.get_plist_count(outdir), 5)
def test_removes_directory_when_exception(self): dirname = None try: with sut.TemporaryDirectory() as tmpdir: self.assertTrue(os.path.isdir(tmpdir)) dirname = tmpdir raise RuntimeError('message') except: self.assertIsNotNone(dirname) self.assertFalse(os.path.exists(dirname))
def test_get_clang_arguments(self): with libear.TemporaryDirectory() as tmpdir: filename = os.path.join(tmpdir, 'test.c') with open(filename, 'w') as handle: handle.write('') result = sut.get_arguments( ['clang', '-c', filename, '-DNDEBUG', '-Dvar="this is it"'], tmpdir) self.assertTrue('NDEBUG' in result) self.assertTrue('var="this is it"' in result)
def run_analyzer(content, failures_report): with libear.TemporaryDirectory() as tmpdir: filename = os.path.join(tmpdir, 'test.cpp') with open(filename, 'w') as handle: handle.write(content) opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'direct_args': [], 'file': filename, 'output_dir': tmpdir, 'output_format': 'plist', 'output_failures': failures_report } spy = Spy() result = sut.run_analyzer(opts, spy.call) return (result, spy.arg)
def test_report_failure_create_files(self): with libear.TemporaryDirectory() as tmpdir: # create input file filename = os.path.join(tmpdir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') uname_msg = ' '.join(os.uname()) + os.linesep error_msg = 'this is my error output' # execute test opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'file': filename, 'output_dir': tmpdir, 'language': 'c', 'error_type': 'other_error', 'error_output': error_msg, 'exit_code': 13 } sut.report_failure(opts) # verify the result result = dict() pp_file = None for root, _, files in os.walk(tmpdir): keys = [os.path.join(root, name) for name in files] for key in keys: with open(key, 'r') as handle: result[key] = handle.readlines() if re.match(r'^(.*/)+clang(.*)\.i$', key): pp_file = key # prepocessor file generated self.assertUnderFailures(pp_file) # info file generated and content dumped info_file = pp_file + '.info.txt' self.assertTrue(info_file in result) self.assertEqual('Other Error\n', result[info_file][1]) self.assertEqual(uname_msg, result[info_file][3]) # error file generated and content dumped error_file = pp_file + '.stderr.txt' self.assertTrue(error_file in result) self.assertEqual([error_msg], result[error_file])
def test_clear_does_not_set_exit_code_if_asked_from_plist(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('clean', tmpdir) exit_code, __ = run_analyzer(tmpdir, cdb, ['--status-bugs', '--plist']) self.assertFalse(exit_code)
def test_regular_sets_exit_code_if_asked_from_plist(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('regular', tmpdir) exit_code, __ = run_analyzer(tmpdir, cdb, ['--status-bugs', '--plist']) self.assertTrue(exit_code)
def test_clear_does_not_set_exit_code(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('clean', tmpdir) exit_code, __ = run_analyzer(tmpdir, cdb, []) self.assertFalse(exit_code)
def test_clear_keeps_report_dir_when_asked(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('clean', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, ['--keep-empty']) self.assertTrue(os.path.isdir(reportdir))
def test_clear_deletes_report_dir(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('clean', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, []) self.assertFalse(os.path.isdir(reportdir))
def test_regular_keeps_report_dir(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('regular', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, []) self.assertTrue(os.path.isdir(reportdir))
def test_given_title_in_report(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('broken', tmpdir) exit_code, reportdir = run_analyzer( tmpdir, cdb, ['--html-title', 'this is the title']) self.assertTitleEqual(reportdir, 'this is the title')
def test_default_title_in_report(self): with libear.TemporaryDirectory() as tmpdir: cdb = prepare_cdb('broken', tmpdir) exit_code, reportdir = run_analyzer(tmpdir, cdb, []) self.assertTitleEqual(reportdir, 'src - analyzer results')
def test_all_exec_calls(self): this_dir, _ = os.path.split(__file__) source_dir = os.path.abspath(os.path.join(this_dir, '..', 'exec')) with libear.TemporaryDirectory() as tmp_dir: expected, result = run(source_dir, tmp_dir) self.assertEqualJson(expected, result)