view tests/testCPE.py @ 55:7653ffbec88b tip

Update setup.py
author Benoît Allard <benoit.allard@greenbone.net>
date Wed, 07 Jan 2015 12:02:23 +0100
parents bb1dd2a55643
children
line wrap: on
line source
# -*- coding: utf-8 -*-
# Description:
# Tests for the CPE parsing methods
#
# Authors:
# BenoƮt Allard <benoit.allard@greenbone.net>
#
# Copyright:
# Copyright (C) 2014 Greenbone Networks GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

import unittest

from farolluz.parsers.cpe import CPE, CPEAttribute, parse

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)

class testParse(unittest.TestCase):

    def testURI(self):
        cpe = parse('cpe:/a:fogproject:fog:0.31')
        self.assertEqual(cpe.part.value, 'a')

    def testFS(self):
        cpe = parse('cpe:2.3:a:tenable:web_ui:2.3.3:*:*:*:*:*:*')
        self.assertEqual(cpe.vendor.value, 'tenable')

    def testGarbage(self):
        self.assertRaises(ValueError, parse, 'garbage')
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)