simple-someip 0.5.3

A lightweight SOME/IP serialization and communication library
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
//! E2E checking functions for validating E2E-protected payloads.

use super::config::{Profile4Config, Profile5Config};
use super::crc::{compute_crc16_p5, compute_crc16_p5_with_header, compute_crc32_p4};
use super::e2e_protector::{PROFILE4_HEADER_SIZE, PROFILE5_HEADER_SIZE};
use super::state::{Profile4State, Profile5State};
use super::{E2ECheckResult, E2ECheckStatus};

/// Check E2E Profile 4 protected data.
///
/// Validates the 12-byte header:
/// - Length (2 bytes): Verifies against actual message length
/// - Counter (2 bytes): Checks sequence continuity
/// - `DataID` (4 bytes): Must match configuration
/// - CRC (4 bytes): Verified against computed CRC-32P4
///
/// # Arguments
/// * `config` - Profile 4 configuration
/// * `state` - Mutable state for counter tracking
/// * `protected` - The protected message (header + payload)
///
/// # Returns
/// An `E2ECheckResult` containing the status, counter, and extracted payload.
pub fn check_profile4<'a>(
    config: &Profile4Config,
    state: &mut Profile4State,
    protected: &'a [u8],
) -> E2ECheckResult<'a> {
    // Check minimum length
    if protected.len() < PROFILE4_HEADER_SIZE {
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    // Parse header
    let length = u16::from_be_bytes([protected[0], protected[1]]);
    let counter = u16::from_be_bytes([protected[2], protected[3]]);
    let data_id = u32::from_be_bytes([protected[4], protected[5], protected[6], protected[7]]);
    let received_crc =
        u32::from_be_bytes([protected[8], protected[9], protected[10], protected[11]]);

    // Verify length field matches actual message length
    if length as usize != protected.len() {
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    // Verify DataID matches configuration
    if data_id != config.data_id {
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    // Extract payload
    let payload = &protected[PROFILE4_HEADER_SIZE..];

    // Compute and verify CRC
    let computed_crc = compute_crc32_p4(length, counter, data_id, payload);
    if computed_crc != received_crc {
        return E2ECheckResult::error(E2ECheckStatus::CrcError);
    }

    // Check sequence
    let status = check_sequence_profile4(state, counter, config.max_delta_counter);

    // Update state
    state.last_counter = Some(counter);

    E2ECheckResult::success(status, u32::from(counter), payload)
}

/// Check E2E Profile 5 protected data.
///
/// Validates the 3-byte header:
/// - CRC (2 bytes, little-endian): Verified against computed CRC-16-CCITT
/// - Counter (1 byte): Checks sequence continuity
///
/// # Arguments
/// * `config` - Profile 5 configuration
/// * `state` - Mutable state for counter tracking
/// * `protected` - The protected message (header + payload)
///
/// # Returns
/// An `E2ECheckResult` containing the status, counter, and extracted payload.
pub fn check_profile5<'a>(
    config: &Profile5Config,
    state: &mut Profile5State,
    protected: &'a [u8],
) -> E2ECheckResult<'a> {
    // Check minimum length
    if protected.len() < PROFILE5_HEADER_SIZE {
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    // Verify data length matches configuration (header + payload = config.data_length)
    let expected_total_length = PROFILE5_HEADER_SIZE + config.data_length as usize;
    if protected.len() != expected_total_length {
        tracing::warn!(
            "E2E Profile 5 length mismatch: expected {} bytes (3 header + {} payload), got {} bytes",
            expected_total_length,
            config.data_length,
            protected.len()
        );
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    // Parse header: CRC (2, little-endian) + Counter (1)
    let received_crc = u16::from_le_bytes([protected[0], protected[1]]);
    let counter = protected[2];

    // Extract payload
    let payload = &protected[PROFILE5_HEADER_SIZE..];

    // Compute and verify CRC
    let computed_crc = compute_crc16_p5(config.data_id, counter, payload);
    if computed_crc != received_crc {
        return E2ECheckResult::error(E2ECheckStatus::CrcError);
    }

    // Check sequence
    let status = check_sequence_profile5(state, counter, config.max_delta_counter);

    // Update state
    state.last_counter = Some(counter);

    E2ECheckResult::success(status, u32::from(counter), payload)
}

/// Check E2E Profile 5 protected data with SOME/IP upper-header in the CRC.
///
/// Validates the 3-byte header:
/// - CRC (2 bytes, little-endian): Verified against CRC-16-CCITT computed over
///   `upper_header(8) + Counter(1) + Payload(N) + DataID(2 LE)`
/// - Counter (1 byte): Checks sequence continuity
///
/// The 8-byte `upper_header` (UPPER-HEADER-BITS-TO-SHIFT = 64 bits) is the
/// second half of the SOME/IP header: `[request_id:4 BE, proto_ver:1,
/// iface_ver:1, msg_type:1, return_code:1]`. It must match exactly what the
/// sender included in its CRC computation, otherwise a `CrcError` is returned.
///
/// # Arguments
/// * `config` - Profile 5 configuration (data ID, data length, max delta counter)
/// * `state` - Mutable state for counter tracking
/// * `protected` - The protected message (3-byte E2E header + payload)
/// * `upper_header` - 8-byte SOME/IP upper header included in the CRC
///
/// # Returns
/// An [`E2ECheckResult`] containing the status, counter, and extracted payload.
pub fn check_profile5_with_header<'a>(
    config: &Profile5Config,
    state: &mut Profile5State,
    protected: &'a [u8],
    upper_header: [u8; 8],
) -> E2ECheckResult<'a> {
    if protected.len() < PROFILE5_HEADER_SIZE {
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    let expected_total_length = PROFILE5_HEADER_SIZE + config.data_length as usize;
    if protected.len() != expected_total_length {
        tracing::warn!(
            "E2E Profile 5 length mismatch: expected {} bytes (3 header + {} payload), got {} bytes",
            expected_total_length,
            config.data_length,
            protected.len()
        );
        return E2ECheckResult::error(E2ECheckStatus::BadArgument);
    }

    let received_crc = u16::from_le_bytes([protected[0], protected[1]]);
    let counter = protected[2];
    let payload = &protected[PROFILE5_HEADER_SIZE..];

    let computed_crc = compute_crc16_p5_with_header(config.data_id, counter, payload, upper_header);
    if computed_crc != received_crc {
        return E2ECheckResult::error(E2ECheckStatus::CrcError);
    }

    let status = check_sequence_profile5(state, counter, config.max_delta_counter);
    state.last_counter = Some(counter);

    E2ECheckResult::success(status, u32::from(counter), payload)
}

/// Check sequence continuity for Profile 4 (16-bit counter).
fn check_sequence_profile4(
    state: &Profile4State,
    received_counter: u16,
    max_delta: u16,
) -> E2ECheckStatus {
    match state.last_counter {
        None => {
            // First message received - always Ok
            E2ECheckStatus::Ok
        }
        Some(last_counter) => {
            // Calculate delta with wraparound handling
            let delta = received_counter.wrapping_sub(last_counter);

            if delta == 0 {
                // Same counter value - repeated message
                E2ECheckStatus::Repeated
            } else if delta == 1 {
                // Consecutive message - perfect
                E2ECheckStatus::Ok
            } else if delta <= max_delta {
                // Some messages lost but within tolerance
                E2ECheckStatus::OkSomeLost
            } else {
                // Too many messages lost or counter went backwards
                E2ECheckStatus::WrongSequence
            }
        }
    }
}

/// Check sequence continuity for Profile 5 (8-bit counter).
fn check_sequence_profile5(
    state: &Profile5State,
    received_counter: u8,
    max_delta: u8,
) -> E2ECheckStatus {
    match state.last_counter {
        None => {
            // First message received - always Ok
            E2ECheckStatus::Ok
        }
        Some(last_counter) => {
            // Calculate delta with wraparound handling
            let delta = received_counter.wrapping_sub(last_counter);

            if delta == 0 {
                // Same counter value - repeated message
                E2ECheckStatus::Repeated
            } else if delta == 1 {
                // Consecutive message - perfect
                E2ECheckStatus::Ok
            } else if delta <= max_delta {
                // Some messages lost but within tolerance
                E2ECheckStatus::OkSomeLost
            } else {
                // Too many messages lost or counter went backwards
                E2ECheckStatus::WrongSequence
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::e2e::{protect_profile4, protect_profile5, protect_profile5_with_header};

    #[test]
    fn test_check_profile4_valid() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"Hello, World!";
        let mut buf = [0u8; 256];
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        let protected = &buf[..len];

        let result = check_profile4(&config, &mut check_state, protected);
        assert_eq!(result.status, E2ECheckStatus::Ok);
        assert_eq!(result.counter, Some(0));
        assert_eq!(result.payload, Some(payload.as_slice()));
    }

    #[test]
    fn test_check_profile4_wrong_data_id() {
        let config1 = Profile4Config::new(0x12345678, 15);
        let config2 = Profile4Config::new(0xDEADBEEF, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];
        let len = protect_profile4(&config1, &mut protect_state, payload, &mut buf).unwrap();

        // Check with different data_id
        let result = check_profile4(&config2, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_check_profile4_corrupted_crc() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();

        // Corrupt CRC (bytes 8-11)
        buf[8] ^= 0xFF;

        let result = check_profile4(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::CrcError);
    }

    #[test]
    fn test_check_profile4_corrupted_payload() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();

        // Corrupt payload
        buf[12] ^= 0xFF;

        let result = check_profile4(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::CrcError);
    }

    #[test]
    fn test_check_profile4_wrong_length() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];
        protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();

        // Truncate message (header says 16 but we only pass 14)
        let result = check_profile4(&config, &mut check_state, &buf[..14]);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_check_profile4_too_short() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut check_state = Profile4State::new();

        let short = [0u8; 11]; // Less than 12-byte header
        let result = check_profile4(&config, &mut check_state, &short);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_check_profile5_valid() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        // Payload must be padded to data_length (20 bytes) for check_profile5
        let mut payload = [0u8; 20];
        payload[..13].copy_from_slice(b"Hello, World!");
        let mut buf = [0u8; 256];
        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        let protected = &buf[..len];

        let result = check_profile5(&config, &mut check_state, protected);
        assert_eq!(result.status, E2ECheckStatus::Ok);
        assert_eq!(result.counter, Some(0));
        assert_eq!(result.payload, Some(payload.as_slice()));
    }

    #[test]
    fn test_check_profile5_corrupted_crc() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        let mut payload = [0u8; 20];
        payload[..4].copy_from_slice(b"test");
        let mut buf = [0u8; 256];
        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();

        // Corrupt CRC (bytes 1-2)
        buf[1] ^= 0xFF;

        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::CrcError);
    }

    #[test]
    fn test_check_profile5_too_short() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut check_state = Profile5State::new();

        let short = [0u8; 2]; // Less than 3-byte header
        let result = check_profile5(&config, &mut check_state, &short);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_sequence_repeated() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        let protected = &buf[..len];

        // First check
        let result1 = check_profile4(&config, &mut check_state, protected);
        assert_eq!(result1.status, E2ECheckStatus::Ok);

        // Replay same message
        let result2 = check_profile4(&config, &mut check_state, protected);
        assert_eq!(result2.status, E2ECheckStatus::Repeated);
    }

    #[test]
    fn test_sequence_consecutive() {
        let config = Profile4Config::new(0x12345678, 15);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];

        for _ in 0..5 {
            let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
            let result = check_profile4(&config, &mut check_state, &buf[..len]);
            assert_eq!(result.status, E2ECheckStatus::Ok);
        }
    }

    #[test]
    fn test_sequence_some_lost() {
        let config = Profile4Config::new(0x12345678, 10);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];

        // First message
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        let result1 = check_profile4(&config, &mut check_state, &buf[..len]);
        assert_eq!(result1.status, E2ECheckStatus::Ok);

        // Skip some messages
        for _ in 0..5 {
            protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        }

        // Check with gap of 6 (within max_delta of 10)
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        let result2 = check_profile4(&config, &mut check_state, &buf[..len]);
        assert_eq!(result2.status, E2ECheckStatus::OkSomeLost);
    }

    #[test]
    fn test_sequence_wrong_sequence() {
        let config = Profile4Config::new(0x12345678, 3);
        let mut protect_state = Profile4State::new();
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];

        // First message
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        let result1 = check_profile4(&config, &mut check_state, &buf[..len]);
        assert_eq!(result1.status, E2ECheckStatus::Ok);

        // Skip many messages
        for _ in 0..10 {
            protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        }

        // Check with gap of 11 (exceeds max_delta of 3)
        let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
        let result2 = check_profile4(&config, &mut check_state, &buf[..len]);
        assert_eq!(result2.status, E2ECheckStatus::WrongSequence);
    }

    #[test]
    fn test_sequence_wraparound() {
        let config = Profile4Config::new(0x12345678, 5);
        let mut protect_state = Profile4State::with_initial_counter(u16::MAX - 2);
        let mut check_state = Profile4State::new();

        let payload = b"test";
        let mut buf = [0u8; 256];

        // Messages around counter wraparound
        for _ in 0..5 {
            let len = protect_profile4(&config, &mut protect_state, payload, &mut buf).unwrap();
            let result = check_profile4(&config, &mut check_state, &buf[..len]);
            assert_eq!(result.status, E2ECheckStatus::Ok);
        }
    }

    #[test]
    fn test_profile5_sequence_wraparound() {
        let config = Profile5Config::new(0x1234, 20, 5);
        let mut protect_state = Profile5State::with_initial_counter(u8::MAX - 2);
        let mut check_state = Profile5State::new();

        let mut payload = [0u8; 20];
        payload[..4].copy_from_slice(b"test");
        let mut buf = [0u8; 256];

        // Messages around counter wraparound
        for _ in 0..5 {
            let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
            let result = check_profile5(&config, &mut check_state, &buf[..len]);
            assert_eq!(result.status, E2ECheckStatus::Ok);
        }
    }

    #[test]
    fn test_check_profile5_with_header_roundtrip() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        let upper_header: [u8; 8] = [0x00, 0x01, 0x00, 0x05, 0x01, 0x03, 0x02, 0x00];

        let mut payload = [0u8; 20];
        payload[..5].copy_from_slice(b"Hello");

        let mut buf = [0u8; 256];
        let len = protect_profile5_with_header(
            &config,
            &mut protect_state,
            &payload,
            upper_header,
            &mut buf,
        )
        .unwrap();
        let result =
            check_profile5_with_header(&config, &mut check_state, &buf[..len], upper_header);

        assert_eq!(result.status, E2ECheckStatus::Ok);
        assert_eq!(result.counter, Some(0));
        assert_eq!(result.payload.as_deref(), Some(payload.as_slice()));
    }

    #[test]
    fn test_check_profile5_length_mismatch() {
        // Config expects data_length=20, so total = 3 + 20 = 23 bytes.
        // Pass a buffer that's >= 3 bytes but != 23 to hit the length mismatch path.
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut check_state = Profile5State::new();

        let buf = [0u8; 10]; // 10 != 23
        let result = check_profile5(&config, &mut check_state, &buf);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_check_profile5_with_header_too_short() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut check_state = Profile5State::new();
        let upper_header: [u8; 8] = [0; 8];

        let buf = [0u8; 2]; // Less than 3-byte header
        let result = check_profile5_with_header(&config, &mut check_state, &buf, upper_header);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_check_profile5_with_header_length_mismatch() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut check_state = Profile5State::new();
        let upper_header: [u8; 8] = [0; 8];

        let buf = [0u8; 10]; // >= 3 but != 23
        let result = check_profile5_with_header(&config, &mut check_state, &buf, upper_header);
        assert_eq!(result.status, E2ECheckStatus::BadArgument);
    }

    #[test]
    fn test_profile5_sequence_some_lost() {
        let config = Profile5Config::new(0x1234, 20, 10);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        let mut payload = [0u8; 20];
        payload[..4].copy_from_slice(b"test");
        let mut buf = [0u8; 256];

        // First message
        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::Ok);

        // Skip 5 messages
        for _ in 0..5 {
            protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        }

        // Check with gap of 6 (within max_delta of 10)
        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::OkSomeLost);
    }

    #[test]
    fn test_profile5_sequence_wrong_sequence() {
        let config = Profile5Config::new(0x1234, 20, 3);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        let mut payload = [0u8; 20];
        payload[..4].copy_from_slice(b"test");
        let mut buf = [0u8; 256];

        // First message
        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::Ok);

        // Skip 10 messages (exceeds max_delta of 3)
        for _ in 0..10 {
            protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        }

        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();
        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::WrongSequence);
    }

    #[test]
    fn test_profile5_sequence_repeated() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        let mut payload = [0u8; 20];
        payload[..4].copy_from_slice(b"test");
        let mut buf = [0u8; 256];

        let len = protect_profile5(&config, &mut protect_state, &payload, &mut buf).unwrap();

        // First check
        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::Ok);

        // Replay same message
        let result = check_profile5(&config, &mut check_state, &buf[..len]);
        assert_eq!(result.status, E2ECheckStatus::Repeated);
    }

    #[test]
    fn test_check_profile5_with_header_mismatch_is_crc_error() {
        let config = Profile5Config::new(0x1234, 20, 15);
        let mut protect_state = Profile5State::new();
        let mut check_state = Profile5State::new();

        let tx_header: [u8; 8] = [0x00, 0x01, 0x00, 0x05, 0x01, 0x03, 0x02, 0x00];
        let rx_header: [u8; 8] = [0x00, 0x02, 0x00, 0x05, 0x01, 0x03, 0x02, 0x00];

        let mut payload = [0u8; 20];
        payload[..5].copy_from_slice(b"Hello");

        let mut buf = [0u8; 256];
        let len = protect_profile5_with_header(
            &config,
            &mut protect_state,
            &payload,
            tx_header,
            &mut buf,
        )
        .unwrap();
        let result = check_profile5_with_header(&config, &mut check_state, &buf[..len], rx_header);

        assert_eq!(result.status, E2ECheckStatus::CrcError);
    }
}