passless-rs 0.12.0

FIDO2 security token emulator
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
//! End-to-end tests for FIDO2 client commands
//!
//! These tests verify that the client commands work correctly with a running authenticator:
//! 1. Start an authenticator
//! 2. Register some credentials
//! 3. Use client commands to manage them (info, list, delete, reset)
//!
//! Run with: cargo test --test e2e_client -- --ignored --test-threads=1

#[allow(dead_code)]
mod harness;

use harness::AuthenticatorHarness;

use soft_fido2::request::{ClientDataHash, MakeCredentialRequest};
use soft_fido2::{Client, TransportList};
use soft_fido2_ctap::types::{RelyingParty, User};

use base64::Engine;
use sha2::{Digest, Sha256};

const RP_ID: &str = "example.com";
const ORIGIN: &str = "https://example.com";
const CMD_PASSLESS_RESET_UV_RETRIES: u8 = 0x42;
const RESET_UV_RETRIES_SUBCOMMAND: u8 = 0x01;

/// Helper to connect to the first available authenticator
fn connect_to_authenticator() -> Result<soft_fido2::Transport, Box<dyn std::error::Error>> {
    let list = TransportList::enumerate()?;
    if list.is_empty() {
        return Err("No authenticators found".into());
    }
    Ok(list.get(0).ok_or("Failed to get authenticator")?)
}

/// Helper to generate client data hash for registration
fn generate_client_data_hash_for_registration(challenge: &[u8]) -> ClientDataHash {
    let client_data = format!(
        r#"{{"type":"webauthn.create","challenge":"{}","origin":"{}","crossOrigin":false}}"#,
        base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(challenge),
        ORIGIN
    );
    let hash = Sha256::digest(client_data.as_bytes());
    ClientDataHash::from_slice(&hash).expect("Failed to create client data hash")
}

/// Register a test credential
fn register_credential(
    transport: &mut soft_fido2::Transport,
    user_id: &[u8],
    user_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    register_credential_with_rp(transport, user_id, user_name, RP_ID)
}

/// Register a test credential with custom RP ID
fn register_credential_with_rp(
    transport: &mut soft_fido2::Transport,
    user_id: &[u8],
    user_name: &str,
    rp_id: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let challenge: [u8; 32] = rand::random();
    let client_data_hash = generate_client_data_hash_for_registration(&challenge);

    let rp = RelyingParty {
        id: rp_id.to_string(),
        name: Some("Example Corp".to_string()),
    };

    let user = User {
        id: user_id.to_vec(),
        name: Some(user_name.to_string()),
        display_name: Some(user_name.split('@').next().unwrap().to_string()),
    };

    let request = MakeCredentialRequest::new(client_data_hash, rp, user)
        .with_resident_key(true)
        .with_user_verification(true);

    Client::make_credential(transport, request)?;
    Ok(())
}

/// Get credential count using credential management
#[allow(dead_code)]
fn get_credential_count(
    transport: &mut soft_fido2::Transport,
) -> Result<u32, Box<dyn std::error::Error>> {
    use soft_fido2_ctap::cbor::MapBuilder;
    let mut builder = MapBuilder::new();
    builder = builder.insert(0x01, 0x01u8)?; // getCredsMetadata
    let params = builder.build()?;

    let response = transport.send_ctap_command(0x0A, &params, 30000)?;

    // Response has status byte
    if response.is_empty() {
        return Err("Empty response".into());
    }
    if response[0] != 0x00 {
        eprintln!(
            "DEBUG: Bad status: {:02x}, response len: {}",
            response[0],
            response.len()
        );
        eprintln!(
            "DEBUG: First 20 bytes: {:02x?}",
            &response[..20.min(response.len())]
        );
        return Err(format!("Bad status: {:02x}", response[0]).into());
    }

    use ciborium::Value;
    let cbor_value: Value = ciborium::from_reader(&response[1..])?;

    if let Value::Map(map) = cbor_value {
        for (k, v) in map {
            if let Value::Integer(key) = k
                && key == 1.into()
                && let Value::Integer(count) = v
            {
                // existingResidentCredentialsCount
                return Ok(count.try_into().unwrap_or(0));
            }
        }
    }

    Err("Failed to get count".into())
}

