forked from wolfSSL/wolfHSM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwh_test_auth.c
More file actions
1833 lines (1588 loc) · 74.2 KB
/
Copy pathwh_test_auth.c
File metadata and controls
1833 lines (1588 loc) · 74.2 KB
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfHSM.
*
* wolfHSM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfHSM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* test/wh_test_auth.c
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "wolfhsm/wh_settings.h"
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_comm.h"
#include "wolfhsm/wh_transport_mem.h"
#include "wolfhsm/wh_client.h"
#include "wolfhsm/wh_server.h"
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
#include "wolfhsm/wh_auth.h"
#include "wolfhsm/wh_message_auth.h"
#include "wolfhsm/wh_auth_base.h"
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
#include "wolfhsm/wh_nvm.h"
#include "wolfhsm/wh_nvm_flash.h"
#include "wolfhsm/wh_flash_ramsim.h"
#include "wolfhsm/wh_message.h"
#include "wh_test_common.h"
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
#include "wh_test_auth.h"
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) && \
defined(WOLFHSM_CFG_TEST_POSIX)
#include "port/posix/posix_transport_tcp.h"
#endif
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
#define BUFFER_SIZE 4096
#ifndef TEST_ADMIN_USERNAME
#define TEST_ADMIN_USERNAME "admin"
#endif
#ifndef TEST_ADMIN_PIN
#define TEST_ADMIN_PIN "1234"
#endif
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
#if !defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) && \
defined(WOLFHSM_CFG_ENABLE_SERVER)
/* Memory transport mode - setup structures */
static uint8_t req_buffer[BUFFER_SIZE] = {0};
static uint8_t resp_buffer[BUFFER_SIZE] = {0};
static whTransportMemConfig tmcf[1] = {0};
static whTransportClientCb tccb[1] = {WH_TRANSPORT_MEM_CLIENT_CB};
static whTransportMemClientContext tmcc[1] = {0};
static whCommClientConfig cc_conf[1] = {0};
static whClientConfig c_conf[1] = {0};
static whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB};
static whTransportMemServerContext tmsc[1] = {0};
static whCommServerConfig cs_conf[1] = {0};
static whServerContext server[1] = {0};
static whClientContext client[1] = {0};
/* NVM setup */
static uint8_t memory[FLASH_RAM_SIZE] = {0};
static whFlashRamsimCtx fc[1] = {0};
static whFlashRamsimCfg fc_conf[1];
static const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
static whTestNvmBackendUnion nvm_setup;
static whNvmConfig n_conf[1] = {0};
static whNvmContext nvm[1] = {{0}};
/* Test-specific authorization override callbacks to verify they are invoked */
static int test_checkRequestAuthorizationCalled = 0;
static int test_checkKeyAuthorizationCalled = 0;
static int test_logoutCallCount = 0;
static int test_CheckRequestAuthorization(void* context, int err,
uint16_t user_id, uint16_t group,
uint16_t action)
{
(void)context;
(void)user_id;
(void)group;
(void)action;
test_checkRequestAuthorizationCalled++;
/* Pass through the error code unchanged */
return err;
}
static int test_CheckKeyAuthorization(void* context, int err, uint16_t user_id,
uint32_t key_id, uint16_t action)
{
(void)context;
(void)user_id;
(void)key_id;
(void)action;
test_checkKeyAuthorizationCalled++;
/* Pass through the error code unchanged */
return err;
}
/* Counts Logout calls for the abrupt-disconnect test. */
static int test_Logout(void* context, uint16_t current_user_id,
uint16_t user_id)
{
test_logoutCallCount++;
return wh_Auth_BaseLogout(context, current_user_id, user_id);
}
/* Auth setup following wh_posix_server pattern */
static whAuthCb default_auth_cb = {
.Init = wh_Auth_BaseInit,
.Cleanup = wh_Auth_BaseCleanup,
.Login = wh_Auth_BaseLogin,
.Logout = test_Logout,
.CheckRequestAuthorization = test_CheckRequestAuthorization,
.CheckKeyAuthorization = test_CheckKeyAuthorization,
.UserAdd = wh_Auth_BaseUserAdd,
.UserDelete = wh_Auth_BaseUserDelete,
.UserSetPermissions = wh_Auth_BaseUserSetPermissions,
.UserGet = wh_Auth_BaseUserGet,
.UserSetCredentials = wh_Auth_BaseUserSetCredentials};
static whAuthContext auth_ctx = {0};
#ifndef WOLFHSM_CFG_NO_CRYPTO
static whServerCryptoContext crypto[1] = {0};
#endif
/* Setup helper for memory transport mode */
static int _whTest_Auth_SetupMemory(whClientContext** out_client)
{
int rc = WH_ERROR_OK;
whAuthPermissions permissions;
uint16_t out_user_id;
int i;
/* Reset so logout-count assertions are self-contained across repeated
* invocations of the suite in a single process. */
test_logoutCallCount = 0;
/* Initialize transport memory config - avoid compound literals for C90 */
tmcf->req = (whTransportMemCsr*)req_buffer;
tmcf->req_size = sizeof(req_buffer);
tmcf->resp = (whTransportMemCsr*)resp_buffer;
tmcf->resp_size = sizeof(resp_buffer);
/* Client configuration - avoid compound literals for C90 compatibility */
cc_conf->transport_cb = tccb;
cc_conf->transport_context = (void*)tmcc;
cc_conf->transport_config = (void*)tmcf;
cc_conf->client_id = WH_TEST_DEFAULT_CLIENT_ID;
c_conf->comm = cc_conf;
/* Server configuration */
cs_conf->transport_cb = tscb;
cs_conf->transport_context = (void*)tmsc;
cs_conf->transport_config = (void*)tmcf;
cs_conf->server_id = 124;
/* Flash RAM sim configuration */
fc_conf->size = FLASH_RAM_SIZE;
fc_conf->sectorSize = FLASH_RAM_SIZE / 2;
fc_conf->pageSize = 8;
fc_conf->erasedByte = (uint8_t)0;
fc_conf->memory = memory;
/* Initialize NVM */
WH_TEST_RETURN_ON_FAIL(whTest_NvmCfgBackend(
WH_NVM_TEST_BACKEND_FLASH, &nvm_setup, n_conf, fc_conf, fc, fcb));
WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf));
#ifndef WOLFHSM_CFG_NO_CRYPTO
WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init());
WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID));
#endif
/* Set up auth context following wh_posix_server pattern */
static void* auth_backend_context = NULL;
static whAuthConfig auth_config = {0};
auth_config.cb = &default_auth_cb;
auth_config.context = auth_backend_context;
rc = wh_Auth_Init(&auth_ctx, &auth_config);
if (rc != WH_ERROR_OK) {
WH_ERROR_PRINT("Failed to initialize Auth Manager: %d\n", rc);
return rc;
}
/* Add and admin user with permissions for everything */
memset(&permissions, 0xFF, sizeof(whAuthPermissions));
permissions.keyIdCount = 0;
for (i = 0; i < WH_AUTH_MAX_KEY_IDS; i++) {
permissions.keyIds[i] = 0;
}
rc = wh_Auth_BaseUserAdd(&auth_ctx, TEST_ADMIN_USERNAME, &out_user_id,
permissions, WH_AUTH_METHOD_PIN, TEST_ADMIN_PIN,
strlen(TEST_ADMIN_PIN));
if (rc != WH_ERROR_OK) {
WH_ERROR_PRINT("Failed to add admin user: %d\n", rc);
return rc;
}
/* Server config with auth - avoid compound literals for C90 */
whServerConfig s_conf[1] = {{0}};
s_conf->comm_config = cs_conf;
s_conf->nvm = nvm;
s_conf->auth = &auth_ctx;
#ifndef WOLFHSM_CFG_NO_CRYPTO
s_conf->crypto = crypto;
#if defined WOLF_CRYPTO_CB
s_conf->devId = INVALID_DEVID;
#endif
#endif
/* Initialize server first (must be before client) */
WH_TEST_RETURN_ON_FAIL(wh_Server_Init(server, s_conf));
/* Initialize client */
WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, c_conf));
/* Verify client comm is initialized */
WH_TEST_ASSERT_RETURN(client->comm != NULL);
WH_TEST_ASSERT_RETURN(client->comm->initialized == 1);
/* For memory transport, set server as connected (connect callback should
* handle this, but we set it explicitly to ensure it's connected) */
WH_TEST_RETURN_ON_FAIL(wh_Server_SetConnected(server, WH_COMM_CONNECTED));
/* Verify server is connected */
whCommConnected server_connected;
WH_TEST_RETURN_ON_FAIL(wh_Server_GetConnected(server, &server_connected));
WH_TEST_ASSERT_RETURN(server_connected == WH_COMM_CONNECTED);
/* Connect client to server - use non-blocking approach for memory transport
*/
uint32_t client_id, server_id;
/* Verify server is ready (should return NOTREADY if no message) */
WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY ==
wh_Server_HandleRequestMessage(server));
/* Send comm init request */
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitRequest(client));
/* Process server message */
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
/* Get comm init response */
WH_TEST_RETURN_ON_FAIL(
wh_Client_CommInitResponse(client, &client_id, &server_id));
WH_TEST_ASSERT_RETURN(client_id == client->comm->client_id);
*out_client = client;
return WH_ERROR_OK;
}
/* Cleanup helper for memory transport mode */
static int _whTest_Auth_CleanupMemory(void)
{
wh_Client_Cleanup(client);
wh_Server_Cleanup(server);
wh_Auth_Cleanup(&auth_ctx);
wh_Nvm_Cleanup(nvm);
#ifndef WOLFHSM_CFG_NO_CRYPTO
wc_FreeRng(crypto->rng);
wolfCrypt_Cleanup();
#endif
return WH_ERROR_OK;
}
#endif /* !WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP && WOLFHSM_CFG_ENABLE_SERVER */
/* ============================================================================
* Test Functions
* ============================================================================
*/
static int _whTest_Auth_LoginOp(whClientContext* client, whAuthMethod method,
const char* username, const void* auth_data,
uint16_t auth_data_len, int32_t* out_rc,
whUserId* out_user_id)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthLogin(client, method, username, auth_data,
auth_data_len, out_rc, out_user_id);
#else
WH_TEST_RETURN_ON_FAIL(wh_Client_AuthLoginRequest(
client, method, username, auth_data, auth_data_len));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthLoginResponse(client, out_rc, out_user_id);
#endif
}
static int _whTest_Auth_LogoutOp(whClientContext* client, whUserId user_id,
int32_t* out_rc)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthLogout(client, user_id, out_rc);
#else
WH_TEST_RETURN_ON_FAIL(wh_Client_AuthLogoutRequest(client, user_id));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthLogoutResponse(client, out_rc);
#endif
}
static int _whTest_Auth_UserAddOp(whClientContext* client, const char* username,
whAuthPermissions permissions,
whAuthMethod method, const void* credentials,
uint16_t credentials_len, int32_t* out_rc,
whUserId* out_user_id)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthUserAdd(client, username, permissions, method,
credentials, credentials_len, out_rc,
out_user_id);
#else
WH_TEST_RETURN_ON_FAIL(wh_Client_AuthUserAddRequest(
client, username, permissions, method, credentials, credentials_len));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthUserAddResponse(client, out_rc, out_user_id);
#endif
}
static int _whTest_Auth_UserDeleteOp(whClientContext* client, whUserId user_id,
int32_t* out_rc)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthUserDelete(client, user_id, out_rc);
#else
WH_TEST_RETURN_ON_FAIL(wh_Client_AuthUserDeleteRequest(client, user_id));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthUserDeleteResponse(client, out_rc);
#endif
}
static int _whTest_Auth_UserSetPermsOp(whClientContext* client,
whUserId user_id,
whAuthPermissions permissions,
int32_t* out_rc)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthUserSetPermissions(client, user_id, permissions,
out_rc);
#else
WH_TEST_RETURN_ON_FAIL(
wh_Client_AuthUserSetPermissionsRequest(client, user_id, permissions));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthUserSetPermissionsResponse(client, out_rc);
#endif
}
static int _whTest_Auth_UserSetCredsOp(
whClientContext* client, whUserId user_id, whAuthMethod method,
const void* current_credentials, uint16_t current_credentials_len,
const void* new_credentials, uint16_t new_credentials_len, int32_t* out_rc)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthUserSetCredentials(
client, user_id, method, current_credentials, current_credentials_len,
new_credentials, new_credentials_len, out_rc);
#else
WH_TEST_RETURN_ON_FAIL(wh_Client_AuthUserSetCredentialsRequest(
client, user_id, method, current_credentials, current_credentials_len,
new_credentials, new_credentials_len));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthUserSetCredentialsResponse(client, out_rc);
#endif
}
static int _whTest_Auth_UserGetOp(whClientContext* client, const char* username,
int32_t* out_rc, whUserId* out_user_id,
whAuthPermissions* out_permissions)
{
#if defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) || \
!defined(WOLFHSM_CFG_ENABLE_SERVER)
return wh_Client_AuthUserGet(client, username, out_rc, out_user_id,
out_permissions);
#else
WH_TEST_RETURN_ON_FAIL(wh_Client_AuthUserGetRequest(client, username));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
return wh_Client_AuthUserGetResponse(client, out_rc, out_user_id,
out_permissions);
#endif
}
static void _whTest_Auth_DeleteUserByName(whClientContext* client,
const char* username)
{
int32_t server_rc = 0;
whUserId user_id = WH_USER_ID_INVALID;
whAuthPermissions perms;
memset(&perms, 0, sizeof(perms));
_whTest_Auth_UserGetOp(client, username, &server_rc, &user_id, &perms);
if (server_rc == WH_ERROR_OK && user_id != WH_USER_ID_INVALID) {
_whTest_Auth_UserDeleteOp(client, user_id, &server_rc);
}
}
static int _whTest_Auth_BadArgs(void)
{
int rc = 0;
int loggedIn = 1;
whAuthContext ctx;
whAuthConfig config;
whAuthPermissions perms;
whUserId user_id = WH_USER_ID_INVALID;
int32_t server_rc = 0;
memset(&ctx, 0, sizeof(ctx));
memset(&config, 0, sizeof(config));
memset(&perms, 0, sizeof(perms));
WH_TEST_PRINT(" Test: Auth core bad args\n");
rc = wh_Auth_Init(NULL, &config);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Init(&ctx, NULL);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Cleanup(NULL);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Cleanup(&ctx);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc =
wh_Auth_Login(NULL, 0, WH_AUTH_METHOD_PIN, "user", "pin", 3, &loggedIn);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Login(&ctx, 0, WH_AUTH_METHOD_PIN, "user", "pin", 3, NULL);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthLoginRequest(NULL, WH_AUTH_METHOD_PIN, "user", "pin", 3);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthLoginResponse(NULL, &server_rc, &user_id);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Logout(NULL, 1);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Logout(&ctx, 1);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_UserAdd(&ctx, "user", &user_id, perms, WH_AUTH_METHOD_PIN,
"pin", 3);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_UserDelete(&ctx, 1);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_UserSetPermissions(&ctx, 1, perms);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_UserGet(&ctx, "user", &user_id, &perms);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_UserSetCredentials(&ctx, 1, WH_AUTH_METHOD_PIN, "pin", 3,
"new", 3);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_Logout(NULL, 999); /* This test may be troublesome if the port
* supports 999 users */
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
WH_TEST_PRINT(" Test: Auth client bad args\n");
rc = wh_Client_AuthLoginRequest(NULL, WH_AUTH_METHOD_PIN,
TEST_ADMIN_USERNAME, TEST_ADMIN_PIN,
strlen(TEST_ADMIN_PIN));
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthLogoutRequest(NULL, WH_USER_ID_INVALID);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Auth_CheckRequestAuthorization(NULL, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_LOGIN);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthUserAddRequest(NULL, "baduser", perms,
WH_AUTH_METHOD_NONE, "x", 1);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthUserDeleteRequest(NULL, WH_USER_ID_INVALID);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthUserSetPermissionsRequest(NULL, WH_USER_ID_INVALID,
perms);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthUserSetCredentialsRequest(
NULL, WH_USER_ID_INVALID, WH_AUTH_METHOD_PIN, NULL, 0, "new", 3);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_Client_AuthUserGetRequest(NULL, "missing");
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
return WH_TEST_SUCCESS;
}
static int _whTest_Auth_MessageBadArgs(void)
{
int rc = 0;
whMessageAuth_SimpleResponse simple = {0};
whMessageAuth_LoginRequest login_hdr = {0};
whMessageAuth_LoginRequest login_out = {0};
whMessageAuth_UserAddRequest add_hdr = {0};
whMessageAuth_UserAddRequest add_out = {0};
whMessageAuth_UserSetCredentialsRequest set_hdr = {0};
WH_TEST_PRINT(" Test: Auth message bad args\n");
rc = wh_MessageAuth_TranslateSimpleResponse(0, NULL, &simple);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_MessageAuth_TranslateSimpleResponse(0, &simple, NULL);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_MessageAuth_TranslateLoginRequest(0, NULL, 0, &login_out);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
rc = wh_MessageAuth_TranslateLoginRequest(0, &login_hdr, 0, &login_out);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
memset(&login_hdr, 0, sizeof(login_hdr));
login_hdr.auth_data_len =
(uint16_t)(WH_MESSAGE_AUTH_LOGIN_MAX_AUTH_DATA_LEN + 1);
rc = wh_MessageAuth_TranslateLoginRequest(0, &login_hdr, sizeof(login_hdr),
&login_out);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
rc = wh_MessageAuth_TranslateUserAddRequest(0, NULL, 0, &add_out);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
memset(&add_hdr, 0, sizeof(add_hdr));
add_hdr.credentials_len =
(uint16_t)(WH_MESSAGE_AUTH_USERADD_MAX_CREDENTIALS_LEN + 1);
rc = wh_MessageAuth_TranslateUserAddRequest(0, &add_hdr, sizeof(add_hdr),
&add_out);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
rc =
wh_MessageAuth_TranslateUserSetCredentialsRequest(0, NULL, 0, &set_hdr);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
memset(&set_hdr, 0, sizeof(set_hdr));
set_hdr.current_credentials_len = 4;
set_hdr.new_credentials_len = 4;
rc = wh_MessageAuth_TranslateUserSetCredentialsRequest(
0, &set_hdr, sizeof(set_hdr), &set_hdr);
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
return WH_TEST_SUCCESS;
}
/* Logout Tests */
int whTest_AuthLogout(whClientContext* client)
{
int32_t server_rc;
whUserId user_id;
int32_t login_rc;
whAuthPermissions out_perms;
if (client == NULL) {
return WH_ERROR_BADARGS;
}
/* Test 2: Logout after login */
WH_TEST_PRINT(" Test: Logout after login\n");
/* First login */
memset(&out_perms, 0, sizeof(out_perms));
login_rc = 0;
user_id = WH_USER_ID_INVALID;
/* Verify client is valid and comm is initialized */
WH_TEST_ASSERT_RETURN(client != NULL);
WH_TEST_ASSERT_RETURN(client->comm != NULL);
WH_TEST_ASSERT_RETURN(client->comm->initialized == 1);
WH_TEST_ASSERT_RETURN(client->comm->hdr != NULL);
WH_TEST_ASSERT_RETURN(client->comm->transport_cb != NULL);
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME,
TEST_ADMIN_PIN, 4, &login_rc, &user_id));
WH_TEST_ASSERT_RETURN(login_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id != WH_USER_ID_INVALID);
/* Then logout - use blocking version */
server_rc = 0;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LogoutOp(client, user_id, &server_rc));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_PRINT(" Test: Logout before login\n");
server_rc = 0;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LogoutOp(client, user_id, &server_rc));
WH_TEST_ASSERT_RETURN(server_rc != WH_ERROR_OK);
/* Test 3: Logout with invalid user id */
WH_TEST_PRINT(
" Test: Logout attempt with invalid user ID (should fail)\n");
server_rc = 0;
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LogoutOp(client, WH_USER_ID_INVALID, &server_rc));
/* Should return error for invalid user ID */
WH_TEST_ASSERT_RETURN(server_rc != WH_ERROR_OK);
return WH_TEST_SUCCESS;
}
/* Login Tests */
int whTest_AuthLogin(whClientContext* client)
{
int32_t server_rc;
whUserId user_id;
if (client == NULL) {
return WH_ERROR_BADARGS;
}
/* Test 1: Login with invalid credentials */
WH_TEST_PRINT(" Test: Login with invalid credentials\n");
server_rc = 0;
user_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN,
TEST_ADMIN_USERNAME, "wrong", 5,
&server_rc, &user_id));
if (server_rc == WH_AUTH_NOT_ENABLED) {
WH_TEST_PRINT("Server does not support authentication, skipping "
"authentication tests\n");
return WH_AUTH_NOT_ENABLED;
}
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_LOGIN_FAILED ||
server_rc != WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id == WH_USER_ID_INVALID);
/* Test 2: Login with valid credentials - use blocking version */
WH_TEST_PRINT(" Test: Login with valid credentials\n");
server_rc = 0;
user_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME,
TEST_ADMIN_PIN, 4, &server_rc, &user_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id != WH_USER_ID_INVALID);
/* Logout for next test */
_whTest_Auth_LogoutOp(client, user_id, &server_rc);
/* Test 3: Login with invalid username */
WH_TEST_PRINT(" Test: Login with invalid username\n");
server_rc = 0;
user_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN,
"nonexistent", TEST_ADMIN_PIN,
4, &server_rc, &user_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_LOGIN_FAILED ||
server_rc != WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id == WH_USER_ID_INVALID);
/* Test 4: Login if already logged in */
WH_TEST_PRINT(" Test: Login if already logged in\n");
/* First login */
server_rc = 0;
user_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME,
TEST_ADMIN_PIN, 4, &server_rc, &user_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id != WH_USER_ID_INVALID);
/* Try to login again without logout */
server_rc = 0;
whUserId user_id2 = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME,
TEST_ADMIN_PIN, 4, &server_rc, &user_id2));
/* Second login should fail */
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_LOGIN_FAILED ||
server_rc != WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Cleanup */
_whTest_Auth_LogoutOp(client, user_id, &server_rc);
return WH_TEST_SUCCESS;
}
/* Add User Tests */
int whTest_AuthAddUser(whClientContext* client)
{
int32_t server_rc;
whUserId user_id;
whAuthPermissions perms;
char long_username[34]; /* 33 chars + null terminator */
int rc;
if (client == NULL) {
return WH_ERROR_BADARGS;
}
/* Login as admin first */
whAuthPermissions admin_perms;
memset(&admin_perms, 0, sizeof(admin_perms));
server_rc = 0;
whUserId admin_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME,
TEST_ADMIN_PIN, 4, &server_rc, &admin_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
/* Test 1: Add user with invalid username (too long) */
WH_TEST_PRINT(" Test: Add user with invalid username (too long)\n");
memset(long_username, 'a', 33);
long_username[33] = '\0';
memset(&perms, 0, sizeof(perms));
server_rc = 0;
user_id = WH_USER_ID_INVALID;
/* Expect client-side rejection due to username length */
rc = wh_Client_AuthUserAddRequest(client, long_username, perms,
WH_AUTH_METHOD_PIN, "test", 4);
WH_TEST_ASSERT_RETURN(rc != WH_ERROR_OK || server_rc != WH_ERROR_OK ||
user_id == WH_USER_ID_INVALID);
/* Test 2: Add user with invalid permissions (keyIdCount > max) */
WH_TEST_PRINT(" Test: Add user with invalid permissions\n");
memset(&perms, 0, sizeof(perms));
perms.keyIdCount = WH_AUTH_MAX_KEY_IDS + 1; /* Invalid: exceeds max */
server_rc = 0;
user_id = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "testuser1", perms, WH_AUTH_METHOD_PIN,
"test", 4, &server_rc, &user_id);
/* Should clamp or reject invalid keyIdCount */
if (server_rc == WH_ERROR_OK) {
/* If it succeeds, keyIdCount should be clamped */
WH_TEST_ASSERT_RETURN(user_id != WH_USER_ID_INVALID);
/* Free the slot so the user table doesn't fill up */
_whTest_Auth_UserDeleteOp(client, user_id, &server_rc);
}
/* Test 3: Add user if already exists */
WH_TEST_PRINT(" Test: Add user if already exists\n");
memset(&perms, 0, sizeof(perms));
server_rc = 0;
user_id = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "testuser2", perms, WH_AUTH_METHOD_PIN,
"test", 4, &server_rc, &user_id);
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id != WH_USER_ID_INVALID);
/* Try to add same user again - should fail duplicate username */
whUserId user_id2 = WH_USER_ID_INVALID;
server_rc = 0;
_whTest_Auth_UserAddOp(client, "testuser2", perms, WH_AUTH_METHOD_PIN,
"test", 4, &server_rc, &user_id2);
WH_TEST_ASSERT_RETURN(server_rc != WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Done with testuser2, free the slot so subsequent tests stay within
* the user table limit */
_whTest_Auth_UserDeleteOp(client, user_id, &server_rc);
/* Test 4: Non-admin cannot add admin user */
WH_TEST_PRINT(" Test: Non-admin cannot add admin user\n");
{
whAuthPermissions nonadmin_add_perms;
memset(&nonadmin_add_perms, 0, sizeof(nonadmin_add_perms));
WH_AUTH_SET_ALLOWED_ACTION(nonadmin_add_perms, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_USER_ADD);
WH_AUTH_SET_IS_ADMIN(nonadmin_add_perms, 0);
server_rc = 0;
user_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_UserAddOp(
client, "addadmin_testuser", nonadmin_add_perms, WH_AUTH_METHOD_PIN,
"pass", 4, &server_rc, &user_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id != WH_USER_ID_INVALID);
_whTest_Auth_LogoutOp(client, admin_id, &server_rc);
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN,
"addadmin_testuser", "pass",
4, &server_rc, &user_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
/* Non-admin can add other non-admin users */
memset(&perms, 0, sizeof(perms));
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_UserAddOp(
client, "other_nonadmin", perms, WH_AUTH_METHOD_PIN, "test", 4,
&server_rc, &user_id2));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id2 != WH_USER_ID_INVALID);
/* Non-admin cannot add admin user */
memset(&perms, 0xFF, sizeof(perms));
perms.keyIdCount = 0;
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "wouldbe_admin", perms,
WH_AUTH_METHOD_PIN, "test", 4, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_PERMISSION_ERROR);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Non-admin can only pass on current or subset of own actions. */
WH_TEST_PRINT(
" Test: Non-admin cannot grant action it lacks\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_ALLOWED_ACTION(perms, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_USER_DELETE);
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_fail_act", perms,
WH_AUTH_METHOD_PIN, "pin", 3, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_PERMISSION_ERROR);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Non-admin can only pass on current or subset of own groups. */
WH_TEST_PRINT(
" Test: Non-admin cannot grant group it lacks\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_ALLOWED_GROUP(perms, WH_MESSAGE_GROUP_CRYPTO);
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_fail_grp", perms,
WH_AUTH_METHOD_PIN, "pin", 3, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_PERMISSION_ERROR);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Non-admin can not set actions outside of own group. */
WH_TEST_PRINT(
" Test: Non-admin cannot grant action without group flag\n");
memset(&perms, 0, sizeof(perms));
{
int _g = (WH_MESSAGE_GROUP_CRYPTO >> 8) & 0xFF;
uint32_t _w, _b;
WH_AUTH_ACTION_TO_WORD_AND_BITMASK(0u, _w, _b);
perms.actionPermissions[_g][_w] = _b;
}
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_fail_actbit", perms,
WH_AUTH_METHOD_PIN, "pin", 3, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_PERMISSION_ERROR);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Non-admin cannot create a user with empty credentials. */
WH_TEST_PRINT(
" Test: Non-admin cannot create user with empty credentials\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_fail_cred", perms,
WH_AUTH_METHOD_PIN, NULL, 0, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_PERMISSION_ERROR);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Non-admin clone of permissions. */
WH_TEST_PRINT(
" Test: Non-admin can grant identical permissions\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_ALLOWED_ACTION(perms, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_USER_ADD);
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_ok_same", perms,
WH_AUTH_METHOD_PIN, "pin", 3, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id2 != WH_USER_ID_INVALID);
}
_whTest_Auth_LogoutOp(client, user_id, &server_rc);
WH_TEST_RETURN_ON_FAIL(
_whTest_Auth_LoginOp(client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME,
TEST_ADMIN_PIN, 4, &server_rc, &admin_id));
/* Free user slots so the keyId subset block has room to add a fresh
* non-admin creator and its child. */
_whTest_Auth_DeleteUserByName(client, "testuser1");
_whTest_Auth_DeleteUserByName(client, "testuser2");
_whTest_Auth_DeleteUserByName(client, "other_nonadmin");
_whTest_Auth_DeleteUserByName(client, "addadmin_testuser");
_whTest_Auth_DeleteUserByName(client, "subset_ok_same");
/* Non-admin can only grant key IDs that appear in its own keyIds list. */
WH_TEST_PRINT(" Test: Non-admin keyId subset restrictions\n");
{
whAuthPermissions creator_perms;
whUserId creator_id = WH_USER_ID_INVALID;
whUserId subset_login = WH_USER_ID_INVALID;
whUserId child_id = WH_USER_ID_INVALID;
memset(&creator_perms, 0, sizeof(creator_perms));
WH_AUTH_SET_ALLOWED_ACTION(creator_perms, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_USER_ADD);
creator_perms.keyIdCount = 1;
creator_perms.keyIds[0] = 0x5555;
WH_AUTH_SET_IS_ADMIN(creator_perms, 0);
server_rc = 0;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_UserAddOp(
client, "subset_creator", creator_perms, WH_AUTH_METHOD_PIN,
"pin", 3, &server_rc, &creator_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(creator_id != WH_USER_ID_INVALID);
/* Switch context to subset_creator */
_whTest_Auth_LogoutOp(client, admin_id, &server_rc);
server_rc = 0;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LoginOp(
client, WH_AUTH_METHOD_PIN, "subset_creator", "pin", 3,
&server_rc, &subset_login));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(subset_login != WH_USER_ID_INVALID);
/* Test keyId not in caller's keyIds list */
WH_TEST_PRINT(" Test: Non-admin cannot grant keyId it lacks\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_ALLOWED_ACTION(perms, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_USER_ADD);
perms.keyIdCount = 1;
perms.keyIds[0] = 0x9999;
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_fail_key", perms,
WH_AUTH_METHOD_PIN, "pin", 3, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_AUTH_PERMISSION_ERROR);
WH_TEST_ASSERT_RETURN(user_id2 == WH_USER_ID_INVALID);
/* Test granting the same keyId the caller holds. */
WH_TEST_PRINT(" Test: Non-admin can grant same keyId\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_ALLOWED_ACTION(perms, WH_MESSAGE_GROUP_AUTH,
WH_MESSAGE_AUTH_ACTION_USER_ADD);
perms.keyIdCount = 1;
perms.keyIds[0] = 0x5555;
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
child_id = WH_USER_ID_INVALID;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_UserAddOp(
client, "subset_child", perms, WH_AUTH_METHOD_PIN, "pin", 3,
&server_rc, &child_id));
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(child_id != WH_USER_ID_INVALID);
/* Test granting strict subset (drop the keyId entirely) */
WH_TEST_PRINT(" Test: Non-admin can drop keyId on grant\n");
memset(&perms, 0, sizeof(perms));
WH_AUTH_SET_IS_ADMIN(perms, 0);
server_rc = 0;
user_id2 = WH_USER_ID_INVALID;
_whTest_Auth_UserAddOp(client, "subset_child_drop", perms,
WH_AUTH_METHOD_PIN, "pin", 3, &server_rc,
&user_id2);
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(user_id2 != WH_USER_ID_INVALID);
/* Restore admin context for cleanup */
_whTest_Auth_LogoutOp(client, subset_login, &server_rc);
server_rc = 0;
WH_TEST_RETURN_ON_FAIL(_whTest_Auth_LoginOp(
client, WH_AUTH_METHOD_PIN, TEST_ADMIN_USERNAME, TEST_ADMIN_PIN,
4, &server_rc, &admin_id));
_whTest_Auth_DeleteUserByName(client, "subset_child_drop");
_whTest_Auth_DeleteUserByName(client, "subset_child");
_whTest_Auth_DeleteUserByName(client, "subset_creator");
}
/* Test Admin is exempt from the empty-credential restriction. */
WH_TEST_PRINT(
" Test: Admin can create user with empty credentials\n");
{
whAuthPermissions admin_child_perms;
whUserId admin_child_id = WH_USER_ID_INVALID;
memset(&admin_child_perms, 0, sizeof(admin_child_perms));
WH_AUTH_SET_IS_ADMIN(admin_child_perms, 0);
server_rc = 0;
_whTest_Auth_UserAddOp(client, "admin_nocred", admin_child_perms,
WH_AUTH_METHOD_NONE, NULL, 0, &server_rc,