matter-commissioning 0.4.0

Matter commissioning state machine: setup payload, attestation, NOC issuance, network commissioning.
Documentation
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
//! Unit tests for the M6.5.2 `NetworkCommissioning` sub-cursor.
//!
//! Covers the `Stage::ReadNetworkCommissioningInfo` dispatch arm and
//! the `Expectation::NetworkCommissioningInfo` response handler's
//! Wi-Fi / Ethernet / Thread / malformed branching logic.
//!
//! Requires the `__test_shortcuts` cargo feature (see `Cargo.toml`
//! `[[test]]` section). The feature gates
//! `Commissioner::position_at_stage_for_test`, which jumps
//! the cursor past M6.4 attestation + NOC crypto that cannot be driven
//! with synthetic data. This is intentional test isolation: the real
//! crypto path is exercised in the in-source glass-box tests in
//! `src/state_machine/commissioner.rs::tests`.

// Test-code carve-out: see CLAUDE.md.
#![allow(clippy::unwrap_used, clippy::expect_used)]

use std::sync::{Arc, OnceLock};

use matter_cert::time::MatterTime;
use matter_commissioning::attestation::CdSigningRoots;
use matter_commissioning::noc::{FabricRecord, NocRng, SystemNocRng};
use matter_commissioning::setup::{
    CommissioningFlow, DiscoveryCapabilities, Discriminator, Passcode, SetupPayload,
};
use matter_commissioning::{
    Action, Commissioner, CommissionerConfig, CommissioningError, Expectation, NetworkCredentials,
    NetworkKind, PaaTrustStore, RemediationHint, SessionContext, Stage, TestStateSeeds,
    ThreadDataset, WiFiCredentials,
};
use matter_crypto::{RingSigner, Signer};

// ---------------------------------------------------------------------------
// Static fixtures (OnceLock pattern from sample_valid_config in commissioner.rs)
// ---------------------------------------------------------------------------

fn static_fabric() -> &'static FabricRecord {
    static FABRIC: OnceLock<FabricRecord> = OnceLock::new();
    FABRIC.get_or_init(|| {
        let (signer, _pkcs8) = RingSigner::generate().expect("ring keypair");
        let signer: Arc<dyn Signer> = Arc::new(signer);
        FabricRecord::new_root_only(
            0x0000_0000_0000_0001,
            signer,
            MatterTime::from_unix_secs(1_704_067_200),
            MatterTime::from_unix_secs(1_735_689_600),
            42,
            &SystemNocRng,
        )
        .expect("valid root fabric")
    })
}

fn static_setup() -> &'static SetupPayload {
    static SETUP: OnceLock<SetupPayload> = OnceLock::new();
    SETUP.get_or_init(|| SetupPayload {
        version: 0,
        vendor_id: Some(0xFFF1),
        product_id: Some(0x8000),
        commissioning_flow: CommissioningFlow::Standard,
        discovery_capabilities: DiscoveryCapabilities::ON_NETWORK,
        discriminator: Discriminator::new(0x0F00).expect("valid discriminator"),
        passcode: Passcode::new(20_202_021).expect("valid passcode"),
    })
}

fn static_paa() -> &'static PaaTrustStore {
    static PAA: OnceLock<PaaTrustStore> = OnceLock::new();
    PAA.get_or_init(PaaTrustStore::with_example_device_roots)
}

fn static_cd() -> &'static CdSigningRoots {
    static CD: OnceLock<CdSigningRoots> = OnceLock::new();
    CD.get_or_init(CdSigningRoots::with_example_device_roots)
}

