datex-core 0.0.14

The DATEX Core Rust implementation
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
use super::protocol_structures::{
    block_header::BlockHeader,
    encrypted_header::EncryptedHeader,
    routing_header::{EncryptionType, RoutingHeader, SignatureType},
};
use crate::{
    channel::mpsc::UnboundedReceiver,
    global::protocol_structures::{
        block_header::BlockType, routing_header::Receivers,
    },
    utils::buffers::write_u16,
    values::core_values::endpoint::Endpoint,
};
use binrw::{
    BinRead, BinWrite,
    io::{Cursor, Read},
};
use core::{fmt::Display, result::Result, unimplemented};
use strum::Display;
use thiserror::Error;

use crate::{prelude::*, utils::maybe_async::MaybeAsync};

#[derive(Debug, Display, Error)]
pub enum HeaderParsingError {
    InsufficientLength,
    InvalidMagicNumber,
}

// TODO #110: RawDXBBlock that is received in com_hub, only containing RoutingHeader, BlockHeader and raw bytes

// TODO #429 @Norbert
// Add optional raw signature, and encrypted part
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
pub struct DXBBlock {
    pub routing_header: RoutingHeader,
    pub block_header: BlockHeader,
    pub signature: Option<Vec<u8>>,
    pub encrypted_header: EncryptedHeader,
    pub body: Vec<u8>,

    #[serde(skip)]
    pub raw_bytes: Option<Vec<u8>>,
}

impl PartialEq for DXBBlock {
    fn eq(&self, other: &Self) -> bool {
        self.routing_header == other.routing_header
            && self.block_header == other.block_header
            && self.encrypted_header == other.encrypted_header
            && self.body == other.body
    }
}

const SIZE_BYTE_POSITION: usize = 3; // magic number (2 bytes) + version (1 byte)
const SIZE_BYTES: usize = 2;

pub type IncomingContextId = u32;
pub type IncomingSectionIndex = u16;
pub type IncomingBlockNumber = u16;
pub type OutgoingContextId = u32;
pub type OutgoingSectionIndex = u16;
pub type OutgoingBlockNumber = u16;

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum IncomingSection {
    /// a single block
    SingleBlock((Option<DXBBlock>, IncomingEndpointContextSectionId)),
    /// a stream of blocks
    /// the stream is finished when a block has the end_of_section flag set
    BlockStream(
        (
            Option<UnboundedReceiver<DXBBlock>>,
            IncomingEndpointContextSectionId,
        ),
    ),
}

impl IncomingSection {
    pub async fn next(&mut self) -> Option<DXBBlock> {
        match self {
            IncomingSection::SingleBlock((block, _)) => block.take(),
            IncomingSection::BlockStream((blocks, _)) => {
                if let Some(receiver) = blocks {
                    receiver.next().await
                } else {
                    None // No blocks to receive
                }
            }
        }
    }

    pub async fn drain(&mut self) -> Vec<DXBBlock> {
        let mut blocks = Vec::new();
        while let Some(block) = self.next().await {
            blocks.push(block);
        }
        blocks
    }
}

impl IncomingSection {
    pub fn get_section_index(&self) -> IncomingSectionIndex {
        self.get_section_context_id().section_index
    }

    pub fn get_sender(&self) -> Endpoint {
        self.get_section_context_id()
            .endpoint_context_id
            .sender
            .clone()
    }

