Completed
Push — master ( b4b69f...c80324 )
by
unknown
12s queued 11s
created

tests.testPortConvert.FullTest.testCompressList()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
# $id$
0 ignored issues
show
Coding Style Naming introduced by
The name testPortConvert does not conform to the module naming conventions ((([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
# description:
3
# Test suites for Port manipulation.
4
#
5
# authors:
6
#   Juan Jose Nicola <[email protected]>
7
#
8
# copyright:
9
# copyright (c) 2018 greenbone networks gmbh
10
#
11
# this program is free software; you can redistribute it and/or
12
# modify it under the terms of the gnu general public license
13
# as published by the free software foundation; either version 2
14
# of the license, or (at your option) any later version.
15
#
16
# this program is distributed in the hope that it will be useful,
17
# but without any warranty; without even the implied warranty of
18
# merchantability or fitness for a particular purpose.  see the
19
# gnu general public license for more details.
20
#
21
# you should have received a copy of the gnu general public license
22
# along with this program; if not, write to the free software
23
# foundation, inc., 51 franklin st, fifth floor, boston, ma 02110-1301 usa.
24
from __future__ import print_function
25
26
import unittest
27
28
from ospd.misc import ports_as_list
29
from ospd.misc import get_udp_port_list
30
from ospd.misc import get_tcp_port_list
31
from ospd.misc import port_list_compress
32
33
class FullTest(unittest.TestCase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
34
35
    def testTcpPorts(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testTcpPorts does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
36
        """ Test only tcp ports."""
37
        tports, uports = ports_as_list('T:1-10,30,31')
38
        self.assertFalse(tports is None)
39
        self.assertEqual(len(uports), 0)
40
        self.assertEqual(len(tports), 12)
41
        for i in range(1, 10):
42
            self.assertTrue(i in tports)
43
        self.assertTrue(30 in tports)
44
        self.assertTrue(31 in tports)
45
46
    def testUdpPorts(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testUdpPorts does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
47
        """ Test only udp ports."""
48
        tports, uports = ports_as_list('U:1-10')
49
        self.assertFalse(uports is None)
50
        self.assertEqual(len(tports), 0)
51
        self.assertEqual(len(uports), 10)
52
        for i in range(1, 10):
53
            self.assertTrue(i in uports)
54
55
    def testBothPorts(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testBothPorts does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
56
        """ Test tcp und udp ports."""
57
        tports, uports = ports_as_list('T:1-10, U:1-10')
58
        self.assertFalse(tports is None)
59
        self.assertFalse(uports is None)
60
        self.assertEqual(len(tports), 10)
61
        self.assertEqual(len(uports), 10)
62
        for i in range(1, 10):
63
            self.assertTrue(i in tports)
64
            self.assertTrue(i in uports)
65
        self.assertFalse(0 in uports)
66
67
    def testBothPortsUdpfirst(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testBothPortsUdpfirst does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
68
        """ Test tcp und udp ports, but udp listed first."""
69
        tports, uports = ports_as_list('U:20-30, T:1-10')
70
        self.assertFalse(tports is None)
71
        self.assertFalse(uports is None)
72
        self.assertEqual(len(tports), 10)
73
        self.assertEqual(len(uports), 11)
74
        for i in range(1, 10):
75
            self.assertTrue(i in tports)
76
        for i in range(20, 30):
77
            self.assertTrue(i in uports)
78
79
    def testNotSpecTypePorts(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testNotSpecTypePorts does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
80
        """ Test port list without specific type. """
81
        tports, uports = ports_as_list('51-60')
82
        self.assertFalse(tports is None)
83
        self.assertEqual(len(uports), 0)
84
        self.assertEqual(len(tports), 10)
85
        for i in range(51, 60):
86
            self.assertTrue(i in tports)
87
88
    def testInvalidCharPort(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testInvalidCharPort does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
89
        """ Test list with a false char. """
90
        tports, uports = ports_as_list('R:51-60')
91
        self.assertTrue(tports is None)
92
        self.assertTrue(uports is None)
93
94
    def testEmptyPort(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testEmptyPort does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
95
        """ Test an empty port list. """
96
        tports, uports = ports_as_list('')
97
        self.assertTrue(tports is None)
98
        self.assertTrue(uports is None)
99
100
    def testGetSpecTypePorts(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testGetSpecTypePorts does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
101
        """ Test get specific type ports."""
102
        uports = get_udp_port_list('U:9392,9393T:22')
103
        self.assertEqual(len(uports), 2)
104
        self.assertTrue(9392 in uports)
0 ignored issues
show
introduced by
Value 'uports' doesn't support membership test
Loading history...
105
        tports = get_tcp_port_list('U:9392T:80,22,443')
106
        self.assertEqual(len(tports), 3)
107
        self.assertTrue(22 in tports)
0 ignored issues
show
introduced by
Value 'tports' doesn't support membership test
Loading history...
108
        self.assertTrue(80 in tports)
0 ignored issues
show
introduced by
Value 'tports' doesn't support membership test
Loading history...
109
        self.assertTrue(443 in tports)
0 ignored issues
show
introduced by
Value 'tports' doesn't support membership test
Loading history...
110
111
    def testMalformedPortStr(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testMalformedPortStr does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
112
        """ Test different malformed port list. """
113
        tports, uports = ports_as_list('TU:1-2')
114
        self.assertTrue(tports is None)
115
        self.assertTrue(uports is None)
116
117
        tports, uports = ports_as_list('U1-2')
118
        self.assertTrue(tports is None)
119
        self.assertTrue(uports is None)
120
121
        tports, uports = ports_as_list('U:1-2t:22')
122
        self.assertTrue(tports is None)
123
        self.assertTrue(uports is None)
124
125
        tports, uports = ports_as_list('U1-2,T22')
126
        self.assertTrue(tports is None)
127
        self.assertTrue(uports is None)
128
129
        tports, uports = ports_as_list('U:1-2,U:22')
130
        self.assertTrue(tports is None)
131
        self.assertTrue(uports is None)
132
133
134
    def testCompressList(self):
0 ignored issues
show
Coding Style Naming introduced by
The name testCompressList does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
135
        """ Test different malformed port list. """
136
        port_list = [1,2,3,4,5,8,9,10,22,24,29,30]
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
137
        string = port_list_compress(port_list)
138
        self.assertEqual(string, '1-5,8-10,22,24,29-30')
139