|
1 | 1 | import asyncio |
| 2 | +import base64 |
2 | 3 | import json |
3 | 4 | import math |
4 | 5 | import time |
@@ -62,6 +63,35 @@ def build_map_response(self, payload: bytes) -> RoborockMessage: |
62 | 63 | seq=self.seq, |
63 | 64 | ) |
64 | 65 |
|
| 66 | + def build_map_part( |
| 67 | + self, |
| 68 | + payload: bytes, |
| 69 | + protocol: RoborockMessageProtocol, |
| 70 | + seq: int, |
| 71 | + random: int, |
| 72 | + timestamp: int, |
| 73 | + ) -> RoborockMessage: |
| 74 | + """Build a MAP_RESPONSE_PART (300) or MAP_RESPONSE (301) message.""" |
| 75 | + return RoborockMessage( |
| 76 | + protocol=protocol, |
| 77 | + payload=payload, |
| 78 | + version=b"B01", |
| 79 | + seq=seq, |
| 80 | + random=random, |
| 81 | + timestamp=timestamp, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def _fragmented_map_payload() -> tuple[bytes, bytes, bytes]: |
| 86 | + """A base64 map payload split into two fragments, like issue #1043. |
| 87 | +
|
| 88 | + The full payload decodes to 48 bytes (a multiple of the AES block size); |
| 89 | + each fragment alone decodes to 24 bytes, so neither fragment is decodable |
| 90 | + on its own. |
| 91 | + """ |
| 92 | + full = base64.b64encode(bytes(range(48))) |
| 93 | + return full, full[:32], full[32:] |
| 94 | + |
65 | 95 |
|
66 | 96 | @pytest.fixture(name="fake_channel") |
67 | 97 | def fake_channel_fixture() -> FakeChannel: |
@@ -350,3 +380,167 @@ async def test_subscribe_map_pushes_skips_undecodable_frames( |
350 | 380 | ): |
351 | 381 | fake_channel.notify_subscribers(message_builder.build_map_response(b"raw-map-payload")) |
352 | 382 | assert received == [b"inflated-payload"] |
| 383 | + |
| 384 | + |
| 385 | +# ----------------------------------------------------------------------------- fragmented map responses (#1043) |
| 386 | + |
| 387 | +MAP_RANDOM = 2716216127 |
| 388 | +MAP_TIMESTAMP = 1794578237 |
| 389 | + |
| 390 | + |
| 391 | +async def test_send_map_command_reassembles_fragmented_frames( |
| 392 | + device: HomeDataDevice, |
| 393 | + product: HomeDataProduct, |
| 394 | + fake_channel: FakeChannel, |
| 395 | + message_builder: B01MessageBuilder, |
| 396 | +) -> None: |
| 397 | + """A map split into a 300 part and a 301 part is decoded once, joined.""" |
| 398 | + channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] |
| 399 | + full, first_part, last_part = _fragmented_map_payload() |
| 400 | + |
| 401 | + with patch( |
| 402 | + "roborock.devices.rpc.b01_q7_channel.decode_map_payload", |
| 403 | + return_value=b"inflated-payload", |
| 404 | + ) as mock_decode: |
| 405 | + task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123})) |
| 406 | + await asyncio.sleep(0) |
| 407 | + |
| 408 | + fake_channel.notify_subscribers( |
| 409 | + message_builder.build_map_part( |
| 410 | + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP |
| 411 | + ) |
| 412 | + ) |
| 413 | + await asyncio.sleep(0) |
| 414 | + assert not task.done() # the first part alone is not a complete frame |
| 415 | + fake_channel.notify_subscribers( |
| 416 | + message_builder.build_map_part( |
| 417 | + last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP |
| 418 | + ) |
| 419 | + ) |
| 420 | + assert await task == b"inflated-payload" |
| 421 | + mock_decode.assert_called_once() |
| 422 | + assert mock_decode.call_args[0][0] == full |
| 423 | + |
| 424 | + |
| 425 | +async def test_send_map_command_reassembles_out_of_order_fragments( |
| 426 | + device: HomeDataDevice, |
| 427 | + product: HomeDataProduct, |
| 428 | + fake_channel: FakeChannel, |
| 429 | + message_builder: B01MessageBuilder, |
| 430 | +) -> None: |
| 431 | + """The trailing 301 part arriving first is buffered until the 300 part arrives.""" |
| 432 | + channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] |
| 433 | + full, first_part, last_part = _fragmented_map_payload() |
| 434 | + |
| 435 | + with patch( |
| 436 | + "roborock.devices.rpc.b01_q7_channel.decode_map_payload", |
| 437 | + return_value=b"inflated-payload", |
| 438 | + ) as mock_decode: |
| 439 | + task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123})) |
| 440 | + await asyncio.sleep(0) |
| 441 | + |
| 442 | + fake_channel.notify_subscribers( |
| 443 | + message_builder.build_map_part( |
| 444 | + last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP |
| 445 | + ) |
| 446 | + ) |
| 447 | + await asyncio.sleep(0) |
| 448 | + assert not task.done() # the trailing fragment alone is not decodable |
| 449 | + fake_channel.notify_subscribers( |
| 450 | + message_builder.build_map_part( |
| 451 | + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP |
| 452 | + ) |
| 453 | + ) |
| 454 | + assert await task == b"inflated-payload" |
| 455 | + mock_decode.assert_called_once() |
| 456 | + assert mock_decode.call_args[0][0] == full |
| 457 | + |
| 458 | + |
| 459 | +async def test_subscribe_map_pushes_reassembles_fragmented_frames( |
| 460 | + device: HomeDataDevice, |
| 461 | + product: HomeDataProduct, |
| 462 | + fake_channel: FakeChannel, |
| 463 | + message_builder: B01MessageBuilder, |
| 464 | +) -> None: |
| 465 | + """Fragmented map pushes are delivered once, as one decoded frame.""" |
| 466 | + channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] |
| 467 | + full, first_part, last_part = _fragmented_map_payload() |
| 468 | + |
| 469 | + received: list[bytes] = [] |
| 470 | + await channel.subscribe_map_pushes(received.append) |
| 471 | + |
| 472 | + with patch( |
| 473 | + "roborock.devices.rpc.b01_q7_channel.decode_map_payload", |
| 474 | + return_value=b"inflated-payload", |
| 475 | + ) as mock_decode: |
| 476 | + fake_channel.notify_subscribers( |
| 477 | + message_builder.build_map_part( |
| 478 | + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP |
| 479 | + ) |
| 480 | + ) |
| 481 | + assert received == [] |
| 482 | + fake_channel.notify_subscribers( |
| 483 | + message_builder.build_map_part( |
| 484 | + last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP |
| 485 | + ) |
| 486 | + ) |
| 487 | + assert received == [b"inflated-payload"] |
| 488 | + mock_decode.assert_called_once() |
| 489 | + assert mock_decode.call_args[0][0] == full |
| 490 | + |
| 491 | + |
| 492 | +async def test_subscribe_map_pushes_buffers_lone_trailing_fragment( |
| 493 | + device: HomeDataDevice, |
| 494 | + product: HomeDataProduct, |
| 495 | + fake_channel: FakeChannel, |
| 496 | + message_builder: B01MessageBuilder, |
| 497 | +) -> None: |
| 498 | + """A lone 301 fragment is buffered, not decoded, until its 300 part arrives.""" |
| 499 | + channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] |
| 500 | + _full, first_part, last_part = _fragmented_map_payload() |
| 501 | + |
| 502 | + received: list[bytes] = [] |
| 503 | + await channel.subscribe_map_pushes(received.append) |
| 504 | + |
| 505 | + with patch( |
| 506 | + "roborock.devices.rpc.b01_q7_channel.decode_map_payload", |
| 507 | + return_value=b"inflated-payload", |
| 508 | + ): |
| 509 | + fake_channel.notify_subscribers( |
| 510 | + message_builder.build_map_part( |
| 511 | + last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP |
| 512 | + ) |
| 513 | + ) |
| 514 | + assert received == [] |
| 515 | + fake_channel.notify_subscribers( |
| 516 | + message_builder.build_map_part( |
| 517 | + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP |
| 518 | + ) |
| 519 | + ) |
| 520 | + assert received == [b"inflated-payload"] |
| 521 | + |
| 522 | + |
| 523 | +async def test_subscribe_map_pushes_ignores_incomplete_frame( |
| 524 | + device: HomeDataDevice, |
| 525 | + product: HomeDataProduct, |
| 526 | + fake_channel: FakeChannel, |
| 527 | + message_builder: B01MessageBuilder, |
| 528 | +) -> None: |
| 529 | + """A lone 300 part with no final part delivers nothing.""" |
| 530 | + channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] |
| 531 | + _full, first_part, _last_part = _fragmented_map_payload() |
| 532 | + |
| 533 | + received: list[bytes] = [] |
| 534 | + await channel.subscribe_map_pushes(received.append) |
| 535 | + |
| 536 | + with patch( |
| 537 | + "roborock.devices.rpc.b01_q7_channel.decode_map_payload", |
| 538 | + return_value=b"inflated-payload", |
| 539 | + ) as mock_decode: |
| 540 | + fake_channel.notify_subscribers( |
| 541 | + message_builder.build_map_part( |
| 542 | + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP |
| 543 | + ) |
| 544 | + ) |
| 545 | + assert received == [] |
| 546 | + mock_decode.assert_not_called() |
0 commit comments