-
Notifications
You must be signed in to change notification settings - Fork 116
/
test_hap_protocol.py
736 lines (555 loc) · 26.4 KB
/
test_hap_protocol.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
"""Tests for the HAPServerProtocol."""
import asyncio
import time
from unittest.mock import MagicMock, Mock, patch
from cryptography.exceptions import InvalidTag
import pytest
from pyhap import hap_handler, hap_protocol
from pyhap.accessory import Accessory, Bridge
from pyhap.hap_handler import HAPResponse
class MockHAPCrypto:
"""Mock HAPCrypto that only returns plaintext."""
def __init__(self):
"""Create the mock object."""
self._crypt_in_buffer = bytearray() # Encrypted buffer
def receive_data(self, buffer):
"""Receive data into the encrypted buffer."""
self._crypt_in_buffer += buffer
def decrypt(self):
"""Mock as plaintext."""
decrypted = self._crypt_in_buffer
self._crypt_in_buffer = bytearray() # Encrypted buffer
return decrypted
def encrypt(self, data): # pylint: disable=no-self-use
"""Mock as plaintext."""
return data
def test_connection_management(driver):
"""Verify closing the connection removes it from the pool."""
loop = MagicMock()
addr_info = ("1.2.3.4", 5)
addr_info2 = ("1.2.3.5", 6)
transport = MagicMock(get_extra_info=Mock(return_value=addr_info))
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
driver.async_subscribe_client_topic(addr_info, "1.1", True)
driver.async_subscribe_client_topic(addr_info, "2.2", True)
driver.async_subscribe_client_topic(addr_info2, "1.1", True)
assert "1.1" in driver.topics
assert "2.2" in driver.topics
assert addr_info in driver.topics["1.1"]
assert addr_info in driver.topics["2.2"]
assert addr_info2 in driver.topics["1.1"]
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
assert len(connections) == 1
assert connections[addr_info] == hap_proto
hap_proto.connection_lost(None)
assert len(connections) == 0
assert "1.1" in driver.topics
assert "2.2" not in driver.topics
assert addr_info not in driver.topics["1.1"]
assert addr_info2 in driver.topics["1.1"]
hap_proto.connection_made(transport)
assert len(connections) == 1
assert connections[addr_info] == hap_proto
hap_proto.close()
assert len(connections) == 0
hap_proto.connection_made(transport)
assert len(connections) == 1
assert connections[addr_info] == hap_proto
hap_proto.connection_lost(None)
assert len(connections) == 0
def test_pair_setup(driver):
"""Verify an non-encrypt request."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
hap_proto.close()
def test_http10_close(driver):
"""Test we handle http/1.0."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.0\r\nConnection:close\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
assert len(writer.call_args_list) == 1
assert not connections
hap_proto.close()
def test_invalid_content_length(driver):
"""Test we handle invalid content length."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.0\r\nConnection:close\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 2\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
hap_proto.data_received(
b"POST /pair-setup HTTP/1.0\r\nConnection:close\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 2\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert (
writer.call_args_list[0][0][0].startswith(
b"HTTP/1.1 500 Internal Server Error\r\n"
)
is True
)
assert len(writer.call_args_list) == 1
assert not connections
hap_proto.close()
def test_invalid_client_closes_connection(driver):
"""Test we handle client closing the connection."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.0\r\nConnection:close\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
hap_proto.data_received(b"")
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
assert len(writer.call_args_list) == 1
assert not connections
hap_proto.close()
def test_pair_setup_split_between_packets(driver):
"""Verify an non-encrypt request."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\n"
)
hap_proto.data_received(b"Content-Length: 6\r\n")
hap_proto.data_received(
b"Content-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01"
)
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
hap_proto.close()
def test_get_accessories_without_crypto(driver):
"""Verify an non-encrypt request that expected to be encrypted."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"GET /accessories HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\n\r\n" # pylint: disable=line-too-long
)
hap_proto.close()
assert b"-70401" in writer.call_args_list[0][0][0]
def test_get_accessories_with_crypto(driver):
"""Verify an encrypt accessories request."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"GET /accessories HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\n\r\n" # pylint: disable=line-too-long
)
hap_proto.close()
assert b"accessories" in writer.call_args_list[0][0][0]
def test_get_characteristics_with_crypto(driver):
"""Verify an encrypt characteristics request."""
loop = MagicMock()
transport = MagicMock()
connections = {}
acc = Accessory(driver, "TestAcc", aid=1)
assert acc.aid == 1
service = acc.driver.loader.get_service("TemperatureSensor")
acc.add_service(service)
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"GET /characteristics?id=3762173001.7 HTTP/1.1\r\nHost: HASS\\032Bridge\\032YPHW\\032B223AD._hap._tcp.local\r\n\r\n" # pylint: disable=line-too-long
)
hap_proto.data_received(
b"GET /characteristics?id=1.5 HTTP/1.1\r\nHost: HASS\\032Bridge\\032YPHW\\032B223AD._hap._tcp.local\r\n\r\n" # pylint: disable=line-too-long
)
hap_proto.close()
assert b"Content-Length:" in writer.call_args_list[0][0][0]
assert b"Transfer-Encoding: chunked\r\n\r\n" not in writer.call_args_list[0][0][0]
assert b"-70402" in writer.call_args_list[0][0][0]
assert b"Content-Length:" in writer.call_args_list[1][0][0]
assert b"Transfer-Encoding: chunked\r\n\r\n" not in writer.call_args_list[1][0][0]
assert b"TestAcc" in writer.call_args_list[1][0][0]
def test_set_characteristics_with_crypto(driver):
"""Verify an encrypt characteristics request."""
loop = MagicMock()
transport = MagicMock()
connections = {}
acc = Accessory(driver, "TestAcc", aid=1)
assert acc.aid == 1
service = acc.driver.loader.get_service("GarageDoorOpener")
acc.add_service(service)
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'PUT /characteristics HTTP/1.1\r\nHost: HASS12\\032AD1C22._hap._tcp.local\r\nContent-Length: 49\r\nContent-Type: application/hap+json\r\n\r\n{"characteristics":[{"aid":1,"iid":9,"ev":true}]}' # pylint: disable=line-too-long
)
hap_proto.close()
assert writer.call_args_list[0][0][0] == b"HTTP/1.1 204 No Content\r\n\r\n"
def test_crypto_failure_closes_connection(driver):
"""Verify a decrypt failure closes the connection."""
loop = MagicMock()
addr_info = ("1.2.3.4", 5)
transport = MagicMock(get_extra_info=Mock(return_value=addr_info))
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
assert connections[addr_info] == hap_proto
with patch.object(hap_proto.hap_crypto, "decrypt", side_effect=InvalidTag):
hap_proto.data_received(b"any") # pylint: disable=line-too-long
assert len(connections) == 0
def test_empty_encrypted_data(driver):
"""Verify an encrypt request when we start with an empty block."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(b"")
hap_proto.data_received(
b"GET /accessories HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\n\r\n" # pylint: disable=line-too-long
)
hap_proto.close()
assert b"accessories" in writer.call_args_list[0][0][0]
def test_http_11_keep_alive(driver):
"""Verify we can handle multiple requests."""
loop = MagicMock()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
hap_proto.close()
@pytest.mark.asyncio
async def test_camera_snapshot_connection_closed(driver):
"""Test camera snapshot when the other side closes the connection."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=True)
connections = {}
async def _async_get_snapshot(*_):
return b"fakesnap"
acc = Accessory(driver, "TestAcc")
acc.async_get_snapshot = _async_get_snapshot
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
hap_proto.close()
await hap_proto.response.task
await asyncio.sleep(0)
assert writer.call_args_list == []
hap_proto.close()
def test_camera_snapshot_without_snapshot_support(driver):
"""Test camera snapshot fails if there is not support for it."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
acc = Accessory(driver, "TestAcc")
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
hap_proto.close()
assert b"-70402" in writer.call_args_list[0][0][0]
@pytest.mark.asyncio
async def test_camera_snapshot_works_sync(driver):
"""Test camera snapshot works if there is support for it."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
def _get_snapshot(*_):
return b"fakesnap"
acc = Accessory(driver, "TestAcc")
acc.get_snapshot = _get_snapshot
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
await hap_proto.response.task
await asyncio.sleep(0)
assert b"fakesnap" in writer.call_args_list[0][0][0]
hap_proto.close()
@pytest.mark.asyncio
async def test_camera_snapshot_works_async(driver):
"""Test camera snapshot works if there is support for it."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
async def _async_get_snapshot(*_):
return b"fakesnap"
acc = Accessory(driver, "TestAcc")
acc.async_get_snapshot = _async_get_snapshot
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
await hap_proto.response.task
await asyncio.sleep(0)
assert b"fakesnap" in writer.call_args_list[0][0][0]
hap_proto.close()
@pytest.mark.asyncio
async def test_camera_snapshot_timeout_async(driver):
"""Test camera snapshot timeout is handled."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
async def _async_get_snapshot(*_):
await asyncio.sleep(10)
return b"fakesnap"
acc = Accessory(driver, "TestAcc")
acc.async_get_snapshot = _async_get_snapshot
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_handler, "RESPONSE_TIMEOUT", 0.1), patch.object(
hap_proto.transport, "write"
) as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
await asyncio.sleep(0.3)
assert b"-70402" in writer.call_args_list[0][0][0]
hap_proto.close()
def test_upgrade_to_encrypted(driver):
"""Test we switch to encrypted wen we get a shared_key."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
acc = Accessory(driver, "TestAcc")
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
assert hap_proto.hap_crypto is None
def _make_response(*_):
response = HAPResponse()
response.shared_key = b"newkey"
return response
with patch.object(hap_proto.transport, "write"), patch.object(
hap_proto.handler, "dispatch", _make_response
):
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert hap_proto.hap_crypto is not None
hap_proto.close()
@pytest.mark.asyncio
async def test_pairing_changed(driver):
"""Test we update mdns when the pairing changes."""
loop = MagicMock()
run_in_executor_called = False
async def _run_in_executor(*_):
nonlocal run_in_executor_called
run_in_executor_called = True
loop.run_in_executor = _run_in_executor
transport = MagicMock()
connections = {}
acc = Accessory(driver, "TestAcc")
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
def _make_response(*_):
response = HAPResponse()
response.pairing_changed = True
return response
with patch.object(hap_proto.transport, "write"), patch.object(
hap_proto.handler, "dispatch", _make_response
):
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
await asyncio.sleep(0)
assert run_in_executor_called is True
hap_proto.close()
@pytest.mark.asyncio
async def test_camera_snapshot_throws_an_exception(driver):
"""Test camera snapshot that throws an exception."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
async def _async_get_snapshot(*_):
raise ValueError("any error")
acc = Accessory(driver, "TestAcc")
acc.async_get_snapshot = _async_get_snapshot
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
try:
await hap_proto.response.task
except Exception: # pylint: disable=broad-except
pass
await asyncio.sleep(0)
assert b"-70402" in writer.call_args_list[0][0][0]
hap_proto.close()
@pytest.mark.asyncio
async def test_camera_snapshot_times_out(driver):
"""Test camera snapshot times out."""
loop = MagicMock()
transport = MagicMock()
transport.is_closing = Mock(return_value=False)
connections = {}
def _get_snapshot(*_):
raise asyncio.TimeoutError("timeout")
acc = Accessory(driver, "TestAcc")
acc.get_snapshot = _get_snapshot
driver.add_accessory(acc)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
try:
await hap_proto.response.task
except Exception: # pylint: disable=broad-except
pass
await asyncio.sleep(0)
assert b"-70402" in writer.call_args_list[0][0][0]
hap_proto.close()
@pytest.mark.asyncio
async def test_camera_snapshot_missing_accessory(driver):
"""Test camera snapshot that throws an exception."""
loop = MagicMock()
transport = MagicMock()
connections = {}
bridge = Bridge(driver, "Test Bridge")
driver.add_accessory(bridge)
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
hap_proto.hap_crypto = MockHAPCrypto()
hap_proto.handler.is_encrypted = True
with patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b'POST /resource HTTP/1.1\r\nHost: HASS\\032Bridge\\032BROZ\\0323BF435._hap._tcp.local\r\nContent-Length: 79\r\nContent-Type: application/hap+json\r\n\r\n{"image-height":360,"resource-type":"image","image-width":640,"aid":1411620844}' # pylint: disable=line-too-long
)
await asyncio.sleep(0)
assert hap_proto.response is None
assert b"-70402" in writer.call_args_list[0][0][0]
hap_proto.close()
@pytest.mark.asyncio
async def test_idle_timeout(driver):
"""Test we close the connection once we reach the idle timeout."""
loop = asyncio.get_event_loop()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_protocol, "IDLE_CONNECTION_TIMEOUT_SECONDS", 0), patch.object(
hap_proto, "close"
) as hap_proto_close, patch.object(hap_proto.transport, "write") as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
hap_proto.check_idle(time.time())
assert hap_proto_close.called is True
@pytest.mark.asyncio
async def test_does_not_timeout(driver):
"""Test we do not timeout the connection if we have not reached the idle."""
loop = asyncio.get_event_loop()
transport = MagicMock()
connections = {}
driver.add_accessory(Accessory(driver, "TestAcc"))
hap_proto = hap_protocol.HAPServerProtocol(loop, connections, driver)
hap_proto.connection_made(transport)
with patch.object(hap_proto, "close") as hap_proto_close, patch.object(
hap_proto.transport, "write"
) as writer:
hap_proto.data_received(
b"POST /pair-setup HTTP/1.1\r\nHost: Bridge\\032C77C47._hap._tcp.local\r\nContent-Length: 6\r\nContent-Type: application/pairing+tlv8\r\n\r\n\x00\x01\x00\x06\x01\x01" # pylint: disable=line-too-long
)
assert writer.call_args_list[0][0][0].startswith(b"HTTP/1.1 200 OK\r\n") is True
hap_proto.check_idle(time.time())
assert hap_proto_close.called is False