natnet 0.1.0

OptiTrack NatNet protocol
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
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
//! Command grammar, request validation, replies, and write-state contracts.

use std::panic::catch_unwind;

use natnet::command::{
    self, CanaryStep, CommandError, CommandResponse, ComponentError, ConnectOptions, ProgressPhase,
    PropertyValue, Request, SetProperty, Tier1Write, Tier2Grant, Tier2Property, Tier2Write,
    WriteTier, classify_property,
};
use natnet::serverinfo::ConnectionInfo;
use natnet::{
    Error, NAT_CONNECT, NAT_REQUEST, NAT_REQUEST_MODELDEF, NAT_RESPONSE, ServerInfo, Version,
};

// Decode a validated command request for readable wire assertions.
#[allow(clippy::expect_used)]
fn command_string(request: &Request) -> String {
    let payload = &request.as_bytes()[4..];
    let end = payload
        .iter()
        .position(|&byte| byte == 0)
        .expect("validated command terminator");
    String::from_utf8(payload[..end].to_vec()).expect("validated command text")
}

fn status_ok() -> Vec<u8> {
    CommandResponse::status_datagram(0)
}

fn value(value: &str) -> Vec<u8> {
    CommandResponse::value_datagram(value)
}

// The shape Motive 3.1.2.1 gives a GetProperty reply: the value's bytes with
// no NUL, and the payload length as the declared size.
#[allow(clippy::expect_used)]
fn unterminated(value: &str) -> Vec<u8> {
    let mut datagram = NAT_RESPONSE.to_le_bytes().to_vec();
    datagram.extend_from_slice(
        &u16::try_from(value.len())
            .expect("short value")
            .to_le_bytes(),
    );
    datagram.extend_from_slice(value.as_bytes());
    datagram
}

#[test]
fn connect_options_use_the_sdk_layout() {
    // The default hello contains only the header.
    assert_eq!(
        command::connect_request().unwrap().as_bytes(),
        &[0, 0, 0, 0]
    );

    let options = ConnectOptions {
        subscribed_data_only: true,
        bitstream_version: Some(Version::new(3, 1, 0, 0)),
    };
    let request = command::connect_request_with(options).unwrap();
    let bytes = request.as_bytes();
    assert_eq!(bytes.len(), 4 + command::CONNECT_OPTIONS_PAYLOAD_LEN);
    // Message id 0, payload length 270.
    assert_eq!(&bytes[..4], &[0, 0, 0x0e, 0x01]);
    let payload = &bytes[4..];
    assert!(payload[..264].iter().all(|&byte| byte == 0), "sender block");
    assert_eq!(payload[264], 1, "subscribedDataOnly");
    assert_eq!(&payload[265..269], &[3, 1, 0, 0], "BitstreamVersion");
    assert_eq!(payload[269], 0);
    assert_eq!(ConnectOptions::parse(payload), Some(options));

    // A zero version requests the server's current bitstream.
    let current = command::connect_request_with(ConnectOptions::default()).unwrap();
    assert_eq!(&current.as_bytes()[4 + 264..], &[0; 6]);
    assert_eq!(
        ConnectOptions::parse(&current.as_bytes()[4..]),
        Some(ConnectOptions::default())
    );

    // A payload shorter than the options block carries no options.
    let mut python = vec![0u8; 270];
    python[..4].copy_from_slice(b"Ping");
    python[265..269].copy_from_slice(&[4, 1, 0, 0]);
    assert_eq!(
        ConnectOptions::parse(&python),
        Some(ConnectOptions::requesting(Version::V4_1))
    );
    assert_eq!(ConnectOptions::parse(&[]), None);
    assert_eq!(ConnectOptions::parse(&python[..268]), None);
    // The options end at byte 268; the trailing zero byte is not required.
    assert_eq!(
        ConnectOptions::parse(&python[..269]),
        Some(ConnectOptions::requesting(Version::V4_1))
    );
}

