Back to index

apport  2.4
test_fileutils.py
Go to the documentation of this file.
00001 import unittest, tempfile, os, shutil, time
00002 
00003 import problem_report
00004 import apport.fileutils
00005 import apport.packaging
00006 
00007 from io import BytesIO
00008 
00009 
00010 class T(unittest.TestCase):
00011     def setUp(self):
00012         self.orig_report_dir = apport.fileutils.report_dir
00013         apport.fileutils.report_dir = tempfile.mkdtemp()
00014         self.orig_config_file = apport.fileutils._config_file
00015 
00016     def tearDown(self):
00017         shutil.rmtree(apport.fileutils.report_dir)
00018         apport.fileutils.report_dir = self.orig_report_dir
00019         self.orig_report_dir = None
00020         apport.fileutils._config_file = self.orig_config_file
00021 
00022     def _create_reports(self, create_inaccessible=False):
00023         '''Create some test reports'''
00024 
00025         r1 = os.path.join(apport.fileutils.report_dir, 'rep1.crash')
00026         r2 = os.path.join(apport.fileutils.report_dir, 'rep2.crash')
00027 
00028         with open(r1, 'w') as fd:
00029             fd.write('report 1')
00030         with open(r2, 'w') as fd:
00031             fd.write('report 2')
00032         os.chmod(r1, 0o600)
00033         os.chmod(r2, 0o600)
00034         if create_inaccessible:
00035             ri = os.path.join(apport.fileutils.report_dir, 'inaccessible.crash')
00036             with open(ri, 'w') as fd:
00037                 fd.write('inaccessible')
00038             os.chmod(ri, 0)
00039             return [r1, r2, ri]
00040         else:
00041             return [r1, r2]
00042 
00043     def test_find_package_desktopfile(self):
00044         '''find_package_desktopfile()'''
00045 
00046         # package without any .desktop file
00047         nodesktop = 'bash'
00048         assert len([f for f in apport.packaging.get_files(nodesktop)
00049                     if f.endswith('.desktop')]) == 0
00050 
00051         # find a package with one and a package with multiple .desktop files
00052         onedesktop = None
00053         multidesktop = None
00054         for d in os.listdir('/usr/share/applications/'):
00055             if not d.endswith('.desktop'):
00056                 continue
00057             pkg = apport.packaging.get_file_package(
00058                 os.path.join('/usr/share/applications/', d))
00059             num = len([f for f in apport.packaging.get_files(pkg)
00060                        if f.endswith('.desktop')])
00061             if not onedesktop and num == 1:
00062                 onedesktop = pkg
00063             elif not multidesktop and num > 1:
00064                 multidesktop = pkg
00065 
00066             if onedesktop and multidesktop:
00067                 break
00068 
00069         if nodesktop:
00070             self.assertEqual(apport.fileutils.find_package_desktopfile(nodesktop), None, 'no-desktop package %s' % nodesktop)
00071         if multidesktop:
00072             self.assertEqual(apport.fileutils.find_package_desktopfile(multidesktop), None, 'multi-desktop package %s' % multidesktop)
00073         if onedesktop:
00074             d = apport.fileutils.find_package_desktopfile(onedesktop)
00075             self.assertNotEqual(d, None, 'one-desktop package %s' % onedesktop)
00076             self.assertTrue(os.path.exists(d))
00077             self.assertTrue(d.endswith('.desktop'))
00078 
00079     def test_likely_packaged(self):
00080         '''likely_packaged()'''
00081 
00082         self.assertEqual(apport.fileutils.likely_packaged('/bin/bash'), True)
00083         self.assertEqual(apport.fileutils.likely_packaged('/usr/bin/foo'), True)
00084         self.assertEqual(apport.fileutils.likely_packaged('/usr/local/bin/foo'), False)
00085         self.assertEqual(apport.fileutils.likely_packaged('/home/test/bin/foo'), False)
00086         self.assertEqual(apport.fileutils.likely_packaged('/tmp/foo'), False)
00087         # ignore crashes in /var/lib (LP#122859, LP#414368)
00088         self.assertEqual(apport.fileutils.likely_packaged('/var/lib/foo'), False)
00089 
00090     def test_find_file_package(self):
00091         '''find_file_package()'''
00092 
00093         self.assertEqual(apport.fileutils.find_file_package('/bin/bash'), 'bash')
00094         self.assertEqual(apport.fileutils.find_file_package('/bin/cat'), 'coreutils')
00095         self.assertEqual(apport.fileutils.find_file_package('/nonexisting'), None)
00096 
00097     def test_seen(self):
00098         '''get_new_reports() and seen_report()'''
00099 
00100         self.assertEqual(apport.fileutils.get_new_reports(), [])
00101         if os.getuid() == 0:
00102             tr = self._create_reports(True)
00103         else:
00104             tr = [r for r in self._create_reports(True) if not 'inaccessible' in r]
00105         self.assertEqual(set(apport.fileutils.get_new_reports()), set(tr))
00106 
00107         # now mark them as seen and check again
00108         nr = set(tr)
00109         for r in tr:
00110             self.assertEqual(apport.fileutils.seen_report(r), False)
00111             nr.remove(r)
00112             apport.fileutils.mark_report_seen(r)
00113             self.assertEqual(apport.fileutils.seen_report(r), True)
00114             self.assertEqual(set(apport.fileutils.get_new_reports()), nr)
00115 
00116     def test_mark_hanging_process(self):
00117         '''mark_hanging_process()'''
00118         pr = problem_report.ProblemReport()
00119         pr['ExecutablePath'] = '/bin/bash'
00120         apport.fileutils.mark_hanging_process(pr, '1')
00121         uid = str(os.getuid())
00122         base = '_bin_bash.%s.1.hanging' % uid
00123         expected = os.path.join(apport.fileutils.report_dir, base)
00124         self.assertTrue(os.path.exists(expected))
00125 
00126     def test_mark_report_upload(self):
00127         '''mark_report_upload()'''
00128         report = os.path.join(apport.fileutils.report_dir, 'report.crash')
00129         apport.fileutils.mark_report_upload(report)
00130         expected = os.path.join(apport.fileutils.report_dir, 'report.upload')
00131         self.assertTrue(os.path.exists(expected))
00132 
00133     def test_get_all_reports(self):
00134         '''get_all_reports()'''
00135 
00136         self.assertEqual(apport.fileutils.get_all_reports(), [])
00137         if os.getuid() == 0:
00138             tr = self._create_reports(True)
00139         else:
00140             tr = [r for r in self._create_reports(True) if not 'inaccessible' in r]
00141         self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
00142 
00143         # now mark them as seen and check again
00144         for r in tr:
00145             apport.fileutils.mark_report_seen(r)
00146 
00147         self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
00148 
00149     def test_get_system_reports(self):
00150         '''get_all_system_reports() and get_new_system_reports()'''
00151 
00152         self.assertEqual(apport.fileutils.get_all_reports(), [])
00153         self.assertEqual(apport.fileutils.get_all_system_reports(), [])
00154         if os.getuid() == 0:
00155             tr = self._create_reports(True)
00156             self.assertEqual(set(apport.fileutils.get_all_system_reports()), set(tr))
00157             self.assertEqual(set(apport.fileutils.get_new_system_reports()), set(tr))
00158 
00159             # now mark them as seen and check again
00160             for r in tr:
00161                 apport.fileutils.mark_report_seen(r)
00162 
00163             self.assertEqual(set(apport.fileutils.get_all_system_reports()), set(tr))
00164             self.assertEqual(set(apport.fileutils.get_new_system_reports()), set([]))
00165         else:
00166             tr = [r for r in self._create_reports(True) if not 'inaccessible' in r]
00167             self.assertEqual(set(apport.fileutils.get_all_system_reports()), set([]))
00168             self.assertEqual(set(apport.fileutils.get_new_system_reports()), set([]))
00169 
00170     def test_delete_report(self):
00171         '''delete_report()'''
00172 
00173         tr = self._create_reports()
00174 
00175         while tr:
00176             self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
00177             apport.fileutils.delete_report(tr.pop())
00178 
00179     def test_get_recent_crashes(self):
00180         '''get_recent_crashes()'''
00181 
00182         # incomplete fields
00183         r = BytesIO(b'''ProblemType: Crash''')
00184         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00185 
00186         r = BytesIO(b'''ProblemType: Crash
00187 Date: Wed Aug 01 00:00:01 1990''')
00188         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00189 
00190         # ancient report
00191         r = BytesIO(b'''ProblemType: Crash
00192 Date: Wed Aug 01 00:00:01 1990
00193 CrashCounter: 3''')
00194         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00195 
00196         # old report (one day + one hour ago)
00197         date = time.ctime(time.mktime(time.localtime()) - 25 * 3600)
00198         r = BytesIO(b'''ProblemType: Crash
00199 Date: ''' + date.encode() + b'''
00200 CrashCounter: 3''')
00201         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00202 
00203         # current report (one hour ago)
00204         date = time.ctime(time.mktime(time.localtime()) - 3600)
00205         r = BytesIO(b'''ProblemType: Crash
00206 Date: ''' + date.encode() + b'''
00207 CrashCounter: 3''')
00208         self.assertEqual(apport.fileutils.get_recent_crashes(r), 3)
00209 
00210     def test_make_report_path(self):
00211         '''make_report_path()'''
00212 
00213         pr = problem_report.ProblemReport()
00214         self.assertRaises(ValueError, apport.fileutils.make_report_path, pr)
00215 
00216         pr['Package'] = 'bash 1'
00217         self.assertTrue(apport.fileutils.make_report_path(pr).startswith('%s/bash' % apport.fileutils.report_dir))
00218         pr['ExecutablePath'] = '/bin/bash'
00219         self.assertTrue(apport.fileutils.make_report_path(pr).startswith('%s/_bin_bash' % apport.fileutils.report_dir))
00220 
00221     def test_check_files_md5(self):
00222         '''check_files_md5()'''
00223 
00224         f1 = os.path.join(apport.fileutils.report_dir, 'test 1.txt')
00225         f2 = os.path.join(apport.fileutils.report_dir, 'test:2.txt')
00226         sumfile = os.path.join(apport.fileutils.report_dir, 'sums.txt')
00227         with open(f1, 'w') as fd:
00228             fd.write('Some stuff')
00229         with open(f2, 'w') as fd:
00230             fd.write('More stuff')
00231         # use one relative and one absolute path in checksums file
00232         with open(sumfile, 'w') as fd:
00233             fd.write('''2e41290da2fa3f68bd3313174467e3b5  %s
00234 f6423dfbc4faf022e58b4d3f5ff71a70  %s
00235 ''' % (f1[1:], f2))
00236         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [], 'correct md5sums')
00237 
00238         with open(f1, 'w') as fd:
00239             fd.write('Some stuff!')
00240         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f1[1:]], 'file 1 wrong')
00241         with open(f2, 'w') as fd:
00242             fd.write('More stuff!')
00243         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f1[1:], f2], 'files 1 and 2 wrong')
00244         with open(f1, 'w') as fd:
00245             fd.write('Some stuff')
00246         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f2], 'file 2 wrong')
00247 
00248     def test_get_config(self):
00249         '''get_config()'''
00250 
00251         # nonexisting
00252         apport.fileutils._config_file = '/nonexisting'
00253         self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
00254         self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
00255         apport.fileutils.get_config.config = None  # trash cache
00256 
00257         # empty
00258         f = tempfile.NamedTemporaryFile()
00259         apport.fileutils._config_file = f.name
00260         self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
00261         self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
00262         apport.fileutils.get_config.config = None  # trash cache
00263 
00264         # nonempty
00265         f.write(b'[main]\none=1\ntwo = TWO\nb1 = 1\nb2=False\n[spethial]\none= 99\n')
00266         f.flush()
00267         self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
00268         self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
00269         self.assertEqual(apport.fileutils.get_config('main', 'one'), '1')
00270         self.assertEqual(apport.fileutils.get_config('main', 'one', default='moo'), '1')
00271         self.assertEqual(apport.fileutils.get_config('main', 'two'), 'TWO')
00272         self.assertEqual(apport.fileutils.get_config('main', 'b1', bool=True), True)
00273         self.assertEqual(apport.fileutils.get_config('main', 'b2', bool=True), False)
00274         self.assertEqual(apport.fileutils.get_config('main', 'b3', bool=True), None)
00275         self.assertEqual(apport.fileutils.get_config('main', 'b3', default=False, bool=True), False)
00276         self.assertEqual(apport.fileutils.get_config('spethial', 'one'), '99')
00277         self.assertEqual(apport.fileutils.get_config('spethial', 'two'), None)
00278         self.assertEqual(apport.fileutils.get_config('spethial', 'one', 'moo'), '99')
00279         self.assertEqual(apport.fileutils.get_config('spethial', 'nope', 'moo'), 'moo')
00280         apport.fileutils.get_config.config = None  # trash cache
00281 
00282         f.close()
00283 
00284 if __name__ == '__main__':
00285     unittest.main()