/// Returns a fully-populated `CommissionerConfig<'static>` with Wi-Fi
/// credentials set (ssid=`b"matter"`, credentials=`b"hunter22"`).
fn make_wifi_config() -> CommissionerConfig<'static> {
    let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
    CommissionerConfig {
        pase_attestation_challenge: [0u8; 16],
        fabric: static_fabric(),
        setup_payload: static_setup(),
        paa_trust_store: static_paa(),
        cd_signing_roots: static_cd(),
        commissioner_node_id: 0x1,
        assigned_node_id: 0x2,
        ipk_epoch_key: [0x42_u8; 16],
        case_admin_subject: 0x1,
        admin_vendor_id: 0xFFF1,
        now: MatterTime::from_unix_secs(1_704_067_200),
        rng,
        network: NetworkCredentials::WiFi(WiFiCredentials {
            ssid: b"matter".to_vec(),
            credentials: b"hunter22".to_vec(),
        }),
    }
}

/// Returns a config without Wi-Fi credentials (for Ethernet-only tests).
fn make_ethernet_config() -> CommissionerConfig<'static> {
    let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
    CommissionerConfig {
        pase_attestation_challenge: [0u8; 16],
        fabric: static_fabric(),
        setup_payload: static_setup(),
        paa_trust_store: static_paa(),
        cd_signing_roots: static_cd(),
        commissioner_node_id: 0x1,
        assigned_node_id: 0x2,
        ipk_epoch_key: [0x42_u8; 16],
        case_admin_subject: 0x1,
        admin_vendor_id: 0xFFF1,
        now: MatterTime::from_unix_secs(1_704_067_200),
        rng,
        network: NetworkCredentials::AlreadyOnNetwork,
    }
}

// ---------------------------------------------------------------------------
// FeatureMap TLV encoder
// ---------------------------------------------------------------------------

/// Encode a bare `u32` `FeatureMap` value as the minimal-width TLV scalar
/// that `decode_feature_map` expects.
///
/// Matter attribute read responses return a bare TLV scalar (not wrapped
/// in a struct). The two encoding widths used in tests:
/// - `bits ≤ 0xFF`: 1-byte unsigned (TLV type byte `0x04`) followed by the
///   value byte.
/// - `bits ≤ 0xFFFF`: 2-byte unsigned (type `0x05`) followed by LE bytes.
#[allow(clippy::cast_possible_truncation)]
fn feature_map_tlv(bits: u32) -> Vec<u8> {
    if bits <= 0xFF {
        vec![0x04, bits as u8]
    } else {
        vec![0x05, (bits & 0xFF) as u8, ((bits >> 8) & 0xFF) as u8]
    }
}

// ---------------------------------------------------------------------------
// Cursor helper
// ---------------------------------------------------------------------------

/// Create a `Commissioner` positioned at `Stage::ReadNetworkCommissioningInfo`
/// using `Commissioner::position_at_stage_for_test` (gated by the
/// `__test_shortcuts` cargo feature).
///
/// This skips M6.4 attestation + NOC crypto; the full crypto path is covered
/// by the in-source glass-box tests in `commissioner.rs::tests`.
fn drive_to_read_network_info(config: CommissionerConfig<'static>) -> Commissioner {
    Commissioner::new(config)
        .expect("valid config must produce a Commissioner")
        .position_at_stage_for_test(
            Stage::ReadNetworkCommissioningInfo,
            TestStateSeeds::default(),
        )
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn wifi_only_feature_map_advances_to_wifi_setup() {
    let mut sm = drive_to_read_network_info(make_wifi_config());
    let action = sm.poll().expect("emit ReadAttribute");
    // Verify the emitted action has the right expectation.
    match action {
        matter_commissioning::Action::ReadAttribute { expect, .. } => {
            assert_eq!(expect, Expectation::NetworkCommissioningInfo);
        }
        other => panic!("expected ReadAttribute, got {other:?}"),
    }
    sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b001),
    )
    .expect("WiFi-only FeatureMap accepted");
    assert_eq!(sm.stage(), Stage::NetworkSetup);
}

#[test]
fn ethernet_only_feature_map_skips_to_evict_case() {
    let mut sm = drive_to_read_network_info(make_ethernet_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b100),
    )
    .expect("Ethernet-only FeatureMap accepted");
    assert_eq!(sm.stage(), Stage::EvictPreviousCaseSessions);
}