#[test]
#[ignore]
fn test_client_info() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Client Info Command");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    println!("📋 Getting authenticator info...");
    let response = Client::authenticator_get_info(&mut transport).expect("Failed to get info");

    println!("   ✓ Received {} bytes", response.len());

    // The response is pure CBOR without a status byte prefix (soft-fido2 client handles status internally)
    assert!(!response.is_empty());

    // Decode as CBOR Value to inspect
    use ciborium::Value;
    let cbor_value: Value = ciborium::from_reader(&response[..]).expect("Failed to decode CBOR");

    // Verify it's a map
    if let Value::Map(map) = cbor_value {
        println!("   ✓ Decoded authenticator info with {} fields", map.len());

        // Look for versions (key 0x01)
        for (k, v) in &map {
            if let Value::Integer(key) = k
                && *key == 1.into()
                && let Value::Array(versions) = v
            {
                println!("   ✓ Found {} version(s)", versions.len());
                assert!(!versions.is_empty(), "No versions");
            }
        }

        let mut saw_options = false;
        for (k, v) in &map {
            if let Value::Integer(key) = k
                && *key == 4.into()
                && let Value::Map(options) = v
            {
                saw_options = true;

                let option_bool = |option_name: &str| {
                    options.iter().find_map(|(option_key, option_value)| {
                        if let Value::Text(name) = option_key
                            && name == option_name
                            && let Value::Bool(value) = option_value
                        {
                            return Some(*value);
                        }

                        None
                    })
                };

                assert_eq!(option_bool("rk"), Some(true));
                assert_eq!(option_bool("up"), Some(true));
                assert_eq!(option_bool("plat"), Some(true));
                assert_eq!(option_bool("uv"), Some(true));
                assert_eq!(option_bool("clientPin"), Some(false));
                assert_eq!(option_bool("alwaysUv"), Some(false));
            }
        }

        assert!(saw_options, "getInfo response should include options");
    } else {
        panic!("Expected CBOR map");
    }

    println!("\n✅ Client info command works!\n");
}

#[test]
#[ignore]
fn test_client_list_credentials() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Client List Command");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Register two credentials
    println!("📝 Registering test credentials...");
    register_credential(&mut transport, &[1, 2, 3, 4], "alice@example.com")
        .expect("Failed to register credential 1");
    println!("   ✓ Registered credential 1");

    register_credential(&mut transport, &[5, 6, 7, 8], "bob@example.com")
        .expect("Failed to register credential 2");
    println!("   ✓ Registered credential 2");

    // List credentials using soft-fido2 credential management API
    println!("\n📋 Testing credential management API...");

    use soft_fido2::request::CredentialManagementRequest;

    // Get metadata
    let request = CredentialManagementRequest::new(None);
    match Client::get_credentials_metadata(&mut transport, request) {
        Ok(metadata) => {
            println!(
                "   ✓ Existing credentials: {}",
                metadata.existing_resident_credentials_count
            );
            println!(
                "   ✓ Max remaining: {}",
                metadata.max_possible_remaining_resident_credentials_count
            );
            assert_eq!(
                metadata.existing_resident_credentials_count, 2,
                "Expected 2 credentials"
            );
        }
        Err(e) => {
            println!("   ⚠ Could not get metadata: {:?}", e);
        }
    }

    // Enumerate RPs
    let request = CredentialManagementRequest::new(None);
    match Client::enumerate_rps(&mut transport, request) {
        Ok(rps) => {
            println!("   ✓ Found {} RP(s)", rps.len());
            assert!(!rps.is_empty(), "Expected at least one RP");

            // Enumerate credentials for each RP
            for rp in &rps {
                println!("   ✓ RP: {}", rp.id);
                let creds_request =
                    soft_fido2::request::EnumerateCredentialsRequest::new(None, rp.rp_id_hash);
                match Client::enumerate_credentials(&mut transport, creds_request) {
                    Ok(creds) => {
                        println!("     ✓ Found {} credential(s)", creds.len());
                        for (i, cred) in creds.iter().enumerate() {
                            if let Some(name) = &cred.user.name {
                                println!("       {}. User: {}", i + 1, name);
                            }
                        }
                    }
                    Err(e) => {
                        println!("     ⚠ Could not enumerate credentials: {:?}", e);
                    }
                }
            }
        }
        Err(e) => {
            println!("   ⚠ Could not enumerate RPs: {:?}", e);
        }
    }

    println!("\n✅ Client list command works!\n");
}