#[test]
fn stream_version_follows_the_sdk_rule() {
    let info = |protocol_version| ServerInfo {
        application_name: "Motive".to_string(),
        server_version: Version::new(3, 1, 2, 1),
        protocol_version,
        high_res_clock_frequency: 10_000_000,
        connection: ConnectionInfo {
            data_port: 1511,
            multicast: false,
            multicast_group: [0; 4],
        },
    };
    let newer = info(Version::new(4, 2, 0, 0));
    let old = info(Version::new(2, 10, 0, 0));
    let request = ConnectOptions::requesting(Version::V4_1);

    assert_eq!(request.stream_version(&newer), Version::V4_1);
    // NatNet 3 is the first major that honours a request.
    assert_eq!(
        request.stream_version(&info(Version::new(3, 0, 0, 0))),
        Version::V4_1
    );
    // A NatNet 2 server cannot change its bitstream.
    assert_eq!(request.stream_version(&old), Version::new(2, 10, 0, 0));
    assert_eq!(
        ConnectOptions::default().stream_version(&newer),
        Version::new(4, 2, 0, 0)
    );
}

#[test]
fn typed_requests_have_expected_ids_and_bytes() {
    let connect = command::connect_request().unwrap();
    assert_eq!(
        u16::from_le_bytes([connect.as_bytes()[0], connect.as_bytes()[1]]),
        NAT_CONNECT
    );

    let modeldef = command::request_modeldef().unwrap();
    assert_eq!(
        u16::from_le_bytes([modeldef.as_bytes()[0], modeldef.as_bytes()[1]]),
        NAT_REQUEST_MODELDEF
    );

    let get = command::get_property("cam-1", " enabled ").unwrap();
    assert_eq!(
        u16::from_le_bytes([get.as_bytes()[0], get.as_bytes()[1]]),
        NAT_REQUEST
    );
    assert_eq!(command_string(&get), "GetProperty,cam-1,Enabled");
}

#[test]
fn command_component_injection_is_rejected_before_formatting() {
    for node in [
        "cam-1,SetProperty,root",
        "cam-1\0root",
        "cam-1\nroot",
        "cém",
    ] {
        assert!(matches!(
            command::get_property(node, "Enabled"),
            Err(CommandError::InvalidComponent { .. })
        ));
    }
    for property in ["Enabled,Record", "Enabled\0Record", "Enabled\tRecord"] {
        assert!(matches!(
            command::get_property("cam-1", property),
            Err(CommandError::InvalidComponent { .. })
        ));
    }
    assert!(matches!(
        SetProperty::plan(
            vec!["cam-1".into()],
            "Enabled",
            PropertyValue::Text("1,GetProperty,root".into())
        ),
        Err(CommandError::InvalidComponent {
            component: command::CommandComponent::Value,
            reason: ComponentError::Comma,
        })
    ));
}

#[test]
fn ordinary_names_and_root_scope_remain_valid() {
    let root = command::get_property("", "Mode").unwrap();
    assert_eq!(command_string(&root), "GetProperty,,Mode");

    let write = Tier1Write::camera_enabled(vec!["cam-1".into()], true)
        .unwrap()
        .begin();
    assert_eq!(
        command_string(write.set_request()),
        "SetProperty,cam-1,Enabled,1"
    );
    assert_eq!(
        command_string(write.reread_request()),
        "GetProperty,cam-1,Enabled"
    );
}

#[test]
fn request_payload_length_includes_nul_and_is_bounded() {
    let property = "x".repeat(65_520);
    let request = command::get_property("n", &property).unwrap();
    assert_eq!(request.as_bytes().len() - 4, usize::from(u16::MAX));

    let too_long = "x".repeat(65_521);
    assert!(matches!(
        command::get_property("n", &too_long),
        Err(CommandError::RequestTooLong {
            length: 65_536,
            max: 65_535
        })
    ));
}