#[test]
fn wifi_creds_thread_only_featuremap_rejects_mismatch() {
    // Wi-Fi credentials against a Thread-only device: routing keys off the
    // *supplied* credential type, so the mismatch is reported as the
    // supplied type being unsupported by the device — `needed: WiFi`, NOT
    // `Thread`. (M9-C2 D6 routing; the pre-C2 behaviour reported the
    // device's offered type here.)
    let mut sm = drive_to_read_network_info(make_wifi_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    let Err(err) = sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b010),
    ) else {
        panic!("Wi-Fi creds on a Thread-only device should fail");
    };
    assert!(
        matches!(
            err,
            CommissioningError::NetworkFeatureUnsupported {
                needed: NetworkKind::WiFi,
            },
        ),
        "got {err:?}",
    );
}

#[test]
fn wifi_device_without_credentials_skips_network_setup() {
    // A Wi-Fi FeatureMap with NO wifi_credentials configured: the device is
    // already on the network (IP commissioning reached it there — e.g. a
    // second-fabric commission of an already-provisioned device; observed:
    // Tapo P110M, M6.6.5 validation). Mirror chip's AutoCommissioner: include
    // the network-setup stages ONLY when credentials are supplied; otherwise
    // skip straight past the Wi-Fi sub-cursor.
    let mut sm = drive_to_read_network_info(make_ethernet_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b001),
    )
    .expect("WiFi FeatureMap with no creds must skip network setup");
    assert_eq!(sm.stage(), Stage::EvictPreviousCaseSessions);
}

#[test]
fn empty_feature_map_is_malformed() {
    let mut sm = drive_to_read_network_info(make_wifi_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    let Err(err) = sm.on_response(Expectation::NetworkCommissioningInfo, &feature_map_tlv(0))
    else {
        panic!("Empty FeatureMap should fail");
    };
    assert!(
        matches!(
            err,
            CommissioningError::MalformedResponse(Stage::ReadNetworkCommissioningInfo),
        ),
        "got {err:?}",
    );
}

// ---------------------------------------------------------------------------
// NetworkSetup cursor helper (Wi-Fi path)
// ---------------------------------------------------------------------------

/// Drive the state machine from `ReadNetworkCommissioningInfo` all the
/// way to `Stage::NetworkSetup` using Task 16's composition pattern.
///
/// Avoids adding a new `position_at_stage_for_test(Stage::NetworkSetup, …)` call:
/// the `__test_shortcuts` surface stays minimal and the full flow through
/// `Expectation::NetworkCommissioningInfo` is exercised as a by-product.
fn drive_to_wifi_network_setup() -> Commissioner {
    let mut sm = drive_to_read_network_info(make_wifi_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b001),
    )
    .expect("WiFi FeatureMap accepted");
    sm
}

// ---------------------------------------------------------------------------
// M6.5.2 Task 17 — NetworkSetup dispatch (Wi-Fi) + NetworkConfigResponse handler
// ---------------------------------------------------------------------------

#[test]
fn wifi_network_setup_happy_path_emits_add_or_update() {
    let mut sm = drive_to_wifi_network_setup();
    let action = sm.poll().expect("poll");
    match action {
        matter_commissioning::Action::Invoke {
            session,
            cluster,
            command,
            payload,
            expect,
            ..
        } => {
            assert_eq!(session, matter_commissioning::SessionContext::Pase);
            assert_eq!(cluster, 0x0031);
            assert_eq!(command, 0x02);
            assert_eq!(expect, Expectation::NetworkConfigResponse);
            // Payload should contain the SSID "matter" literal.
            assert!(
                payload.windows(6).any(|w| w == b"matter"),
                "payload should contain SSID bytes: {payload:02x?}",
            );
        }
        other => panic!("expected Invoke, got {other:?}"),
    }
}