    pub fn get_section_context_id(&self) -> &IncomingEndpointContextSectionId {
        match self {
            IncomingSection::SingleBlock((_, section_context_id))
            | IncomingSection::BlockStream((_, section_context_id)) => {
                section_context_id
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IncomingEndpointContextId {
    pub sender: Endpoint,
    pub context_id: IncomingContextId,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IncomingEndpointContextSectionId {
    pub endpoint_context_id: IncomingEndpointContextId,
    pub section_index: IncomingSectionIndex,
}

impl IncomingEndpointContextSectionId {
    pub fn new(
        endpoint_context_id: IncomingEndpointContextId,
        section_index: IncomingSectionIndex,
    ) -> Self {
        IncomingEndpointContextSectionId {
            endpoint_context_id,
            section_index,
        }
    }
}

/// An identifier that defines a globally unique block
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockId {
    pub endpoint_context_id: IncomingEndpointContextId,
    pub timestamp: u64,
    pub current_section_index: IncomingSectionIndex,
    pub current_block_number: IncomingBlockNumber,
}

#[derive(Debug)]
pub enum DXBBlockParseError {
    IOError(String),
    ParseError(binrw::Error),
    IllegalState,
}

#[derive(Debug)]
pub enum SignatureValidationError {
    /// The block is expected to have a signature, but no signature was found.
    MissingSignature,
    /// The signature could not be parsed correctly (e.g. wrong length).
    SignatureParseError,
    /// The signature is invalid (e.g. does not match the expected value).
    InvalidSignature,
}

impl From<binrw::Error> for DXBBlockParseError {
    fn from(err: binrw::Error) -> Self {
        DXBBlockParseError::ParseError(err)
    }
}

impl From<String> for DXBBlockParseError {
    fn from(err: String) -> Self {
        DXBBlockParseError::IOError(err)
    }
}

impl DXBBlock {
    pub fn new_with_body(body: &[u8]) -> DXBBlock {
        let mut block = DXBBlock {
            body: body.to_vec(),
            ..DXBBlock::default()
        };
        block.recalculate_struct();
        block
    }
    pub fn new(
        routing_header: RoutingHeader,
        block_header: BlockHeader,
        encrypted_header: EncryptedHeader,
        body: Vec<u8>,
    ) -> DXBBlock {
        let mut block = DXBBlock {
            routing_header,
            block_header,
            signature: None,
            encrypted_header,
            body,
            raw_bytes: None,
        };
        block.recalculate_struct();
        block
    }

    // TODO #743: guarantee that all unwraps are safe and no binrw::Error is possible
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut writer = Cursor::new(Vec::new());
        self.routing_header.write(&mut writer).unwrap();
        self.signature.write(&mut writer).unwrap();
        self.block_header.write(&mut writer).unwrap();
        self.encrypted_header.write(&mut writer).unwrap();
        let mut bytes = writer.into_inner();
        bytes.extend_from_slice(&self.body);
        DXBBlock::adjust_block_length(bytes)
    }
    pub fn recalculate_struct(&mut self) -> &mut Self {
        let bytes = self.to_bytes();
        let size = bytes.len() as u16;
        self.routing_header.block_size = size;
        self
    }

    fn adjust_block_length(mut bytes: Vec<u8>) -> Vec<u8> {
        let size = bytes.len() as u32;
        write_u16(&mut bytes, &mut SIZE_BYTE_POSITION.clone(), size as u16);
        bytes
    }

    pub fn has_dxb_magic_number(dxb: &[u8]) -> bool {
        dxb.len() >= 2 && dxb[0] == 0x01 && dxb[1] == 0x64
    }

    pub fn extract_dxb_block_length(
        dxb: &[u8],
    ) -> Result<u16, HeaderParsingError> {
        if dxb.len() < SIZE_BYTE_POSITION + SIZE_BYTES {
            return Err(HeaderParsingError::InsufficientLength);
        }

        // make sure magic number is correct
        if !DXBBlock::has_dxb_magic_number(dxb) {
            return Err(HeaderParsingError::InvalidMagicNumber);
        }

        // block size is u16 at SIZE_BYTE_POSITION
        let block_size_bytes =
            &dxb[SIZE_BYTE_POSITION..SIZE_BYTE_POSITION + SIZE_BYTES];
        Ok(u16::from_le_bytes(block_size_bytes.try_into().unwrap()))
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<DXBBlock, DXBBlockParseError> {
        let mut reader = Cursor::new(bytes);
        let routing_header = RoutingHeader::read(&mut reader)?;

        let signature = match routing_header.flags.signature_type() {
            SignatureType::Encrypted => {
                // extract next 255 bytes as the signature
                let mut signature = Vec::from([0u8; 108]);
                reader.read_exact(&mut signature).map_err(|e| {
                    DXBBlockParseError::IOError(format!(
                        "Failed to read encrypted signature: {}",
                        e
                    ))
                })?;

                // TODO #111: decrypt the signature
                Some(signature)
            }
            SignatureType::Unencrypted => {
                // extract next 255 bytes as the signature
                let mut signature = Vec::from([0u8; 108]);
                reader.read_exact(&mut signature).map_err(|e| {
                    DXBBlockParseError::IOError(format!(
                        "Failed to read unencrypted signature: {}",
                        e
                    ))
                })?;
                Some(signature)
            }
            SignatureType::None => None,
        };

        let decrypted_bytes = match routing_header.flags.encryption_type() {
            EncryptionType::Encrypted => {
                // TODO #113: decrypt the body
                let mut decrypted_bytes = Vec::from([0u8; 255]);
                reader.read_exact(&mut decrypted_bytes).map_err(|e| {
                    DXBBlockParseError::IOError(format!(
                        "Failed to read encrypted body: {}",
                        e
                    ))
                })?;
                decrypted_bytes
            }
            EncryptionType::None => {
                let mut bytes = Vec::new();
                reader.read_to_end(&mut bytes).map_err(|e| e.to_string())?;
                bytes
            }
        };

        let mut reader = Cursor::new(decrypted_bytes);
        let block_header = BlockHeader::read(&mut reader)?;

        // invalid state: trace / trace back blocks with a signature
        if signature.is_some()
            && matches!(
                block_header.flags_and_timestamp.block_type(),
                BlockType::Trace | BlockType::TraceBack
            )
        {
            return Err(DXBBlockParseError::IllegalState);
        }

        let encrypted_header = EncryptedHeader::read(&mut reader)?;

        let mut body = Vec::new();
        reader.read_to_end(&mut body).map_err(|e| e.to_string())?;

        let block = DXBBlock {
            routing_header,
            block_header,
            signature,
            encrypted_header,
            body,
            raw_bytes: Some(bytes.to_vec()),
        };

        Ok(block)
    }

    /// Validates the signature of the block based on the signature type specified in the routing header.
    /// Returns Ok(self) if the signature is valid, or a SignatureValidationError if the signature is missing, cannot be parsed, or is invalid.
    pub fn validate_signature(
        self,
    ) -> MaybeAsync<
        Result<DXBBlock, SignatureValidationError>,
        impl Future<Output = Result<DXBBlock, SignatureValidationError>>,
    > {
        cfg_select! {
            // if not crypto_enabled, but allow_unsigned_blocks is set, just return Ok(()) for all blocks, as signature validation is not possible
            all(
                not(feature = "crypto_enabled"),
                feature = "allow_unsigned_blocks"
            ) => {
                log::info!(
                    "Crypto and signature validation are disabled, allowing block without signature validation"
                );
                MaybeAsync::<
                    Result<DXBBlock, SignatureValidationError>,
                    core::future::Ready<
                        Result<DXBBlock, SignatureValidationError>,
                    >,
                >::Sync(Ok(self))
            }

            // if crypto is enabled, perform signature validation based on the signature type specified in the routing header
            _ => {
                use crate::crypto::CryptoImpl;
                use datex_crypto_facade::crypto::Crypto;

                // TODO #179 check for creation time, withdraw if too old (TBD) or in the future
                match self.routing_header.flags.signature_type() {
                    // TODO #180: verify signature and abort if invalid
                    // Check if signature is following in some later block and add them to
                    // a pool of incoming blocks awaiting some signature
                    signature_type @ (SignatureType::Encrypted
                    | SignatureType::Unencrypted) => {
                        MaybeAsync::Async(async move {
                            let is_valid = match signature_type {
                                SignatureType::Encrypted => {
                                    let raw_sign = self.signature.as_ref().ok_or(
                                SignatureValidationError::MissingSignature,
                            )?;
                                    let (enc_sign, pub_key) =
                                        raw_sign.split_at(64);
                                    let hash = CryptoImpl::hkdf_sha256(pub_key, &[0u8; 16])
                                .await
                                .map_err(|_| {
                                    SignatureValidationError::SignatureParseError
                                })?;
                                    let signature = CryptoImpl::aes_ctr_decrypt(
                                &hash, &[0u8; 16], enc_sign,
                            )
                            .await
                            .map_err(|_| {
                                SignatureValidationError::SignatureParseError
                            })?;

                                    let raw_signed =
                                        [pub_key, &self.body.clone()].concat();
                                    let hashed_signed = CryptoImpl::hash_sha256(
                                &raw_signed,
                            )
                            .await
                            .map_err(|_| {
                                SignatureValidationError::SignatureParseError
                            })?;

                                    CryptoImpl::ver_ed25519(
                                pub_key,
                                &signature,
                                &hashed_signed,
                            )
                            .await
                            .map_err(|_| {
                                SignatureValidationError::InvalidSignature
                            })?
                                }

                                SignatureType::Unencrypted => {
                                    let raw_sign = self.signature.as_ref().ok_or(
                                SignatureValidationError::MissingSignature,
                            )?;
                                    let (signature, pub_key) =
                                        raw_sign.split_at(64);

                                    let raw_signed =
                                        [pub_key, &self.body.clone()].concat();
                                    let hashed_signed = CryptoImpl::hash_sha256(
                                &raw_signed,
                            )
                            .await
                            .map_err(|_| {
                                SignatureValidationError::SignatureParseError
                            })?;

                                    CryptoImpl::ver_ed25519(
                                pub_key,
                                signature,
                                &hashed_signed,
                            )
                            .await
                            .map_err(|_| {
                                SignatureValidationError::InvalidSignature
                            })?
                                }
                                _ => unreachable!(),
                            };

                            match is_valid {
                                true => Ok(self),
                                false => Err(
                                    SignatureValidationError::InvalidSignature,
                                ),
                            }
                        })
                    }

                    SignatureType::None => {
                        let is_valid = {
                            cfg_if::cfg_if! {
                                // if unsigned blocks are allowed, return true
                                if #[cfg(feature = "allow_unsigned_blocks")] {
                                    log::info!("Signature validation is disabled, allowing block without signature validation");
                                    true
                                }
                                // otherwise, only allow unsigned Trace and TraceBack blocks,
                                // as they are used for debugging and should not be used in production with real data
                                else {
                                    match self.block_type() {
                                        BlockType::Trace | BlockType::TraceBack => true,
                                        // TODO #181 Check if the sender is trusted (endpoint + interface) connection
                                        _ => return MaybeAsync::Sync(Err(SignatureValidationError::MissingSignature))
                                    }
                                }
                            }
                        };

                        MaybeAsync::Sync(match is_valid {
                            true => Ok(self),
                            false => {
                                Err(SignatureValidationError::InvalidSignature)
                            }
                        })
                    }
                }
            }
        }
    }

    /// Get a list of all receiver endpoints from the routing header.
    pub fn receiver_endpoints(&self) -> Vec<Endpoint> {
        match self.routing_header.receivers() {
            Receivers::Endpoints(endpoints) => endpoints,
            Receivers::EndpointsWithKeys(endpoints_with_keys) => {
                endpoints_with_keys.into_iter().map(|(e, _)| e).collect()
            }
            Receivers::PointerId(_) => unimplemented!(),
            _ => Vec::new(),
        }
    }
    pub fn receivers(&self) -> Receivers {
        self.routing_header.receivers()
    }

    /// Update the receivers list in the routing header.
    pub fn set_receivers<T>(&mut self, endpoints: T)
    where
        T: Into<Receivers>,
    {
        self.routing_header.set_receivers(endpoints.into());
    }

    pub fn set_bounce_back(&mut self, bounce_back: bool) {
        self.routing_header.flags.set_is_bounce_back(bounce_back);
    }

    pub fn is_bounce_back(&self) -> bool {
        self.routing_header.flags.is_bounce_back()
    }

    pub fn sender(&self) -> &Endpoint {
        &self.routing_header.sender
    }

    pub fn block_type(&self) -> BlockType {
        self.block_header.flags_and_timestamp.block_type()
    }

    pub fn get_endpoint_context_id(&self) -> IncomingEndpointContextId {
        IncomingEndpointContextId {
            sender: self.routing_header.sender.clone(),
            context_id: self.block_header.context_id,
        }
    }

    pub fn get_block_id(&self) -> BlockId {
        BlockId {
            endpoint_context_id: self.get_endpoint_context_id(),
            timestamp: self
                .block_header
                .flags_and_timestamp
                .creation_timestamp(),
            current_section_index: self.block_header.section_index,
            current_block_number: self.block_header.block_number,
        }
    }

    /// Returns true if the block has a fixed number of receivers
    /// without wildcard instances, and no @@any receiver.
    pub fn has_exact_receiver_count(&self) -> bool {
        !self
            .receiver_endpoints()
            .iter()
            .any(|e| e.instance_is_broadcast() || e.is_any())
    }

    pub fn clone_with_new_receivers<T>(&self, new_receivers: T) -> DXBBlock
    where
        T: Into<Receivers>,
    {
        let mut new_block = self.clone();
        new_block.set_receivers(new_receivers.into());
        new_block
    }
}

impl Display for DXBBlock {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        let block_type = self.block_header.flags_and_timestamp.block_type();
        let sender = &self.routing_header.sender;
        let receivers = self.receivers();
        core::write!(f, "[{block_type}] {sender} -> {receivers}")?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use core::str::FromStr;
    // TODO enable tests for no-std target
    #[cfg(feature = "std")]
    use crate::{
        crypto::CryptoImpl, global::dxb_block::SignatureValidationError,
    };

    #[cfg(feature = "std")]
    use datex_crypto_facade::crypto::Crypto;

    use crate::{
        global::{
            dxb_block::{DXBBlock, DXBBlockParseError},
            protocol_structures::{
                block_header::BlockType,
                encrypted_header::{self, EncryptedHeader},
                routing_header::{RoutingHeader, SignatureType},
            },
        },
        prelude::*,
        values::core_values::endpoint::Endpoint,
    };
    use core::assert_matches;

    #[test]
    pub fn recalculate() {
        let mut routing_header = RoutingHeader::default()
            .with_sender(Endpoint::from_str("@test").unwrap())
            .to_owned();
        routing_header.flags.set_signature_type(SignatureType::None);
        routing_header.set_size(420);
        let mut block = DXBBlock {
            body: vec![0x01, 0x02, 0x03],
            encrypted_header: EncryptedHeader {
                flags: encrypted_header::Flags::default()
                    .with_user_agent(encrypted_header::UserAgent::Unused11),
                ..Default::default()
            },
            routing_header,
            ..DXBBlock::default()
        };

        {
            // invalid block size
            let block_bytes = block.to_bytes();
            let block2: DXBBlock = DXBBlock::from_bytes(&block_bytes).unwrap();
            assert_ne!(block, block2);
        }

        {
            // valid block size
            block.recalculate_struct();
            let block_bytes = block.to_bytes();
            let block3: DXBBlock = DXBBlock::from_bytes(&block_bytes).unwrap();
            assert_eq!(block, block3);
        }
    }

    #[tokio::test]
    #[cfg(feature = "std")]
    pub async fn signature_to_and_from_bytes() {
        // setup block
        let mut routing_header = RoutingHeader::default()
            .with_sender(Endpoint::from_str("@test").unwrap())
            .to_owned();
        routing_header.set_size(157);
        let mut block = DXBBlock {
            body: vec![0x01, 0x02, 0x03],
            encrypted_header: EncryptedHeader {
                ..Default::default()
            },
            routing_header,
            ..DXBBlock::default()
        };

        // setup correct signature
        block
            .routing_header
            .flags
            .set_signature_type(SignatureType::Unencrypted);

        let (pub_key, pri_key) = CryptoImpl::gen_ed25519().await.unwrap();
        let raw_signed = [pub_key.clone(), block.body.clone()].concat();
        let hashed_signed = CryptoImpl::hash_sha256(&raw_signed).await.unwrap();

        let signature = CryptoImpl::sig_ed25519(&pri_key, &hashed_signed)
            .await
            .unwrap();
        // 64 + 44 = 108
        block.signature = Some([signature.to_vec(), pub_key.clone()].concat());

        let block_bytes = block.to_bytes();
        let block2: DXBBlock = DXBBlock::from_bytes(&block_bytes).unwrap();
        assert_eq!(block, block2);
        assert_eq!(block.signature, block2.signature);

        // setup faulty signature
        let mut other_sig = signature;
        if other_sig[42] != 42u8 {
            other_sig[42] = 42u8;
        } else {
            other_sig[42] = 43u8;
        }
        block.signature = Some([other_sig.to_vec(), pub_key].concat());
        let block_bytes2 = block.to_bytes();
        let signature_validation = DXBBlock::from_bytes(&block_bytes2)
            .unwrap()
            .validate_signature()
            .into_future()
            .await;
        assert!(signature_validation.is_err());
        assert_matches!(
            signature_validation.unwrap_err(),
            SignatureValidationError::InvalidSignature
        )
    }

    #[test]
    fn illegal_signed_trace_block() {
        let mut block = DXBBlock::new_with_body(&[0x01, 0x02, 0x03]);
        block
            .block_header
            .flags_and_timestamp
            .set_block_type(BlockType::Trace);
        block
            .routing_header
            .flags
            .set_signature_type(SignatureType::Unencrypted);
        block.signature = Some(vec![0u8; 108]);

        let block_bytes = block.to_bytes();
        let parse_result = DXBBlock::from_bytes(&block_bytes);
        assert!(parse_result.is_err());
        assert_matches!(
            parse_result.unwrap_err(),
            DXBBlockParseError::IllegalState
        );
    }
}