#[test]
fn all_targets_are_validated_and_exact_duplicates_rejected() {
    assert!(matches!(
        Tier1Write::camera_enabled(vec!["cam-1".into(), "cam-1".into()], true),
        Err(CommandError::DuplicateTarget { ref target }) if target == "cam-1"
    ));
    assert!(matches!(
        Tier1Write::camera_enabled(vec![String::new()], true),
        Err(CommandError::InvalidComponent { .. })
    ));
    assert!(matches!(
        Tier1Write::camera_enabled(vec![], true),
        Err(CommandError::NoTargets)
    ));
}

#[test]
fn classification_is_fail_closed_and_canonicalized() {
    assert_eq!(classify_property("video mode"), WriteTier::Tier1);
    assert_eq!(classify_property("Transmission Type"), WriteTier::Tier2);
    assert_eq!(classify_property("Frobnicate"), WriteTier::Unsanctioned);

    let plan = SetProperty::plan(
        vec!["cam-1".into()],
        " eNaBlEd ",
        PropertyValue::Bool(false),
    )
    .unwrap();
    assert_eq!(plan.property(), "Enabled");
    assert_eq!(
        command_string(plan.begin().set_request()),
        "SetProperty,cam-1,Enabled,0"
    );
}

#[test]
fn tier2_grant_binds_authority_and_action() {
    let grant = Tier2Grant::sign_off("owner @ console 2026-09-08", Tier2Property::Port).unwrap();
    assert_eq!(grant.authority(), "owner @ console 2026-09-08");
    assert_eq!(grant.action(), Tier2Property::Port);

    let write = Tier2Write::authorize(grant, "", PropertyValue::Int(1510)).unwrap();
    assert_eq!(write.authority(), "owner @ console 2026-09-08");
    assert_eq!(write.action(), Tier2Property::Port);
    assert_eq!(command_string(write.request()), "SetProperty,,Port,1510");
    assert_eq!(write.request().as_bytes()[2..4], [23, 0]);
}

#[test]
fn tier2_malformed_authority_and_unsupported_asset_definition_fail() {
    assert!(matches!(
        Tier2Grant::sign_off("", Tier2Property::Port),
        Err(CommandError::InvalidComponent { .. })
    ));
    assert!(matches!(
        Tier2Grant::sign_off("owner\n", Tier2Property::Port),
        Err(CommandError::InvalidComponent { .. })
    ));
    assert!(matches!(
        Tier2Grant::sign_off("   ", Tier2Property::Port),
        Err(CommandError::InvalidComponent {
            component: command::CommandComponent::Authority,
            reason: ComponentError::Empty,
        })
    ));
    let padded = Tier2Grant::sign_off(" owner ", Tier2Property::Port).unwrap();
    assert_eq!(padded.authority(), " owner ");

    let grant = Tier2Grant::sign_off("owner", Tier2Property::AssetDefinition).unwrap();
    assert!(matches!(
        Tier2Write::authorize(grant, "asset", PropertyValue::Text("x".into())),
        Err(CommandError::UnsupportedOperation {
            operation: "Asset Definition"
        })
    ));
}

#[test]
fn every_tier2_action_is_scoped_and_asset_definition_stays_unsupported() {
    for action in Tier2Property::ALL {
        let grant = Tier2Grant::sign_off("owner", action).unwrap();
        let result = Tier2Write::authorize(grant, "node", PropertyValue::Int(1));
        if action == Tier2Property::AssetDefinition {
            assert!(matches!(
                result,
                Err(CommandError::UnsupportedOperation {
                    operation: "Asset Definition"
                })
            ));
        } else {
            let write = result.unwrap();
            assert_eq!(write.action(), action);
            assert_eq!(write.authority(), "owner");
            assert!(command_string(write.request()).starts_with("SetProperty,node,"));
        }
    }
}

