808
808
return session_start_time <= report_time
814
if __name__ == '__main__':
818
class _T(unittest.TestCase):
819
def test_module_license_evaluation(self):
820
'''module licenses can be validated correctly.'''
822
def _build_ko(license):
823
asm = tempfile.NamedTemporaryFile(prefix='%s-' % (license),
825
asm.write(('.section .modinfo\n.string "license=%s"\n' % (license)).encode())
827
ko = tempfile.NamedTemporaryFile(prefix='%s-' % (license),
829
subprocess.call(['/usr/bin/as',asm.name,'-o',ko.name])
832
good_ko = _build_ko('GPL')
833
bad_ko = _build_ko('BAD')
836
# - loaded real module
837
# - unfindable module
841
# direct license check
842
self.assertTrue('GPL' in _get_module_license('isofs'))
843
self.assertEqual(_get_module_license('does-not-exist'), 'invalid')
844
self.assertTrue('GPL' in _get_module_license(good_ko.name))
845
self.assertTrue('BAD' in _get_module_license(bad_ko.name))
847
# check via nonfree_kernel_modules logic
848
f = tempfile.NamedTemporaryFile()
849
f.write(('isofs\ndoes-not-exist\n%s\n%s\n' %
850
(good_ko.name,bad_ko.name)).encode())
852
nonfree = nonfree_kernel_modules(f.name)
853
self.assertFalse('isofs' in nonfree)
854
self.assertTrue('does-not-exist' in nonfree)
855
self.assertFalse(good_ko.name in nonfree)
856
self.assertTrue(bad_ko.name in nonfree)
858
def test_attach_dmesg(self):
859
'''attach_dmesg() does not overwrite already existing data'''
864
self.assertTrue(report['BootDmesg'].startswith('['))
865
self.assertTrue(len(report['BootDmesg']) > 500)
866
self.assertTrue(report['CurrentDmesg'].startswith(b'['))
868
def test_dmesg_overwrite(self):
869
'''attach_dmesg() does not overwrite already existing data'''
871
report = {'BootDmesg': 'existingboot'}
874
self.assertEqual(report['BootDmesg'][:50], 'existingboot')
875
self.assertTrue(report['CurrentDmesg'].startswith(b'['))
877
report = {'BootDmesg': 'existingboot', 'CurrentDmesg': 'existingcurrent' }
880
self.assertEqual(report['BootDmesg'], 'existingboot')
881
self.assertEqual(report['CurrentDmesg'], 'existingcurrent')
883
def test_attach_file(self):
886
with open('/etc/motd', 'rb') as f:
887
motd_contents = f.read().strip()
888
with open('/etc/issue', 'rb') as f:
889
issue_contents = f.read().strip()
893
attach_file(report, '/etc/motd')
894
self.assertEqual(list(report), ['.etc.motd'])
895
self.assertEqual(report['.etc.motd'], motd_contents)
899
attach_file(report, '/etc/motd', 'Motd')
900
self.assertEqual(list(report), ['Motd'])
901
self.assertEqual(report['Motd'], motd_contents)
905
attach_file(report, '/nonexisting')
906
self.assertEqual(list(report), ['.nonexisting'])
907
self.assertTrue(report['.nonexisting'].startswith('Error: '))
911
attach_file(report, '/etc/motd')
912
attach_file(report, '/etc/motd')
913
self.assertEqual(list(report), ['.etc.motd'])
914
self.assertEqual(report['.etc.motd'], motd_contents)
916
attach_file(report, '/etc/issue', '.etc.motd', overwrite=False)
917
self.assertEqual(sorted(report.keys()), ['.etc.motd', '.etc.motd_'])
918
self.assertEqual(report['.etc.motd'], motd_contents)
919
self.assertEqual(report['.etc.motd_'], issue_contents)
921
def test_attach_file_if_exists(self):
922
'''attach_file_if_exists()'''
924
with open('/etc/motd', 'rb') as f:
925
motd_contents = f.read().strip()
929
attach_file_if_exists(report, '/etc/motd')
930
self.assertEqual(list(report), ['.etc.motd'])
931
self.assertEqual(report['.etc.motd'], motd_contents)
935
attach_file_if_exists(report, '/etc/motd', 'Motd')
936
self.assertEqual(list(report), ['Motd'])
937
self.assertEqual(report['Motd'], motd_contents)
941
attach_file_if_exists(report, '/nonexisting')
942
self.assertEqual(list(report), [])
944
def test_recent_logfile(self):
945
self.assertEqual(recent_logfile('/nonexisting', re.compile('.')), '')
946
self.assertEqual(recent_syslog(re.compile('ThisCantPossiblyHitAnything')), '')
947
self.assertNotEqual(len(recent_syslog(re.compile('.'))), 0)
949
@unittest.skipIf(in_session_of_problem(apport.Report()) is None, 'no ConsoleKit session')
950
def test_in_session_of_problem(self):
951
'''in_session_of_problem()'''
953
old_ctime = locale.getlocale(locale.LC_TIME)
954
locale.setlocale(locale.LC_TIME, 'C')
956
report = {'Date': 'Sat Jan 1 12:00:00 2011'}
957
self.assertFalse(in_session_of_problem(report))
959
report = {'Date': 'Mon Oct 10 21:06:03 2009'}
960
self.assertFalse(in_session_of_problem(report))
962
report = {'Date': 'Tue Jan 1 12:00:00 2211'}
963
self.assertTrue(in_session_of_problem(report))
965
locale.setlocale(locale.LC_TIME, '')
967
report = {'Date': 'Sat Jan 1 12:00:00 2011'}
968
self.assertFalse(in_session_of_problem(report))
970
report = {'Date': 'Mon Oct 10 21:06:03 2009'}
971
self.assertFalse(in_session_of_problem(report))
973
report = apport.Report()
974
self.assertTrue(in_session_of_problem(report))
976
self.assertEqual(in_session_of_problem({}), None)
978
locale.setlocale(locale.LC_TIME, old_ctime)
980
def test_no_crashes(self):
981
'''functions do not crash (very shallow)'''
984
attach_hardware(report)
986
attach_network(report)
988
attach_printing(report)
989
attach_conffiles(report, 'bash')
990
attach_conffiles(report, 'apport')
991
attach_conffiles(report, 'nonexisting')
992
attach_upstart_overrides(report, 'apport')
993
attach_upstart_overrides(report, 'nonexisting')