benoit@41: # -*- coding: utf-8 -*- benoit@41: # Description: benoit@41: # Tests for the CPE parsing methods benoit@41: # benoit@41: # Authors: benoit@41: # BenoƮt Allard benoit@41: # benoit@41: # Copyright: benoit@41: # Copyright (C) 2014 Greenbone Networks GmbH benoit@41: # benoit@41: # This program is free software; you can redistribute it and/or benoit@41: # modify it under the terms of the GNU General Public License benoit@41: # as published by the Free Software Foundation; either version 2 benoit@41: # of the License, or (at your option) any later version. benoit@41: # benoit@41: # This program is distributed in the hope that it will be useful, benoit@41: # but WITHOUT ANY WARRANTY; without even the implied warranty of benoit@41: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the benoit@41: # GNU General Public License for more details. benoit@41: # benoit@41: # You should have received a copy of the GNU General Public License benoit@41: # along with this program; if not, write to the Free Software benoit@41: # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. benoit@41: benoit@40: import unittest benoit@40: benoit@41: from farolluz.parsers.cpe import CPE, CPEAttribute, parse 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) benoit@41: benoit@41: class testParse(unittest.TestCase): benoit@41: benoit@41: def testURI(self): benoit@41: cpe = parse('cpe:/a:fogproject:fog:0.31') benoit@41: self.assertEqual(cpe.part.value, 'a') benoit@41: benoit@41: def testFS(self): benoit@41: cpe = parse('cpe:2.3:a:tenable:web_ui:2.3.3:*:*:*:*:*:*') benoit@41: self.assertEqual(cpe.vendor.value, 'tenable') benoit@41: benoit@41: def testGarbage(self): benoit@41: self.assertRaises(ValueError, parse, 'garbage')