#[test]
fn tier2_stringly_path_never_builds_a_write() {
    for property in ["Transmission Type", "Asset Definition", "Frobnicate"] {
        assert!(matches!(
            SetProperty::plan(vec!["node".into()], property, PropertyValue::Int(1)),
            Err(CommandError::Tier2Refused { .. })
        ));
    }
}

#[test]
fn canary_state_unlocks_one_target_at_a_time_and_records_progress() {
    let write =
        Tier1Write::camera_enabled(vec!["cam-1".into(), "cam-2".into(), "cam-3".into()], true)
            .unwrap();

    let first: CanaryStep = write.begin();
    assert!(first.is_canary());
    assert_eq!(first.target(), "cam-1");
    assert_eq!(
        command_string(first.set_request()),
        "SetProperty,cam-1,Enabled,1"
    );
    let second = first
        .verify_status(&status_ok(), &value("1"))
        .unwrap()
        .next()
        .unwrap();
    assert!(!second.is_canary());
    assert_eq!(second.target(), "cam-2");
    assert_eq!(
        command_string(second.set_request()),
        "SetProperty,cam-2,Enabled,1"
    );

    let third = second
        .verify_status(&status_ok(), &value("true"))
        .unwrap()
        .next()
        .unwrap();
    assert_eq!(third.target(), "cam-3");
    let done = third.verify_status(&status_ok(), &value("1")).unwrap();
    let log = done.finish().unwrap();
    assert_eq!(log.records().len(), 3);
    assert_eq!(log.records()[0].phase(), ProgressPhase::Canary);
    assert_eq!(log.records()[1].phase(), ProgressPhase::Target);
    assert_eq!(log.records()[0].status(), Some(0));
    assert_eq!(log.records()[2].target(), "cam-3");
}

#[test]
fn canary_status_failure_and_reread_mismatch_stop_progression() {
    let write = Tier1Write::camera_enabled(vec!["cam-1".into(), "cam-2".into()], true).unwrap();
    let step = write.begin();
    let err = step.verify_status(&CommandResponse::status_datagram(7), &value("1"));
    assert!(matches!(
        err,
        Err(CommandError::StatusFailure { ref target, status: 7 }) if target == "cam-1"
    ));

    let step = Tier1Write::camera_enabled(vec!["cam-1".into(), "cam-2".into()], true)
        .unwrap()
        .begin();
    assert!(matches!(
        step.verify_status(&status_ok(), &value("0")),
        Err(CommandError::RereadMismatch { ref target, .. }) if target == "cam-1"
    ));
}

#[test]
fn second_target_mismatch_retains_canary_progress() {
    let error = Tier1Write::camera_enabled(vec!["cam-1".into(), "cam-2".into()], true)
        .unwrap()
        .begin()
        .verify_status(&status_ok(), &value("1"))
        .unwrap()
        .next()
        .unwrap()
        .verify_status(&status_ok(), &value("0"))
        .expect_err("second target mismatch must stop progression");

    match &error {
        CommandError::VerificationFailed { source, progress } => {
            assert!(matches!(
                source.as_ref(),
                CommandError::RereadMismatch { target, .. } if target == "cam-2"
            ));
            assert_eq!(progress.len(), 1);
            assert_eq!(progress[0].target(), "cam-1");
        }
        other => panic!("expected retained verification progress, got {other:?}"),
    }
    assert_eq!(error.progress().len(), 1);
}

#[test]
// Shows four-byte payloads can parse as status or request-context values.
fn response_decoder_keeps_four_byte_ambiguity_documented() {
    for (text, status) in [("abc", 6_513_249), ("1.0", 3_157_553), ("yes", 7_562_617)] {
        let reply = CommandResponse::value_datagram(text);
        assert_eq!(
            CommandResponse::parse(&reply).unwrap(),
            CommandResponse::Status(status)
        );
        assert_eq!(CommandResponse::parse_value(&reply).unwrap(), text);
    }
    assert_eq!(
        CommandResponse::parse(&CommandResponse::value_datagram("abcd")).unwrap(),
        CommandResponse::Value("abcd".into())
    );
}