#[test]
#[ignore]
fn test_client_delete_credential() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Client Delete Command");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Register a credential
    println!("📝 Registering test credential...");
    register_credential(&mut transport, &[1, 2, 3, 4], "alice@example.com")
        .expect("Failed to register credential");
    println!("   ✓ Registered credential");

    // Get UV token for credential management
    use soft_fido2::PinProtocol;
    println!("\n🔐 Acquiring UV token for credential management...");
    let pin_uv_auth =
        match Client::get_uv_token_for_credential_management(&mut transport, PinProtocol::V2) {
            Ok(token) => {
                println!("   ✓ UV token acquired");
                Some(token)
            }
            Err(e) => {
                println!("   ⚠ UV token failed: {:?}", e);
                println!("   Note: This test requires UV support");
                println!("\n✅ Client delete command structure is implemented!");
                return;
            }
        };

    // Get the credential ID
    use soft_fido2::request::CredentialManagementRequest;

    let request = CredentialManagementRequest::new(pin_uv_auth.clone());
    let rps = Client::enumerate_rps(&mut transport, request).expect("Failed to enumerate RPs");
    assert!(!rps.is_empty(), "No RPs found");

    let rp = &rps[0];
    let creds_request =
        soft_fido2::request::EnumerateCredentialsRequest::new(pin_uv_auth.clone(), rp.rp_id_hash);
    let creds = Client::enumerate_credentials(&mut transport, creds_request)
        .expect("Failed to enumerate credentials");
    assert!(!creds.is_empty(), "No credentials found");

    let credential_id = creds[0].credential_id.id.clone();
    println!(
        "   ✓ Found credential ID: {}",
        hex::encode(&credential_id[..8.min(credential_id.len())])
    );

    // Delete the credential
    println!("\n🗑️  Deleting credential...");
    let delete_request =
        soft_fido2::request::DeleteCredentialRequest::new(pin_uv_auth.clone(), credential_id);
    Client::delete_credential(&mut transport, delete_request).expect("Failed to delete credential");
    println!("   ✓ Credential deleted successfully");

    // Verify deletion
    let request = CredentialManagementRequest::new(pin_uv_auth);
    match Client::get_credentials_metadata(&mut transport, request) {
        Ok(metadata) => {
            println!(
                "   ✓ Credentials after deletion: {}",
                metadata.existing_resident_credentials_count
            );
            assert_eq!(
                metadata.existing_resident_credentials_count, 0,
                "Credential was not deleted"
            );
        }
        Err(e) => {
            println!("   ⚠ Could not verify deletion: {:?}", e);
        }
    }

    println!("\n✅ Client delete command works!\n");
}

#[test]
#[ignore]
fn test_client_reset() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Client Reset Command");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Register some credentials
    println!("📝 Registering test credentials...");
    register_credential(&mut transport, &[1, 2, 3, 4], "alice@example.com")
        .expect("Failed to register credential 1");
    register_credential(&mut transport, &[5, 6, 7, 8], "bob@example.com")
        .expect("Failed to register credential 2");
    println!("   ✓ Registered 2 credentials");

    // Reset the authenticator
    println!("\n🔄 Resetting authenticator...");
    transport
        .send_ctap_command(0x07, &[], 30000)
        .expect("Failed to reset");
    println!("   ✓ Reset command executed successfully");

    println!("   Note: Reset clears all credentials from storage");
    println!("   ✓ Reset command structure is implemented");

    println!("\n✅ Client reset command works!\n");
}

#[test]
fn test_client_cli_help() {
    // Test that the CLI commands are properly registered
    use clap::CommandFactory;
    use passless_core::Args;

    let app = Args::command();
    let client_cmd = app
        .find_subcommand("client")
        .expect("client subcommand not found");

    // Verify all expected subcommands exist
    let subcommands: Vec<_> = client_cmd.get_subcommands().map(|c| c.get_name()).collect();

    assert!(subcommands.contains(&"info"));
    assert!(subcommands.contains(&"reset"));
    assert!(subcommands.contains(&"list"));
    assert!(subcommands.contains(&"delete"));
    assert!(subcommands.contains(&"pin"));

    println!("✓ All client subcommands are registered:");
    for cmd in &subcommands {
        if *cmd != "help" {
            println!("  - {}", cmd);
        }
    }
}

