changeset 41:bb1dd2a55643

CPE: Add a generic 'parse' method
author Benoît Allard <benoit.allard@greenbone.net>
date Mon, 29 Dec 2014 14:58:29 +0100
parents 1d9b2b06067e
children 9ed24f48df01
files CHANGES farolluz/parsers/cpe.py tests/testCPE.py
diffstat 3 files changed, 81 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES	Mon Dec 29 14:30:39 2014 +0100
+++ b/CHANGES	Mon Dec 29 14:58:29 2014 +0100
@@ -1,7 +1,15 @@
+FarolLuz next
+=============
+
+Main changes since FarolLuz 1.0:
+--------------------------------
+* Add parsing for CPE.
+
+
 FarolLuz 1.0 (2014-11-05)
 =========================
 
-THis is the first public release of FarolLuz. FarolLuz is a set of library /
+This is the first public release of FarolLuz. FarolLuz is a set of library /
 utilities to manipulate Security Advisories. It is part of Farol, the Security
 Advisory Management Platform.
 
--- a/farolluz/parsers/cpe.py	Mon Dec 29 14:30:39 2014 +0100
+++ b/farolluz/parsers/cpe.py	Mon Dec 29 14:58:29 2014 +0100
@@ -1,3 +1,27 @@
+# -*- coding: utf-8 -*-
+# Description:
+# Methods for parsing CPEs
+#
+# 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.
+
 """\
 a cpe class to ease the creation of a producttree based on cpe
 
@@ -257,3 +281,13 @@
                 self.target_hw = v
             elif idx == 12:
                 self.other = v
+
+def parse(s):
+    cpe = CPE()
+    if s[:5] == 'cpe:/':
+        cpe.unbind_URI(s)
+    elif s[:8] == 'cpe:2.3:':
+        cpe.unbind_fs(s)
+    else:
+        raise ValueError(s)
+    return cpe
--- a/tests/testCPE.py	Mon Dec 29 14:30:39 2014 +0100
+++ b/tests/testCPE.py	Mon Dec 29 14:58:29 2014 +0100
@@ -1,6 +1,30 @@
+# -*- 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
+from farolluz.parsers.cpe import CPE, CPEAttribute, parse
 
 class testbindToURI(unittest.TestCase):
     
@@ -210,3 +234,16 @@
         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)