TestTracePkt.test_prepare_next_packet_no_vlan()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 24
nop 2
dl 0
loc 40
ccs 23
cts 23
cp 1
crap 1
rs 9.304
c 0
b 0
f 0
1
"""
2
    Test tracing.trace_entries
3
"""
4
5 1
from unittest.mock import MagicMock, patch
6 1
from pyof.foundation.network_types import Ethernet
7 1
import pytest
8
9 1
from napps.amlight.sdntrace.tracing import trace_pkt
10 1
from napps.amlight.sdntrace.shared.switches import Switches
11 1
from napps.amlight.sdntrace.tracing.trace_entries import TraceEntries
12 1
from kytos.lib.helpers import (
13
    get_interface_mock,
14
    get_link_mock,
15
    get_switch_mock,
16
    get_controller_mock,
17
)
18
19
20
# pylint: disable=too-many-public-methods, too-many-lines, protected-access
21 1
class TestTracePkt:
22
    """Test all combinations for DPID"""
23
24 1
    @pytest.fixture(autouse=True)
25 1
    def commomn_patches(self, request):
26
        """This function handles setup and cleanup for patches"""
27
        # This fixture sets up the common patches,
28
        # and a finalizer is added using addfinalizer to stop
29
        # the common patches after each test. This ensures that the cleanup
30
        # is performed after each test, and additional patch decorators
31
        # can be used within individual test functions.
32
33 1
        patcher = patch("kytos.core.helpers.run_on_thread", lambda x: x)
34 1
        mock_patch = patcher.start()
35
36 1
        _ = request.function.__name__
37
38 1
        def cleanup():
39 1
            patcher.stop()
40
41 1
        request.addfinalizer(cleanup)
42 1
        return mock_patch
43
44 1
    def setup_method(self):
45
        """Set up before each test method"""
46 1
        self.create_basic_switches(get_controller_mock())
47
48 1 View Code Duplication
    @classmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
49 1
    def create_basic_switches(cls, controller):
50
        """Create basic mock switches for Kytos controller."""
51 1
        dpid_a = "00:00:00:00:00:00:00:01"
52 1
        dpid_b = "00:00:00:00:00:00:00:02"
53
54 1
        mock_switch_a = get_switch_mock(dpid_a, 0x04)
55 1
        mock_switch_b = get_switch_mock(dpid_b, 0x04)
56 1
        mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a)
57 1
        mock_interface_b = get_interface_mock("s2-eth1", 1, mock_switch_b)
58
59 1
        mock_link = get_link_mock(mock_interface_a, mock_interface_b)
60 1
        mock_link.id = "cf0f4071be4"
61 1
        mock_switch_a.id = dpid_a
62 1
        mock_switch_a.as_dict.return_value = {"metadata": {}}
63 1
        mock_switch_b.id = dpid_b
64 1
        mock_switch_b.as_dict.return_value = {"metadata": {}}
65
66 1
        controller.switches = {dpid_a: mock_switch_a, dpid_b: mock_switch_b}
67
68 1
        Switches(MagicMock())._switches = controller.switches
69
70 1 View Code Duplication
    @patch("napps.amlight.sdntrace.shared.extd_nw_types.randrange")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
71 1
    def test_generate_trace_pkt_tcp(self, mock_rand):
72
        """Test trace manager new trace creation."""
73 1
        mock_rand.return_value = 0
74 1
        eth = {"dl_vlan": 100}
75 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
76 1
        ip = {"nw_proto": 6}
77 1
        tp = {"tp_src": 1, "tp_dst": 2}
78 1
        switch = {"switch": dpid, "eth": eth, "ip": ip, "tp": tp}
79 1
        entries = {"trace": switch}
80
81 1
        trace_entries = TraceEntries()
82 1
        trace_entries.load_entries(entries)
83 1
        r_id = 999
84 1
        step = 9
85
86
        # Coloring REST response
87 1
        color = {"color_value": "ee:ee:ee:ee:ee:01"}
88
89
        # new_trace does not check duplicated request.