#[test]
#[ignore]
fn test_client_list_with_rp_filter() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Client List with RP Filter");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Register credentials for different RPs
    println!("📝 Registering test credentials for multiple RPs...");

    register_credential_with_rp(
        &mut transport,
        &[1, 2, 3],
        "alice@example.com",
        "example.com",
    )
    .expect("Failed to register credential 1");
    println!("   ✓ Registered credential for example.com");

    register_credential_with_rp(&mut transport, &[4, 5, 6], "bob@github.com", "github.com")
        .expect("Failed to register credential 2");
    println!("   ✓ Registered credential for github.com");

    register_credential_with_rp(
        &mut transport,
        &[7, 8, 9],
        "charlie@google.com",
        "google.com",
    )
    .expect("Failed to register credential 3");
    println!("   ✓ Registered credential for google.com");

    // Verify all RPs are registered
    println!("\n📋 Verifying all RPs are present using API...");
    use soft_fido2::PinProtocol;
    use soft_fido2::request::CredentialManagementRequest;

    // Get UV token for credential management
    let pin_uv_auth =
        Client::get_uv_token_for_credential_management(&mut transport, PinProtocol::V2)
            .expect("Failed to get UV token");

    let request = CredentialManagementRequest::new(Some(pin_uv_auth.clone()));
    let all_rps = Client::enumerate_rps(&mut transport, request).expect("Failed to enumerate RPs");
    println!("   ✓ Found {} RPs total", all_rps.len());
    assert_eq!(all_rps.len(), 3, "Expected 3 RPs");

    // Verify specific RPs are present
    let rp_ids: Vec<String> = all_rps.iter().map(|rp| rp.id.clone()).collect();
    assert!(
        rp_ids.contains(&"example.com".to_string()),
        "Should have example.com"
    );
    assert!(
        rp_ids.contains(&"github.com".to_string()),
        "Should have github.com"
    );
    assert!(
        rp_ids.contains(&"google.com".to_string()),
        "Should have google.com"
    );

    println!("\n🔍 Testing RP filter logic (simulated)...");

    // Test filter matching logic
    println!("\n   Simulating filter: 'github'");
    let filtered: Vec<_> = all_rps
        .iter()
        .filter(|rp| rp.id.contains("github"))
        .collect();
    println!("   ✓ Would show {} RPs", filtered.len());
    assert_eq!(filtered.len(), 1, "Should match 1 RP");
    assert_eq!(filtered[0].id, "github.com", "Should match github.com");

    println!("\n   Simulating filter: 'com'");
    let filtered: Vec<_> = all_rps.iter().filter(|rp| rp.id.contains("com")).collect();
    println!("   ✓ Would show {} RPs", filtered.len());
    assert_eq!(filtered.len(), 3, "Should match all 3 RPs ending in .com");

    println!("\n   Simulating filter: 'nonexistent'");
    let filtered: Vec<_> = all_rps
        .iter()
        .filter(|rp| rp.id.contains("nonexistent"))
        .collect();
    println!("   ✓ Would show {} RPs", filtered.len());
    assert_eq!(filtered.len(), 0, "Should match 0 RPs");

    println!("\n✅ RP filtering logic works correctly!");
}

#[test]
#[ignore]
fn test_client_device_selection() {
    println!(
        "
═══════════════════════════════════════"
    );
    println!("  TEST: Device Selection");
    println!(
        "═══════════════════════════════════════
"
    );

    // Start two authenticators with different storage paths and unique device IDs
    let mut harness1 = AuthenticatorHarness::with_local().expect("Failed to create harness 1");
    harness1.set_device_ids(0x1111, 0x0001);

    let mut harness2 = AuthenticatorHarness::with_local().expect("Failed to create harness 2");
    harness2.set_device_ids(0x2222, 0x0002);

    println!("Starting first authenticator...");
    harness1.start().expect("Failed to start authenticator 1");
    println!(
        "   ✓ First authenticator started
"
    );

    println!("Starting second authenticator...");
    harness2
        .start_and_wait_for_device_count(2)
        .expect("Failed to start authenticator 2");
    println!(
        "   ✓ Second authenticator started
"
    );

    // Connect and enumerate devices
    println!("📋 Enumerating devices...");
    let list = TransportList::enumerate().expect("Failed to enumerate");
    println!("   ✓ Found {} device(s)", list.len());
    assert_eq!(list.len(), 2, "Expected 2 authenticators");

    // Register a credential on each device
    println!(
        "
📝 Registering credentials on each device..."
    );

    let mut transport1 = list.get(0).expect("Failed to get device 0");
    transport1.open().expect("Failed to open device 0");
    register_credential_with_rp(
        &mut transport1,
        &[1, 2, 3],
        "alice@device0.com",
        "device0.example.com",
    )
    .expect("Failed to register on device 0");
    println!("   ✓ Registered credential on device 0");
    transport1.close();

    let mut transport2 = list.get(1).expect("Failed to get device 1");
    transport2.open().expect("Failed to open device 1");
    register_credential_with_rp(
        &mut transport2,
        &[4, 5, 6],
        "bob@device1.com",
        "device1.example.com",
    )
    .expect("Failed to register on device 1");
    println!("   ✓ Registered credential on device 1");
    transport2.close();

    // Test device selection via API
    println!(
        "
🔍 Testing device selection..."
    );

    // Verify device 0 has the first credential
    println!(
        "
   Checking device 0:"
    );
    let mut transport = list.get(0).expect("Failed to get device 0");
    transport.open().expect("Failed to open device 0");

    use soft_fido2::PinProtocol;

    let pin_uv_auth =
        Client::get_uv_token_for_credential_management(&mut transport, PinProtocol::V2)
            .expect("Failed to get UV token");

    let request = soft_fido2::request::CredentialManagementRequest::new(Some(pin_uv_auth));
    let rps = Client::enumerate_rps(&mut transport, request)
        .expect("Failed to enumerate RPs on device 0");

    assert_eq!(rps.len(), 1, "Device 0 should have 1 RP");
    assert_eq!(
        rps[0].id, "device0.example.com",
        "Device 0 should have device0.example.com"
    );
    println!("   ✓ Device 0 has credential for device0.example.com");
    transport.close();

    // Verify device 1 has the second credential
    println!(
        "
   Checking device 1:"
    );
    let mut transport = list.get(1).expect("Failed to get device 1");
    transport.open().expect("Failed to open device 1");

    let pin_uv_auth =
        Client::get_uv_token_for_credential_management(&mut transport, PinProtocol::V2)
            .expect("Failed to get UV token");

    let request = soft_fido2::request::CredentialManagementRequest::new(Some(pin_uv_auth));
    let rps = Client::enumerate_rps(&mut transport, request)
        .expect("Failed to enumerate RPs on device 1");

    assert_eq!(rps.len(), 1, "Device 1 should have 1 RP");
    assert_eq!(
        rps[0].id, "device1.example.com",
        "Device 1 should have device1.example.com"
    );
    println!("   ✓ Device 1 has credential for device1.example.com");
    transport.close();

    println!(
        "
✅ Device selection works correctly!"
    );
    println!("   - Each device maintains separate credential storage");
    println!("   - Devices can be accessed independently by index");
}

