| 1 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # Copyright (C) 2018 Greenbone Networks GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # SPDX-License-Identifier: GPL-3.0-or-later | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | # You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | # along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | from collections import OrderedDict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | from gvm.xml import XmlCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class XmlCommandTestCase(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     def test_should_create_command(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         self.assertEqual(cmd.to_string(), '<foo/>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     def test_should_allow_to_add_element(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         cmd.add_element('bar') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self.assertEqual(cmd.to_string(), '<foo><bar/></foo>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     def test_should_allow_to_add_element_with_text(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         cmd.add_element('bar', '1') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self.assertEqual(cmd.to_string(), '<foo><bar>1</bar></foo>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     def test_should_allow_to_set_attribute(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         cmd.set_attribute('bar', '1') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self.assertEqual(cmd.to_string(), '<foo bar="1"/>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     def test_should_allow_to_set_attributes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         cmd.set_attributes(OrderedDict([('bar', '1'), ('baz', '2')])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         self.assertEqual(cmd.to_string(), '<foo bar="1" baz="2"/>') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     def test_should_convert_to_string(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         self.assertEqual(str(cmd), '<foo/>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     def test_invalid_xml(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         with self.assertRaises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             XmlCommand('Foo & Bar') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def test_xml_escaping(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         cmd.add_element('bar', 'Foo & Bar') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         self.assertEqual(cmd.to_string(), '<foo><bar>Foo & Bar</bar></foo>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         cmd.set_attribute('bar', 'Foo & Bar') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         self.assertEqual(cmd.to_string(), '<foo bar="Foo & Bar"/>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         cmd = XmlCommand('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         cmd.set_attribute('bar', 'Foo "Bar"') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         self.assertEqual(cmd.to_string(), '<foo bar="Foo "Bar""/>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | if __name__ == '__main__': | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     unittest.main() | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  |