1
|
|
|
"""Test utils methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from kytos.lib.helpers import get_switch_mock, get_connection_mock |
6
|
|
|
from napps.kytos.of_core.utils import (of_slicer, _emit_message, _unpack_int, |
7
|
|
|
emit_message_in, emit_message_out) |
8
|
|
|
from napps.kytos.of_core.utils import GenericHello |
9
|
|
|
from tests.helpers import get_controller_mock |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestUtils(TestCase): |
13
|
|
|
"""Test utils.""" |
14
|
|
|
|
15
|
|
|
def setUp(self): |
16
|
|
|
"""Execute steps before each tests.""" |
17
|
|
|
self.mock_controller = get_controller_mock() |
18
|
|
|
self.mock_switch = get_switch_mock('00:00:00:00:00:00:00:01', 0x04) |
19
|
|
|
self.mock_connection = get_connection_mock(0x04, self.mock_switch) |
20
|
|
|
|
21
|
|
|
def test_of_slicer(self): |
22
|
|
|
"""Test of_slicer.""" |
23
|
|
|
data = b'\x04\x00\x00\x10\x00\x00\x00\x3e' |
24
|
|
|
data += b'\x00\x01\x00\x08\x00\x00\x00\x10' |
25
|
|
|
response = of_slicer(data) |
26
|
|
|
self.assertEqual(data, response[0][0]) |
27
|
|
|
self.assertCountEqual(response[1], []) |
28
|
|
|
|
29
|
|
|
def test_unpack_int(self): |
30
|
|
|
"""Test test_unpack_int.""" |
31
|
|
|
mock_packet = MagicMock() |
32
|
|
|
response = _unpack_int(mock_packet) |
33
|
|
|
self.assertEqual(int.from_bytes(mock_packet, |
34
|
|
|
byteorder='big'), response) |
35
|
|
|
|
36
|
|
|
@patch('napps.kytos.of_core.utils.KytosEvent') |
37
|
|
|
def test_emit_message(self, mock_event): |
38
|
|
|
"""Test emit_message.""" |
39
|
|
|
mock_message = MagicMock() |
40
|
|
|
_emit_message(self.mock_controller, self.mock_connection, mock_message, |
41
|
|
|
'in') |
42
|
|
|
mock_event.assert_called() |
43
|
|
|
|
44
|
|
|
_emit_message(self.mock_controller, self.mock_connection, mock_message, |
45
|
|
|
'out') |
46
|
|
|
mock_event.assert_called() |
47
|
|
|
|
48
|
|
|
@patch('napps.kytos.of_core.utils._emit_message') |
49
|
|
|
def test_emit_message_in_out(self, mock_message_in): |
50
|
|
|
"""Test emit_message in and out.""" |
51
|
|
|
|
52
|
|
|
emit_message_in(self.mock_controller, self.mock_connection, 'in') |
53
|
|
|
mock_message_in.assert_called() |
54
|
|
|
|
55
|
|
|
emit_message_out(self.mock_controller, self.mock_connection, 'in') |
56
|
|
|
mock_message_in.assert_called() |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
class TestGenericHello(TestCase): |
60
|
|
|
"""Test GenericHello.""" |
61
|
|
|
|
62
|
|
|
data = b'\x04\x00\x00\x10\x00\x00\x00\x00\x00\x01\x00\x08\x00\x00\x00\x10' |
63
|
|
|
|
64
|
|
|
@patch('napps.kytos.of_core.utils.OFPTYPE') |
65
|
|
|
def test_pack(self, mock_ofptype): |
66
|
|
|
"""Test pack.""" |
67
|
|
|
mock_ofptype.return_value = True |
68
|
|
|
generic = GenericHello(packet=self.data, versions=b'\x04') |
69
|
|
|
response = generic.pack() |
70
|
|
|
self.assertEqual(self.data, response) |
71
|
|
|
|