benoit@40: import unittest benoit@40: benoit@40: from farolluz.parsers.cpe import CPE, CPEAttribute benoit@40: benoit@40: class testbindToURI(unittest.TestCase): benoit@40: benoit@40: def test_example1(self): benoit@40: 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)) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:microsoft:internet_explorer:8.0.6001:beta') benoit@40: benoit@40: def test_example2(self): benoit@40: cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.*'), update=CPEAttribute('sp?')) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:microsoft:internet_explorer:8.%02:sp%01') benoit@40: benoit@40: def test_example3(self): benoit@40: 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")) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~') benoit@40: benoit@40: def test_example4(self): benoit@40: cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('openview_network_manager'), version=CPEAttribute(r'7\.51'), target_sw=CPEAttribute('linux')) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:hp:openview_network_manager:7.51::~~~linux~~') benoit@40: benoit@40: def test_example5(self): benoit@40: 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")) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:foo%5cbar:big%24money_manager_2010:::~~special~ipod_touch~80gb~') benoit@40: benoit@40: class testunbindURI(unittest.TestCase): benoit@40: benoit@40: def test_example1(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.0.6001:beta') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'microsoft') benoit@40: self.assertEqual(cpe.product.value, 'internet_explorer') benoit@40: self.assertEqual(cpe.version.value, r'8\.0\.6001') benoit@40: self.assertEqual(cpe.update.value, 'beta') benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example2(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.%2a:sp%3f') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'microsoft') benoit@40: self.assertEqual(cpe.product.value, 'internet_explorer') benoit@40: self.assertEqual(cpe.version.value, r'8\.\*') benoit@40: self.assertEqual(cpe.update.value, 'sp\?') benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example3(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.%02:sp%01') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'microsoft') benoit@40: self.assertEqual(cpe.product.value, 'internet_explorer') benoit@40: self.assertEqual(cpe.version.value, r'8\.*') benoit@40: self.assertEqual(cpe.update.value, 'sp?') benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example4(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_URI('cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'hp') benoit@40: self.assertEqual(cpe.product.value, 'insight_diagnostics') benoit@40: self.assertEqual(cpe.version.value, '7\.4\.0\.1570') benoit@40: self.assertTrue(cpe.update.any) benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertEqual(cpe.sw_edition.value, 'online') benoit@40: self.assertEqual(cpe.target_sw.value, 'win2003') benoit@40: self.assertEqual(cpe.target_hw.value, 'x64') benoit@40: self.assertTrue(cpe.other.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example5(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_URI('cpe:/a:hp:openview_network_manager:7.51:-:~~~linux~~') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'hp') benoit@40: self.assertEqual(cpe.product.value, 'openview_network_manager') benoit@40: self.assertEqual(cpe.version.value, '7\.51') benoit@40: self.assertTrue(cpe.update.na) benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.sw_edition.any) benoit@40: self.assertEqual(cpe.target_sw.value, 'linux') benoit@40: self.assertTrue(cpe.target_hw.any) benoit@40: self.assertTrue(cpe.other.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: benoit@40: def test_example6(self): benoit@40: cpe = CPE() benoit@40: self.assertRaises(KeyError, cpe.unbind_URI, 'cpe:/a:foo%5cbar:big%24money_2010%07:::~~special~ipod_touch~80gb~') benoit@40: benoit@40: benoit@40: def test_example7(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_URI('cpe:/a:foo~bar:big%7emoney_2010') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'foo\~bar') benoit@40: self.assertEqual(cpe.product.value, 'big\~money_2010') benoit@40: self.assertTrue(cpe.version.any) benoit@40: self.assertTrue(cpe.update.any) benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example8(self): benoit@40: cpe = CPE() benoit@40: self.assertRaises(ValueError, cpe.unbind_URI, 'cpe:/a:foo:bar:12.%02.1234') benoit@40: benoit@40: class testbindFS(unittest.TestCase): benoit@40: benoit@40: def test_example1(self): benoit@40: 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)) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*') benoit@40: benoit@40: def test_example2(self): benoit@40: cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.*'), update=CPEAttribute('sp?'), edition=CPEAttribute(any=True)) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.*:sp?:*:*:*:*:*:*') benoit@40: benoit@40: cpe.version = CPEAttribute(r'8\.\*') benoit@40: benoit@40: self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.\*:sp?:*:*:*:*:*:*') benoit@40: benoit@40: def test_example3(self): benoit@40: 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")) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:hp:insight:7.4.0.1570:-:*:*:online:win2003:x64:*') benoit@40: benoit@40: def test_example4(self): benoit@40: cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('openview_network_manager'), version=CPEAttribute(r'7\.51'), target_sw=CPEAttribute('linux')) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:hp:openview_network_manager:7.51:*:*:*:*:linux:*:*') benoit@40: benoit@40: def test_example5(self): benoit@40: 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")) benoit@40: benoit@40: self.assertEqual(cpe.bind_to_fs(), r'cpe:2.3:a:foo\\bar:big\$money_2010:*:*:*:*:special:ipod_touch:80gb:*') benoit@40: benoit@40: class testunbind_fs(unittest.TestCase): benoit@40: benoit@40: def test_example1(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_fs('cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'microsoft') benoit@40: self.assertEqual(cpe.product.value, 'internet_explorer') benoit@40: self.assertEqual(cpe.version.value, '8\.0\.6001') benoit@40: self.assertEqual(cpe.update.value, "beta") benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.sw_edition.any) benoit@40: self.assertTrue(cpe.target_sw.any) benoit@40: self.assertTrue(cpe.target_hw.any) benoit@40: self.assertTrue(cpe.other.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example2(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_fs('cpe:2.3:a:microsoft:internet_explorer:8.*:sp?:*:*:*:*:*:*') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'microsoft') benoit@40: self.assertEqual(cpe.product.value, 'internet_explorer') benoit@40: self.assertEqual(cpe.version.value, '8\.*') benoit@40: self.assertEqual(cpe.update.value, "sp?") benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertTrue(cpe.sw_edition.any) benoit@40: self.assertTrue(cpe.target_sw.any) benoit@40: self.assertTrue(cpe.target_hw.any) benoit@40: self.assertTrue(cpe.other.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: def test_example3(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_fs('cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win2003:x64:*') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, 'hp') benoit@40: self.assertEqual(cpe.product.value, 'insight_diagnostics') benoit@40: self.assertEqual(cpe.version.value, '7\.4\.0\.1570') benoit@40: self.assertTrue(cpe.update.na) benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertEqual(cpe.sw_edition.value, "online") benoit@40: self.assertEqual(cpe.target_sw.value, "win2003") benoit@40: self.assertEqual(cpe.target_hw.value, "x64") benoit@40: self.assertTrue(cpe.other.any) benoit@40: self.assertTrue(cpe.language.any) benoit@40: benoit@40: self.assertRaises(ValueError, cpe.unbind_fs, 'cpe:2.3:a:hp:insight_diagnostics:7.4.*.1570:*:*:*:*:*:*') benoit@40: benoit@40: benoit@40: def test_example4(self): benoit@40: cpe = CPE() benoit@40: cpe.unbind_fs(r'cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*') benoit@40: self.assertEqual(cpe.part.value, 'a') benoit@40: self.assertEqual(cpe.vendor.value, r'foo\\bar') benoit@40: self.assertEqual(cpe.product.value, 'big\$money') benoit@40: self.assertEqual(cpe.version.value, '2010') benoit@40: self.assertTrue(cpe.update.any) benoit@40: self.assertTrue(cpe.edition.any) benoit@40: self.assertEqual(cpe.sw_edition.value, "special") benoit@40: self.assertEqual(cpe.target_sw.value, "ipod_touch") benoit@40: self.assertEqual(cpe.target_hw.value, "80gb") benoit@40: self.assertTrue(cpe.other.any) benoit@40: self.assertTrue(cpe.language.any)