@@ -128,14 +128,14 @@ test.register_coroutine_test(
128128 )})
129129 test .socket .capability :__expect_send (mock_device :generate_test_message (" main" ,
130130 capabilities .lockUsers .users ({
131- {userIndex = 1 , userName = " Alice " , userType = " guest" },
131+ {userIndex = 1 , userName = " Guest 1 " , userType = " guest" },
132132 {userIndex = 4 , userName = " Guest 4" , userType = " guest" },
133133 }, {visibility = {displayed = false }})
134134 ))
135135 test .socket .capability :__expect_send (mock_device :generate_test_message (" main" ,
136136 capabilities .lockCredentials .credentials ({
137- {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " },
138- {userIndex = 1 , credentialIndex = 5 , credentialType = " pin" , credentialName = " Alice " },
137+ {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " },
138+ {userIndex = 1 , credentialIndex = 5 , credentialType = " pin" , credentialName = " Guest 1 " },
139139 {userIndex = 4 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Guest 4" },
140140 }, {visibility = {displayed = false }})
141141 ))
@@ -221,12 +221,12 @@ test.register_coroutine_test(
221221 )})
222222 expect_tables (
223223 {
224- {userIndex = 1 , userName = " Alice " , userType = " guest" },
225- {userIndex = 4 , userName = " Dan " , userType = " guest" },
224+ {userIndex = 1 , userName = " Guest 1 " , userType = " guest" },
225+ {userIndex = 4 , userName = " Guest 4 " , userType = " guest" },
226226 },
227227 {
228- {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " },
229- {userIndex = 4 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Dan " },
228+ {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " },
229+ {userIndex = 4 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Guest 4 " },
230230 }
231231 )
232232 test .wait_for_events ()
@@ -248,12 +248,12 @@ test.register_coroutine_test(
248248 test .socket .matter :__queue_receive ({mock_device .id , get_user_response (3 , " Bob" , OCCUPIED , UNRESTRICTED , {pin (2 )}, nil )})
249249 expect_tables (
250250 {
251- {userIndex = 1 , userName = " Alice " , userType = " guest" },
252- {userIndex = 3 , userName = " Bob " , userType = " guest" },
251+ {userIndex = 1 , userName = " Guest 1 " , userType = " guest" },
252+ {userIndex = 3 , userName = " Guest 3 " , userType = " guest" },
253253 },
254254 {
255- {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " },
256- {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Bob " },
255+ {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " },
256+ {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Guest 3 " },
257257 }
258258 )
259259 test .wait_for_events ()
@@ -273,12 +273,12 @@ test.register_coroutine_test(
273273 -- Bob is still in the tables
274274 expect_tables (
275275 {
276- {userIndex = 1 , userName = " Alice " , userType = " guest" },
277- {userIndex = 3 , userName = " Bob " , userType = " guest" },
276+ {userIndex = 1 , userName = " Guest 1 " , userType = " guest" },
277+ {userIndex = 3 , userName = " Guest 3 " , userType = " guest" },
278278 },
279279 {
280- {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " },
281- {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Bob " },
280+ {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " },
281+ {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Guest 3 " },
282282 }
283283 )
284284 test .wait_for_events ()
@@ -306,8 +306,8 @@ test.register_coroutine_test(
306306 13 , " " , types .UserStatusEnum .OCCUPIED_ENABLED , types .UserTypeEnum .REMOTE_ONLY_USER , {pin (12 )}, nil
307307 )})
308308 expect_tables (
309- {{userIndex = 1 , userName = " Alice " , userType = " guest" }},
310- {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " }}
309+ {{userIndex = 1 , userName = " Guest 1 " , userType = " guest" }},
310+ {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " }}
311311 )
312312 test .wait_for_events ()
313313 end ,
@@ -353,12 +353,12 @@ test.register_coroutine_test(
353353 test .socket .matter :__queue_receive ({mock_device .id , get_user_response (3 , " Bob" , OCCUPIED , UNRESTRICTED , {pin (2 )}, nil )})
354354 expect_tables (
355355 {
356- {userIndex = 1 , userName = " Alice " , userType = " guest" },
357- {userIndex = 3 , userName = " Bob " , userType = " guest" },
356+ {userIndex = 1 , userName = " Guest 1 " , userType = " guest" },
357+ {userIndex = 3 , userName = " Guest 3 " , userType = " guest" },
358358 },
359359 {
360- {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " },
361- {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Bob " },
360+ {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " },
361+ {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Guest 3 " },
362362 }
363363 )
364364 test .wait_for_events ()
@@ -433,8 +433,8 @@ test.register_coroutine_test(
433433 -- user 3 does not answer in time
434434 test .mock_time .advance_time (10 )
435435 expect_tables (
436- {{userIndex = 1 , userName = " Alice " , userType = " guest" }},
437- {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " }}
436+ {{userIndex = 1 , userName = " Guest 1 " , userType = " guest" }},
437+ {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " }}
438438 )
439439 test .wait_for_events ()
440440 assert (mock_device :get_field (lock_sync .USERS_SYNCED ) == nil , " sync should not be complete" )
@@ -445,12 +445,12 @@ test.register_coroutine_test(
445445 test .socket .matter :__queue_receive ({mock_device .id , get_user_response (3 , " Bob" , OCCUPIED , UNRESTRICTED , {pin (2 )}, nil )})
446446 expect_tables (
447447 {
448- {userIndex = 1 , userName = " Alice " , userType = " guest" },
449- {userIndex = 3 , userName = " Bob " , userType = " guest" },
448+ {userIndex = 1 , userName = " Guest 1 " , userType = " guest" },
449+ {userIndex = 3 , userName = " Guest 3 " , userType = " guest" },
450450 },
451451 {
452- {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " },
453- {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Bob " },
452+ {userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " },
453+ {userIndex = 3 , credentialIndex = 2 , credentialType = " pin" , credentialName = " Guest 3 " },
454454 }
455455 )
456456 test .wait_for_events ()
@@ -502,6 +502,8 @@ local function store_tables_like_old_migration(codes)
502502 table.insert (users , {userIndex = slot , userName = name , userType = " guest" })
503503 table.insert (creds , {userIndex = slot , credentialIndex = slot , credentialType = " pin" , credentialName = name })
504504 end
505+ -- the lock's user names are not read, so the names come from the legacy codes, as in a real migration
506+ mock_device :set_field (lock_utils .LOCK_CODES_FOR_MIGRATION , codes , {persist = true })
505507 sync_with_lock (lock_users , users , creds )
506508 mock_device :set_field (lock_sync .USERS_SYNCED , nil , {persist = true })
507509end
@@ -581,8 +583,8 @@ test.register_coroutine_test(
581583 {index = 1 , name = " Alice" , pins = {1 }},
582584 {index = 2 , type = REMOTE_ONLY , pins = {2 }}, -- the driver's COTA user
583585 },
584- {{userIndex = 1 , userName = " Alice " , userType = " guest" }},
585- {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Alice " }}
586+ {{userIndex = 1 , userName = " Guest 1 " , userType = " guest" }},
587+ {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1 " }}
586588 )
587589 end ,
588590 { min_api_version = 15 }
@@ -626,6 +628,137 @@ test.register_coroutine_test(
626628 { min_api_version = 15 }
627629)
628630
631+ test .register_coroutine_test (
632+ " The SUCCESS status that accompanies a GetUserResponse does not advance the sync a second time" ,
633+ function ()
634+ set_total_users_supported (3 )
635+ refresh_and_wait_for_sync ()
636+ expect_get_user (1 )
637+ test .wait_for_events ()
638+
639+ test .socket .matter :__queue_receive ({mock_device .id , DoorLock :build_test_command_response (
640+ mock_device , 1 , DoorLock .ID , DoorLock .server .commands .GetUser .ID , nil , im .InteractionResponse .Status .SUCCESS )})
641+ test .wait_for_events () -- nothing is sent
642+
643+ test .socket .matter :__queue_receive ({mock_device .id , get_user_response (
644+ 1 , " Alice" , types .UserStatusEnum .OCCUPIED_ENABLED , types .UserTypeEnum .UNRESTRICTED_USER , {pin (1 )}, 2
645+ )})
646+ expect_get_user (2 )
647+ test .wait_for_events ()
648+ end ,
649+ { min_api_version = 15 }
650+ )
651+
652+ test .register_coroutine_test (
653+ " A GetUserResponse received when no addUser or sync is in progress does not send SetUser" ,
654+ function ()
655+ test .socket .matter :__queue_receive ({mock_device .id , get_user_response (
656+ 10 , nil , types .UserStatusEnum .AVAILABLE , nil , nil , nil
657+ )})
658+ test .wait_for_events () -- nothing is sent or emitted
659+ end ,
660+ { min_api_version = 15 }
661+ )
662+
663+ -- The replies below are copied from an ULTRALOQ Smart Door Lock on a production hub.
664+ -- A successful GetUser returns two blocks in one message: the GetUserResponse, then a SUCCESS status on
665+ -- the GetUser command path. User 1's name is not valid UTF-8, users 3 and 6 are NUL-padded to 10 bytes,
666+ -- user 6 holds only a programming PIN, and user 6 reports no NextUserIndex.
667+ local ULTRALOQ_USERS = {
668+ {index = 1 , name = " \xC4\x0E\x02\x20\x00\x00\x00\x00\xA4\x0E " , type = types .UserTypeEnum .UNRESTRICTED_USER , pin = 1 , next = 2 },
669+ {index = 2 , name = " Brian" , type = types .UserTypeEnum .SCHEDULE_RESTRICTED_USER , pin = 2 , next = 3 },
670+ {index = 3 , name = " Alex\x00 " , type = types .UserTypeEnum .SCHEDULE_RESTRICTED_USER , pin = 3 , next = 4 },
671+ {index = 4 , name = " Hudson " , type = types .UserTypeEnum .SCHEDULE_RESTRICTED_USER , pin = 4 , next = 5 },
672+ {index = 5 , name = " Dog" , type = types .UserTypeEnum .SCHEDULE_RESTRICTED_USER , pin = 5 , next = 6 },
673+ {index = 6 , name = " Nobody\x00 " , type = types .UserTypeEnum .SCHEDULE_RESTRICTED_USER , programming_pin = 0 , next = nil },
674+ }
675+
676+ -- A GetUserResponse followed by the SUCCESS status block for the GetUser command, in one message.
677+ local function get_user_reply_as_sent_by_lock (user )
678+ local credentials
679+ if user .pin then
680+ credentials = {pin (user .pin )}
681+ else
682+ credentials = {types .CredentialStruct ({
683+ credential_type = types .CredentialTypeEnum .PROGRAMMINGPIN , credential_index = user .programming_pin
684+ })}
685+ end
686+ local reply = get_user_response (user .index , user .name , OCCUPIED , user .type , credentials , user .next )
687+ local status_block = DoorLock :build_test_command_response (
688+ mock_device , 1 , DoorLock .ID , DoorLock .server .commands .GetUser .ID , nil , im .InteractionResponse .Status .SUCCESS
689+ ).info_blocks [1 ]
690+ table.insert (reply .info_blocks , status_block )
691+ return reply
692+ end
693+
694+ -- The datastore save JSON-encodes every persisted string, and the hub's encoder rejects invalid UTF-8.
695+ local function assert_utf8_only (value , path )
696+ if type (value ) == " string" then
697+ assert (utf8.len (value ) ~= nil , string.format (" persisted string at %s is not valid UTF-8" , path ))
698+ elseif type (value ) == " table" then
699+ for k , v in pairs (value ) do
700+ assert_utf8_only (k , path .. " [key]" )
701+ assert_utf8_only (v , path .. " ." .. tostring (k ))
702+ end
703+ end
704+ end
705+
706+ test .register_coroutine_test (
707+ " The ULTRALOQ's real GetUser replies sync every user and persist only valid UTF-8" ,
708+ function ()
709+ set_total_users_supported (10 )
710+ refresh_and_wait_for_sync ()
711+ expect_get_user (1 )
712+ test .wait_for_events ()
713+
714+ for _ , user in ipairs (ULTRALOQ_USERS ) do
715+ test .socket .matter :__queue_receive ({mock_device .id , get_user_reply_as_sent_by_lock (user )})
716+ if user .next then
717+ -- exactly one GetUser for the next index: the SUCCESS status block must not skip ahead
718+ expect_get_user (user .next )
719+ else
720+ local users , creds = {}, {}
721+ for _ , u in ipairs (ULTRALOQ_USERS ) do
722+ table.insert (users , {userIndex = u .index , userName = " Guest " .. u .index , userType = " guest" })
723+ if u .pin then
724+ table.insert (creds , {userIndex = u .index , credentialIndex = u .pin , credentialType = " pin" , credentialName = " Guest " .. u .index })
725+ end
726+ end
727+ expect_tables (users , creds )
728+ end
729+ test .wait_for_events ()
730+ assert_utf8_only (mock_device :get_field (" syncReportedUsers" ), " syncReportedUsers" )
731+ end
732+ assert (mock_device :get_field (lock_sync .USERS_SYNCED ) == lock_sync .VERSION , " sync should be complete" )
733+ end ,
734+ { min_api_version = 15 }
735+ )
736+
737+ test .register_coroutine_test (
738+ " A late GetUserResponse for an empty index after the sync finishes does not add a user to the lock" ,
739+ function ()
740+ -- left behind by an earlier command, as on the production hub
741+ mock_device :set_field (lock_utils .USER_NAME , " Melinda" , {persist = true })
742+ set_total_users_supported (10 )
743+ refresh_and_wait_for_sync ()
744+ expect_get_user (1 )
745+ test .wait_for_events ()
746+
747+ local only_user = {index = 1 , name = nil , type = types .UserTypeEnum .UNRESTRICTED_USER , pin = 1 , next = nil }
748+ test .socket .matter :__queue_receive ({mock_device .id , get_user_reply_as_sent_by_lock (only_user )})
749+ expect_tables (
750+ {{userIndex = 1 , userName = " Guest 1" , userType = " guest" }},
751+ {{userIndex = 1 , credentialIndex = 1 , credentialType = " pin" , credentialName = " Guest 1" }}
752+ )
753+ test .wait_for_events ()
754+
755+ -- the lock's answer for an empty index carries only the user index; every other field is null
756+ test .socket .matter :__queue_receive ({mock_device .id , get_user_response (10 , nil , nil , nil , nil , nil )})
757+ test .wait_for_events () -- no SetUser is sent and no commandResult is emitted
758+ end ,
759+ { min_api_version = 15 }
760+ )
761+
629762-- A device paired with the "lock" profile, which has no lockUsers, as a newly paired lock has before
630763-- match_profile runs.
631764local new_lock = test .mock_device .build_test_matter_device ({
0 commit comments