1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2021 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 gvm.utils import check_command_status |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class TestCheckCommandStatus(unittest.TestCase): |
25
|
|
|
def test_check_command_status_empty(self): |
26
|
|
|
with self.assertLogs('gvm.utils', level="ERROR") as error_logger: |
27
|
|
|
false = check_command_status(xml=False) |
28
|
|
|
self.assertFalse(false) |
29
|
|
|
|
30
|
|
|
false = check_command_status(xml=0) |
31
|
|
|
self.assertFalse(false) |
32
|
|
|
self.assertEqual( |
33
|
|
|
error_logger.output, |
34
|
|
|
[ |
35
|
|
|
"ERROR:gvm.utils:XML Command is empty.", |
36
|
|
|
"ERROR:gvm.utils:XML Command is empty.", |
37
|
|
|
], |
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
def test_check_command_status_failed(self): |
41
|
|
|
response = ( |
42
|
|
|
'<authenticatio status="400" ><asaaa<fsd' |
43
|
|
|
'fsn_response status_text="Auth failed"/>' |
44
|
|
|
) |
45
|
|
|
with self.assertLogs('gvm.utils', level="ERROR") as error_logger: |
46
|
|
|
self.assertFalse(check_command_status(xml=response)) |
47
|
|
|
self.assertEqual( |
48
|
|
|
error_logger.output, |
49
|
|
|
[ |
50
|
|
|
'ERROR:gvm.utils:etree.XML(xml): error parsing attribute name, ' |
51
|
|
|
'line 1, column 36 (<string>, line 1)' |
52
|
|
|
], |
53
|
|
|
) |
54
|
|
|
|
55
|
|
|
def test_check_command_status_error(self): |
56
|
|
|
response = '<a><b/></a>' |
57
|
|
|
with self.assertLogs('gvm.utils', level="ERROR") as error_logger: |
58
|
|
|
self.assertFalse(check_command_status(xml=response)) |
59
|
|
|
self.assertEqual( |
60
|
|
|
error_logger.output, |
61
|
|
|
[ |
62
|
|
|
'ERROR:gvm.utils:Not received an ' |
63
|
|
|
'status code within the response.' |
64
|
|
|
], |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
def test_check_command_status(self): |
68
|
|
|
response = ( |
69
|
|
|
'<authentication status="200" status_text="Auth successfully"/>' |
70
|
|
|
) |
71
|
|
|
self.assertTrue(check_command_status(xml=response)) |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
if __name__ == '__main__': |
75
|
|
|
unittest.main() |
76
|
|
|
|