Issues (17)

tests/test_xml.py (2 issues)

1
# Copyright (C) 2014-2021 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: AGPL-3.0-or-later
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Affero General Public License as
7
# published by the Free Software Foundation, either version 3 of the
8
# 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 Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
from collections import OrderedDict
19
20
from unittest import TestCase
21
22
from xml.etree.ElementTree import Element, tostring, fromstring
23
24
from ospd.xml import elements_as_text, escape_ctrl_chars
25
26
27
class ElementsAsText(TestCase):
28
    def test_simple_element(self):
29
        elements = {'foo': 'bar'}
30
        text = elements_as_text(elements)
31
32
        self.assertEqual(text, '\t  foo                    bar\n')
33
34
    def test_simple_elements(self):
35
        elements = OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')])
36
        text = elements_as_text(elements)
37
38
        self.assertEqual(
39
            text,
40
            '\t  foo                    bar\n'
41
            '\t  lorem                  ipsum\n',
42
        )
43
44
    def test_elements(self):
45
        elements = OrderedDict(
46
            [
47
                ('foo', 'bar'),
48
                (
49
                    'lorem',
50
                    OrderedDict(
51
                        [
52
                            ('dolor', 'sit amet'),
53
                            ('consectetur', 'adipiscing elit'),
54
                        ]
55
                    ),
56
                ),
57
            ]
58
        )
59
        text = elements_as_text(elements)
60
61
        self.assertEqual(
62
            text,
63
            '\t  foo                    bar\n'
64
            '\t  lorem                  \n'
65
            '\t    dolor                  sit amet\n'
66
            '\t    consectetur            adipiscing elit\n',
67
        )
68
69
70
class EscapeText(TestCase):
71
    def test_escape_xml_valid_text(self):
72
        text = 'this is a valid xml'
73
        res = escape_ctrl_chars(text)
74
75
        self.assertEqual(text, res)
76
77 View Code Duplication
    def test_escape_xml_invalid_char(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
78
        text = 'End of transmission is not printable \x04.'
79
        res = escape_ctrl_chars(text)
80
        self.assertEqual(res, 'End of transmission is not printable \\x0004.')
81
82
        # Create element
83
        elem = Element('text')
84
        elem.text = res
85
        self.assertEqual(
86
            tostring(elem),
87
            b'<text>End of transmission is not printable \\x0004.</text>',
88
        )
89
90
        # The string format of the element does not break the xml.
91
        elem_as_str = tostring(elem, encoding='utf-8')
92
        new_elem = fromstring(elem_as_str)
93
        self.assertEqual(
94
            b'<text>' + new_elem.text.encode('utf-8') + b'</text>', elem_as_str
95
        )
96
97 View Code Duplication
    def test_escape_xml_printable_char(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
98
        text = 'Latin Capital Letter A With Circumflex \xc2 is printable.'
99
        res = escape_ctrl_chars(text)
100
        self.assertEqual(
101
            res, 'Latin Capital Letter A With Circumflex  is printable.'
102
        )
103
104
        # Create the element
105
        elem = Element('text')
106
        elem.text = res
107
        self.assertEqual(
108
            tostring(elem),
109
            b'<text>Latin Capital Letter A With Circumflex &#194; is '
110
            b'printable.</text>',
111
        )
112
113
        # The string format of the element does not break the xml
114
        elem_as_str = tostring(elem, encoding='utf-8')
115
        new_elem = fromstring(elem_as_str)
116
        self.assertEqual(
117
            b'<text>' + new_elem.text.encode('utf-8') + b'</text>', elem_as_str
118
        )
119