Completed
Push — master ( 8082e9...516ce7 )
by
unknown
17s queued 13s
created

ConvertTargetListsTestCase.test_range()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
# Copyright (C) 2014-2018 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
""" Test suites for Target manipulations.
20
"""
21
22
import unittest
23
24
from ospd.misc import (
25
    target_str_to_list,
26
    get_hostname_by_address,
27
    is_valid_address,
28
)
29
30
31
class ConvertTargetListsTestCase(unittest.TestCase):
32
    def test_24_net(self):
33
        addresses = target_str_to_list('195.70.81.0/24')
34
        self.assertFalse(addresses is None)
35
        self.assertEqual(len(addresses), 254)
36
        for i in range(1, 255):
37
            self.assertTrue('195.70.81.%d' % i in addresses)
38
39
    def test_range(self):
40
        addresses = target_str_to_list('195.70.81.1-10')
41
        self.assertFalse(addresses is None)
42
        self.assertEqual(len(addresses), 10)
43
        for i in range(1, 10):
44
            self.assertTrue('195.70.81.%d' % i in addresses)
45
46
    def test_get_hostname_by_address(self):
47
        hostname = get_hostname_by_address('127.0.0.1')
48
        self.assertEqual(hostname, 'localhost')
49
50
        hostname = get_hostname_by_address('')
51
        self.assertEqual(hostname, '')
52
53
        hostname = get_hostname_by_address('127.0.0.1111')
54
        self.assertEqual(hostname, '')
55
56
    def test_is_valid_address(self):
57
        self.assertFalse(is_valid_address(None))
58
        self.assertFalse(is_valid_address(''))
59
        self.assertFalse(is_valid_address('foo'))
60
        self.assertFalse(is_valid_address('127.0.0.1111'))
61
        self.assertFalse(is_valid_address('127.0.0,1'))
62
63
        self.assertTrue(is_valid_address('127.0.0.1'))
64
        self.assertTrue(is_valid_address('192.168.0.1'))
65
        self.assertTrue(is_valid_address('::1'))
66
        self.assertTrue(is_valid_address('fc00::'))
67
        self.assertTrue(is_valid_address('fec0::'))
68
        self.assertTrue(
69
            is_valid_address('2001:0db8:85a3:08d3:1319:8a2e:0370:7344')
70
        )
71