#!/usr/bin/env python import unittest import sys import kernellib as lib import guidexml """ import matplotlib.pyplot as pyplot def draw_graph(): 'Draws a graph x=kernel releases, y=amount of vulnerabilities' LINUX26_RELEASES = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '8.1', '9', '10', '11', '11.1', '11.2', '11.3', '11.4', '11.5', '11.6', '11.7', '11.8', '11.9', '11.10', '11.11', '11.12', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '12.6', '13', '13.1', '13.2', '13.3', '13.4', '13.5', '14', '14.1', '14.2', '14.3', '14.4', '14.5', '14.6', '14.7', '15', '15.1', '15.2', '15.3', '15.4', '15.5', '15.6', '15.7', '16', '16.1', '16.2', '16.3', '16.4', '16.5', '16.6', '16.7', '16.8', '16.9', '16.10', '16.11', '16.12', '16.13', '16.14', '16.15', '16.16', '16.17', '16.18', '16.19', '16.20', '16.21', '16.22', '16.23', '16.24', '16.25', '16.26', '16.27', '16.28', '16.29', '16.30', '16.31', '16.32', '16.33', '16.34', '16.35', '16.36', '16.37', '16.38', '16.39', '16.40', '16.41', '16.42', '16.43', '16.44', '16.45', '16.46', '16.47', '16.48', '16.49', '16.50', '16.51', '16.52', '16.53', '16.54', '16.55', '16.56', '16.57', '16.58', '16.59', '16.60', '16.61', '16.62', '17', '17.1', '17.2', '17.3', '17.4', '17.5', '17.6', '17.7', '17.8', '17.9', '17.10', '17.11', '17.12', '17.13', '17.14', '18', '18.1', '18.2', '18.3', '18.4', '18.5', '18.6', '18.7', '18.8', '19', '19.1', '19.2', '19.3', '19.4', '19.5', '19.6', '19.7', '20', '20.1', '20.2', '20.3', '20.4', '20.5', '20.6', '20.7', '20.8', '20.9', '20.10', '20.11', '20.12', '20.13', '20.14', '20.15', '20.16', '20.17', '20.18', '20.19', '20.20', '20.21', '21', '21.1', '21.2', '21.3', '21.4', '21.5', '21.6', '21.7', '22', '22.1', '22.2', '22.3', '22.4', '22.5', '22.6', '22.7', '22.8', '22.9', '22.10', '22.11', '22.12', '22.13', '22.14', '22.15', '22.16', '22.17', '22.18', '22.19', '23', '23.1', '23.2', '23.3', '23.4', '23.5', '23.6', '23.7', '23.8', '23.9', '23.10', '23.11', '23.12', '23.13', '23.14', '23.15', '23.16', '23.17', '24', '24.1', '24.2', '24.3', '24.4', '24.5', '24.6', '24.7', '25', '25.1', '25.2', '25.3', '25.4', '25.5', '25.6', '25.7', '25.8', '25.9', '25.10', '25.11', '25.12', '25.13', '25.14', '25.15', '25.16', '25.17', '25.18', '25.19', '25.20', '26', '26.1', '26.2', '26.3', '26.4', '26.5', '26.6', '26.7', '26.8', '27', '27.1', '27.2', '27.3', '27.4', '27.5', '27.6', '27.7', '27.8', '27.9', '27.10', '27.11', '27.12', '27.13', '27.14', '27.15', '27.16', '27.17', '27.18', '27.19', '27.20', '27.21', '27.22', '27.23', '27.24', '27.25', '27.26', '27.27', '27.28', '28', '28.1', '28.2', '28.3', '28.4', '28.5', '28.6', '28.7', '28.8', '28.9', '28.10', '29', '29.1', '29.2', '29.3', '29.4', '29.5', '29.6', '30', '30.1', '30.2', '30.3', '30.4', '30.5' ] vul, line, value = [], [], [] for item in LINUX26_RELEASES: kernel = lib.Kernel('gentoo-sources') kernel.revision = '' kernel.version = '2.6.' + item best = lib.best_version(kernel.source) schedule = lib.parse_cve_files('out', kernel, best, 'all') #FIXME vul.append(len(schedule.canfix)) if '.' not in item: line.append(len(schedule.canfix)) value.append(len(vul) - 1) pyplot.plot(vul, color='#7A5ADA', linewidth=1) for i in range(0, len(line)): pyplot.bar(value[i], line[i], color='#7A5ADA', edgecolor='#FFFFFF') pyplot.axis('tight') pyplot.ylabel('Number of vulnerabilities') pyplot.xlabel('Kernel release') pyplot.show() def write_guidexml(): 'Testcases for guidexml' doc = guidexml.Document('mydocument', '2.0', 'Sehr nuetzliches infos', 'en', '/doc/en/bla') doc.addAuthor('creator', 'asyme', 'asymmail@googlemail.de') doc.addAuthor('creator', 'rbu') chap = guidexml.Chapter('1 chapter') sect = guidexml.Section('1 section') taguri = guidexml.uri("http://google.de") tagpara = guidexml.paragraph("hello world, Visit: %s.das ist ein sehr langer' \ 'text mit mehr als MAXLENGTH Zeichen." % repr(taguri)) doc.append(chap) chap.append(sect) sect.append(tagpara) doc.create('/', 'file') def test_kernel_version(): 'Test if kernel releases are parsed correctly' kernelcases = { '2.6.29.1' : {'source' : 'vanilla', 'revision' : 'r0', 'version' : '2.6.29.1'}, '2.6.18-xen-r11' : {'source' : 'xen', 'revision' : 'r11', 'version' : '2.6.18'}, '2.6.27-gentoo-r2' : {'source' : 'gentoo', 'revision' : 'r2', 'version' : '2.6.27'}, '2.6.28-hardened-r77' : {'source' : 'hardened', 'revision' : 'r77', 'version' : '2.6.28'}, '2.6.28-r7' : {'source' : 'vanilla', 'revision' : 'r7', 'version' : '2.6.28'}, '2.6.28-rc8' : {'source' : 'vanilla', 'revision' : 'r0', 'version' : '2.6.28_rc8'}, '2.6.28-rc8-git6' : {'source' : 'git', 'revision' : 'r6', 'version' : '2.6.28_rc8'}, '2' : {'source' : 'vanilla', 'revision' : 'r0', 'version' : '2'}, '2.6.28-hardened-r7777' : {'source' : 'hardened', 'revision' : 'r0', 'version' : '2.6.28'}, '2.6.2833-hardened-r7' : None, } suite = unittest.TestSuite() for name, dict in kernelcases.iteritems(): newtest = KernelVersionTestCase(name, dict) suite.addTest(newtest) unittest.TextTestRunner(verbosity=2).run(suite) class KernelVersionTestCase(unittest.TestCase): def __init__(self, name, dict): unittest.TestCase.__init__(self, 'testFunction') self.name = name self.dict = dict def testFunction(self): rvalue = lib.extract_version_from(self.name) self.assertEqual(self.dict, rvalue) """ def test_interval(): 'Test if a kernel release is affected' kernelcase = ['gentoo', '2.6.30', 'r4', '2.6.30-5'] intervalcases = { '[linux >=2.6.27 <=2.6.30] [gp <2.6.30.3]' : False, '[linux >=2.6.27 <=2.6.30] [gp <2.6.30.6]' : True, '[linux >=2.6.28 <=2.6.31] [gp <=2.6.30.5]' : True, '[linux >=2.6.26 <2.6.28] [linux >2.6.29 <=2.6.30]' : True, } suite = unittest.TestSuite() kernel = lib.Kernel(kernelcase[0]) kernel.version = kernelcase[1] kernel.revision = kernelcase[2] kernel_gp = lib.Genpatch(kernelcase[3]) for wb, affected in intervalcases.iteritems(): interval_list = lib.interval_from_whiteboard(wb) newtest = IntervalTestCase(interval_list, kernel, kernel_gp, affected) suite.addTest(newtest) unittest.TextTestRunner(verbosity=2).run(suite) class IntervalTestCase(unittest.TestCase): def __init__(self, interval_list, kernel, kernel_gp, affected): unittest.TestCase.__init__(self, 'testFunction') self.interval_list = interval_list self.kernel = kernel self.kernel_gp = kernel_gp self.item = lib.Vulnerability('000000') self.affected = affected def testFunction(self): rvalue = lib.is_affected(self.interval_list, self.kernel, self.kernel_gp, self.item) self.assertEqual(self.affected, rvalue) if __name__ == '__main__': test_interval()