Fix off by one in HomeKit iid allocator (#81793)

This commit is contained in:
J. Nick Koston 2022-11-08 09:20:03 -06:00 committed by GitHub
parent 88a7c76739
commit 2cb58b8189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View File

@ -109,12 +109,12 @@ class AccessoryIIDStorage:
# AID must be a string since JSON keys cannot be int # AID must be a string since JSON keys cannot be int
aid_str = str(aid) aid_str = str(aid)
accessory_allocation = self.allocations.setdefault(aid_str, {}) accessory_allocation = self.allocations.setdefault(aid_str, {})
accessory_allocated_iids = self.allocated_iids.setdefault(aid_str, []) accessory_allocated_iids = self.allocated_iids.setdefault(aid_str, [1])
if service_hap_type == ACCESSORY_INFORMATION_SERVICE and char_uuid is None: if service_hap_type == ACCESSORY_INFORMATION_SERVICE and char_uuid is None:
allocated_iid = 1 return 1
elif allocation_key in accessory_allocation: if allocation_key in accessory_allocation:
return accessory_allocation[allocation_key] return accessory_allocation[allocation_key]
elif accessory_allocated_iids: if accessory_allocated_iids:
allocated_iid = accessory_allocated_iids[-1] + 1 allocated_iid = accessory_allocated_iids[-1] + 1
else: else:
allocated_iid = 2 allocated_iid = 2

View File

@ -51,7 +51,6 @@ async def test_config_entry_running(hass, hass_client, hk_driver, mock_async_zer
"3E__23_": 5, "3E__23_": 5,
"3E__30_": 6, "3E__30_": 6,
"3E__52_": 7, "3E__52_": 7,
"3E___": 1,
"A2__37_": 9, "A2__37_": 9,
"A2___": 8, "A2___": 8,
} }
@ -278,7 +277,6 @@ async def test_config_entry_accessory(
"3E__23_": 5, "3E__23_": 5,
"3E__30_": 6, "3E__30_": 6,
"3E__52_": 7, "3E__52_": 7,
"3E___": 1,
"43__25_": 11, "43__25_": 11,
"43___": 10, "43___": 10,
"A2__37_": 9, "A2__37_": 9,

View File

@ -152,23 +152,29 @@ async def test_iid_generation_and_restore_v2(hass, iid_storage, hass_storage):
1, "000000AA-0000-1000-8000-0026BB765291", None, None, None 1, "000000AA-0000-1000-8000-0026BB765291", None, None, None
) )
assert not_accessory_info_service_iid == 2 assert not_accessory_info_service_iid == 2
assert iid_storage.allocated_iids == {"1": [1, 2]}
not_accessory_info_service_iid_2 = iid_storage.get_or_allocate_iid( not_accessory_info_service_iid_2 = iid_storage.get_or_allocate_iid(
1, "000000BB-0000-1000-8000-0026BB765291", None, None, None 1, "000000BB-0000-1000-8000-0026BB765291", None, None, None
) )
assert not_accessory_info_service_iid_2 == 3 assert not_accessory_info_service_iid_2 == 3
assert iid_storage.allocated_iids == {"1": [1, 2, 3]}
not_accessory_info_service_iid_2 = iid_storage.get_or_allocate_iid( not_accessory_info_service_iid_2 = iid_storage.get_or_allocate_iid(
1, "000000BB-0000-1000-8000-0026BB765291", None, None, None 1, "000000BB-0000-1000-8000-0026BB765291", None, None, None
) )
assert not_accessory_info_service_iid_2 == 3 assert not_accessory_info_service_iid_2 == 3
assert iid_storage.allocated_iids == {"1": [1, 2, 3]}
accessory_info_service_iid = iid_storage.get_or_allocate_iid( accessory_info_service_iid = iid_storage.get_or_allocate_iid(
1, "0000003E-0000-1000-8000-0026BB765291", None, None, None 1, "0000003E-0000-1000-8000-0026BB765291", None, None, None
) )
assert accessory_info_service_iid == 1 assert accessory_info_service_iid == 1
assert iid_storage.allocated_iids == {"1": [1, 2, 3]}
accessory_info_service_iid = iid_storage.get_or_allocate_iid( accessory_info_service_iid = iid_storage.get_or_allocate_iid(
1, "0000003E-0000-1000-8000-0026BB765291", None, None, None 1, "0000003E-0000-1000-8000-0026BB765291", None, None, None
) )
assert accessory_info_service_iid == 1 assert accessory_info_service_iid == 1
assert iid_storage.allocated_iids == {"1": [1, 2, 3]}
accessory_info_service_iid = iid_storage.get_or_allocate_iid( accessory_info_service_iid = iid_storage.get_or_allocate_iid(
2, "0000003E-0000-1000-8000-0026BB765291", None, None, None 2, "0000003E-0000-1000-8000-0026BB765291", None, None, None
) )
assert accessory_info_service_iid == 1 assert accessory_info_service_iid == 1
assert iid_storage.allocated_iids == {"1": [1, 2, 3], "2": [1]}