#[test]
#[ignore]
fn test_client_pin_set_and_change() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Client PIN Set and Change");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Get initial authenticator info
    println!("📋 Getting initial authenticator info...");
    let response = Client::authenticator_get_info(&mut transport).expect("Failed to get info");

    use ciborium::Value;
    let cbor_value: Value = ciborium::from_reader(&response[..]).expect("Failed to decode CBOR");

    // Check if clientPin is supported (clientPin present means PIN is supported)
    // According to CTAP spec:
    // - clientPin: true = PIN capability AND PIN is set
    // - clientPin: false = PIN capability but PIN is NOT set
    // - clientPin not present = no PIN capability
    let mut client_pin_present = false;
    if let Value::Map(map) = &cbor_value {
        for (k, v) in map {
            if let Value::Integer(key) = k
                && *key == 4.into()
                && let Value::Map(options) = v
            {
                for (opt_k, opt_v) in options {
                    if let (Value::Text(k), Value::Bool(_v)) = (opt_k, opt_v)
                        && k == "clientPin"
                    {
                        client_pin_present = true;
                    }
                }
            }
        }
    }

    assert!(client_pin_present, "Authenticator should support clientPin");
    println!("   ✓ Authenticator supports clientPin");

    // Test PIN set
    println!("\n🔐 Testing PIN set...");
    let test_pin = "1234";

    let mut encapsulation =
        soft_fido2::PinUvAuthEncapsulation::new(&mut transport, soft_fido2::PinProtocol::V2)
            .expect("Failed to create PIN encapsulation");

    encapsulation
        .set_pin(&mut transport, test_pin)
        .expect("Failed to set PIN");
    println!("   ✓ PIN set successfully");

    // Verify PIN is set by checking authenticator info
    println!("\n📋 Verifying PIN is set...");
    let response = Client::authenticator_get_info(&mut transport).expect("Failed to get info");
    let cbor_value: Value = ciborium::from_reader(&response[..]).expect("Failed to decode CBOR");

    let mut pin_set = false;
    if let Value::Map(map) = &cbor_value {
        for (k, v) in map {
            if let Value::Integer(key) = k
                && *key == 4.into()
                && let Value::Map(options) = v
            {
                for (opt_k, opt_v) in options {
                    if let (Value::Text(k), Value::Bool(v)) = (opt_k, opt_v)
                        && k == "clientPin"
                    {
                        pin_set = *v;
                    }
                }
            }
        }
    }

    assert!(pin_set, "PIN should be set");
    println!("   ✓ PIN is set on authenticator");

    // Test PIN change
    println!("\n🔐 Testing PIN change...");
    let new_pin = "5678";

    let mut encapsulation =
        soft_fido2::PinUvAuthEncapsulation::new(&mut transport, soft_fido2::PinProtocol::V2)
            .expect("Failed to create PIN encapsulation");

    encapsulation
        .change_pin(&mut transport, test_pin, new_pin)
        .expect("Failed to change PIN");
    println!("   ✓ PIN changed successfully");

    println!("\n✅ PIN set and change commands work!\n");
}

/// Helper to check if PIN is set via authenticator info
fn is_pin_set(transport: &mut soft_fido2::Transport) -> bool {
    let response = Client::authenticator_get_info(transport).expect("Failed to get info");

    use ciborium::Value;
    let cbor_value: Value = ciborium::from_reader(&response[..]).expect("Failed to decode CBOR");

    if let Value::Map(map) = &cbor_value {
        for (k, v) in map {
            if let Value::Integer(key) = k
                && *key == 4.into()
                && let Value::Map(options) = v
            {
                for (opt_k, opt_v) in options {
                    if let (Value::Text(k), Value::Bool(v)) = (opt_k, opt_v)
                        && k == "clientPin"
                    {
                        return *v;
                    }
                }
            }
        }
    }
    false
}

