Mercurial > farol > farolluz
view tests/testCPE.py @ 40:1d9b2b06067e
Add a CPE parser (and tests)
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Mon, 29 Dec 2014 14:30:39 +0100 |
parents | |
children | bb1dd2a55643 |
line wrap: on
line source
import unittest from farolluz.parsers.cpe import CPE, CPEAttribute class testbindToURI(unittest.TestCase): def test_example1(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.0\.6001'), update=CPEAttribute('beta'), edition=CPEAttribute(any=True)) self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:microsoft:internet_explorer:8.0.6001:beta') def test_example2(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.*'), update=CPEAttribute('sp?')) self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:microsoft:internet_explorer:8.%02:sp%01') def test_example3(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('insight_diagnostics'), version=CPEAttribute(r'7\.4\.0\.1570'), update=CPEAttribute(na=True), sw_edition=CPEAttribute('online'), target_sw=CPEAttribute('win2003'), target_hw=CPEAttribute("x64")) self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~') def test_example4(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('openview_network_manager'), version=CPEAttribute(r'7\.51'), target_sw=CPEAttribute('linux')) self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:hp:openview_network_manager:7.51::~~~linux~~') def test_example5(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute(r'foo\\bar'), product=CPEAttribute(r'big\$money_manager_2010'), sw_edition=CPEAttribute('special'), target_sw=CPEAttribute('ipod_touch'), target_hw=CPEAttribute("80gb")) self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:foo%5cbar:big%24money_manager_2010:::~~special~ipod_touch~80gb~') class testunbindURI(unittest.TestCase): def test_example1(self): cpe = CPE() cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.0.6001:beta') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'microsoft') self.assertEqual(cpe.product.value, 'internet_explorer') self.assertEqual(cpe.version.value, r'8\.0\.6001') self.assertEqual(cpe.update.value, 'beta') self.assertTrue(cpe.edition.any) self.assertTrue(cpe.language.any) def test_example2(self): cpe = CPE() cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.%2a:sp%3f') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'microsoft') self.assertEqual(cpe.product.value, 'internet_explorer') self.assertEqual(cpe.version.value, r'8\.\*') self.assertEqual(cpe.update.value, 'sp\?') self.assertTrue(cpe.edition.any) self.assertTrue(cpe.language.any) def test_example3(self): cpe = CPE() cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.%02:sp%01') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'microsoft') self.assertEqual(cpe.product.value, 'internet_explorer') self.assertEqual(cpe.version.value, r'8\.*') self.assertEqual(cpe.update.value, 'sp?') self.assertTrue(cpe.edition.any) self.assertTrue(cpe.language.any) def test_example4(self): cpe = CPE() cpe.unbind_URI('cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'hp') self.assertEqual(cpe.product.value, 'insight_diagnostics') self.assertEqual(cpe.version.value, '7\.4\.0\.1570') self.assertTrue(cpe.update.any) self.assertTrue(cpe.edition.any) self.assertEqual(cpe.sw_edition.value, 'online') self.assertEqual(cpe.target_sw.value, 'win2003') self.assertEqual(cpe.target_hw.value, 'x64') self.assertTrue(cpe.other.any) self.assertTrue(cpe.language.any) def test_example5(self): cpe = CPE() cpe.unbind_URI('cpe:/a:hp:openview_network_manager:7.51:-:~~~linux~~') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'hp') self.assertEqual(cpe.product.value, 'openview_network_manager') self.assertEqual(cpe.version.value, '7\.51') self.assertTrue(cpe.update.na) self.assertTrue(cpe.edition.any) self.assertTrue(cpe.sw_edition.any) self.assertEqual(cpe.target_sw.value, 'linux') self.assertTrue(cpe.target_hw.any) self.assertTrue(cpe.other.any) self.assertTrue(cpe.language.any) def test_example6(self): cpe = CPE() self.assertRaises(KeyError, cpe.unbind_URI, 'cpe:/a:foo%5cbar:big%24money_2010%07:::~~special~ipod_touch~80gb~') def test_example7(self): cpe = CPE() cpe.unbind_URI('cpe:/a:foo~bar:big%7emoney_2010') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'foo\~bar') self.assertEqual(cpe.product.value, 'big\~money_2010') self.assertTrue(cpe.version.any) self.assertTrue(cpe.update.any) self.assertTrue(cpe.edition.any) self.assertTrue(cpe.language.any) def test_example8(self): cpe = CPE() self.assertRaises(ValueError, cpe.unbind_URI, 'cpe:/a:foo:bar:12.%02.1234') class testbindFS(unittest.TestCase): def test_example1(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.0\.6001'), update=CPEAttribute('beta'), edition=CPEAttribute(any=True)) self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*') def test_example2(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.*'), update=CPEAttribute('sp?'), edition=CPEAttribute(any=True)) self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.*:sp?:*:*:*:*:*:*') cpe.version = CPEAttribute(r'8\.\*') self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.\*:sp?:*:*:*:*:*:*') def test_example3(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('insight'), version=CPEAttribute(r'7\.4\.0\.1570'), update=CPEAttribute(na=True), sw_edition=CPEAttribute("online"), target_sw=CPEAttribute("win2003"), target_hw=CPEAttribute("x64")) self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:hp:insight:7.4.0.1570:-:*:*:online:win2003:x64:*') def test_example4(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('openview_network_manager'), version=CPEAttribute(r'7\.51'), target_sw=CPEAttribute('linux')) self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:hp:openview_network_manager:7.51:*:*:*:*:linux:*:*') def test_example5(self): cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute(r'foo\\bar'), product=CPEAttribute(r'big\$money_2010'), sw_edition=CPEAttribute('special'), target_sw=CPEAttribute('ipod_touch'), target_hw=CPEAttribute("80gb")) self.assertEqual(cpe.bind_to_fs(), r'cpe:2.3:a:foo\\bar:big\$money_2010:*:*:*:*:special:ipod_touch:80gb:*') class testunbind_fs(unittest.TestCase): def test_example1(self): cpe = CPE() cpe.unbind_fs('cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'microsoft') self.assertEqual(cpe.product.value, 'internet_explorer') self.assertEqual(cpe.version.value, '8\.0\.6001') self.assertEqual(cpe.update.value, "beta") self.assertTrue(cpe.edition.any) self.assertTrue(cpe.sw_edition.any) self.assertTrue(cpe.target_sw.any) self.assertTrue(cpe.target_hw.any) self.assertTrue(cpe.other.any) self.assertTrue(cpe.language.any) def test_example2(self): cpe = CPE() cpe.unbind_fs('cpe:2.3:a:microsoft:internet_explorer:8.*:sp?:*:*:*:*:*:*') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'microsoft') self.assertEqual(cpe.product.value, 'internet_explorer') self.assertEqual(cpe.version.value, '8\.*') self.assertEqual(cpe.update.value, "sp?") self.assertTrue(cpe.edition.any) self.assertTrue(cpe.sw_edition.any) self.assertTrue(cpe.target_sw.any) self.assertTrue(cpe.target_hw.any) self.assertTrue(cpe.other.any) self.assertTrue(cpe.language.any) def test_example3(self): cpe = CPE() cpe.unbind_fs('cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win2003:x64:*') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, 'hp') self.assertEqual(cpe.product.value, 'insight_diagnostics') self.assertEqual(cpe.version.value, '7\.4\.0\.1570') self.assertTrue(cpe.update.na) self.assertTrue(cpe.edition.any) self.assertEqual(cpe.sw_edition.value, "online") self.assertEqual(cpe.target_sw.value, "win2003") self.assertEqual(cpe.target_hw.value, "x64") self.assertTrue(cpe.other.any) self.assertTrue(cpe.language.any) self.assertRaises(ValueError, cpe.unbind_fs, 'cpe:2.3:a:hp:insight_diagnostics:7.4.*.1570:*:*:*:*:*:*') def test_example4(self): cpe = CPE() cpe.unbind_fs(r'cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*') self.assertEqual(cpe.part.value, 'a') self.assertEqual(cpe.vendor.value, r'foo\\bar') self.assertEqual(cpe.product.value, 'big\$money') self.assertEqual(cpe.version.value, '2010') self.assertTrue(cpe.update.any) self.assertTrue(cpe.edition.any) self.assertEqual(cpe.sw_edition.value, "special") self.assertEqual(cpe.target_sw.value, "ipod_touch") self.assertEqual(cpe.target_hw.value, "80gb") self.assertTrue(cpe.other.any) self.assertTrue(cpe.language.any)