#[test]
fn wifi_network_setup_ok_response_advances_to_failsafe_before_wifi_enable() {
    let mut sm = drive_to_wifi_network_setup();
    let _ = sm.poll().expect("emit Invoke");
    let response = vec![0x15, 0x24, 0x00, 0x00, 0x18]; // { 0: 0_u8 } — OK
    sm.on_response(Expectation::NetworkConfigResponse, &response)
        .expect("ok response accepted");
    assert_eq!(sm.stage(), Stage::FailsafeBeforeNetworkEnable);
}

#[test]
fn wifi_network_setup_auth_failure_carries_remediation_hint() {
    let mut sm = drive_to_wifi_network_setup();
    let _ = sm.poll().expect("emit Invoke");
    // { 0: 7_u8 } = AuthFailure → CheckPassphrase
    let response = vec![0x15, 0x24, 0x00, 0x07, 0x18];
    let Err(err) = sm.on_response(Expectation::NetworkConfigResponse, &response) else {
        panic!("AuthFailure should fail");
    };
    match err {
        CommissioningError::NetworkRejected {
            stage,
            networking_status,
            remediation_hint,
            ..
        } => {
            assert_eq!(stage, Stage::NetworkSetup);
            assert_eq!(networking_status, 7);
            assert_eq!(remediation_hint, RemediationHint::CheckPassphrase);
        }
        other => panic!("expected NetworkRejected, got {other:?}"),
    }
}