#[test]
fn command_response_parses_status_and_value() {
    assert_eq!(
        CommandResponse::parse(&CommandResponse::status_datagram(0)).unwrap(),
        CommandResponse::Status(0)
    );
    assert_eq!(
        CommandResponse::parse(&CommandResponse::value_datagram("2")).unwrap(),
        CommandResponse::Value("2".into())
    );
}

// `value_datagram` adds a NUL, so three characters fill four bytes. Motive
// sends values without one, so on its wire the collision is at four.
#[test]
fn three_byte_values_have_the_documented_literal_status_codes() {
    for (text, status) in [
        ("abc", 6_513_249_i32),
        ("1.0", 3_157_553_i32),
        ("yes", 7_562_617_i32),
        ("off", 6_710_895_i32),
        ("100", 3_158_065_i32),
    ] {
        let reply = CommandResponse::value_datagram(text);
        assert_eq!(reply.len() - 4, 4);
        assert_eq!(
            CommandResponse::parse(&reply).unwrap(),
            CommandResponse::Status(status)
        );
    }
    for text in ["", "a", "ab", "abcd", "Enabled"] {
        assert_ne!(CommandResponse::value_datagram(text).len() - 4, 4);
    }
}

// Three characters with `value_datagram`'s NUL; four on Motive's wire.
#[test]
fn ambiguity_population_collides_exactly_at_three_bytes() {
    const ALPHABET: [&[u8]; 6] = [b"a", b"1", b".", b"Z", "\u{e9}".as_bytes(), b"\x7f"];
    let mut words: Vec<Vec<u8>> = vec![Vec::new()];
    let mut all: Vec<Vec<u8>> = vec![Vec::new()];
    for _ in 0..5 {
        let mut next = Vec::new();
        for word in &words {
            for symbol in ALPHABET {
                let mut candidate = word.clone();
                candidate.extend_from_slice(symbol);
                next.push(candidate);
            }
        }
        all.extend(next.iter().cloned());
        words = next;
    }
    let mut checked = 0;
    let mut collided = 0;
    for raw in &all {
        let text = core::str::from_utf8(raw).unwrap();
        let parsed = CommandResponse::parse(&CommandResponse::value_datagram(text)).unwrap();
        checked += 1;
        if raw.len() == 3 {
            collided += 1;
            assert!(matches!(parsed, CommandResponse::Status(_)));
        } else {
            assert_eq!(parsed, CommandResponse::Value(text.into()));
        }
    }
    assert_eq!(checked, 9_331);
    assert_eq!(collided, 135);
}

#[test]
fn request_context_reads_value_at_every_payload_length() {
    for text in ["abc", "1.0", "yes", "off", "100", "", "a", "ab", "abcd"] {
        assert_eq!(
            CommandResponse::parse_value(&CommandResponse::value_datagram(text)).unwrap(),
            text
        );
    }
    assert_eq!(
        CommandResponse::parse_value(&CommandResponse::status_datagram(0)).unwrap(),
        ""
    );
    assert_eq!(
        CommandResponse::parse_value(&CommandResponse::status_datagram(49)).unwrap(),
        "1"
    );
    // No NUL: the whole payload is the value, invalid UTF-8 replaced.
    assert_eq!(
        CommandResponse::parse_value(&CommandResponse::status_datagram(-1)).unwrap(),
        "\u{fffd}".repeat(4)
    );
    assert!(matches!(
        CommandResponse::parse_value(command::connect_request().unwrap().as_bytes()),
        Err(Error::UnexpectedMessageId { expected: 3, .. })
    ));
    assert_eq!(
        CommandResponse::parse_value_payload(b"1.0\0").unwrap(),
        "1.0"
    );
}