/// Helper to set PIN via soft-fido2 API
fn set_pin(
    transport: &mut soft_fido2::Transport,
    pin: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut encapsulation =
        soft_fido2::PinUvAuthEncapsulation::new(transport, soft_fido2::PinProtocol::V2)?;
    encapsulation.set_pin(transport, pin)?;
    Ok(())
}

/// Helper to get UV token with PIN
fn get_uv_token_with_pin(
    transport: &mut soft_fido2::Transport,
    pin: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let mut encapsulation =
        soft_fido2::PinUvAuthEncapsulation::new(transport, soft_fido2::PinProtocol::V2)?;
    // Permission: 0x01 = makeCredential, 0x02 = getAssertion
    let token = encapsulation.get_pin_uv_auth_token_using_pin_with_permissions(
        transport, pin, 0x03, // makeCredential | getAssertion
        None,
    )?;
    Ok(token)
}

fn get_uv_retries(transport: &mut soft_fido2::Transport) -> Result<u8, Box<dyn std::error::Error>> {
    use soft_fido2_ctap::cbor::MapBuilder;

    let params = MapBuilder::new().insert(0x02, 0x07u8)?.build()?;
    let response = transport.send_ctap_command(0x06, &params, 30000)?;

    if response.is_empty() {
        return Err("getUvRetries returned empty response".into());
    }

    use ciborium::Value;
    let cbor_value: Value = if response[0] == 0x00 {
        ciborium::from_reader(&response[1..])?
    } else {
        ciborium::from_reader(&response[..])?
    };

    if let Value::Map(map) = cbor_value {
        for (key, value) in map {
            if key == Value::Integer(0x05.into())
                && let Value::Integer(retries) = value
            {
                return u8::try_from(i128::from(retries)).map_err(|_| "invalid uvRetries".into());
            }
        }
    }

    Err("uvRetries missing from response".into())
}

fn build_authenticated_uv_reset_payload(
    transport: &mut soft_fido2::Transport,
    pin: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    use soft_fido2_ctap::cbor::MapBuilder;

    let mut encapsulation =
        soft_fido2::PinUvAuthEncapsulation::new(transport, soft_fido2::PinProtocol::V2)?;
    let token = encapsulation.get_pin_uv_auth_token_using_pin_with_permissions(
        transport, pin, 0x04, // credentialManagement
        None,
    )?;
    let auth_data = [CMD_PASSLESS_RESET_UV_RETRIES, RESET_UV_RETRIES_SUBCOMMAND];
    let pin_uv_auth_param = encapsulation.authenticate(&auth_data, &token)?;

    Ok(MapBuilder::new()
        .insert(0x01, RESET_UV_RETRIES_SUBCOMMAND)?
        .insert(0x03, 0x02u8)?
        .insert_bytes(0x04, &pin_uv_auth_param)?
        .build()?)
}

#[test]
#[ignore]
fn test_authenticated_uv_retry_reset_command() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Authenticated UV Retry Reset");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");
    set_pin(&mut transport, "1234").expect("Failed to set PIN");

    let unauthenticated = transport.send_ctap_command(CMD_PASSLESS_RESET_UV_RETRIES, &[], 30000);
    if let Ok(response) = unauthenticated {
        assert_ne!(response, vec![0x00, 0xa0]);
    }

    let payload = build_authenticated_uv_reset_payload(&mut transport, "1234")
        .expect("Failed to build authenticated reset payload");
    let authenticated = transport
        .send_ctap_command(CMD_PASSLESS_RESET_UV_RETRIES, &payload, 30000)
        .expect("Failed to send authenticated reset command");
    assert!(authenticated == vec![0x00, 0xa0] || authenticated == vec![0xa0]);
    assert_eq!(
        get_uv_retries(&mut transport).expect("Failed to get UV retries"),
        3
    );

    harness.stop();
}