90 1
        in_port, pkt = trace_pkt.generate_trace_pkt(trace_entries, color, r_id, step)
91
92 1
        assert entries["trace"]["switch"]["in_port"] == in_port
93 1
        assert (
94
            pkt == b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee\x01"
95
            b"\x81\x00\x00d\x08\x00E\x00\x00\x8f\x00\x00\x00\x00\xff\x06"
96
            b"\xb7d\x01\x01\x01\x01\x01\x01\x01\x02\x00\x01\x00\x02\x00"
97
            b"\x00\x00\x00\x00\x00\x00\x00P\x02\x00S\x86G\x00\x00\x80\x04"
98
            b"\x95\\\x00\x00\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace"
99
            b".tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)\x81\x94}"
100
            b"\x94(\x8c\x0b_request_id\x94M\xe7\x03\x8c\x05_step\x94K\tub."
101
        )
102
103 1 View Code Duplication
    @patch("napps.amlight.sdntrace.shared.extd_nw_types.randrange")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
104 1
    def test_generate_trace_pkt_udp(self, mock_rand):
105
        """Test trace manager new trace creation."""
106 1
        mock_rand.return_value = 0
107 1
        eth = {"dl_vlan": 100}
108 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
109 1
        ip = {"nw_proto": 17}
110 1
        tp = {"tp_src": 1, "tp_dst": 2}
111 1
        switch = {"switch": dpid, "eth": eth, "ip": ip, "tp": tp}
112 1
        entries = {"trace": switch}
113
114 1
        trace_entries = TraceEntries()
115 1
        trace_entries.load_entries(entries)
116 1
        r_id = 999
117 1
        step = 9
118
119
        # Coloring REST response
120 1
        color = {"color_value": "ee:ee:ee:ee:ee:01"}
121
122
        # new_trace does not check duplicated request.
123 1
        in_port, pkt = trace_pkt.generate_trace_pkt(trace_entries, color, r_id, step)
124 1
        assert entries["trace"]["switch"]["in_port"] == in_port
125 1
        assert (
126
            pkt == b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee\x01\x81"
127
            b"\x00\x00d\x08\x00E\x00\x00\x83\x00\x00\x00\x00\xff\x11\xb7e"
128
            b"\x01\x01\x01\x01\x01\x01\x01\x02\x00\x01\x00\x02\x00o\xd6."
129
            b"\x80\x04\x95\\\x00\x00\x00\x00\x00\x00\x00\x8c(napps.amlight"
130
            b".sdntrace.tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
131
            b"\x81\x94}\x94(\x8c\x0b_request_id\x94M\xe7\x03\x8c\x05_step"
132
            b"\x94K\tub."
133
        )
134
135 1
    @patch("napps.amlight.sdntrace.shared.colors.Colors.get_switch_color")
136 1
    def test_get_node_color_from_dpid(self, mock_switch_colors):
137
        """Test get color from dpid."""
138 1
        mock_switch_colors.return_value = "ee:ee:ee:ee:ee:01"
139
140 1
        switch, color = trace_pkt._get_node_color_from_dpid("00:00:00:00:00:00:00:01")
141
142 1
        assert switch.dpid == "00:00:00:00:00:00:00:01"
143 1
        assert color == "ee:ee:ee:ee:ee:01"
144 1
        mock_switch_colors.assert_called_once()
145
146 1
    def test_get_node_color_unknown_dpid(self):
147
        """Test get color from unknown dpid."""
148 1
        switch, color = trace_pkt._get_node_color_from_dpid("99:99:99:99:99:99:99:99")
149
150 1
        assert switch == 0
151 1
        assert color == 0
152
153 1
    def test_get_vlan_from_pkt(self):
154
        """Test get color from unknown dpid."""
155 1
        pkt = (
156
            b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee"
157
            b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00"
158
            b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01"
159
            b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00"
160
            b"\x00\x00\x8c(napps.amlight.sdntrace.tracing."
161
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
162
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7"
163
            b"\x03sb."
164
        )
165
166 1
        vid = trace_pkt._get_vlan_from_pkt(pkt)