#[test]
// Motive 3.1.2.1 sends GetProperty values without a NUL (read-only sweep,
// 2026-10-10).
fn unterminated_values_read_to_the_payload_end() {
    for text in ["1", "2", "error", "1.000000", "100.000000", "4294967295"] {
        let reply = unterminated(text);
        assert_eq!(CommandResponse::parse_value(&reply).unwrap(), text);
        assert_eq!(
            CommandResponse::parse(&reply).unwrap(),
            CommandResponse::Value(text.into())
        );
    }
    assert_eq!(CommandResponse::parse_value(&unterminated("")).unwrap(), "");
    assert_eq!(
        CommandResponse::parse(&unterminated("")).unwrap(),
        CommandResponse::Value(String::new())
    );
    // A NUL still ends the value, and what follows it is ignored.
    assert_eq!(
        CommandResponse::parse_value_payload(b"1\0stale").unwrap(),
        "1"
    );
}

#[test]
// Without a NUL, four-character values have the status length.
fn four_byte_unterminated_values_need_the_request_context() {
    for text in ["true", "1510", "1511"] {
        let reply = unterminated(text);
        let bytes: [u8; 4] = text.as_bytes().try_into().unwrap();
        assert_eq!(
            CommandResponse::parse(&reply).unwrap(),
            CommandResponse::Status(i32::from_le_bytes(bytes))
        );
        assert_eq!(CommandResponse::parse_value(&reply).unwrap(), text);
    }
}

#[test]
fn canary_verifies_unterminated_rereads() {
    let enable = || {
        Tier1Write::camera_enabled(vec!["cam-1".into(), "cam-2".into()], true)
            .unwrap()
            .begin()
    };
    let verified = enable()
        .verify_status(&status_ok(), &unterminated("true"))
        .unwrap();
    assert_eq!(verified.progress()[0].reread(), "true");
    assert_eq!(verified.next().unwrap().target(), "cam-2");
    // `verify` reads any four bytes as a status, so "true" needs `verify_status`.
    assert!(matches!(
        enable().verify(&unterminated("true")),
        Err(CommandError::UnverifiableReply)
    ));
    assert!(matches!(
        enable().verify_status(&status_ok(), &unterminated("false")),
        Err(CommandError::RereadMismatch { ref found, .. }) if found == "false"
    ));

    let mode = || {
        Tier1Write::camera_video_mode(vec!["cam-1".into()], 2)
            .unwrap()
            .begin()
    };
    assert!(
        mode()
            .verify_status(&status_ok(), &unterminated("2"))
            .is_ok()
    );
    assert!(mode().verify(&unterminated("2")).is_ok());
}

#[test]
// Uses value context to verify ambiguous three-byte canary echoes (three
// characters with `value_datagram`'s NUL; four on Motive's wire).
fn three_byte_canary_echo_uses_the_request_context_escape() {
    let plan = || Tier1Write::camera_video_mode(vec!["cam-1".into(), "cam-2".into()], 100).unwrap();
    let step = plan().begin();
    let reread = CommandResponse::value_datagram("100");
    assert!(matches!(
        step.verify(&reread),
        Err(CommandError::UnverifiableReply)
    ));

    let echo = CommandResponse::parse_value(&reread).unwrap();
    let verified = plan().begin().verify_echo(&echo).unwrap();
    assert_eq!(verified.next().unwrap().target(), "cam-2");

    let short = Tier1Write::camera_video_mode(vec!["cam-1".into(), "cam-2".into()], 10)
        .unwrap()
        .begin();
    assert!(short.verify(&CommandResponse::value_datagram("10")).is_ok());

    let text = SetProperty::plan(
        vec!["asset-1".into()],
        "Streaming",
        PropertyValue::Text("Yes".into()),
    )
    .unwrap()
    .begin();
    let text_reply = CommandResponse::value_datagram("Yes");
    assert!(matches!(
        text.verify(&text_reply),
        Err(CommandError::UnverifiableReply)
    ));

    let real_status = CommandResponse::status_datagram(0);
    assert_eq!(CommandResponse::parse_value(&real_status).unwrap(), "");
    assert!(matches!(
        plan().begin().verify_echo(""),
        Err(CommandError::RereadMismatch { ref found, .. }) if found.is_empty()
    ));
}