#[test]
#[ignore]
fn test_pin_persistence_after_restart() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: PIN Persistence After Restart");
    println!("═══════════════════════════════════════\n");

    // Use a fixed path that persists across restarts
    let storage_path = std::env::temp_dir().join("passless_pin_persistence_test");
    let _ = std::fs::remove_dir_all(&storage_path); // Clean up from any previous run
    std::fs::create_dir_all(&storage_path).expect("Failed to create storage dir");

    // Start first authenticator instance with the fixed storage path
    println!("📦 Starting first authenticator instance...");
    println!("   Storage path: {}", storage_path.display());
    let mut harness = AuthenticatorHarness::with_local_path(storage_path.clone());
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Verify no PIN is set initially
    println!("📋 Checking initial PIN state...");
    assert!(
        !is_pin_set(&mut transport),
        "PIN should not be set initially"
    );
    println!("   ✓ No PIN set initially");

    // Set PIN
    println!("\n🔐 Setting PIN...");
    set_pin(&mut transport, "1234").expect("Failed to set PIN");
    println!("   ✓ PIN set");

    // Verify PIN is set
    assert!(is_pin_set(&mut transport), "PIN should be set");
    println!("   ✓ PIN is set in authenticator info");

    // Verify PIN file exists
    let pin_file = storage_path.join("pin_state.json");
    assert!(pin_file.exists(), "PIN state file should exist");
    println!("   ✓ PIN state file exists at {:?}", pin_file);

    // Stop the first instance
    println!("\n🔄 Stopping authenticator...");
    harness.stop();
    std::thread::sleep(std::time::Duration::from_millis(1000));

    // Start second instance with same storage path
    println!("📦 Starting second authenticator instance...");
    println!("   Storage path: {}", storage_path.display());
    let mut harness2 = AuthenticatorHarness::with_local_path(storage_path.clone());
    harness2
        .start()
        .expect("Failed to start second authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport2 = connect_to_authenticator().expect("Failed to connect");

    // Verify PIN is still set
    println!("📋 Verifying PIN persistence...");
    assert!(
        is_pin_set(&mut transport2),
        "PIN should still be set after restart"
    );
    println!("   ✓ PIN persisted across restart");

    // Verify we can authenticate with the PIN
    println!("\n🔐 Verifying PIN authentication works...");
    let token = get_uv_token_with_pin(&mut transport2, "1234");
    assert!(
        token.is_ok(),
        "Should be able to get UV token with correct PIN"
    );
    println!("   ✓ PIN authentication successful");

    println!("\n✅ PIN persistence test passed!\n");

    // Cleanup
    harness2.stop();
    let _ = std::fs::remove_dir_all(&storage_path);
}

#[test]
#[ignore]
fn test_pin_enforcement_required_blocks_without_pin() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: PIN Enforcement Required");
    println!("═══════════════════════════════════════\n");

    println!("📋 This test verifies that when pin.enforcement=required:");
    println!("   - Operations require PIN UV auth param");
    println!("   - Notification fallback is NOT available");
    println!();

    // Note: We can't fully test this without modifying the authenticator's
    // enforcement config at runtime. The enforcement is read from config at startup.
    // For a complete test, we would need to:
    // 1. Start authenticator with PASSLESS_PIN_ENFORCEMENT=required
    // 2. Set a PIN
    // 3. Try to register without PIN -> should fail
    // 4. Register with PIN -> should succeed

    // For now, test the default enforcement=optional behavior
    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Set PIN
    println!("🔐 Setting PIN...");
    set_pin(&mut transport, "1234").expect("Failed to set PIN");
    println!("   ✓ PIN set");

    // With enforcement=optional (default) and always_uv=false (default),
    // notification fallback should be used
    // We test this by attempting a registration (which should work via notification fallback
    // in E2E test mode with PASSLESS_E2E_AUTO_ACCEPT_UV=1)
    println!("\n📝 Testing registration with PIN set (enforcement=optional)...");
    register_credential(&mut transport, &[1, 2, 3, 4], "test@example.com")
        .expect("Registration should succeed with notification fallback");
    println!("   ✓ Registration succeeded (notification fallback worked)");

    println!("\n✅ PIN enforcement behavior verified!\n");
    println!("   Note: To fully test enforcement=required, start authenticator with:");
    println!("   PASSLESS_PIN_ENFORCEMENT=required");
}

#[test]
#[ignore]
fn test_pin_required_for_make_credential_with_always_uv() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: PIN Required for MakeCredential with always_uv");
    println!("═══════════════════════════════════════\n");

    // With enforcement=optional (default) and always_uv=true:
    // When PIN is set, operations should require PIN

    println!("📋 Test scenario:");
    println!("   - enforcement=optional (default)");
    println!("   - always_uv=true");
    println!("   - PIN is set");
    println!("   -> PIN should be required for UV");
    println!();

    // For this test, we simulate by checking that:
    // 1. Without PIN, UV token request would require PIN
    // 2. With PIN, UV token can be obtained

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Set PIN
    println!("🔐 Setting PIN...");
    set_pin(&mut transport, "1234").expect("Failed to set PIN");
    println!("   ✓ PIN set");

    // Get UV token with correct PIN - should succeed
    println!("\n🔐 Getting UV token with correct PIN...");
    let token_result = get_uv_token_with_pin(&mut transport, "1234");
    assert!(token_result.is_ok(), "Should get UV token with correct PIN");
    println!("   ✓ UV token obtained with correct PIN");

    // Get UV token with wrong PIN - should fail
    println!("\n🔐 Testing UV token with wrong PIN...");
    let wrong_token = get_uv_token_with_pin(&mut transport, "0000");
    assert!(
        wrong_token.is_err(),
        "Should NOT get UV token with wrong PIN"
    );
    println!("   ✓ UV token correctly rejected with wrong PIN");

    println!("\n✅ PIN verification for UV token works correctly!\n");
}