167
168 1
        assert vid == 100
169
170 1
    def test_process_packet(self):
171
        """Test process packet to find the trace message."""
172 1
        pkt = (
173
            b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee"
174
            b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00"
175
            b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01"
176
            b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00"
177
            b"\x00\x00\x8c(napps.amlight.sdntrace.tracing."
178
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
179
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7"
180
            b"\x03sb."
181
        )
182 1
        expected = (
183
            b"\x80\x04\x95Q\x00\x00\x00\x00\x00"
184
            b"\x00\x00\x8c(napps.amlight.sdntrace.tracing."
185
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
186
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7"
187
            b"\x03sb."
188
        )
189
190 1
        ethernet = Ethernet()
191 1
        ethernet.unpack(pkt)
192
193 1
        trace_msg = trace_pkt.process_packet(ethernet)
194 1
        assert trace_msg == expected
195
196 1
    def test_process_packet_tcp(self):
197
        """Test process TCP packet to find the trace message."""
198 1
        pkt = (
199
            b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee"
200
            b"\x01\x81\x00\x00d\x08\x00E\x00\x00\x84\x00\x00\x00"
201
            b"\x00\xff\x06\xb7o\x01\x01\x01\x01\x01\x01\x01\x02"
202
            b"\x00\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00P"
203
            b"\x02\x00\x00\xb5\x9f\x00\x00\x80\x04\x95Q\x00\x00"
204
            b"\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace."
205
            b"tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
206
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7\x03sb."
207
        )
208 1
        expected = (
209
            b"\x80\x04\x95Q\x00\x00\x00\x00\x00"
210
            b"\x00\x00\x8c(napps.amlight.sdntrace.tracing."
211
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
212
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7"
213
            b"\x03sb."
214
        )
215 1
        ethernet = Ethernet()
216 1
        ethernet.unpack(pkt)
217 1
        trace_msg = trace_pkt.process_packet(ethernet)
218 1
        assert trace_msg == expected
219
220 1
    def test_process_packet_udp(self):
221
        """Test process UDP packet to find the trace message."""
222 1
        pkt = (
223
            b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee"
224
            b"\x01\x81\x00\x00d\x08\x00E\x00\x00x\x00\x00\x00\x00"
225
            b"\xff\x11\xb7p\x01\x01\x01\x01\x01\x01\x01\x02\x00"
226
            b"\x01\x00\x02\x00\x08\x05\x9b\x80\x04\x95Q\x00\x00"
227
            b"\x00\x00\x00\x00\x00\x8c(napps.amlight.sdntrace.tracing."
228
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)\x81\x94}\x94"
229
            b"\x8c\x0b_request_id\x94M\xe7\x03sb."
230
        )
231 1
        expected = (
232
            b"\x80\x04\x95Q\x00\x00\x00\x00\x00"
233
            b"\x00\x00\x8c(napps.amlight.sdntrace.tracing."
234
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
235
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7"
236
            b"\x03sb."
237
        )
238 1
        ethernet = Ethernet()
239 1
        ethernet.unpack(pkt)
240 1
        trace_msg = trace_pkt.process_packet(ethernet)
241 1
        assert trace_msg == expected
242
243 1
    @patch("napps.amlight.sdntrace.tracing.trace_pkt._get_node_color_from_dpid")
244 1
    def test_prepare_next_packet(self, mock_get_color):
245
        """Test trace prepare next packet."""
246 1
        color_switch = MagicMock()
247 1
        color_switch.dpid = "00:00:00:00:00:00:00:01"
248 1
        mock_get_color.return_value = [color_switch, "ee:ee:ee:ee:ee:01"]
249
250 1
        eth = {"dl_vlan": 100}
251 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
252 1
        switch = {"switch": dpid, "eth": eth}
253 1
        entries = {"trace": switch}
254
255 1
        trace_entries = TraceEntries()
256 1
        trace_entries.load_entries(entries)
