|
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
|
|
|
""" Test suites for Target manipulations. |
|
19
|
|
|
""" |
|
20
|
|
|
|
|
21
|
|
|
import unittest |
|
22
|
|
|
from unittest.mock import patch |
|
23
|
|
|
|
|
24
|
|
|
from ospd.network import ( |
|
25
|
|
|
target_str_to_list, |
|
26
|
|
|
get_hostname_by_address, |
|
27
|
|
|
is_valid_address, |
|
28
|
|
|
target_to_ipv4, |
|
29
|
|
|
socket, |
|
30
|
|
|
) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class ConvertTargetListsTestCase(unittest.TestCase): |
|
34
|
|
|
def test_24_net(self): |
|
35
|
|
|
addresses = target_str_to_list('195.70.81.0/24') |
|
36
|
|
|
|
|
37
|
|
|
self.assertIsNotNone(addresses) |
|
38
|
|
|
self.assertEqual(len(addresses), 254) |
|
39
|
|
|
|
|
40
|
|
|
for i in range(1, 255): |
|
41
|
|
|
self.assertIn('195.70.81.%d' % i, addresses) |
|
42
|
|
|
|
|
43
|
|
|
def test_bad_ipv4_cidr(self): |
|
44
|
|
|
addresses = target_str_to_list('195.70.81.0/32') |
|
45
|
|
|
self.assertIsNotNone(addresses) |
|
46
|
|
|
self.assertEqual(len(addresses), 0) |
|
47
|
|
|
|
|
48
|
|
|
addresses = target_str_to_list('195.70.81.0/31') |
|
49
|
|
|
self.assertIsNotNone(addresses) |
|
50
|
|
|
self.assertEqual(len(addresses), 0) |
|
51
|
|
|
|
|
52
|
|
|
def test_good_ipv4_cidr(self): |
|
53
|
|
|
addresses = target_str_to_list('195.70.81.0/30') |
|
54
|
|
|
self.assertIsNotNone(addresses) |
|
55
|
|
|
self.assertEqual(len(addresses), 2) |
|
56
|
|
|
|
|
57
|
|
|
def test_range(self): |
|
58
|
|
|
addresses = target_str_to_list('195.70.81.0-10') |
|
59
|
|
|
|
|
60
|
|
|
self.assertIsNotNone(addresses) |
|
61
|
|
|
self.assertEqual(len(addresses), 11) |
|
62
|
|
|
|
|
63
|
|
|
for i in range(0, 10): |
|
64
|
|
|
self.assertIn('195.70.81.%d' % i, addresses) |
|
65
|
|
|
|
|
66
|
|
|
def test_target_str_with_trailing_comma(self): |
|
67
|
|
|
addresses = target_str_to_list(',195.70.81.1,195.70.81.2,') |
|
68
|
|
|
|
|
69
|
|
|
self.assertIsNotNone(addresses) |
|
70
|
|
|
self.assertEqual(len(addresses), 2) |
|
71
|
|
|
|
|
72
|
|
|
for i in range(1, 2): |
|
73
|
|
|
self.assertIn('195.70.81.%d' % i, addresses) |
|
74
|
|
|
|
|
75
|
|
|
def test_get_hostname_by_address(self): |
|
76
|
|
|
with patch.object(socket, "getfqdn", return_value="localhost"): |
|
77
|
|
|
hostname = get_hostname_by_address('127.0.0.1') |
|
78
|
|
|
self.assertEqual(hostname, 'localhost') |
|
79
|
|
|
|
|
80
|
|
|
hostname = get_hostname_by_address('') |
|
81
|
|
|
self.assertEqual(hostname, '') |
|
82
|
|
|
|
|
83
|
|
|
hostname = get_hostname_by_address('127.0.0.1111') |
|
84
|
|
|
self.assertEqual(hostname, '') |
|
85
|
|
|
|
|
86
|
|
|
def test_is_valid_address(self): |
|
87
|
|
|
self.assertFalse(is_valid_address(None)) |
|
88
|
|
|
self.assertFalse(is_valid_address('')) |
|
89
|
|
|
self.assertFalse(is_valid_address('foo')) |
|
90
|
|
|
self.assertFalse(is_valid_address('127.0.0.1111')) |
|
91
|
|
|
self.assertFalse(is_valid_address('127.0.0,1')) |
|
92
|
|
|
|
|
93
|
|
|
self.assertTrue(is_valid_address('127.0.0.1')) |
|
94
|
|
|
self.assertTrue(is_valid_address('192.168.0.1')) |
|
95
|
|
|
self.assertTrue(is_valid_address('::1')) |
|
96
|
|
|
self.assertTrue(is_valid_address('fc00::')) |
|
97
|
|
|
self.assertTrue(is_valid_address('fec0::')) |
|
98
|
|
|
self.assertTrue( |
|
99
|
|
|
is_valid_address('2001:0db8:85a3:08d3:1319:8a2e:0370:7344') |
|
100
|
|
|
) |
|
101
|
|
|
|
|
102
|
|
|
def test_target_to_ipv4(self): |
|
103
|
|
|
self.assertIsNone(target_to_ipv4('foo')) |
|
104
|
|
|
self.assertIsNone(target_to_ipv4('')) |
|
105
|
|
|
self.assertIsNone(target_to_ipv4('127,0,0,1')) |
|
106
|
|
|
self.assertIsNone(target_to_ipv4('127.0.0')) |
|
107
|
|
|
self.assertIsNone(target_to_ipv4('127.0.0.11111')) |
|
108
|
|
|
|
|
109
|
|
|
self.assertEqual(target_to_ipv4('127.0.0.1'), ['127.0.0.1']) |
|
110
|
|
|
self.assertEqual(target_to_ipv4('192.168.1.1'), ['192.168.1.1']) |
|
111
|
|
|
|