#[test]
fn verify_echo_matches_verify_on_unambiguous_replies() {
    let step = || {
        Tier1Write::camera_enabled(vec!["cam-1".into()], true)
            .unwrap()
            .begin()
    };
    for echo in ["1", "true", "TRUE", "0", "false", "", "wrong", "11111"] {
        let reply = CommandResponse::value_datagram(echo);
        let via_bytes = step().verify(&reply);
        let via_echo = step().verify_echo(echo);
        match (&via_bytes, &via_echo) {
            (Ok(left), Ok(right)) => assert_eq!(left.progress(), right.progress()),
            (
                Err(CommandError::RereadMismatch {
                    target: target_a,
                    property: property_a,
                    expected: expected_a,
                    found: found_a,
                }),
                Err(CommandError::RereadMismatch {
                    target: target_b,
                    property: property_b,
                    expected: expected_b,
                    found: found_b,
                }),
            ) => assert_eq!(
                (target_a, property_a, expected_a, found_a),
                (target_b, property_b, expected_b, found_b)
            ),
            other => panic!("verify and verify_echo disagreed: {other:?}"),
        }
    }
    assert!(step().verify_echo("1").is_ok());
    assert!(matches!(
        step().verify_echo("wrong"),
        Err(CommandError::RereadMismatch { .. })
    ));
    assert!(matches!(
        step().verify_echo(""),
        Err(CommandError::RereadMismatch { .. })
    ));
}

#[test]
fn command_reply_prefixes_and_random_bytes_never_panic() {
    let replies = [
        CommandResponse::status_datagram(0),
        CommandResponse::value_datagram("Enabled"),
        command::connect_request().unwrap().as_bytes().to_vec(),
        command::get_property("cam-1", "Enabled")
            .unwrap()
            .as_bytes()
            .to_vec(),
    ];
    for full in &replies {
        for length in 0..=full.len() {
            let prefix = full[..length].to_vec();
            assert!(catch_unwind(|| CommandResponse::parse(&prefix)).is_ok());
        }
    }

    let mut state: u64 = 0x0bad_c0de_dead_beef;
    let mut next = || {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        state
    };
    for _ in 0..4_000 {
        let length = (next() % 40) as usize;
        let mut bytes = vec![0; length];
        for byte in &mut bytes {
            *byte = (next() & 0xff) as u8;
        }
        assert!(catch_unwind(|| CommandResponse::parse(&bytes)).is_ok());
    }
}

#[test]
fn canary_decoding_is_total_on_bad_replies() {
    let make_step = || {
        Tier1Write::camera_enabled(vec!["cam-1".into()], true)
            .unwrap()
            .begin()
    };
    assert!(matches!(
        make_step().verify(&CommandResponse::status_datagram(0)),
        Err(CommandError::UnverifiableReply)
    ));
    assert!(matches!(
        make_step().verify(&[3, 0]),
        Err(CommandError::Wire(_))
    ));
}

#[test]
fn property_value_match_tolerates_common_echo_encodings() {
    assert!(PropertyValue::Bool(true).matches("1"));
    assert!(PropertyValue::Bool(true).matches("true"));
    assert!(PropertyValue::Bool(true).matches("TRUE"));
    assert!(!PropertyValue::Bool(true).matches("0"));
    assert!(PropertyValue::Int(2).matches(" 2 "));
    assert!(!PropertyValue::Int(2).matches("3"));
    assert!(PropertyValue::Text("Object".into()).matches("Object"));
    assert!(!PropertyValue::Text("Object".into()).matches(" Object"));
    assert!(!PropertyValue::Text("Object".into()).matches("Object "));
    assert!(PropertyValue::Text(" Object ".into()).matches(" Object "));
}