#[test]
#[ignore]
fn test_pin_registration_without_pin_set() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Registration Without PIN Set");
    println!("═══════════════════════════════════════\n");

    println!("📋 This test verifies that when no PIN is set:");
    println!("   - Registration uses notification-based UV");
    println!("   - No PIN is required");
    println!();

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Verify no PIN is set
    println!("📋 Checking PIN state...");
    assert!(!is_pin_set(&mut transport), "PIN should not be set");
    println!("   ✓ No PIN set");

    // Register without PIN - should succeed via notification
    println!("\n📝 Registering credential without PIN...");
    register_credential(&mut transport, &[1, 2, 3, 4], "nopin@example.com")
        .expect("Registration should succeed without PIN");
    println!("   ✓ Registration succeeded via notification UV");

    // Authenticate without PIN - should succeed
    println!("\n🔓 Authenticating without PIN...");
    let challenge: [u8; 32] = rand::random();
    let client_data_hash = generate_client_data_hash_for_registration(&challenge);

    use soft_fido2::request::GetAssertionRequest;
    let request =
        GetAssertionRequest::new(client_data_hash, RP_ID.to_string()).with_user_verification(true);

    let result = Client::get_assertion(&mut transport, request);
    assert!(result.is_ok(), "Authentication should succeed without PIN");
    println!("   ✓ Authentication succeeded via notification UV");

    println!("\n✅ Registration without PIN works correctly!\n");
}

#[test]
#[ignore]
fn test_pin_info_shows_correct_state() {
    println!("\n═══════════════════════════════════════");
    println!("  TEST: Authenticator Info Shows Correct PIN State");
    println!("═══════════════════════════════════════\n");

    let mut harness = AuthenticatorHarness::with_local().expect("Failed to create harness");
    harness.start().expect("Failed to start authenticator");
    std::thread::sleep(std::time::Duration::from_millis(500));

    let mut transport = connect_to_authenticator().expect("Failed to connect");

    // Check initial state - no PIN
    println!("📋 Initial state (no PIN):");
    let response = Client::authenticator_get_info(&mut transport).expect("Failed to get info");

    use ciborium::Value;
    let cbor_value: Value = ciborium::from_reader(&response[..]).expect("Failed to decode CBOR");

    let mut client_pin_value = None;
    let mut uv_value = None;

    if let Value::Map(map) = &cbor_value {
        for (k, v) in map {
            if let Value::Integer(key) = k
                && *key == 4.into()
                && let Value::Map(options) = v
            {
                for (opt_k, opt_v) in options {
                    if let (Value::Text(k), Value::Bool(v)) = (opt_k, opt_v) {
                        match k.as_str() {
                            "clientPin" => client_pin_value = Some(*v),
                            "uv" => uv_value = Some(*v),
                            _ => {}
                        }
                    }
                }
            }
        }
    }

    println!("   clientPin: {:?}", client_pin_value);
    println!("   uv: {:?}", uv_value);

    assert_eq!(
        client_pin_value,
        Some(false),
        "clientPin should be false (not set)"
    );
    assert_eq!(
        uv_value,
        Some(true),
        "uv should be true (capability exists)"
    );

    // Set PIN
    println!("\n🔐 Setting PIN...");
    set_pin(&mut transport, "1234").expect("Failed to set PIN");
    println!("   ✓ PIN set");

    // Check state after PIN is set
    println!("\n📋 State after PIN is set:");
    let response = Client::authenticator_get_info(&mut transport).expect("Failed to get info");
    let cbor_value: Value = ciborium::from_reader(&response[..]).expect("Failed to decode CBOR");

    let mut client_pin_value = None;
    let mut uv_value = None;

    if let Value::Map(map) = &cbor_value {
        for (k, v) in map {
            if let Value::Integer(key) = k
                && *key == 4.into()
                && let Value::Map(options) = v
            {
                for (opt_k, opt_v) in options {
                    if let (Value::Text(k), Value::Bool(v)) = (opt_k, opt_v) {
                        match k.as_str() {
                            "clientPin" => client_pin_value = Some(*v),
                            "uv" => uv_value = Some(*v),
                            _ => {}
                        }
                    }
                }
            }
        }
    }

    println!("   clientPin: {:?}", client_pin_value);
    println!("   uv: {:?}", uv_value);

    assert_eq!(
        client_pin_value,
        Some(true),
        "clientPin should be true (PIN is set)"
    );
    // Note: uv remains true because soft-fido2 doesn't dynamically change it
    // This is expected behavior - the UV enforcement is handled in passless callbacks

    println!("\n✅ Authenticator info shows correct PIN state!\n");
}