#[test]
fn wifi_network_setup_bounds_exceeded_maps_to_slots_full() {
    let mut sm = drive_to_wifi_network_setup();
    let _ = sm.poll().expect("emit Invoke");
    let response = vec![0x15, 0x24, 0x00, 0x02, 0x18]; // BoundsExceeded
    let Err(err) = sm.on_response(Expectation::NetworkConfigResponse, &response) else {
        panic!("BoundsExceeded should fail");
    };
    match err {
        CommissioningError::NetworkRejected {
            remediation_hint, ..
        } => {
            assert_eq!(remediation_hint, RemediationHint::DeviceNetworkSlotsFull);
        }
        other => panic!("got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// M6.5.2 Task 18 — FailsafeBeforeNetworkEnable dispatch + second ArmFailSafe
// ---------------------------------------------------------------------------

fn drive_to_failsafe_before_wifi_enable() -> Commissioner {
    let mut sm = drive_to_wifi_network_setup();
    let _ = sm.poll().expect("AddOrUpdateWiFiNetwork");
    let ok = vec![0x15, 0x24, 0x00, 0x00, 0x18];
    sm.on_response(Expectation::NetworkConfigResponse, &ok)
        .expect("ok accepted");
    sm
}

#[test]
fn failsafe_before_wifi_enable_emits_second_arm_failsafe() {
    let mut sm = drive_to_failsafe_before_wifi_enable();
    let action = sm.poll().expect("emit second ArmFailSafe");
    match action {
        matter_commissioning::Action::Invoke {
            cluster,
            command,
            expect,
            ..
        } => {
            assert_eq!(cluster, 0x0030);
            assert_eq!(command, 0x00);
            assert_eq!(expect, Expectation::ArmFailsafeResponse);
        }
        other => panic!("expected Invoke, got {other:?}"),
    }
    let ok = vec![0x15, 0x24, 0x00, 0x00, 0x18];
    sm.on_response(Expectation::ArmFailsafeResponse, &ok)
        .expect("ok accepted");
    assert_eq!(sm.stage(), Stage::NetworkEnable);
}

// ---------------------------------------------------------------------------
// M6.5.2 Task 19 — NetworkEnable dispatch (Wi-Fi) + ConnectNetworkResponse handler
// ---------------------------------------------------------------------------

fn drive_to_wifi_network_enable() -> Commissioner {
    let mut sm = drive_to_failsafe_before_wifi_enable();
    let _ = sm.poll().expect("emit second ArmFailSafe");
    let ok = vec![0x15, 0x24, 0x00, 0x00, 0x18];
    sm.on_response(Expectation::ArmFailsafeResponse, &ok)
        .expect("ok accepted");
    sm
}

#[test]
fn wifi_network_enable_happy_path_emits_connect_network() {
    let mut sm = drive_to_wifi_network_enable();
    let action = sm.poll().expect("emit ConnectNetwork");
    match action {
        matter_commissioning::Action::Invoke {
            cluster,
            command,
            payload,
            expect,
            ..
        } => {
            assert_eq!(cluster, 0x0031);
            assert_eq!(command, 0x06);
            assert_eq!(expect, Expectation::ConnectNetworkResponse);
            assert!(payload.windows(6).any(|w| w == b"matter"));
        }
        other => panic!("expected Invoke, got {other:?}"),
    }
}

#[test]
fn wifi_network_enable_ok_response_advances_to_evict_case() {
    let mut sm = drive_to_wifi_network_enable();
    let _ = sm.poll().expect("emit Invoke");
    let response = vec![0x15, 0x24, 0x00, 0x00, 0x18]; // { 0: 0 } OK
    sm.on_response(Expectation::ConnectNetworkResponse, &response)
        .expect("ok accepted");
    assert_eq!(sm.stage(), Stage::EvictPreviousCaseSessions);
}

#[test]
fn wifi_network_enable_network_not_found_maps_to_check_ssid() {
    let mut sm = drive_to_wifi_network_enable();
    let _ = sm.poll().expect("emit Invoke");
    let response = vec![0x15, 0x24, 0x00, 0x05, 0x18]; // NetworkNotFound
    let Err(err) = sm.on_response(Expectation::ConnectNetworkResponse, &response) else {
        panic!("NetworkNotFound should fail");
    };
    match err {
        CommissioningError::NetworkRejected {
            stage,
            networking_status,
            remediation_hint,
            ..
        } => {
            assert_eq!(stage, Stage::NetworkEnable);
            assert_eq!(networking_status, 5);
            assert_eq!(remediation_hint, RemediationHint::CheckSsid);
        }
        other => panic!("got {other:?}"),
    }
}

#[test]
fn wifi_network_enable_unknown_status_maps_to_none() {
    let mut sm = drive_to_wifi_network_enable();
    let _ = sm.poll().expect("emit Invoke");
    let response = vec![0x15, 0x24, 0x00, 0x0C, 0x18]; // UnknownError
    let Err(err) = sm.on_response(Expectation::ConnectNetworkResponse, &response) else {
        panic!("UnknownError should fail");
    };
    match err {
        CommissioningError::NetworkRejected {
            remediation_hint, ..
        } => {
            assert_eq!(remediation_hint, RemediationHint::None);
        }
        other => panic!("got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// M6.5.2 Task 20 — Ethernet-only end-to-end walk
// ---------------------------------------------------------------------------

/// Full end-to-end walk for an Ethernet-only device (no Wi-Fi or Thread):
/// `EvictPreviousCaseSessions` (sentinel NOC key pre-set) →
/// `FindOperationalForComplete` (`EstablishCase`) → `on_case_established` →
/// `SendComplete` (`CommissioningComplete`) → `Cleanup` (Done).
///
/// The cursor is placed at `EvictPreviousCaseSessions` via
/// `position_at_stage_for_test` with a synthetic NOC public key so that
/// `Stage::Cleanup` can emit `Action::Done` (which requires
/// `issued_noc_public_key` set by NOC issuance in M6.4.4 — skipped here).
/// The `ReadNetworkCommissioningInfo` → Ethernet `FeatureMap` → `EvictPreviousCaseSessions`
/// transition is covered by `ethernet_only_feature_map_skips_to_evict_case`
/// and the proptest totality suite.
#[test]
fn ethernet_only_e2e_reaches_done() {
    let mut sm = Commissioner::new(make_ethernet_config())
        .expect("valid config")
        .position_at_stage_for_test(
            Stage::EvictPreviousCaseSessions,
            TestStateSeeds {
                synthetic_noc_pubkey: Some([0xCC; 65]),
            },
        );

    // EvictPreviousCaseSessions is a no-op in M6.4/5 (advances internally);
    // next poll routes through FindOperationalForComplete and emits EstablishCase.
    let action = sm.poll().expect("emit EstablishCase");
    match action {
        Action::EstablishCase {
            fabric_id: _,
            peer_node_id: _,
        } => {}
        other => panic!("expected EstablishCase, got {other:?}"),
    }

    // Driver-layer signal: CASE handshake succeeded.
    sm.on_case_established().expect("CASE established");

    // SendComplete — emit CommissioningComplete invoke.
    let action = sm.poll().expect("emit CommissioningComplete");
    match action {
        Action::Invoke {
            cluster,
            command,
            expect,
            session,
            ..
        } => {
            assert_eq!(cluster, 0x0030);
            assert_eq!(command, 0x04);
            assert_eq!(expect, Expectation::CommissioningCompleteResponse);
            assert_eq!(session, SessionContext::Case);
        }
        other => panic!("expected Invoke, got {other:?}"),
    }

    // CommissioningComplete OK response.
    let ok = vec![0x15, 0x24, 0x00, 0x00, 0x18]; // { 0: 0 }
    sm.on_response(Expectation::CommissioningCompleteResponse, &ok)
        .expect("ok CommissioningCompleteResponse");

    // Cleanup — terminal Done.
    let action = sm.poll().expect("emit Done");
    match action {
        Action::Done(_) => {}
        other => panic!("expected Done, got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// M9-C2 Task 5 — Thread network provisioning (route + dispatch)
// ---------------------------------------------------------------------------

/// Reference OTBR operational dataset (111 bytes, `ot-ctl dataset active -x`).
/// Vector: `test-vectors/thread/network_commissioning.json`
/// (`reference_operational_dataset`). Extended PAN ID (TLV `02 08`) =
/// `7896217f787f6ebe`.
const THREAD_DS_HEX: &str = "0e08000000000001000000030000184a0300001235060004001fffe0\
02087896217f787f6ebe0708fdec3f34f3cd2020051071dccee3f164f15da92254e0b9c8a3a5030f4f706\
56e5468726561642d38396437010289d70410dc4b544c7a58671a2ce4f876f5d6dcd90c0402a0f7f8";

/// Extended PAN ID extracted from `THREAD_DS_HEX` — the `ConnectNetwork`
/// `network_id` for the Thread path (NOT an SSID).
const THREAD_EXT_PAN_ID: [u8; 8] = [0x78, 0x96, 0x21, 0x7f, 0x78, 0x7f, 0x6e, 0xbe];

/// A `CommissionerConfig<'static>` carrying a validated Thread operational
/// dataset (`NetworkCredentials::Thread`).
fn make_thread_config() -> CommissionerConfig<'static> {
    let dataset = ThreadDataset::new(hex::decode(THREAD_DS_HEX).expect("valid dataset hex"))
        .expect("valid Thread dataset");
    let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);
    CommissionerConfig {
        pase_attestation_challenge: [0u8; 16],
        fabric: static_fabric(),
        setup_payload: static_setup(),
        paa_trust_store: static_paa(),
        cd_signing_roots: static_cd(),
        commissioner_node_id: 0x1,
        assigned_node_id: 0x2,
        ipk_epoch_key: [0x42_u8; 16],
        case_admin_subject: 0x1,
        admin_vendor_id: 0xFFF1,
        now: MatterTime::from_unix_secs(1_704_067_200),
        rng,
        network: NetworkCredentials::Thread(dataset),
    }
}

/// Thread creds + a Thread-only `FeatureMap` route to `NetworkSetup`, emit
/// `AddOrUpdateThreadNetwork` bytes matching the captured vector, then
/// `ConnectNetwork` with the Extended PAN ID as the `network_id`.
#[test]
fn thread_creds_thread_featuremap_routes_and_provisions() {
    let mut sm = drive_to_read_network_info(make_thread_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    // THREAD bit only (0b010).
    sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b010),
    )
    .expect("Thread FeatureMap accepted for Thread creds");
    assert_eq!(sm.stage(), Stage::NetworkSetup);

    // NetworkSetup emits AddOrUpdateThreadNetwork == the vector (breadcrumb 1,
    // since position_at_stage_for_test skipped the breadcrumb-bearing M6.4
    // stages, so NetworkSetup is the first breadcrumb consumer).
    let action = sm.poll().expect("emit AddOrUpdateThreadNetwork");
    match action {
        Action::Invoke {
            session,
            cluster,
            command,
            payload,
            expect,
            ..
        } => {
            assert_eq!(session, SessionContext::Pase);
            assert_eq!(cluster, 0x0031);
            assert_eq!(command, 0x03, "ADD_OR_UPDATE_THREAD_NETWORK");
            assert_eq!(expect, Expectation::NetworkConfigResponse);
            // Vector: test-vectors/thread/network_commissioning.json
            // "add_or_update_thread_network" (dataset + breadcrumb=1).
            let expected = "1530006f0e08000000000001000000030000184a0300001235060004001\
fffe002087896217f787f6ebe0708fdec3f34f3cd2020051071dccee3f164f15da92254e0b9c8a3a5030f4\
f70656e5468726561642d38396437010289d70410dc4b544c7a58671a2ce4f876f5d6dcd90c0402a0f7f82\
4010118";
            assert_eq!(hex::encode(&payload), expected, "payload: {payload:02x?}");
        }
        other => panic!("expected Invoke, got {other:?}"),
    }

    // Accept the NetworkConfigResponse → FailsafeBeforeNetworkEnable.
    let ok = vec![0x15, 0x24, 0x00, 0x00, 0x18];
    sm.on_response(Expectation::NetworkConfigResponse, &ok)
        .expect("ok NetworkConfigResponse");
    assert_eq!(sm.stage(), Stage::FailsafeBeforeNetworkEnable);

    // Second ArmFailSafe → NetworkEnable.
    let _ = sm.poll().expect("emit second ArmFailSafe");
    sm.on_response(Expectation::ArmFailsafeResponse, &ok)
        .expect("ok ArmFailsafeResponse");
    assert_eq!(sm.stage(), Stage::NetworkEnable);

    // NetworkEnable emits ConnectNetwork with network_id = Extended PAN ID.
    let action = sm.poll().expect("emit ConnectNetwork");
    match action {
        Action::Invoke {
            cluster,
            command,
            payload,
            expect,
            ..
        } => {
            assert_eq!(cluster, 0x0031);
            assert_eq!(command, 0x06, "CONNECT_NETWORK");
            assert_eq!(expect, Expectation::ConnectNetworkResponse);
            // network_id is the Ext-PAN-ID (the "connect_network_thread"
            // vector's network_id), NOT an SSID. The breadcrumb differs from
            // that vector (it is the 3rd breadcrumb here), so assert the
            // network_id field is present rather than the whole payload.
            assert!(
                payload.windows(8).any(|w| w == THREAD_EXT_PAN_ID),
                "ConnectNetwork network_id must be the Ext-PAN-ID: {payload:02x?}",
            );
        }
        other => panic!("expected Invoke, got {other:?}"),
    }
}

/// Thread creds against a Wi-Fi-only `FeatureMap` must fail fast with
/// `NetworkFeatureUnsupported{ needed: Thread }` — NOT silently skip
/// provisioning (the T4 review carry-forward).
#[test]
fn thread_creds_wifi_only_featuremap_rejects_mismatch() {
    let mut sm = drive_to_read_network_info(make_thread_config());
    let _ = sm.poll().expect("emit ReadAttribute");
    // WIFI bit only (0b001) — Thread not offered.
    let Err(err) = sm.on_response(
        Expectation::NetworkCommissioningInfo,
        &feature_map_tlv(0b001),
    ) else {
        panic!("Thread creds on a Wi-Fi-only device must fail");
    };
    assert!(
        matches!(
            err,
            CommissioningError::NetworkFeatureUnsupported {
                needed: NetworkKind::Thread,
            },
        ),
        "got {err:?}",
    );
}