Completed
Pull Request — master (#498)
by
unknown
02:55
created

IsValidIpActionTestCase.test_run_valid_ip_v4()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
15
from networking_utils_base_test_case import NetworkingUtilsBaseActionTestCase
16
17
from is_valid_ip import IsValidIpAction
18
19
__all__ = [
20
    'IsValidIpActionTestCase'
21
]
22
23
24
class IsValidIpActionTestCase(NetworkingUtilsBaseActionTestCase):
25
    __test__ = True
26
    action_cls = IsValidIpAction
27
28
    def test_run_valid_ip_v4(self):
29
        expected = {'is_link_local': False,
30
                    'is_loopback': False,
31
                    'is_multicast': False,
32
                    'is_private': True,
33
                    'is_reserved': False,
34
                    'is_unspecified': False,
35
                    'reverse_pointer': '1.0.168.192.in-addr.arpa',
36
                    'version': 4}
37
38
        action = self.get_action_instance()
39
40
        result = action.run("192.168.0.1")
41
        self.assertEqual(result, expected)
42
43
    def test_run_valid_ip_v6(self):
44
        expected = {'is_link_local': False,
45
                    'is_loopback': False,
46
                    'is_multicast': False,
47
                    'is_private': False,
48
                    'is_reserved': True,
49
                    'is_unspecified': False,
50
                    'reverse_pointer': '8.1.f.a.1.0.0.0.3.0.b.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.6.7.1.ip6.arpa',  # noqa
51
                    'version': 6}
52
53
        action = self.get_action_instance()
54
        result = action.run("1762:0:0:0:0:B03:1:AF18")
55
        self.assertEqual(result, expected)
56
57
    def test_run_invalid_ip_v6(self):
58
        action = self.get_action_instance()
59
60
        self.assertRaises(ValueError,
61
                          action.run,
62
                          "1762:%:0:0:0:B03:1:AF18")
63
64
    def test_run_ipv4_loopback_invalid_with_no_loopback(self):
65
        action = self.get_action_instance()
66
67
        self.assertRaises(ValueError,
68
                          action.run,
69
                          "127.0.0.1",
70
                          no_loopback=True)
71
72
    def test_run_ipv6_loopback_invalid_with_no_loopback(self):
73
        action = self.get_action_instance()
74
75
        self.assertRaises(ValueError,
76
                          action.run,
77
                          "::1",
78
                          no_loopback=True)
79
80
    def test_run_ipv4_valid_with_only_v4(self):
81
        expected = {'is_link_local': False,
82
                    'is_loopback': False,
83
                    'is_multicast': False,
84
                    'is_private': True,
85
                    'is_reserved': False,
86
                    'is_unspecified': False,
87
                    'reverse_pointer': '1.0.168.192.in-addr.arpa',
88
                    'version': 4}
89
90
        action = self.get_action_instance()
91
        result = action.run("192.168.0.1",
92
                            only_v4=True)
93
        self.assertEqual(result, expected)
94
95
    def test_run_ipv6_invalid_with_only_v4(self):
96
        action = self.get_action_instance()
97
98
        self.assertRaises(ValueError,
99
                          action.run,
100
                          "1762:0:0:0:0:B03:1:AF18",
101
                          only_v4=True)
102
103
    def test_run_ipv6_valid_with_only_v6(self):
104
        expected = {'is_link_local': False,
105
                    'is_loopback': False,
106
                    'is_multicast': False,
107
                    'is_private': False,
108
                    'is_reserved': True,
109
                    'is_unspecified': False,
110
                    'reverse_pointer': '8.1.f.a.1.0.0.0.3.0.b.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.6.7.1.ip6.arpa',  # noqa
111
                    'version': 6}
112
113
        action = self.get_action_instance()
114
        result = action.run("1762:0:0:0:0:B03:1:AF18",
115
                            only_v6=True)
116
        self.assertEqual(result, expected)
117
118
    def test_run_ipv4_invalid_with_only_v6(self):
119
        action = self.get_action_instance()
120
121
        self.assertRaises(ValueError,
122
                          action.run,
123
                          "127.0.0.1",
124
                          only_v6=True)
125