"Fossies" - the Fresh Open Source Software Archive

Member "ospd-2.0.1/tests/test_xml.py" (12 May 2020, 2627 Bytes) of package /linux/misc/openvas/ospd-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (C) 2020 Greenbone Networks GmbH
    2 #
    3 # SPDX-License-Identifier: GPL-2.0-or-later
    4 #
    5 # This program is free software; you can redistribute it and/or
    6 # modify it under the terms of the GNU General Public License
    7 # as published by the Free Software Foundation; either version 2
    8 # of the License, or (at your option) any later version.
    9 #
   10 # This program is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License
   16 # along with this program; if not, write to the Free Software
   17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   18 
   19 from unittest import TestCase
   20 
   21 from ospd.xml import escape_ctrl_chars
   22 
   23 from xml.etree.ElementTree import Element, tostring, fromstring
   24 
   25 
   26 class EscapeText(TestCase):
   27     def test_escape_xml_valid_text(self):
   28         text = 'this is a valid xml'
   29         res = escape_ctrl_chars(text)
   30 
   31         self.assertEqual(text, res)
   32 
   33     def test_escape_xml_invalid_char(self):
   34         text = 'End of transmission is not printable \x04.'
   35         res = escape_ctrl_chars(text)
   36         self.assertEqual(res, 'End of transmission is not printable \\x0004.')
   37 
   38         # Create element
   39         elem = Element('text')
   40         elem.text = res
   41         self.assertEqual(
   42             tostring(elem),
   43             b'<text>End of transmission is not printable \\x0004.</text>',
   44         )
   45 
   46         # The string format of the element does not break the xml.
   47         elem_as_str = tostring(elem, encoding='utf-8')
   48         new_elem = fromstring(elem_as_str)
   49         self.assertEqual(
   50             b'<text>' + new_elem.text.encode('utf-8') + b'</text>', elem_as_str
   51         )
   52 
   53     def test_escape_xml_printable_char(self):
   54         text = 'Latin Capital Letter A With Circumflex \xc2 is printable.'
   55         res = escape_ctrl_chars(text)
   56         self.assertEqual(
   57             res, 'Latin Capital Letter A With Circumflex  is printable.'
   58         )
   59 
   60         # Create the element
   61         elem = Element('text')
   62         elem.text = res
   63         self.assertEqual(
   64             tostring(elem),
   65             b'<text>Latin Capital Letter A With Circumflex &#194; is printable.</text>',
   66         )
   67 
   68         # The string format of the element does not break the xml
   69         elem_as_str = tostring(elem, encoding='utf-8')
   70         new_elem = fromstring(elem_as_str)
   71         self.assertEqual(
   72             b'<text>' + new_elem.text.encode('utf-8') + b'</text>', elem_as_str
   73         )