257
258 1
        raw = (
259
            b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee"
260
            b"\x01\x81\x00\x00d\x08\x00E\x00\x00p\x00\x00"
261
            b"\x00\x00\xff\x00\xb7\x89\x01\x01\x01\x01\x01"
262
            b"\x01\x01\x02\x80\x04\x95Q\x00\x00\x00\x00\x00"
263
            b"\x00\x00\x8c(napps.amlight.sdntrace.tracing."
264
            b"trace_msg\x94\x8c\x08TraceMsg\x94\x93\x94)"
265
            b"\x81\x94}\x94\x8c\x0b_request_id\x94M\xe7"
266
            b"\x03sb."
267
        )
268
269 1
        packet_in_msg = MagicMock()
270 1
        packet_in_msg.data.value = raw
271 1
        packet_in_msg.in_port.value = 1
272 1
        packet_in_msg.header.version = 1
273
274 1
        event = MagicMock()
275 1
        event.source.switch = "00:00:00:00:00:00:00:02"
276 1
        event.content = {"message": packet_in_msg}
277
278 1
        pkt_in = {
279
            "dpid": "00:00:00:00:00:00:00:01",
280
            "in_port": 1,
281
            "msg": "",
282
            "ethernet": "",
283
            "event": event,
284
        }
285 1
        result = {"dpid": pkt_in["dpid"], "port": pkt_in["in_port"]}
286
287
        # result = [result_trace, result_color, result_switch]
288 1
        result = trace_pkt.prepare_next_packet(trace_entries, result, event)
289 1
        assert result[0] == trace_entries
290 1
        assert result[1] == (mock_get_color())[1]
291 1
        assert result[2].dpid == color_switch.dpid
292
293 1
    @patch("napps.amlight.sdntrace.tracing.trace_pkt._get_node_color_from_dpid")
294 1
    def test_prepare_next_packet_no_vlan(self, mock_get_color):
295
        """Test trace prepare next packet."""
296 1
        color_switch = MagicMock()
297 1
        color_switch.dpid = "00:00:00:00:00:00:00:01"
298 1
        mock_get_color.return_value = [color_switch, "ee:ee:ee:ee:ee:01"]
299 1
        ip = {"nw_src": "0.0.0.1", "nw_dst": "0.0.0.2", "nw_tos": 5, "nw_proto": 6}
300 1
        tp = {"tp_src": 1, "tp_dst": 2}
301 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
302 1
        switch = {"switch": dpid, "ip": ip, "tp": tp}
303 1
        entries = {"trace": switch}
304
305 1
        trace_entries = TraceEntries()
306 1
        trace_entries.load_entries(entries)
307
308 1
        raw = (
309
            b"\xca\xfe\xca\xfe\xca\xfe\xee\xee\xee\xee\xee"
310
            b"\x01\x08\x00E\x14\x00\x84\x00\x00\x00\x00\xff"
311
            b"\x06\xbb]\x00\x00\x00\x01\x00\x00\x00\x02\x00"
312
            b"\x01\x00\x02\x19,Jh\x00\x00\x00\x00P\x02\x00"
313
            b"\x00\xe4\xbd\x00\x00\x80\x04\x95Q\x00\x00\x00"
314
            b"\x00\x00\x00\x00\x8c(napps.amlight.sdntrace."
315
            b"tracing.trace_msg\x94\x8c\x08TraceMsg\x94\x93"
316
            b"\x94)\x81\x94}\x94\x8c\x0b_request_id\x94M6usb."
317
        )
318
319 1
        packet_in_msg = MagicMock()
320 1
        packet_in_msg.data.value = raw
321 1
        packet_in_msg.in_port.value = 1
322 1
        packet_in_msg.header.version = 1
323
324 1
        event = MagicMock()
325 1
        event.source.switch = "00:00:00:00:00:00:00:02"
326 1
        event.content = {"message": packet_in_msg}
327
328 1
        result = {"dpid": "00:00:00:00:00:00:00:01"}
329
330 1
        result = trace_pkt.prepare_next_packet(trace_entries, result, event)
331
332
        assert result[0].dl_vlan == 0
333