-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
connectionstate_test.py
144 lines (127 loc) · 5.69 KB
/
connectionstate_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Unit test for KNX/IP ConnectionState Request/Response."""
import asyncio
import unittest
from unittest.mock import patch
from xknx import XKNX
from xknx.io import UDPClient
from xknx.io.request_response import ConnectionState
from xknx.knxip import (
HPAI,
ConnectionStateRequest,
ConnectionStateResponse,
ErrorCode,
KNXIPFrame,
KNXIPServiceType,
)
class TestConnectionState(unittest.TestCase):
"""Test class for xknx/io/ConnectionState objects."""
def setUp(self):
"""Set up test class."""
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def tearDown(self):
"""Tear down test class."""
self.loop.close()
def test_connectionstate(self):
"""Test connectionstateing from KNX bus."""
xknx = XKNX()
communication_channel_id = 23
udp_client = UDPClient(xknx, ("192.168.1.1", 0), ("192.168.1.2", 1234))
connectionstate = ConnectionState(
xknx, udp_client, communication_channel_id, route_back=False
)
connectionstate.timeout_in_seconds = 0
self.assertEqual(
connectionstate.awaited_response_class, ConnectionStateResponse
)
self.assertEqual(
connectionstate.communication_channel_id, communication_channel_id
)
# Expected KNX/IP-Frame:
exp_knxipframe = KNXIPFrame.init_from_body(
ConnectionStateRequest(
xknx,
communication_channel_id=communication_channel_id,
control_endpoint=HPAI(ip_addr="192.168.1.3", port=4321),
)
)
with patch("xknx.io.UDPClient.send") as mock_udp_send, patch(
"xknx.io.UDPClient.getsockname"
) as mock_udp_getsockname:
mock_udp_getsockname.return_value = ("192.168.1.3", 4321)
self.loop.run_until_complete(connectionstate.start())
mock_udp_send.assert_called_with(exp_knxipframe)
# Response KNX/IP-Frame with wrong type
wrong_knxipframe = KNXIPFrame(xknx)
wrong_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_REQUEST)
with patch("logging.Logger.warning") as mock_warning:
connectionstate.response_rec_callback(wrong_knxipframe, None)
mock_warning.assert_called_with("Could not understand knxipframe")
# Response KNX/IP-Frame with error:
err_knxipframe = KNXIPFrame(xknx)
err_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE)
err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID
with patch("logging.Logger.debug") as mock_warning:
connectionstate.response_rec_callback(err_knxipframe, None)
mock_warning.assert_called_with(
"Error: KNX bus responded to request of type '%s' with error in '%s': %s",
type(connectionstate).__name__,
type(err_knxipframe.body).__name__,
ErrorCode.E_CONNECTION_ID,
)
# Correct Response KNX/IP-Frame:
res_knxipframe = KNXIPFrame(xknx)
res_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE)
connectionstate.response_rec_callback(res_knxipframe, None)
self.assertTrue(connectionstate.success)
def test_connectionstate_route_back_true(self):
"""Test connectionstateing from KNX bus."""
xknx = XKNX()
communication_channel_id = 23
udp_client = UDPClient(xknx, ("192.168.1.1", 0), ("192.168.1.2", 1234))
connectionstate = ConnectionState(
xknx, udp_client, communication_channel_id, route_back=True
)
connectionstate.timeout_in_seconds = 0
self.assertEqual(
connectionstate.awaited_response_class, ConnectionStateResponse
)
self.assertEqual(
connectionstate.communication_channel_id, communication_channel_id
)
# Expected KNX/IP-Frame:
exp_knxipframe = KNXIPFrame.init_from_body(
ConnectionStateRequest(
xknx,
communication_channel_id=communication_channel_id,
)
)
with patch("xknx.io.UDPClient.send") as mock_udp_send, patch(
"xknx.io.UDPClient.getsockname"
) as mock_udp_getsockname:
mock_udp_getsockname.return_value = ("192.168.1.3", 4321)
self.loop.run_until_complete(connectionstate.start())
mock_udp_send.assert_called_with(exp_knxipframe)
# Response KNX/IP-Frame with wrong type
wrong_knxipframe = KNXIPFrame(xknx)
wrong_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_REQUEST)
with patch("logging.Logger.warning") as mock_warning:
connectionstate.response_rec_callback(wrong_knxipframe, None)
mock_warning.assert_called_with("Could not understand knxipframe")
# Response KNX/IP-Frame with error:
err_knxipframe = KNXIPFrame(xknx)
err_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE)
err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID
with patch("logging.Logger.debug") as mock_warning:
connectionstate.response_rec_callback(err_knxipframe, None)
mock_warning.assert_called_with(
"Error: KNX bus responded to request of type '%s' with error in '%s': %s",
type(connectionstate).__name__,
type(err_knxipframe.body).__name__,
ErrorCode.E_CONNECTION_ID,
)
# Correct Response KNX/IP-Frame:
res_knxipframe = KNXIPFrame(xknx)
res_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE)
connectionstate.response_rec_callback(res_knxipframe, None)
self.assertTrue(connectionstate.success)